如何使用webpack分别捆绑供应商脚本和主要脚本?

问题描述 投票:2回答:1

我非常感谢在这种情况下在此提供的帮助,在最终的构建操作中,我想将我的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个回答
2
投票

由于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
]

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

© www.soinside.com 2019 - 2024. All rights reserved.