CommonsChunkPlugin的一些总结
CommonsChunkPlugin
官方文档地址
https://webpack.github.io/docs/list-of-plugins.html#commonschunkplugin
new webpack.optimize.CommonsChunkPlugin(options)
相关设置总结
options.nameoroptions.names(string|string[])
设置公共代码块的name。- 如果name的值不与任何已存在的chunk相同,则会从options.chunks中提取出公共代码,并形成新的chunk,并以options.name去设置该chunk的name
- 如果
name选中的是已存在的chunk,则会从options.chunks中提取出被name选中的chunk。 - 如果
name是不存在的chunk,则会根据其他配置提取出公共chunk,并将该chunk的name值设为opitons.name的值 - 如果
name是个数组,则等同于每个name轮番调用该插件。 - 与
options.filename的区别。options.filename是chunk的文件名的,而options.name相当于chunk的唯一标识符,在filename值省略的情况下,options.filename会默认取options.name的值。
官方文档及个人翻译
The chunk name of the commons chunk. An existing chunk can be selected by passing a name of an existing chunk. If an array of strings is passed this is equal to invoking the plugin multiple times for each chunk name. If omitted and options.async or options.children is set all chunks are used, otherwise options.filename is used as chunk name.
公共chunk(代码块,个人习惯叫chunk)的chunk
name值。通过传入一个已存在的chunkname的值可以选中该chunk。传入一个数组的话就等同于用每一个name轮番调用。如果省略该值并且options.async或options.children被设为了全部chunks可用,则options.filename会被用作name的值。
options.filename(string)
设置代码块的文件名称options.chunks(string[])
设置公共代码的入口文件。默认是所有的entry。options.minChunks(number|Infinity|function(module, count) -> boolean)
设置最小被引用次数,最小是2options.children(string[])
If true all children of the commons chunk are selected.
options.async(boolean|string)
If true a new async commons chunk is created as child of options.name and sibling of options.chunks. It is loaded in parallel with options.chunks. It is possible to change the name of the output file by providing the desired string instead of true.
options.minSize(number)
Minimum size of all common module before a commons chunk is created.
如何分别打包第三方库和公共代码库
{
entry: {
// 主入口文件1
main1: './mian1.js',
// 主入口文件2
mian2: './mian2.js',
// 第三方库
vendor: [
'vue',
'vuex',
'whatwg-fetch',
'es6-promise'
],
},
output: {
path: path.resolve(__dirname, './dist'),
filename: 'js/[name].bundle.js'
},
// ...
// ...
// ...
plugins: {
// 将 main1 和 main2 的公共代码提取出来打包
new webpack.optimize.CommonsChunkPlugin({
name: 'common',
chunks: ['main1', 'main2'],
filename: 'js/common.bundle.js',
minChunks: 2,
}),
// 将 vendor 从 common 中提取出来分别打包
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
chunks: ['common'],
filename: 'js/vendor.bundle.js',
minChunks: Infinity,
}),
},
}
结果:
打包出四个文件。
main1.bundle.js // 仅包含 main1.js 独有代码main2.bundle.js // 仅包含 main2.js 独有代码common.bundle.js // 包含main1 和 main2 的公共代码(不包含第三方库)vendor.bundle.js // 仅包含第三方库
作者博客:pspgbhu http://www.cnblogs.com/pspgbhu/
作者GitHub:https://github.com/pspgbhu
欢迎转载,但请注明出处,谢谢!
CommonsChunkPlugin的一些总结的更多相关文章
- 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中一直不明白为什 ...
- [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 ...
- Can someone explain Webpack's CommonsChunkPlugin
I get the general gist that the CommonsChunkPlugin looks at all the entry points, checks to see if t ...
- CommonsChunkPlugin并不是分离第三方库的好办法(DllPlugin科学利用浏览器缓存)
webpack算是个磨人的小妖精了.之前一直站在glup阵营,使用browserify打包,发现webpack已经火到爆炸,深怕被社区遗落,赶紧拿起来把玩一下.本来只想玩一下的.尝试打包了以后,就想启 ...
- 谈谈CommonsChunkPlugin抽取公共模块
引言 webpack插件CommonsChunkPlugin的主要作用是抽取webpack项目入口chunk的公共部分,具体的用法就不做过多介绍,不太了解可以参考webpack官网介绍: 该插件是we ...
随机推荐
- careercup-递归和动态规划 9.3
9.3 在数组A[0...n-1]中,有所谓的魔术索引,满足条件A[i]=i.给定一个有序整数数组,元素值给不相同,编写一个方法,在数组A中找出一个魔术索引,若存在的话. 进阶: 如果数组元素有重复值 ...
- 谈谈Javascript线程
其实,大家都知道Javascript的语言执行环境是单线程的,浏览器无论在什么时候都有且只有一个线程在运行Javascript程序.那Ajax发送异步请求怎么解释,setTimeout/s ...
- VB学习笔记
stack segment stack 'stack' dw dup() ;此处输入堆栈段代码 stack ends data segment ;IBUF OBUF 看成是内存的地址,IBUF+1和I ...
- 【排障】每次打开word都提示要安装配置
为什么每次打开word都提示要安装配置?很多人在打开word时,总是提示要安装配置一遍,花去不少时间,这是由于电脑里有两个不同版本的office软件,产生的原因可能是原来的卸载了没卸载干净,或是安装了 ...
- apache性能配置优化
最近在进行apache性能优化设置.在修改apache配置文件之前需要备份原有的配置文件夹conf,这是网站架设的好习惯.以下的apache配置调优均是在red had的环境下进行的. httpd相关 ...
- Web内容禁止选中的两种方式
为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/ShiJiaqi. http://www.cnblogs.com/shijiaqi1066/p/5761818. ...
- Scala可变参数列表,命名参数和参数缺省
重复参数 Scala在定义函数时允许指定最后一个参数可以重复(变长参数),从而允许函数调用者使用变长参数列表来调用该函数,Scala中使用“*”来指明该参数为重复参数.例如: scala> de ...
- java.lang.NoClassDefFoundError的原因及解决
[O] 安卓应用在低版本(V2.3.6)系统上运行时报错: java.lang.NoClassDefFoundError 完整错误信息如下: 05-29 13:56:13.687: E/Android ...
- MVC小系列(七)【分部视图中的POST】
MVC小系列(七)[分部视图中的POST] 在PartialView中进行表单提交的作用:1 这个表单不止一个地方用到,2 可能涉及到异步的提交问题 这两种情况都可能需要把表单建立在分部视图上, 使用 ...
- 转载:在Visual Studio 2013中管理中国特色的社会主义Windows Azure
原文链接: http://www.pstips.net/get-azurechinacloud-settings.html 谷歌被豪迈地放弃了中国市场,微软仍旧在中国市场摸爬滚打,跪着挣钱.其中私人定 ...