Skip to content

Commit 2a1546d

Browse files
committed
fix reversed order in OccurenceOrderPlugin
try to keep original order fallback order by name if index order fails
1 parent ba00d74 commit 2a1546d

File tree

31 files changed

+561
-549
lines changed

31 files changed

+561
-549
lines changed

lib/Compilation.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,10 @@ class Compilation extends Tapable {
838838
modules.sort((a, b) => {
839839
if(a.index < b.index) return -1;
840840
if(a.index > b.index) return 1;
841+
const identA = a.identifier();
842+
const identB = b.identifier();
843+
if(identA < identB) return -1;
844+
if(identA > identB) return 1;
841845
return 0;
842846
});
843847
}

lib/optimize/OccurrenceOrderPlugin.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,12 @@ class OccurrenceOrderPlugin {
5151
});
5252
}
5353

54+
const originalOrder = new Map();
55+
let i = 0;
5456
modules.forEach(m => {
5557
const result = m.reasons.reduce(countOccurs, 0) + m.getNumberOfChunks() + entryCountMap.get(m);
5658
occursInAllChunksMap.set(m, result);
59+
originalOrder.set(m, i++);
5760
});
5861

5962
modules.sort((a, b) => {
@@ -67,20 +70,23 @@ class OccurrenceOrderPlugin {
6770
const bOccurs = occursInAllChunksMap.get(b);
6871
if(aOccurs > bOccurs) return -1;
6972
if(aOccurs < bOccurs) return 1;
70-
if(a.index > b.index) return 1;
71-
if(a.index < b.index) return -1;
72-
return 0;
73+
const orgA = originalOrder.get(a);
74+
const orgB = originalOrder.get(b);
75+
return orgB - orgA;
7376
});
7477
});
7578
compilation.hooks.optimizeChunkOrder.tap("OccurrenceOrderPlugin", (chunks) => {
7679
const occursInInitialChunksMap = new Map();
80+
const originalOrder = new Map();
7781

82+
let i = 0;
7883
chunks.forEach(c => {
7984
const result = c.getParents().reduce((sum, p) => {
8085
if(p.isInitial()) return sum + 1;
8186
return sum;
8287
}, 0);
83-
return occursInInitialChunksMap.set(c, result);
88+
occursInInitialChunksMap.set(c, result);
89+
originalOrder.set(c, i++);
8490
});
8591

8692
const occurs = c => {
@@ -96,7 +102,9 @@ class OccurrenceOrderPlugin {
96102
const bOccurs = occurs(b);
97103
if(aOccurs > bOccurs) return -1;
98104
if(aOccurs < bOccurs) return 1;
99-
return a.compareTo(b);
105+
const orgA = originalOrder.get(a);
106+
const orgB = originalOrder.get(b);
107+
return orgB - orgA;
100108
});
101109
});
102110
});
Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
1-
Hash: a1332ffdc8f016e06e2da1332ffdc8f016e06e2d
1+
Hash: 0a5db84405553d6f7edc0a5db84405553d6f7edc
22
Child fitting:
3-
Hash: a1332ffdc8f016e06e2d
3+
Hash: 0a5db84405553d6f7edc
44
Time: Xms
55
Asset Size Chunks Chunk Names
66
8687608dde0469fd3fba.js 2.23 KiB 0 [emitted]
7-
76eb20f234cf58f9823b.js 1.93 KiB 4 [emitted]
8-
a1f333026966f9f4cd86.js 5.78 KiB 5 [emitted]
9-
3dad677280e44d774e7d.js 1 KiB 6 [emitted]
10-
Entrypoint main = a1f333026966f9f4cd86.js 3dad677280e44d774e7d.js 76eb20f234cf58f9823b.js 8687608dde0469fd3fba.js
7+
47e0fac01fc36a2064e8.js 5.78 KiB 4 [emitted]
8+
f1e5018e4333f2466ecc.js 1 KiB 5 [emitted]
9+
ddccfa0cc7de77007a74.js 1.93 KiB 6 [emitted]
10+
Entrypoint main = 47e0fac01fc36a2064e8.js f1e5018e4333f2466ecc.js ddccfa0cc7de77007a74.js 8687608dde0469fd3fba.js
1111
chunk {0} 8687608dde0469fd3fba.js 1.87 KiB [initial] [rendered]
1212
> aggressive-splitted main [4] ./index.js
1313
[4] ./index.js 112 bytes {0} [built]
1414
[6] ./f.js 899 bytes {0} [built]
1515
[7] ./g.js 899 bytes {0} [built]
16-
chunk {4} 76eb20f234cf58f9823b.js 1.76 KiB [initial] [rendered] [recorded]
16+
chunk {4} 47e0fac01fc36a2064e8.js 1.76 KiB [entry] [rendered] [recorded]
1717
> aggressive-splitted main [4] ./index.js
18-
[0] ./b.js 899 bytes {4} [built]
19-
[1] ./c.js 899 bytes {4} [built]
20-
chunk {5} a1f333026966f9f4cd86.js 1.76 KiB [entry] [rendered] [recorded]
18+
[2] ./d.js 899 bytes {4} [built]
19+
[5] ./a.js 899 bytes {4} [built]
20+
chunk {5} f1e5018e4333f2466ecc.js 899 bytes [initial] [rendered]
2121
> aggressive-splitted main [4] ./index.js
22-
[2] ./d.js 899 bytes {5} [built]
23-
[5] ./a.js 899 bytes {5} [built]
24-
chunk {6} 3dad677280e44d774e7d.js 899 bytes [initial] [rendered]
22+
[3] ./e.js 899 bytes {5} [built]
23+
chunk {6} ddccfa0cc7de77007a74.js 1.76 KiB [initial] [rendered] [recorded]
2524
> aggressive-splitted main [4] ./index.js
26-
[3] ./e.js 899 bytes {6} [built]
25+
[0] ./b.js 899 bytes {6} [built]
26+
[1] ./c.js 899 bytes {6} [built]
2727
Child content-change:
28-
Hash: a1332ffdc8f016e06e2d
28+
Hash: 0a5db84405553d6f7edc
2929
Time: Xms
3030
Asset Size Chunks Chunk Names
3131
8687608dde0469fd3fba.js 2.23 KiB 0 [emitted]
32-
76eb20f234cf58f9823b.js 1.93 KiB 4 [emitted]
33-
a1f333026966f9f4cd86.js 5.78 KiB 5 [emitted]
34-
3dad677280e44d774e7d.js 1 KiB 6 [emitted]
35-
Entrypoint main = a1f333026966f9f4cd86.js 3dad677280e44d774e7d.js 76eb20f234cf58f9823b.js 8687608dde0469fd3fba.js
32+
47e0fac01fc36a2064e8.js 5.78 KiB 4 [emitted]
33+
f1e5018e4333f2466ecc.js 1 KiB 5 [emitted]
34+
ddccfa0cc7de77007a74.js 1.93 KiB 6 [emitted]
35+
Entrypoint main = 47e0fac01fc36a2064e8.js f1e5018e4333f2466ecc.js ddccfa0cc7de77007a74.js 8687608dde0469fd3fba.js
3636
chunk {0} 8687608dde0469fd3fba.js 1.87 KiB [initial] [rendered]
3737
> aggressive-splitted main [4] ./index.js
3838
[4] ./index.js 112 bytes {0} [built]
3939
[6] ./f.js 899 bytes {0} [built]
4040
[7] ./g.js 899 bytes {0} [built]
41-
chunk {4} 76eb20f234cf58f9823b.js 1.76 KiB [initial] [rendered] [recorded]
41+
chunk {4} 47e0fac01fc36a2064e8.js 1.76 KiB [entry] [rendered] [recorded]
4242
> aggressive-splitted main [4] ./index.js
43-
[0] ./b.js 899 bytes {4} [built]
44-
[1] ./c.js 899 bytes {4} [built]
45-
chunk {5} a1f333026966f9f4cd86.js 1.76 KiB [entry] [rendered] [recorded]
43+
[2] ./d.js 899 bytes {4} [built]
44+
[5] ./a.js 899 bytes {4} [built]
45+
chunk {5} f1e5018e4333f2466ecc.js 899 bytes [initial] [rendered]
4646
> aggressive-splitted main [4] ./index.js
47-
[2] ./d.js 899 bytes {5} [built]
48-
[5] ./a.js 899 bytes {5} [built]
49-
chunk {6} 3dad677280e44d774e7d.js 899 bytes [initial] [rendered]
47+
[3] ./e.js 899 bytes {5} [built]
48+
chunk {6} ddccfa0cc7de77007a74.js 1.76 KiB [initial] [rendered] [recorded]
5049
> aggressive-splitted main [4] ./index.js
51-
[3] ./e.js 899 bytes {6} [built]
50+
[0] ./b.js 899 bytes {6} [built]
51+
[1] ./c.js 899 bytes {6} [built]
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
chunk {0} 0.js 21 bytes {3} [rendered]
2-
> async commons [1] ./index.js 2:1-5:3
3-
> async commons [1] ./index.js 9:1-13:3
4-
> async commons [1] ./index.js 17:1-21:3
2+
> async commons [3] ./index.js 2:1-5:3
3+
> async commons [3] ./index.js 9:1-13:3
4+
> async commons [3] ./index.js 17:1-21:3
55
[0] ./a.js 21 bytes {0} [built]
66
chunk {1} 1.js 21 bytes {3} [rendered]
7-
> [1] ./index.js 17:1-21:3
8-
[3] ./c.js 21 bytes {1} [built]
7+
> [3] ./index.js 9:1-13:3
8+
[1] ./b.js 21 bytes {1} [built]
99
chunk {2} 2.js 21 bytes {3} [rendered]
10-
> [1] ./index.js 9:1-13:3
11-
[2] ./b.js 21 bytes {2} [built]
10+
> [3] ./index.js 17:1-21:3
11+
[2] ./c.js 21 bytes {2} [built]
1212
chunk {3} entry.js (entry) 550 bytes [entry] [rendered]
13-
> entry [1] ./index.js
14-
[1] ./index.js 550 bytes {3} [built]
13+
> entry [3] ./index.js
14+
[3] ./index.js 550 bytes {3} [built]
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
chunk {0} 0.js 21 bytes {3} [rendered]
2-
> async commons [1] ./index.js 2:1-5:3
3-
> async commons [1] ./index.js 9:1-13:3
4-
> async commons [1] ./index.js 17:1-21:3
2+
> async commons [3] ./index.js 2:1-5:3
3+
> async commons [3] ./index.js 9:1-13:3
4+
> async commons [3] ./index.js 17:1-21:3
55
[0] ./a.js 21 bytes {0} [built]
66
chunk {1} 1.js 21 bytes {3} [rendered]
7-
> [1] ./index.js 17:1-21:3
8-
[3] ./c.js 21 bytes {1} [built]
7+
> [3] ./index.js 9:1-13:3
8+
[1] ./b.js 21 bytes {1} [built]
99
chunk {2} 2.js 21 bytes {3} [rendered]
10-
> [1] ./index.js 9:1-13:3
11-
[2] ./b.js 21 bytes {2} [built]
10+
> [3] ./index.js 17:1-21:3
11+
[2] ./c.js 21 bytes {2} [built]
1212
chunk {3} main.js (main) 550 bytes [entry] [rendered]
13-
> main [1] ./index.js
14-
[1] ./index.js 550 bytes {3} [built]
13+
> main [3] ./index.js
14+
[3] ./index.js 550 bytes {3} [built]

