diff --git a/lib/optimize/AggressiveMergingPlugin.js b/lib/optimize/AggressiveMergingPlugin.js index 3c09fe0da94..b26788eebab 100644 --- a/lib/optimize/AggressiveMergingPlugin.js +++ b/lib/optimize/AggressiveMergingPlugin.js @@ -31,86 +31,80 @@ class AggressiveMergingPlugin { for(let i = 0; i < idx; i++) { const b = chunks[i]; if(b.isInitial()) continue; - combinations.push([b, a]); + combinations.push({ + a, + b, + improvement: undefined + }); } }); combinations.forEach((pair) => { - const a = pair[0].size({ + const a = pair.b.size({ chunkOverhead: 0 }); - const b = pair[1].size({ + const b = pair.a.size({ chunkOverhead: 0 }); - const ab = pair[0].integratedSize(pair[1], { + const ab = pair.b.integratedSize(pair.a, { chunkOverhead: 0 }); - pair.push({ - a: a, - b: b, - ab: ab - }); let newSize; if(ab === false) { - pair.unshift(false); + pair.improvement = false; + return; } else if(options.moveToParents) { const aOnly = ab - b; const bOnly = ab - a; const common = a + b - ab; - newSize = common + getParentsWeight(pair[0]) * aOnly + getParentsWeight(pair[1]) * bOnly; - pair.push({ - aOnly: aOnly, - bOnly: bOnly, - common: common, - newSize: newSize - }); + newSize = common + getParentsWeight(pair.b) * aOnly + getParentsWeight(pair.a) * bOnly; } else { newSize = ab; } - pair.unshift((a + b) / newSize); + pair.improvement = (a + b) / newSize; }); combinations = combinations.filter((pair) => { - return pair[0] !== false; + return pair.improvement !== false; }); combinations.sort((a, b) => { - return b[0] - a[0]; + return b.improvement - a.improvement; }); const pair = combinations[0]; if(!pair) return; - if(pair[0] < minSizeReduce) return; + if(pair.improvement < minSizeReduce) return; if(options.moveToParents) { - const commonModules = pair[1].modules.filter((m) => { - return pair[2].modules.indexOf(m) >= 0; + const commonModules = pair.b.modules.filter((m) => { + return pair.a.modules.indexOf(m) >= 0; }); - const aOnlyModules = pair[1].modules.filter((m) => { + const aOnlyModules = pair.b.modules.filter((m) => { return commonModules.indexOf(m) < 0; }); - const bOnlyModules = pair[2].modules.filter((m) => { + const bOnlyModules = pair.a.modules.filter((m) => { return commonModules.indexOf(m) < 0; }); aOnlyModules.forEach((m) => { - pair[1].removeModule(m); - m.removeChunk(pair[1]); - pair[1].parents.forEach((c) => { + pair.b.removeModule(m); + m.removeChunk(pair.b); + pair.b.parents.forEach((c) => { c.addModule(m); m.addChunk(c); }); }); bOnlyModules.forEach((m) => { - pair[2].removeModule(m); - m.removeChunk(pair[2]); - pair[2].parents.forEach((c) => { + pair.a.removeModule(m); + m.removeChunk(pair.a); + pair.a.parents.forEach((c) => { c.addModule(m); m.addChunk(c); }); }); } - if(pair[1].integrate(pair[2], "aggressive-merge")) { - chunks.splice(chunks.indexOf(pair[2]), 1); + if(pair.b.integrate(pair.a, "aggressive-merge")) { + chunks.splice(chunks.indexOf(pair.a), 1); return true; } });