Error: webpack.optimize.CommonsChunkPlugin has been removed, please use config.optimization.splitChu

跟着 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'
+     }
+   }
  };

运行结果如下:Error: webpack.optimize.CommonsChunkPlugin has been removed, please use config.optimization.splitChu_第1张图片

这里生成了一个名字为 vendors~another~app.bundle.js的文件,我想用自己命名的也可以。

 optimization: {
        splitChunks: {
            chunks: 'all',
            name:'common'
        }
    },

npm run build 后,如下图

Error: webpack.optimize.CommonsChunkPlugin has been removed, please use config.optimization.splitChu_第2张图片

dist 文件下的目录:
Error: webpack.optimize.CommonsChunkPlugin has been removed, please use config.optimization.splitChu_第3张图片
例子链接:代码分离/demo13_splitting-code/

你可能感兴趣的:(webpack,webpack)