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