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

运行结果如下:在这里插入图片描述

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

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

npm run build 后,如下图

在这里插入图片描述

dist 文件下的目录:
在这里插入图片描述
例子链接:代码分离/demo13_splitting-code/

发布了56 篇原创文章 · 获赞 24 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/u014196765/article/details/99563779
今日推荐