1
2
3
4
5
6
7
8
9
10
11
12
//optimization与entry/plugins同级
optimization: {
splitChunks: {
cacheGroups: {
commons: {
name: "commons",
chunks: "initial",
minChunks: 2
}
}
}
},

  

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

哦,原来是原来的插件不能用了,这个中文指南,标的是webpack4.7.0,结果这块都没更新啊。。。于是必应搜了一下,第一个出来的是这个

webpack4 Error: webpack.optimize.CommonsChunkPlugin has been removed, please use config.optimization.splitChunks instead 的解决方法

照着在webpack.config.js改了一下代码:

1
2
3
4
5
6
7
8
9
10
11
12
module.exports = {
plugins: [
- new webpack.optimize.CommonsChunkPlugin({
- name: 'common' // 指定公共 bundle 的名称。
- })
],
 
+ optimization: {
+ splitChunks: {
+ name: 'common'
+ }
+ },

  


倒是不报错了,但仔细检查了一下,发现结果是:

1
2
3
4
5
6
7
//app.bundle.js 和 another.bundle.js 是共享 lodash模块的
Asset Size Chunks Chunk Names
another.bundle.js 70.1 KiB 0 [emitted] another
app.bundle.js 70.2 KiB 1 [emitted] app
another.bundle.js.map 668 KiB 0 [emitted] another
app.bundle.js.map 668 KiB 1 [emitted] app
index.html 253 bytes [emitted]

  



app.bundle.js 和 another.bundle.js 的大小都在70kib左右(1kib = 1,024Byte),相差无几且都大得不对劲,而且common.js根本没有生出来啊。。。这绝壁是有问题吧!!!

返回去老老实实看官方文档吧。。。

果然,是要这么配滴:



稍微解释一下含义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cacheGroups is an object where keys are the cache group names. All options from the ones listed above are possible: chunks, minSize, minChunks, maxAsyncRequests, maxInitialRequests, name. 可以自己设置一组一组的cache group来配对应的共享模块
commons里面的name就是生成的共享模块bundle的名字
With the chunks option the selected chunks can be configured.
chunks 有三个可选值,”initial”, “async” 和 “all”. 分别对应优化时只选择初始的chunks,所需要的chunks 还是所有chunks 。
minChunks 是split前,有共享模块的chunks的最小数目 ,默认值是1, 但我看示例里的代码在default里把它重写成2了,从常理上讲,minChunks = 2 应该是一个比较合理的选择吧。
出来的结果是:
 
Asset Size Chunks Chunk Names
commons.bundle.js 69.5 KiB 0 [emitted] commons
another.bundle.js 1.21 KiB 1 [emitted] another
app.bundle.js 1.26 KiB 2 [emitted] app
commons.bundle.js.map 664 KiB 0 [emitted] commons
another.bundle.js.map 6.9 KiB 1 [emitted] another
app.bundle.js.map 7.31 KiB 2 [emitted] app
index.html 317 bytes [emitted]
posted on   ygunoil  阅读(554)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示