forked from rollup/rollup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
executionOrder.ts
94 lines (84 loc) · 2.72 KB
/
executionOrder.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
85
86
87
88
89
90
91
92
93
94
import type ExternalModule from '../ExternalModule';
import Module from '../Module';
interface OrderedExecutionUnit {
execIndex: number;
}
const compareExecIndex = <T extends OrderedExecutionUnit>(unitA: T, unitB: T) =>
unitA.execIndex > unitB.execIndex ? 1 : -1;
export function sortByExecutionOrder(units: OrderedExecutionUnit[]): void {
units.sort(compareExecIndex);
}
// This process is currently faulty in so far as it only takes the first entry
// module into account and assumes that dynamic imports are imported in a
// certain order.
// A better algorithm would follow every possible execution path and mark which
// modules are executed before or after which other modules. THen the chunking
// would need to take care that in each chunk, all modules are always executed
// in the same sequence.
export function analyseModuleExecution(entryModules: readonly Module[]): {
cyclePaths: string[][];
orderedModules: Module[];
} {
let nextExecIndex = 0;
const cyclePaths: string[][] = [];
const analysedModules = new Set<Module | ExternalModule>();
const dynamicImports = new Set<Module>();
const parents = new Map<Module | ExternalModule, Module | null>();
const orderedModules: Module[] = [];
const analyseModule = (module: Module | ExternalModule) => {
if (module instanceof Module) {
for (const dependency of module.dependencies) {
if (parents.has(dependency)) {
if (!analysedModules.has(dependency)) {
cyclePaths.push(getCyclePath(dependency as Module, module, parents));
}
continue;
}
parents.set(dependency, module);
analyseModule(dependency);
}
for (const dependency of module.implicitlyLoadedBefore) {
dynamicImports.add(dependency);
}
for (const { resolution } of module.dynamicImports) {
if (resolution instanceof Module) {
dynamicImports.add(resolution);
}
}
orderedModules.push(module);
}
module.execIndex = nextExecIndex++;
analysedModules.add(module);
};
for (const currentEntry of entryModules) {
if (!parents.has(currentEntry)) {
parents.set(currentEntry, null);
analyseModule(currentEntry);
}
}
for (const currentEntry of dynamicImports) {
if (!parents.has(currentEntry)) {
parents.set(currentEntry, null);
analyseModule(currentEntry);
}
}
return { cyclePaths, orderedModules };
}
function getCyclePath(
module: Module,
parent: Module,
parents: ReadonlyMap<Module | ExternalModule, Module | null>
): string[] {
const cycleSymbol = Symbol(module.id);
const path = [module.id];
let nextModule = parent;
module.cycles.add(cycleSymbol);
while (nextModule !== module) {
nextModule.cycles.add(cycleSymbol);
path.push(nextModule.id);
nextModule = parents.get(nextModule)!;
}
path.push(path[0]);
path.reverse();
return path;
}