/
chunkColouring.ts
84 lines (76 loc) · 2.62 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
82
83
84
import ExternalModule from '../ExternalModule';
import Module from '../Module';
import { randomUint8Array, Uint8ArrayXor } from './entryHashing';
import { error } from './error';
import { relative } from './path';
export function assignChunkColouringHashes(
entryModules: Module[],
manualChunkModules: Record<string, Module[]>
) {
let currentEntry: Module, currentEntryHash: Uint8Array;
let modulesVisitedForCurrentEntry: { [id: string]: boolean };
const handledEntryPoints: { [id: string]: boolean } = {};
const dynamicImports: Module[] = [];
const addCurrentEntryColourToModule = (module: Module) => {
if (currentEntry.chunkAlias) {
module.chunkAlias = currentEntry.chunkAlias;
module.entryPointsHash = currentEntryHash;
} else {
Uint8ArrayXor(module.entryPointsHash, currentEntryHash);
}
for (const dependency of module.dependencies) {
if (dependency instanceof ExternalModule || dependency.id in modulesVisitedForCurrentEntry) {
continue;
}
modulesVisitedForCurrentEntry[dependency.id] = true;
if (!handledEntryPoints[dependency.id] && !dependency.chunkAlias)
addCurrentEntryColourToModule(dependency);
}
for (const { resolution } of module.dynamicImports) {
if (
resolution instanceof Module &&
resolution.dynamicallyImportedBy.length > 0 &&
!resolution.chunkAlias
) {
dynamicImports.push(resolution);
}
}
};
if (manualChunkModules) {
for (const chunkName of Object.keys(manualChunkModules)) {
currentEntryHash = randomUint8Array(10);
for (currentEntry of manualChunkModules[chunkName]) {
if (currentEntry.chunkAlias) {
error({
code: 'INVALID_CHUNK',
message: `Cannot assign ${relative(
process.cwd(),
currentEntry.id
)} to the "${chunkName}" chunk as it is already in the "${
currentEntry.chunkAlias
}" chunk.
Try defining "${chunkName}" first in the manualChunks definitions of the Rollup configuration.`
});
}
currentEntry.chunkAlias = chunkName;
modulesVisitedForCurrentEntry = { [currentEntry.id]: true };
addCurrentEntryColourToModule(currentEntry);
}
}
}
for (currentEntry of entryModules) {
handledEntryPoints[currentEntry.id] = true;
currentEntryHash = randomUint8Array(10);
modulesVisitedForCurrentEntry = { [currentEntry.id]: null };
addCurrentEntryColourToModule(currentEntry);
}
for (currentEntry of dynamicImports) {
if (handledEntryPoints[currentEntry.id]) {
continue;
}
handledEntryPoints[currentEntry.id] = true;
currentEntryHash = randomUint8Array(10);
modulesVisitedForCurrentEntry = { [currentEntry.id]: null };
addCurrentEntryColourToModule(currentEntry);
}
}