webpack-CommonsChunkPlugin

官方文档:

CommonsChunkPlugin

new webpack.optimize.CommonsChunkPlugin(options)
  • options.name or options.names (string|string[]): 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.

公共块的块名称。可以通过传递现有块的名称来选择现有块。如果传递一个字符串数组,这等于为每个块名称多次调用插件,如果省略并且options.asyncoptions.children设置为使用所有块,则使用options.filename作为块名。

  • options.filename (string): The filename template for the commons chunk. Can contain the same placeholder as output.filename. If omitted the original filename is not modified (usually output.filename or output.chunkFilename).

公共块的文件名。可以包含与output.filename相同的占位符。如果省略,原始文件名不会被修改(一般是output.filename or output.chunkFilename).

  • options.minChunks (number|Infinity|function(module, count) -> boolean): The minimum number of chunks which need to contain a module before it's moved into the commons chunk. The number must be greater than or equal 2 and lower than or equal to the number of chunks. Passing Infinity just creates the commons chunk, but moves no modules into it. By providing a function you can add custom logic. (Defaults to the number of chunks)

在模块移动到公共块之前需要包含该模块的块的最小数量。该数字必须大于或等于2且小于或等于块的数量,设置为Infinity只是创建公共块,但不移动模块。通过提供一个函数,你可以添加自定义逻辑

  • options.chunks (string[]): Select the source chunks by chunk names. The chunk must be a child of the commons chunk. If omitted all entry chunks are selected.

按块名称选择源块。块必须是公共块的子节点。如果省略,则选择所有块

  • options.children (boolean): If true all children of the commons chunk are selected

如果为true,则选择公共块的所有子块(当存在code splitting的时候,入口chunk会存在多个子chunk,子chunk之间也会存在重复的引用模块,如果该选项为true,则子chunk会将公共模块提取到入口模块当中去)

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

假如设置为true一个新的异步commons chunk被创建为options.name的子块和options.chunks的兄弟块。它与options.chunks并行加载。可以通过提供所需的字符串而不是true来更改输出文件的名称。

  • options.minSize (number): Minimum size of all common module before a commons chunk is created.

创建公共块之前所有公共模块的最小大小

解读

  • 这个插件主要是用来创建的chunk中提取公共的模块单独打包,所以主要用来做两件事情:单独打包公共模块以及分离第三方依赖库。
  • 打包第三方依赖库的原理就是options.name可以选择现有的chunk,entry中将所有的第三方依赖库放入vendor中,把minChunks设置为Infinity,其他的共有模块就打不进来,这个vendor就作为一个公共模块单独出来了

案例

打包多个公共模块

pageA.js

require("./modules/a-b-c");
require("./modules/a-b");
require("./modules/a-c");

pageB.js

require("./modules/a-b-c");
require("./modules/a-b");
require("./modules/a-c");

pageC.js

require("./modules/a-b-c");
require("./modules/a-b");
require("./modules/b-c");

adminPageA.js

require("./modules/a-b-c");
require("./modules/admin");

adminPageB.js

require("./modules/a-b-c");
require("./modules/admin");

adminPageC.js

require("./modules/a-b-c");
require("./modules/admin");

webpack.config.js

var path = require("path");
var CommonsChunkPlugin = require("../../lib/optimize/CommonsChunkPlugin");
module.exports = {
    entry: {
        pageA: "./pageA",
        pageB: "./pageB",
        pageC: "./pageC",
        adminPageA: "./adminPageA",
        adminPageB: "./adminPageB",
        adminPageC: "./adminPageC",
    },
    output: {
        path: path.join(__dirname, "js"),
        filename: "[name].js"
    },
    plugins: [
        new CommonsChunkPlugin({
            name: "admin-commons",
            chunks: ["adminPageA", "adminPageB"]
        }),
        new CommonsChunkPlugin({
            name: "commons",
            chunks: ["pageA", "pageB", "admin-commons"],
            minChunks: 2
        }),
        new CommonsChunkPlugin({
            name: "c-commons",
            chunks: ["pageC", "adminPageC"]
        }),
    ]
}

info

Hash: 295aa8d9fd845ec1d1b0
Version: webpack 2.2.0-rc.2
           Asset       Size  Chunks             Chunk Names
        pageC.js  768 bytes       0  [emitted]  pageC
        pageB.js  567 bytes       1  [emitted]  pageB
        pageA.js  567 bytes       2  [emitted]  pageA
   adminPageC.js  544 bytes    3, 4  [emitted]  adminPageC
admin-commons.js  233 bytes       4  [emitted]  admin-commons
   adminPageB.js  337 bytes       5  [emitted]  adminPageB
   adminPageA.js  337 bytes       6  [emitted]  adminPageA
      commons.js       6 kB    7, 8  [emitted]  commons
    c-commons.js    5.76 kB       8  [emitted]  c-commons
Entrypoint pageA = commons.js pageA.js
Entrypoint pageB = commons.js pageB.js
Entrypoint pageC = c-commons.js pageC.js
Entrypoint adminPageA = commons.js admin-commons.js adminPageA.js
Entrypoint adminPageB = commons.js admin-commons.js adminPageB.js
Entrypoint adminPageC = c-commons.js adminPageC.js

同时打包公共模块和vendor

有如下依赖:

  • pageA
  • utility1
  • utility2
  • pageB
  • utility2
  • utility3
  • pageC
  • utility2
  • utility3

最终想产生这样的包关系

  • vendor
  • webpack runtime
  • vendor1
  • vendor2
  • common
  • utility2
  • utility3
  • pageA
  • pageA
  • utility1
  • pageB
  • pageB
  • pageC
  • pageC

webpack.config.js

var path = require("path");
var CommonsChunkPlugin = require("../../lib/optimize/CommonsChunkPlugin");

module.exports = {
    entry: {
        vendor: ["./vendor1", "./vendor2"],
        pageA: "./pageA",
        pageB: "./pageB",
        pageC: "./pageC"
        // older versions of webpack may require an empty entry point declaration here
        // common: []
    },
    output: {
        path: path.join(__dirname, "js"),
        filename: "[name].js"
    },
    plugins: [
        new CommonsChunkPlugin({
            // The order of this array matters
            names: ["common", "vendor"],
            minChunks: 2
        })
    ]
};

原理就是names: ["common", "vendor"]会将公共的部分打包进common然后再将第三方依赖库放进vendor中

其中有三个个疑问:

  • common在打包的时候会不会将vendor的chunk事先排除在外
  • vendor在打包的时候会将已经打好包的common排除在外
  • minChunks等参数对common、vendor是否都有效

刚刚考虑一下,觉得上面的问题有了答案:

  • 第一个不会,官网意思很明确“如果传递一个字符串数组,这等于为每个块名称多次调用插件”,所以相当于是设置了两个CommonsChunkPlugin,但是否存在vendor打包进common(pageA等可能引用vendor,但是vendor不会引用自身,故不存在公共模块,肯定不会打进common),当name值和entry入口相同的时候(vendor),此时就是取到抽离chunk的目的。
  • 第二个其实chunks为[vendor, pageA, pageB, pageC,common]
  • 第三个应该是对的

参考

  1. 官网说明
  2. webpack/examples
  3. 知乎-怎么分别打包第三方库,公用组件和业务代码
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,525评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,203评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,862评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,728评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,743评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,590评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,330评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,244评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,693评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,885评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,001评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,723评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,343评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,919评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,042评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,191评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,955评论 2 355

推荐阅读更多精彩内容