test/statsCases/chunks/expected.txt

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
Hash: cbb94bfd28c61629de06
1+
Hash: ee41f06ebdd1a9a95e6a
22
Time: Xms
33
Asset Size Chunks Chunk Names
4-
0.bundle.js 288 bytes 0 [emitted]
5-
1.bundle.js 152 bytes 1 [emitted]
6-
2.bundle.js 232 bytes 2 [emitted]
7-
bundle.js 6.95 KiB 3 [emitted] main
8-
chunk {0} 0.bundle.js 54 bytes {3} [rendered]
9-
> [0] ./index.js 3:0-16
10-
[3] ./c.js 54 bytes {0} [built]
11-
amd require ./c [0] ./index.js 3:0-16
12-
[0] Xms -> factory:Xms building:Xms = Xms
13-
chunk {1} 1.bundle.js 22 bytes {3} [rendered]
14-
> [0] ./index.js 2:0-16
15-
[2] ./b.js 22 bytes {1} [built]
16-
amd require ./b [0] ./index.js 2:0-16
17-
[0] Xms -> factory:Xms building:Xms = Xms
18-
chunk {2} 2.bundle.js 44 bytes {0} [rendered]
4+
0.bundle.js 152 bytes 0 [emitted]
5+
1.bundle.js 288 bytes 1 [emitted]
6+
2.bundle.js 227 bytes 2 [emitted]
7+
bundle.js 6.96 KiB 3 [emitted] main
8+
chunk {0} 0.bundle.js 22 bytes {3} [rendered]
9+
> [4] ./index.js 2:0-16
10+
[2] ./b.js 22 bytes {0} [built]
11+
amd require ./b [4] ./index.js 2:0-16
12+
[4] Xms -> factory:Xms building:Xms = Xms
13+
chunk {1} 1.bundle.js 54 bytes {3} [rendered]
14+
> [4] ./index.js 3:0-16
15+
[3] ./c.js 54 bytes {1} [built]
16+
amd require ./c [4] ./index.js 3:0-16
17+
[4] Xms -> factory:Xms building:Xms = Xms
18+
chunk {2} 2.bundle.js 44 bytes {1} [rendered]
1919
> [3] ./c.js 1:0-52
20-
[4] ./d.js 22 bytes {2} [built]
20+
[0] ./d.js 22 bytes {2} [built]
2121
require.ensure item ./d [3] ./c.js 1:0-52
22-
[0] Xms -> [3] Xms -> factory:Xms building:Xms = Xms
23-
[5] ./e.js 22 bytes {2} [built]
22+
[4] Xms -> [3] Xms -> factory:Xms building:Xms = Xms
23+
[1] ./e.js 22 bytes {2} [built]
2424
require.ensure item ./e [3] ./c.js 1:0-52
25-
[0] Xms -> [3] Xms -> factory:Xms building:Xms = Xms
25+
[4] Xms -> [3] Xms -> factory:Xms building:Xms = Xms
2626
chunk {3} bundle.js (main) 73 bytes [entry] [rendered]
27-
> main [0] ./index.js
28-
[0] ./index.js 51 bytes {3} [built]
27+
> main [4] ./index.js
28+
[4] ./index.js 51 bytes {3} [built]
2929
single entry ./index main
3030
factory:Xms building:Xms = Xms
31-
[1] ./a.js 22 bytes {3} [built]
32-
cjs require ./a [0] ./index.js 1:0-14
33-
[0] Xms -> factory:Xms building:Xms = Xms
31+
[5] ./a.js 22 bytes {3} [built]
32+
cjs require ./a [4] ./index.js 1:0-14
33+
[4] Xms -> factory:Xms building:Xms = Xms
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
chunk {0} 0.bundle.js (b) 49 bytes {2} {3} [rendered]
2-
[2] ./module-b.js 49 bytes {0} [built]
3-
chunk {1} 1.bundle.js (a) 49 bytes {2} {3} [rendered]
4-
[0] ./module-a.js 49 bytes {1} [built]
1+
chunk {0} 0.bundle.js (a) 49 bytes {2} {3} [rendered]
2+
[1] ./module-a.js 49 bytes {0} [built]
3+
chunk {1} 1.bundle.js (b) 49 bytes {2} {3} [rendered]
4+
[2] ./module-b.js 49 bytes {1} [built]
55
chunk {2} 2.bundle.js (c) 98 bytes {0} {1} [rendered]
6-
[1] ./module-c.js 98 bytes {2} [built]
6+
[0] ./module-c.js 98 bytes {2} [built]
77
chunk {3} bundle.js (main) 98 bytes [entry] [rendered]
88
[3] ./index.js 98 bytes {3} [built]

0 commit comments

Comments
 (0)