-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
chunkColouring.ts
81 lines (73 loc) · 2.42 KB
/
chunkColouring.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import ExternalModule from '../ExternalModule';
import Module from '../Module';
import { randomUint8Array, Uint8ArrayXor } from './entryHashing';
export function assignChunkColouringHashes(
entryModules: Module[],
manualChunkModules: Record<string, Module[]>
) {
let currentEntry: Module, currentEntryHash: Uint8Array;
let modulesVisitedForCurrentEntry: Set<string>;
const handledEntryPoints: Set<string> = new Set();
const dynamicImports: Module[] = [];
const addCurrentEntryColourToModule = (module: Module) => {
if (currentEntry.manualChunkAlias) {
module.manualChunkAlias = currentEntry.manualChunkAlias;
module.entryPointsHash = currentEntryHash;
} else {
Uint8ArrayXor(module.entryPointsHash, currentEntryHash);
}
for (const { resolution } of module.dynamicImports) {
if (resolution instanceof Module && resolution.inlineDynamicImport) {
module.dependencies.push(resolution);
}
}
for (const dependency of module.dependencies) {
if (
dependency instanceof ExternalModule ||
modulesVisitedForCurrentEntry.has(dependency.id)
) {
continue;
}
modulesVisitedForCurrentEntry.add(dependency.id);
if (!handledEntryPoints.has(dependency.id) && !dependency.manualChunkAlias) {
addCurrentEntryColourToModule(dependency);
}
}
for (const { resolution } of module.dynamicImports) {
if (
resolution instanceof Module &&
!resolution.inlineDynamicImport &&
resolution.dynamicallyImportedBy.length > 0 &&
!resolution.manualChunkAlias
) {
dynamicImports.push(resolution);
}
}
};
if (manualChunkModules) {
for (const chunkName of Object.keys(manualChunkModules)) {
currentEntryHash = randomUint8Array(10);
for (currentEntry of manualChunkModules[chunkName]) {
modulesVisitedForCurrentEntry = new Set(currentEntry.id);
addCurrentEntryColourToModule(currentEntry);
}
}
}
for (currentEntry of entryModules) {
handledEntryPoints.add(currentEntry.id);
currentEntryHash = randomUint8Array(10);
modulesVisitedForCurrentEntry = new Set(currentEntry.id);
if (!currentEntry.manualChunkAlias) {
addCurrentEntryColourToModule(currentEntry);
}
}
for (currentEntry of dynamicImports) {
if (handledEntryPoints.has(currentEntry.id)) {
continue;
}
handledEntryPoints.add(currentEntry.id);
currentEntryHash = randomUint8Array(10);
modulesVisitedForCurrentEntry = new Set(currentEntry.id);
addCurrentEntryColourToModule(currentEntry);
}
}