CommonsChunkPlugin

官方文档地址

https://webpack.github.io/docs/list-of-plugins.html#commonschunkplugin

new webpack.optimize.CommonsChunkPlugin(options)

相关设置总结

  • options.name or options.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值。通过传入一个已存在的chunk name的值可以选中该chunk。传入一个数组的话就等同于用每一个name轮番调用。如果省略该值并且options.asyncoptions.children被设为了全部chunks可用,则options.filename会被用作name的值。

  • options.filename (string)

    设置代码块的文件名称
  • options.chunks (string[])

    设置公共代码的入口文件。默认是所有的entry。
  • options.minChunks (number|Infinity|function(module, count) -> boolean)

    设置最小被引用次数,最小是2
  • options.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,
}),
},
}

结果:

打包出四个文件。

  1. main1.bundle.js // 仅包含 main1.js 独有代码
  2. main2.bundle.js // 仅包含 main2.js 独有代码
  3. common.bundle.js // 包含main1 和 main2 的公共代码(不包含第三方库)
  4. vendor.bundle.js // 仅包含第三方库

作者博客:pspgbhu http://www.cnblogs.com/pspgbhu/

作者GitHubhttps://github.com/pspgbhu

欢迎转载,但请注明出处,谢谢!

CommonsChunkPlugin的一些总结的更多相关文章

  1. webpack CommonsChunkPlugin详细教程

    1.demo结构: 2.package.json配置: { "name": "webpack-simple-demo", "version" ...

  2. 关于webpack.optimize.CommonsChunkPlugin的使用二

    Note:当有多个入口节点的时候,只有所有入口节点都引入了同一个模块的时候,webpack.optimize.CommonsChunkPlugin才会将那个模块提取出来,如果其中一个入口节点没有引入该 ...

  3. webpack.optimize.CommonsChunkPlugin插件的使用

    方式一,传入字符串参数 new webpack.optimize.CommonsChunkPlugin('common.js'), // 默认会把所有入口节点的公共代码提取出来,生成一个common. ...

  4. CommonsChunkPlugin的使用(关于angular2中的polyfills和vendor的疑问解决)

    seed: angular2-webpack-starter(在github上可以找到) polyfills:提供api以方便兼容不同的浏览器 vendor:项目插件扩展 在学习ng2中一直不明白为什 ...

  5. [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 ...

  6. [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 ...

  7. 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 ...

  8. CommonsChunkPlugin并不是分离第三方库的好办法(DllPlugin科学利用浏览器缓存)

    webpack算是个磨人的小妖精了.之前一直站在glup阵营,使用browserify打包,发现webpack已经火到爆炸,深怕被社区遗落,赶紧拿起来把玩一下.本来只想玩一下的.尝试打包了以后,就想启 ...

  9. 谈谈CommonsChunkPlugin抽取公共模块

    引言 webpack插件CommonsChunkPlugin的主要作用是抽取webpack项目入口chunk的公共部分,具体的用法就不做过多介绍,不太了解可以参考webpack官网介绍: 该插件是we ...

随机推荐

  1. iOS 并行编程:NSOperation Queues

    1 简介 1.1 功能        Operation Queue也是IOS的一种并行编程技术,类似Dispatch Queue可以帮助用户管理多线程.但是Operation Queue将任务封装在 ...

  2. oracle修改字段类型

    有一个表名为tb,字段段名为name,数据类型nchar(20). 1.假设字段数据为空,则不管改为什么字段类型,可以直接执行:alter table tb modify (name nvarchar ...

  3. 【转】企业级Java应用最重要的4个性能指标

    应用性能管理(APM)是一种即时监控以实现对应用程序性能管理和故障管理的系统化解决方案.目前主要指对企业的关键业务应用进行监测.优化,最终达到提高企业应用的可靠性和质量,保证用户得到良好的服务,降低I ...

  4. 3s自动跳转到登陆界面

    cdn资源 Bootstrap是Twitter推出的一个用于前端开发的开源工具包.它由Twitter的设计师Mark Otto和Jacob Thornton合作开发,是一个CSS/HTML框架.Boo ...

  5. 用变量a给出下面的定义

    a)一个整型数(An integer)b) 一个指向整型数的指针(A pointer to an integer)  c) 一个指向指针的的指针,它指向的指针是指向一个整型数(A pointer to ...

  6. ios PullToRefresh using animated GIF or image array or Vector image

    说说那些令人惊叹的下拉效果 1. 动画下拉,这里借用一下github的资源 优点:直接用gif图处理,下拉进度完全按照gif图运行时间,只要时间和下拉进度匹配就可以了, 效果很流畅 https://d ...

  7. sqlite3 语句总结

      一. iOS客户端设计数据库时一般使用  sqlite,以sqlite3 为例,简单介绍一下. 二. sqlite3常用命令当前目录下建立或打开test.db数据库文件,并进入sqlite命令终端 ...

  8. ServiceStack.Redis 破解

    在github上下载了ServiceStack.Redis,做测试发现有限制,居然从v4开始就收费,无聊时,做了个源码分析 废话不多,上测试代码 try { ; i < ; i++) { red ...

  9. Google Test Frame 简单使用例子

    1 序言——为什么折腾Google Test 被逼无奈的. 最近研究google开源的基于列存储的数据库查询引擎supersonic源码.初略的浏览了一遍代码,竟然没有main函数,顿时惊讶的目瞪口呆 ...

  10. Java中printStackTrace()、toString()、getMessage()的区别

    一.三者之间的关系图: 二.演示 1.printStackTrace()演示: public class Test {     public int div(int a, int b)     {   ...