webpack.optimize.CommonsChunkPlugin的minChunks解析

CommonsChunkPlugin, 顧名思義,是用來把公用模塊打包到一塊兒的插件,以減少打包後js文件的體積。測試

使人迷惑的minChunks

中文社區和官網都對此屬性語焉不詳。插件

首先,minChunks的Chunk是什麼意思?翻譯

…… a separate file (known as a chunk).
意思是當entry屬性的值爲對象時,做爲多個入口的文件們,每一個都是一個chunk。

理解了chunk的定義,再來看看官網對minChunks的解釋:code

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)

須要重點關注的額是minChunks的number值。
官網的解釋我實在看不懂:在被放到共同chunks以前須要包含模塊的chunks的最小數量。
這是什麼鬼意思,有沒有洋文好的大佬翻譯一下?對象

minChunks:number

那麼minChunks的值爲number時,由什麼效果呢?
通過我測試,發現minChunks是指某個模塊最少被多少個入口文件依賴。
當大於等於minChunks設定的值時,該模塊就會被打包到公用包中。
小於這個值時,該模塊就會被和每一個入口文件打包在一塊兒。it

好比,有八個入口文件,minChunks值爲7,那麼,就算某個模塊被6個入口文件依賴了,這個模塊也會被打包6次,每一個依賴他的文件中都有一份相同的代碼。io

minChunks:Infinity

搞懂了minChunks的number屬性,Infinity屬性就很好理解了。也就是不會把任何依賴的模塊提取出來打包公用。社區

minChunks默認值

當忽略此屬性時,只有在被全部入口文件都依賴時,纔會提取相應模塊。function

水平有限,說錯了輕噴。module

相關文章
相關標籤/搜索