Error: webpack.optimize.CommonsChunkPlugin has been removed,

简介: Error: webpack.optimize.CommonsChunkPlugin has been removed,

如果使用中文webpack文档做代码分离,会有报错,报错详情是:

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

解决办法可以参考官方英文文档:https://webpack.js.org/guides/code-splitting/

具体实现如下:如下optimization配置

module.exports = {
    entry: {
        app: "./src/index.js",
        another: './src/another-module.js'
    },
    plugins: [
        new HtmlWebpackPlugin ({
            title: "code Splitting "
        }),
    ],
    optimization: {
        splitChunks: {
            chunks: 'all'
        }
    },
    output: {
        filename: "[name].bundle.js",
        path: path.resolve (__dirname, 'dist')
    },
};

然后执行npm run build,运行结果如下:



在这里生成了一个名字为 vendors~another.bundle.js的文件,如果不想用这个默认生成的名称完全可以,自己命名。

给optimization配置一个name属性

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

再次执行npm run build ,会发现生成了一个common.bundle.js,没问题


交流

我是老礼,公众号「进军全栈攻城狮」作者 ,对前端技术保持学习爱好者。我会经常分享自己所学所看的干货,在进阶的路上,共勉!

相关文章
|
6月前
webpack.config.js配置文件报错:The ‘mode‘ option has not been set
webpack.config.js配置文件报错:The ‘mode‘ option has not been set
101 0
AxiosError: Network Error at XMLHttpRequest.handleError (webpack-internal:///./node_modules/axio
AxiosError: Network Error at XMLHttpRequest.handleError (webpack-internal:///./node_modules/axio
|
11月前
|
JavaScript
Error: Unsupported URL Type: npm:fork-ts-checker-webpack-plugin@^5.0.11
如果你去搜索,答案都是去升级你的npm 在StackOverflow说是vue3得在非vue-cli环境得单独配置。地址: 还有说npm install --save-dev fork-ts-checker-webpack-plugin 众说纷纭,都解决不了
121 0
|
11月前
Webpack打包出错:Error: Cannot find module ‘webpack/lib/node/NodeTemplatePlugin‘ Require stack:
Webpack打包出错:Error: Cannot find module ‘webpack/lib/node/NodeTemplatePlugin‘ Require stack:
Error: Cannot find module ‘webpack‘ 问题解决办法
Error: Cannot find module ‘webpack‘ 问题解决办法
697 1
|
JavaScript Cloud Native Go
Error: Cannot find module ‘webpack/bin/config-yargs‘ at Function.Module._resolveFilename (intern
Error: Cannot find module ‘webpack/bin/config-yargs‘ at Function.Module._resolveFilename (intern
78 0
|
JavaScript
UniApp 解决 Error: Cannot find module ‘webpack/lib/RuleSet‘
UniApp 解决 Error: Cannot find module ‘webpack/lib/RuleSet‘
971 0
使用antd-theme-webpack-plugin报错Error LessError: Cannot find module ‘antd/lib/style/themes/default.less
使用antd-theme-webpack-plugin报错Error LessError: Cannot find module ‘antd/lib/style/themes/default.less
1124 0
使用antd-theme-webpack-plugin报错Error LessError: Cannot find module ‘antd/lib/style/themes/default.less
Error: Cannot find module ‘webpack-cli/bin/config-yargs‘
Error: Cannot find module ‘webpack-cli/bin/config-yargs‘
Error: Cannot find module ‘webpack-cli/bin/config-yargs‘
解决报错信息 —— Error: Cannot find module ‘webpack-cli/bin/config-yargs’
解决报错信息 —— Error: Cannot find module ‘webpack-cli/bin/config-yargs’
384 0
下一篇
无影云桌面