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 ...
随机推荐
- 15 Linux Split and Join Command Examples to Manage Large Files--reference
by HIMANSHU ARORA on OCTOBER 16, 2012 http://www.thegeekstuff.com/2012/10/15-linux-split-and-join-co ...
- HTTP请求、响应报文格式
HTTP请求报文格式: HTTP请求报文主要由请求行.请求头部.空行以及请求正文4部分组成 1,请求行由3部分组成,分别为:请求方式,URI(注意这里不是URL)以及协议版本组成,之间由空格分隔 请求 ...
- Java并发——同步工具类
CountDownLatch 同步倒数计数器 CountDownLatch是一个同步倒数计数器.CountDownLatch允许一个或多个线程等待其他线程完成操作. CountDownLatch对象 ...
- JQuery对象与DOM对象分析
一.定义: DOM对象(文档对象模型):暂时这么理解:通过JavaScript获取的HTML元素,称为DOM对象.如:var domID=document.getElementById("i ...
- jQuery导航菜单防刷新
为了实现最主要的功能,只写了一个粗糙的案例 CSS样式 ul,li{ list-style-type:none;} .nav { width: 100%; height: 35px; line-hei ...
- 数据交互 ajax代码整理
请求列表通用 /** **加载对应的试卷套题 ** */ function loadQuestions(){ var businessSubClass = { pageNo:pageNo, pageS ...
- C# DataTable转List And List转DataTable
// DataTable转List: IList<HousesEntity> Ilist = TableAndList.ConvertTo<HousesEntity>(dt); ...
- 通过修改ajaxFileUpload.js实现多图片动态上传并实现预览
参考:http://smotive.iteye.com/blog/1903606 大部分我也是根据他的方法修改的,我也要根据name实现动态的多文件上传功能,但是有个问题使我一直无法实现多文件上传. ...
- SqlServer优化博客网址
CareySon Sql Server MVP : http://www.cnblogs.com/CareySon/
- SQL Cursor 游标的使用
DECLARE @name VARCHAR(50) --声明游标 DECLARE cursor_VAA1 CURSOR FOR SELECT VAA05 FROM VAA1 --打开游标 OPEN ...