【问题标题】:How to bundle vendor and main scripts separately using webpack?如何使用 webpack 分别捆绑供应商和主脚本?
【发布时间】:2019-10-13 10:08:12
【问题描述】:

非常感谢这里的一些帮助,在这种情况下,我想在最终构建操作时将 vendor.js 和 main.js 分开。

我之前尝试过在我的 package.json devDependency 中循环以分离我的第三方库并将其放入 vendor.js,它工作正常,但它产生了 vendor.js,这在构建过程中是不必要的,因为我的第三个库已经在我的 main.js 中了

这是我的 weppack.config.js

var config = {
    devtool: 'eval-source-map',
    cache: true,
    entry: {
        main: path.join(__dirname, "app", "App.js"),
    },
    output: {
        path: path.join(__dirname, 'scripts', 'js'),
        filename: '[name].js',
        chunkFilename: '[name].js',
        sourceMapFilename: '[file].map',
        publicPath: '/scripts/js/'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: [
                            ['es2015', { modules: false }],
                            'react',
                        ],
                        plugins: [
                            'syntax-dynamic-import',
                            'transform-object-rest-spread',
                            'transform-class-properties',
                            'transform-object-assign',
                        ],
                    }
                },
            },
        ],
    },
    resolve: {
        extensions: ['.js', '.jsx' ,'.css', '.ts'],
        alias: {
            'react-loadable': path.resolve(__dirname, 'app/app.js'),
        },
    },
};

【问题讨论】:

标签: reactjs webpack chunks


【解决方案1】:

由于this answer

在他的 webpack.config.js(版本 1,2,3)文件中,他有

function isExternal(module) {


var context = module.context;

  if (typeof context !== 'string') {
    return false;
  }

  return context.indexOf('node_modules') !== -1;
}

在他的插件数组中

plugins: [
  new CommonsChunkPlugin({
    name: 'vendors',
    minChunks: function(module) {
      return isExternal(module);
    }
  }),
  // Other plugins
]

这应该像我一样解决你的问题。

【讨论】:

    猜你喜欢
    • 2022-07-10
    • 2015-07-31
    • 2017-08-12
    • 2020-09-20
    • 2016-10-30
    • 2023-03-23
    • 2017-10-25
    • 1970-01-01
    • 2017-03-14
    相关资源
    最近更新 更多