Skip to content

Commit 1738215

Browse files
authored
Sort manual chunks generated via a function by name (#4397)
* Sort manual chunks via functions are sorted by name * Improve test
1 parent 5f4d04f commit 1738215

15 files changed

Lines changed: 66 additions & 3 deletions

File tree

src/Bundle.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ export default class Bundle {
156156
}
157157

158158
private assignManualChunks(getManualChunk: GetManualChunk): Map<Module, string> {
159-
const manualChunkAliasByEntry = new Map<Module, string>();
159+
const manualChunkAliasesWithEntry: [alias: string, module: Module][] = [];
160160
const manualChunksApi = {
161161
getModuleIds: () => this.graph.modulesById.keys(),
162162
getModuleInfo: this.graph.getModuleInfo
@@ -165,10 +165,17 @@ export default class Bundle {
165165
if (module instanceof Module) {
166166
const manualChunkAlias = getManualChunk(module.id, manualChunksApi);
167167
if (typeof manualChunkAlias === 'string') {
168-
addModuleToManualChunk(manualChunkAlias, module, manualChunkAliasByEntry);
168+
manualChunkAliasesWithEntry.push([manualChunkAlias, module]);
169169
}
170170
}
171171
}
172+
manualChunkAliasesWithEntry.sort(([aliasA], [aliasB]) =>
173+
aliasA > aliasB ? 1 : aliasA < aliasB ? -1 : 0
174+
);
175+
const manualChunkAliasByEntry = new Map<Module, string>();
176+
for (const [alias, module] of manualChunkAliasesWithEntry) {
177+
addModuleToManualChunk(alias, module, manualChunkAliasByEntry);
178+
}
172179
return manualChunkAliasByEntry;
173180
}
174181

test/chunking-form/samples/manual-chunks-function/_config.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const assert = require('assert');
2+
13
module.exports = {
24
description: 'allows to define manual chunks via a function',
35
options: {
@@ -8,6 +10,19 @@ module.exports = {
810
return `chunk-${id[id.length - 4]}`;
911
}
1012
}
11-
}
13+
},
14+
plugins: [
15+
{
16+
generateBundle(options, bundle) {
17+
// This also asserts the sorting order, which should be alphabetical
18+
// between the manual chunks
19+
assert.deepStrictEqual(Object.keys(bundle), [
20+
'main-a.js',
21+
'generated-chunk-b.js',
22+
'generated-chunk-c.js'
23+
]);
24+
}
25+
}
26+
]
1227
}
1328
};

test/chunking-form/samples/manual-chunks-function/_expected/amd/generated-chunk-b.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ define((function () { 'use strict';
44

55
console.log('dep-b');
66

7+
console.log('dep2-b');
8+
9+
console.log('dep3-b');
10+
711
}));

test/chunking-form/samples/manual-chunks-function/_expected/amd/main-a.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ define(['./generated-chunk-c', './generated-chunk-b'], (function (chunkC, chunkB
44

55
console.log('dep-a');
66

7+
console.log('dep2-a');
8+
9+
console.log('dep3-a');
10+
711
console.log('main-a');
812

913
}));

test/chunking-form/samples/manual-chunks-function/_expected/cjs/generated-chunk-b.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33
console.log('dep2');
44

55
console.log('dep-b');
6+
7+
console.log('dep2-b');
8+
9+
console.log('dep3-b');

test/chunking-form/samples/manual-chunks-function/_expected/cjs/main-a.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@ console.log('dep1');
77

88
console.log('dep-a');
99

10+
console.log('dep2-a');
11+
12+
console.log('dep3-a');
13+
1014
console.log('main-a');
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
console.log('dep2');
22

33
console.log('dep-b');
4+
5+
console.log('dep2-b');
6+
7+
console.log('dep3-b');

test/chunking-form/samples/manual-chunks-function/_expected/es/main-a.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@ console.log('dep1');
55

66
console.log('dep-a');
77

8+
console.log('dep2-a');
9+
10+
console.log('dep3-a');
11+
812
console.log('main-a');

test/chunking-form/samples/manual-chunks-function/_expected/system/generated-chunk-b.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ System.register([], (function () {
77

88
console.log('dep-b');
99

10+
console.log('dep2-b');
11+
12+
console.log('dep3-b');
13+
1014
})
1115
};
1216
}));

test/chunking-form/samples/manual-chunks-function/_expected/system/main-a.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ System.register(['./generated-chunk-c.js', './generated-chunk-b.js'], (function
88

99
console.log('dep-a');
1010

11+
console.log('dep2-a');
12+
13+
console.log('dep3-a');
14+
1115
console.log('main-a');
1216

1317
})

0 commit comments

Comments
 (0)