Skip to content

Commit

Permalink
Merge branch 'master' into deps/i18n-webpack-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Jul 24, 2017
2 parents 54c79e6 + e72a88a commit 98a7cb6
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 101 deletions.
60 changes: 27 additions & 33 deletions lib/optimize/AggressiveMergingPlugin.js
Expand Up @@ -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;
}
});
Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -25,7 +25,7 @@
"uglifyjs-webpack-plugin": "^0.4.6",
"watchpack": "^1.4.0",
"webpack-sources": "^1.0.1",
"yargs": "^6.0.0"
"yargs": "^8.0.2"
},
"license": "MIT",
"devDependencies": {
Expand All @@ -43,7 +43,7 @@
"eslint-plugin-node": "^5.1.1",
"express": "~4.13.1",
"extract-text-webpack-plugin": "^2.0.0-beta",
"file-loader": "^0.11.1",
"file-loader": "^0.11.2",
"i18n-webpack-plugin": "^1.0.0",
"istanbul": "^0.4.5",
"jade": "^1.11.0",
Expand Down

0 comments on commit 98a7cb6

Please sign in to comment.