Skip to content

Commit 2949c59

Browse files
committed
refactor for more readablity
1 parent e2dfdec commit 2949c59

1 file changed

Lines changed: 27 additions & 33 deletions

File tree

lib/optimize/AggressiveMergingPlugin.js

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,86 +31,80 @@ class AggressiveMergingPlugin {
3131
for(let i = 0; i < idx; i++) {
3232
const b = chunks[i];
3333
if(b.isInitial()) continue;
34-
combinations.push([b, a]);
34+
combinations.push({
35+
a,
36+
b,
37+
improvement: undefined
38+
});
3539
}
3640
});
3741

3842
combinations.forEach((pair) => {
39-
const a = pair[0].size({
43+
const a = pair.b.size({
4044
chunkOverhead: 0
4145
});
42-
const b = pair[1].size({
46+
const b = pair.a.size({
4347
chunkOverhead: 0
4448
});
45-
const ab = pair[0].integratedSize(pair[1], {
49+
const ab = pair.b.integratedSize(pair.a, {
4650
chunkOverhead: 0
4751
});
48-
pair.push({
49-
a: a,
50-
b: b,
51-
ab: ab
52-
});
5352
let newSize;
5453
if(ab === false) {
55-
pair.unshift(false);
54+
pair.improvement = false;
55+
return;
5656
} else if(options.moveToParents) {
5757
const aOnly = ab - b;
5858
const bOnly = ab - a;
5959
const common = a + b - ab;
60-
newSize = common + getParentsWeight(pair[0]) * aOnly + getParentsWeight(pair[1]) * bOnly;
61-
pair.push({
62-
aOnly: aOnly,
63-
bOnly: bOnly,
64-
common: common,
65-
newSize: newSize
66-
});
60+
newSize = common + getParentsWeight(pair.b) * aOnly + getParentsWeight(pair.a) * bOnly;
6761
} else {
6862
newSize = ab;
6963
}
7064

71-
pair.unshift((a + b) / newSize);
65+
pair.improvement = (a + b) / newSize;
7266
});
7367
combinations = combinations.filter((pair) => {
74-
return pair[0] !== false;
68+
return pair.improvement !== false;
7569
});
7670
combinations.sort((a, b) => {
77-
return b[0] - a[0];
71+
return b.improvement - a.improvement;
7872
});
7973

8074
const pair = combinations[0];
8175

8276
if(!pair) return;
83-
if(pair[0] < minSizeReduce) return;
77+
if(pair.improvement < minSizeReduce) return;
8478

8579
if(options.moveToParents) {
86-
const commonModules = pair[1].modules.filter((m) => {
87-
return pair[2].modules.indexOf(m) >= 0;
80+
const commonModules = pair.b.modules.filter((m) => {
81+
return pair.a.modules.indexOf(m) >= 0;
8882
});
89-
const aOnlyModules = pair[1].modules.filter((m) => {
83+
const aOnlyModules = pair.b.modules.filter((m) => {
9084
return commonModules.indexOf(m) < 0;
9185
});
92-
const bOnlyModules = pair[2].modules.filter((m) => {
86+
const bOnlyModules = pair.a.modules.filter((m) => {
9387
return commonModules.indexOf(m) < 0;
9488
});
9589
aOnlyModules.forEach((m) => {
96-
pair[1].removeModule(m);
97-
m.removeChunk(pair[1]);
98-
pair[1].parents.forEach((c) => {
90+
pair.b.removeModule(m);
91+
m.removeChunk(pair.b);
92+
pair.b.parents.forEach((c) => {
9993
c.addModule(m);
10094
m.addChunk(c);
10195
});
10296
});
10397
bOnlyModules.forEach((m) => {
104-
pair[2].removeModule(m);
105-
m.removeChunk(pair[2]);
106-
pair[2].parents.forEach((c) => {
98+
pair.a.removeModule(m);
99+
m.removeChunk(pair.a);
100+
pair.a.parents.forEach((c) => {
107101
c.addModule(m);
108102
m.addChunk(c);
109103
});
110104
});
111105
}
112-
if(pair[1].integrate(pair[2], "aggressive-merge")) {
113-
chunks.splice(chunks.indexOf(pair[2]), 1);
106+
if(pair.b.integrate(pair.a, "aggressive-merge")) {
107+
chunks.splice(chunks.indexOf(pair.a), 1);
114108
return true;
115109
}
116110
});

0 commit comments

Comments
 (0)