CommonsChunkPlugin的一些总结
CommonsChunkPlugin
官方文档地址
https://webpack.github.io/docs/list-of-plugins.html#commonschunkplugin
new webpack.optimize.CommonsChunkPlugin(options)
相关设置总结
options.name
oroptions.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 ...
随机推荐
- block_dump观察Linux IO写入的具体文件(mysqld)
一.使用方法: 二.基本原理: 三.总结 很多情况下开发者调测程序需要在Linux下获取具体的IO的状况,目前常用的IO观察工具用vmstat和iostat,具体功能上说当然是iostat更胜一筹 ...
- lambda显式声明返回值
10.21 编写一个lambda,捕获一个局部int变量,并递减变量值,直至它变为0.一旦变量变为0,再调用lambda应该不再递减变量.lambda应该返回一个bool值,指出捕获的变量是否为0. ...
- 截获导航控制器系统返回按钮的点击pop及右滑pop事件
前几天看了@栾小布的一篇文章:Custom backBarButtonItem,在跟着做的时候我又顺便扩展了一些,写此文章的目的是为了总结一下自己所写的东西,方便以后翻看容易,同时也是自己入行iOS一 ...
- Java基础知识强化之集合框架笔记72:集合特点和数据结构总结
1. 集合 (1)Collection(单列集合) List(有序,可重复): ArrayList:底层数据结构是数组,查询块,增删慢.线程不安全,效率 ...
- Flex学习教程网站地址
http://www.985school.com/flex/complex_controls.html
- H5上传文件
XMLHttpRequest 在Html5 规范中已经有全新的变化,规定了XMLHttpRequest Level 2规范(目前最新版本)包含下列新的特性: 处理字节流,例如作为上传或者下载的File ...
- wxPython ImportError DLL load failed: %1 不是有效的 Win32 应用程序 解决办法
原因:python是32位的,装了64位的Wxpython时会报错,具体错误是安装库文件与python的动态库的位数未一致. 特别留意32位/64位指的不是 系统的位数,而是Python的位数. 64 ...
- C#三元运算符
?:表达式1?表达式2:表达式3如果1为真则执行2,为假执行3
- 20151225jquery学习笔记---选项卡UI
圣诞节快乐,哈哈哈....选项卡(tab),是一种能提供给用户在同一个页面切换不同内容的 UI. 尤其是在页面布局紧凑的页面上,提供了非常好的用户体验.一. 使用 tabs使用 tabs 比较简单,但 ...
- windows下Nginx配置与测试
1.获取 下载地址:http://nginx.org/en/download.html 参考网址:http://nginx.org/en/docs/windows.html 2.准备工作 ...