跟着 webpack4中文网做代码分离时npm run build
时出错,
Error: webpack.optimize.CommonsChunkPlugin has been removed, please use config.optimization.splitChunks instead.
自己去官网看了看,https://webpack.js.org/guides/code-splitting/
是这样加的。
const path = require('path');
module.exports = {
mode: 'development',
entry: {
index: './src/index.js',
another: './src/another-module.js'
},
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'dist')
},
+ optimization: {
+ splitChunks: {
+ chunks: 'all'
+ }
+ }
};
运行结果如下:
这里生成了一个名字为 vendors~another~app.bundle.js
的文件,我想用自己命名的也可以。
optimization: {
splitChunks: {
chunks: 'all',
name:'common'
}
},
npm run build 后,如下图
dist 文件下的目录:
例子链接:代码分离/demo13_splitting-code/