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 ...
随机推荐
- Linux--------------安装mysql(2)
在 CentOS7 上安装 MySQL5.7 1 通过 SecureCRT 连接到阿里云 CentOS7 服务器: 2 进入到目录 /usr/local/ 中:cd /usr/local/ 3 创建目 ...
- install lua client for redis-server on Mac
1. lua client library for redis-server https://github.com/nrk/redis-lua 2. dependent luasocket https ...
- 深入理解计算机系统第二版习题解答CSAPP 2.9
基于三元色R(红)G(绿)B(蓝)关闭(0)和打开(1),能够创建8种不同的颜色,如下: R G B 颜色 R G B 颜色 0 0 0 黑色 1 0 0 红色 0 0 1 蓝色 1 0 1 红紫色 ...
- 实现一个脚本语言Raven(一)
之前实现了Raven语言的0.1版,仅仅支持表达式处理与控制语句,由于不支持数组.函数.类,甚至都不是图灵完全的语言. 现在参考vczh的博客打算重新写一遍Raven语言.陈祖不愧是神啊,高中就写出支 ...
- xml、xhtml、html、dhtml的区别
1.XML 可扩展标记语言,标准通用标记语言的子集,是一种用于标记电子文件使其具有结构性的标记语言. 可扩展标记语言可以对文档和数据进行结构化处理,从而能够在部门.客户和供应商之间进行交换,实现动态内 ...
- loadrunner简单的例子(demo)
刚刚做了一个loadrunner进行负载测试,把步骤截图给大伙看看.一共三个步骤 一创建/编辑脚本 二运行负载测试 三分析测试结果 首先是第一步的流程:第一步创建/编辑脚本 图一 图二 图三 图四 图 ...
- Velocity 入门(一)
Velocity是一种Java模版引擎技术,该项目由Apache提出.因为非常好用,和工作中有啥用,所以我在在理简单的入门一下. 网上找了很多教程,写的不是很明白,要么就是全部拷贝下来时候运行不起来. ...
- OnePlus One(一加1)刷机Kali Nethunter完整教程
设备信息: 设备名称:OnePlus One(一加1) OS:ColorOS 1.2 设备型号:A0001 目标: 在OnePlus One(一加1)上将 ColorOS 1.2 刷机为 Kali N ...
- UIPanGestureRecognizer的使用
UIGestureRecognizer是一个定义基本手势的抽象类,具体什么手势,在以下子类中包含: 1.拍击UITapGestureRecognizer (任意次数的拍击) 2.向里或向外捏 ...
- javascript DOM 节点 第18节
<html> <head> <title>DOM对象</title> </head><body><div >DOM对 ...