Webpack:如何使用CommonsChunkPlugin和多个拆分包

时间:2016-06-13 22:15:20

标签: javascript webpack

我有两个独立的应用程序部分,它们都分为“应用程序”和“应用程序”。和'供应商'束。

Webpack条目:

entry: {
    'client-app': path.join(BASE_DIR, 'front-end-sources', 'client', 'scripts', 'main.ls'),
    'client-vendor': path.join(BASE_DIR, 'front-end-sources', 'client', 'scripts', 'vendor.ls'),
    'admin-app': path.join(BASE_DIR, 'front-end-sources', 'admin', 'scripts', 'main.ls'),
    'admin-vendor': path.join(BASE_DIR, 'front-end-sources', 'admin', 'scripts', 'vendor.ls')
},

插件:

plugins: [
    new ExtractTextPlugin('[name].bundle.css'),
    new webpack.optimize.CommonsChunkPlugin({
        names: ['client-app', 'client-vendor'],
        minChunks: Infinity
    }),
    new webpack.optimize.CommonsChunkPlugin({
        names: ['admin-app', 'admin-vendor'],
        minChunks: Infinity
    })
]

只有客户端应用程序'和'客户 - 供应商'或者' admin-app'和' admin-vendor'和单个CommonsChunkPlugin一起工作完美,它生成2个捆绑(app& vendor)但是这个对失败并出错:

ERROR in CommonsChunkPlugin: While running in normal mode it's not allowed to use a non-entry chunk (admin-app)
ERROR in CommonsChunkPlugin: While running in normal mode it's not allowed to use a non-entry chunk (admin-vendor)

我该怎么做?

1 个答案:

答案 0 :(得分:4)

我有相同的要求并通过将配置分成两个并为module.exports提供数组来解决它。

module.exports = [{
    entry: {
        'client-app': path.join(BASE_DIR, 'front-end-sources', 'client', 'scripts', 'main.ls'),
        'client-vendor': path.join(BASE_DIR, 'front-end-sources', 'client', 'scripts', 'vendor.ls')
    },
    ...
    plugins: [
        new ExtractTextPlugin('[name].bundle.css'),
        new webpack.optimize.CommonsChunkPlugin({
            names: ['client-app', 'client-vendor'],
            minChunks: Infinity
        })
   ]
},
{
    entry: {
        'admin-app': path.join(BASE_DIR, 'front-end-sources', 'admin', 'scripts', 'main.ls'),
        'admin-vendor': path.join(BASE_DIR, 'front-end-sources', 'admin', 'scripts', 'vendor.ls')
    },
    ...
    plugins: [
        new ExtractTextPlugin('[name].bundle.css'),
        new webpack.optimize.CommonsChunkPlugin({
            names: ['admin-app', 'admin-vendor'],
            minChunks: Infinity
        })
   ]

}]