CommonsChunkPlugin VS SplitChunksPlugin
等了好久终于等到你, webpack团队人员卧薪尝胆五个多月的时间终于带来的webpack4.0,个人觉得webpack4带来的最大优化便是对于懒加载块拆分的优化,删除了CommonsChunkPlugin,新增了优化后的SplitChunksPlugin,那么CommonsChunkPlugin的痛点在哪?SplitChunksPlugin的优化又是在哪?
1、CommonsChunkPlugin的痛
记得17年始,我刚开始用webpack搭建一个vue的单页应用框架时,我陆续的抛出了几个问题:
1、如何避免单页应用首次的入口文件过大?
这个问题处理起来倒简单,webpack支持import()语法实现模块的懒加载,可以做到随用随载,也就是除了首页要用到文件,其他模块使用懒加载就能有效的避免入口文件过大
2、入口模块以及剩下的懒加载模块引用公用的模块时,代码会重复吗?webpack会处理吗?怎么处理?
代码重复是肯定的,如果父级模块中没有引入懒加载模块的共用模块,那么懒加载各模块间就会出现代码重复;webpack能处理,那么怎么处理呢?这时CommonsChunkPlugin就信誓旦旦地登场了,它能够将全部的懒加载模块引入的共用模块统一抽取出来,形成一个新的common块,这样就避免了懒加载模块间的代码重复了,哇!好强大,点个赞。可惜的是,又回到了第一个问题,你把共用的东西都抽出来了,这样又造成了入口文件过大了。以下是CommonsChunkPlugin时代常用的配置
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
// 引入node_modules的依赖全抽出来
minChunks: function (module, count) {
// any required modules inside node_modules are extracted to vendor
return (
module.resource &&
/\.js$/.test(module.resource) &&
module.resource.indexOf(
path.join(__dirname, '../node_modules')
) === 0
)
}
// 或者直接minChunks: 2,重复模块大于2的全部抽出来
}),
总之你在代码重复与入口文件控制方面你得做个平衡,而这个平衡挺不利于多人开发的,也不易于优化,我刚接触时有写了一篇博文也是关于懒加载,这里对于项目平衡与取舍有做了一些分析,这里就不再展开。
CommonsChunkPlugin的痛,痛在只能统一抽取模块到父模块,造成父模块过大,不易于优化
2、SplitChunksPlugin的好
前面讲了那么多,其实SplitChunksPlugin的登场就是为了抹平之前CommonsChunkPlugin的痛的,它能够抽出懒加载模块之间的公共模块,并且不会抽到父级,而是会与首次用到的懒加载模块并行加载,这样我们就可以放心的使用懒加载模块了,以下是官网说明的一些例子:
假设存在以下chunk-a~chunk-d
chunk-a: react, react-dom, some components
chunk-b: react, react-dom, some other components
chunk-c: angular, some components
chunk-d: angular, some other components
webpack会自动创建两个chunk模块,结果如下:
chunk-a~chunk-b: react, react-dom
chunk-c~chunk-d: angular
chunk-a to chunk-d: Only the components
SplitChunksPlugin使用官网默认配置基本可以满足大多数单页应用了,以下是我对于多页应用补充的配置
optimization: {
splitChunks: {
// 抽离入口文件公共模块为commmons模块
cacheGroups: {
commons: {
name: "commons",
chunks: "initial",
minChunks: 2
}
}
}
},
vue-cli这是我升级到webpack4.0后的vue-cli配置,结合了happypack,vue-loader@16,单页与多页自动构建等的脚手架
SplitChunksPlugin的好,好在解决了入口文件过大的问题还能有效自动化的解决懒加载模块之间的代码重复问题
CommonsChunkPlugin VS SplitChunksPlugin的更多相关文章
- webpack关于CommonsChunkPlugin在高版本被移除的替代方案问题
1.在指南的缓存章节里webpack.config.js文件中,使用new的方法会报错 const webpack = require('webpack'); + new webpack.optimi ...
- Webpack 4 SplitChunksPlugin配置方案(转)
通常情况下我们的 WebApp 是有我们的自身代码和第三方库组成的,我们自身的代码是会常常变动的,而第三方库除非有较大的版本升级,不然是不会变的,所以第三方库和我们的代码需要分开打包,我们可以给第三方 ...
- webpack CommonsChunkPlugin详细教程
1.demo结构: 2.package.json配置: { "name": "webpack-simple-demo", "version" ...
- 关于webpack.optimize.CommonsChunkPlugin的使用二
Note:当有多个入口节点的时候,只有所有入口节点都引入了同一个模块的时候,webpack.optimize.CommonsChunkPlugin才会将那个模块提取出来,如果其中一个入口节点没有引入该 ...
- webpack.optimize.CommonsChunkPlugin插件的使用
方式一,传入字符串参数 new webpack.optimize.CommonsChunkPlugin('common.js'), // 默认会把所有入口节点的公共代码提取出来,生成一个common. ...
- CommonsChunkPlugin的使用(关于angular2中的polyfills和vendor的疑问解决)
seed: angular2-webpack-starter(在github上可以找到) polyfills:提供api以方便兼容不同的浏览器 vendor:项目插件扩展 在学习ng2中一直不明白为什 ...
- CommonsChunkPlugin的一些总结
CommonsChunkPlugin 官方文档地址 https://webpack.github.io/docs/list-of-plugins.html#commonschunkplugin new ...
- [Webpack 2] Chunking common modules from multiple apps with the Webpack CommonsChunkPlugin
If you have a multi-page application (as opposed to a single page app), you’re likely sharing module ...
- [Webpack 2] Grouping vendor files with the Webpack CommonsChunkPlugin
Often, you have dependencies which you rarely change. In these cases, you can leverage the CommonsCh ...
随机推荐
- SAML2.0 SP端处理
sso response解析 import java.io.ByteArrayInputStream; import java.io.InputStream; import java.security ...
- 利用pyinstaller生成exe之后,运行不能正常产生结果文件问题记录
https://segmentfault.com/q/1010000011284617/a-1020000011493026 在此链接已解决问题,现在在这里在详细记录一次 问题描述: 利用pychar ...
- Rsync备份功能总结
备份服务笔记====================================================================== Rsync是一款开源的.快速的.多功能的.可实 ...
- springboot问题总结
前端使用jsp界面,但是jsp界面中引用的静态资源无论如何也加载不出来,弄一天了,哎 最后把pom文件里的jar全干掉,代码移除,就剩下登录界面,看css能不能进来,结果没问题, 然后看类里面的注解, ...
- visual studio 2013 几个测试工具(Nunit 3、xUnit)
一.Nunit 3 1.在解决方案里添加一个类库——引用——右键(如下图)) 3.搜索nunit 并安装(如图) 3.注意引入命名空间并给测试类和测试方法添加特性(如图) 4.如果测试通过则为绿色(如 ...
- 远程git仓库的搭建
具体的操作见另一篇 第一部分: 安装 1. 下载地址: https://git-scm.com/download/win; 如果速度慢, 使用 迅雷下载; 2. 点击安装, 然后下一步, 直到下面这 ...
- delphi:Exception EInvalidPointer in module Project1.exe
在用delphi XE5编程时遇到如下问题: Exception EInvalidPointer in module Project1.exe at 00007595. Invalid pointer ...
- groovy 知识集锦
对应官方的<Program structure>的中文翻译 http://www.cnblogs.com/zhaoxia0815/p/7404387.html
- SpringBoot之SOAP WebService
SpringBoot的Web Service类型常见有RESTful Web Service和SOAP Web Service两种,RESTful风格的web服务比较常用,但实际工作中仍有部分场景用到 ...
- Spring 配置 定时任务
官档地址:https://docs.spring.io/spring/docs/5.1.4.RELEASE/spring-framework-reference/integration.html#sc ...