2013-01-31 01:49:25 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
|
|
|
function LimitChunkCountPlugin(options) {
|
2015-05-28 00:46:47 +08:00
|
|
|
if(options !== undefined && typeof options !== "object" || Array.isArray(options)) {
|
2015-07-21 06:29:53 +08:00
|
|
|
throw new Error("Argument should be an options object.\nFor more info on options, see https://webpack.github.io/docs/list-of-plugins.html");
|
2015-05-28 00:46:47 +08:00
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
this.options = options || {};
|
|
|
|
}
|
|
|
|
module.exports = LimitChunkCountPlugin;
|
|
|
|
|
|
|
|
LimitChunkCountPlugin.prototype.apply = function(compiler) {
|
|
|
|
var options = this.options;
|
|
|
|
compiler.plugin("compilation", function(compilation) {
|
2015-07-07 06:11:13 +08:00
|
|
|
compilation.plugin("optimize-chunks-advanced", function(chunks) {
|
2013-01-31 01:49:25 +08:00
|
|
|
var maxChunks = options.maxChunks;
|
|
|
|
if(!maxChunks) return;
|
|
|
|
if(maxChunks < 1) return;
|
|
|
|
if(chunks.length <= maxChunks) return;
|
|
|
|
|
2013-02-11 17:58:24 +08:00
|
|
|
if(chunks.length > maxChunks) {
|
2013-01-31 01:49:25 +08:00
|
|
|
var combinations = [];
|
|
|
|
chunks.forEach(function(a, idx) {
|
|
|
|
for(var i = 0; i < idx; i++) {
|
|
|
|
var b = chunks[i];
|
|
|
|
combinations.push([b, a]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
combinations.forEach(function(pair) {
|
2013-06-18 00:55:11 +08:00
|
|
|
var a = pair[0].size(options);
|
|
|
|
var b = pair[1].size(options);
|
|
|
|
var ab = pair[0].integratedSize(pair[1], options);
|
2013-01-31 01:49:25 +08:00
|
|
|
pair.unshift(a + b - ab, ab);
|
2015-07-07 06:11:13 +08:00
|
|
|
pair.push(a, b);
|
2013-01-31 01:49:25 +08:00
|
|
|
});
|
2014-02-04 01:12:19 +08:00
|
|
|
combinations = combinations.filter(function(pair) {
|
|
|
|
return pair[1] !== false;
|
|
|
|
});
|
2015-04-24 05:55:50 +08:00
|
|
|
combinations.sort(function(a, b) {
|
2013-01-31 01:49:25 +08:00
|
|
|
var diff = b[0] - a[0];
|
2014-06-25 00:53:32 +08:00
|
|
|
if(diff !== 0) return diff;
|
2013-01-31 01:49:25 +08:00
|
|
|
return a[1] - b[1];
|
|
|
|
});
|
|
|
|
|
|
|
|
var pair = combinations[0];
|
|
|
|
|
2014-07-19 20:32:48 +08:00
|
|
|
if(pair && pair[2].integrate(pair[3], "limit")) {
|
2014-02-04 01:12:19 +08:00
|
|
|
chunks.splice(chunks.indexOf(pair[3]), 1);
|
2015-07-07 06:11:13 +08:00
|
|
|
return true;
|
2014-02-04 01:12:19 +08:00
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|