我正在尝试从 3 迁移到 webpack4 并且我很难迁移CommonsChunkPlugin
.
在 WP3 上,我有以下简化配置:
const entries = {
client: './src/index.js',
common1: ['lodash'],
common2: ['react', 'react-dom']
};
module.exports = {
...
entry: entries,
plugins: [
new webpack.optimize.CommonsChunkPlugin({
names: ['common1', 'common2'],
minChunks: Infinity
})
]
};
在 WP4 上,我已经使用optimization.splitChunks尝试了多种组合,但我无法让它以共享公共依赖项的方式正确捆绑我的模块,就像以前在 WP3 上一样。
这是我上次尝试时使用的,但最终捆绑包的大小要大得多:
// same entries object
const entries = { ... };
module.exports = {
...
entry: entries,
optimization: {
splitChunks: {
minChunks: Infinity,
chunks: 'initial',
cacheGroups: {
common1: { test: 'common1' },
common2: { test: 'common2' },
// disables the default definition of these cache groups
vendors: false,
default: false
}
}
};