I need help migrating the following code from webpack 3 to 4.
new webpack.optimize.CommonsChunkPlugin({
minChunks: module => module.context &&
I have two entry files and want only the dependencies of the first one to be included in the vendor chunk. The dependencies of the second entry should all stay in its own bundle.
Assuming your entrypoints are main and secondary:
entry: {
main: 'path-to/main.js',
secondary: 'path-to/secondary.js'
}
Using webpack-4 You can extract only the vendors modules from main chunk but leave other third parties modules referenced in secondary inside that chunk using the test function of the cacheGroups you want to create.
optimization: {
splitChunks: {
cacheGroups: {
vendors: {
name: 'vendors',
chunks: 'all',
reuseExistingChunk: true,
priority: 1,
enforce: true,
test(module, chunks) {
const name = module.nameForCondition && module.nameForCondition();
return chunks.some(chunk => {
return chunk.name === 'main' && /[\\/]node_modules[\\/]/.test(name);
});
}
},
secondary: {
name: 'secondary',
chunks: 'all',
priority: 2,
enforce: true,
test(module, chunks) {
return chunks.some(chunk => chunk.name === 'secondary');
}
}
}
}
}