-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
Expand file tree
/
Copy pathCircularModulesPlugin.js
More file actions
190 lines (170 loc) · 5.26 KB
/
Copy pathCircularModulesPlugin.js
File metadata and controls
190 lines (170 loc) · 5.26 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Natsu @xiaoxiaojx
*/
"use strict";
/** @typedef {import("./Compiler")} Compiler */
/** @typedef {import("./ModuleGraph")} ModuleGraph */
/** @typedef {import("./Module")} Module */
/** @typedef {import("./Module").BuildInfo} BuildInfo */
const PLUGIN_NAME = "CircularModulesPlugin";
/**
* Detects circular dependencies among synchronous module imports.
*
* Builds an adjacency layout from each module's synchronous outgoing
* connections (skipping weak and async-block edges), then runs an iterative
* SCC algorithm to find circular modules.
*
* Use the static `build()` method to create an instance. All intermediate data
* (adjacency layout, index mappings) is local to `build()` and released on
* return. The instance only holds the result.
*/
class CycleGraph {
/**
* @param {Set<Module>} circularModules modules involved in circular dependencies
*/
constructor(circularModules) {
/** @type {Set<Module>} */
this.circularModules = circularModules;
}
/**
* Builds a CycleGraph by constructing the synchronous outgoing-connection
* adjacency list and running iterative SCC to detect circular modules.
* @param {Iterable<Module>} modules the set of modules
* @param {ModuleGraph} moduleGraph the module graph
* @returns {CycleGraph} the result
*/
static build(modules, moduleGraph) {
/** @type {Module[]} */
const moduleList = [];
/** @type {Map<Module, number>} */
const moduleToIndex = new Map();
for (const module of modules) {
moduleToIndex.set(module, moduleList.length);
moduleList.push(module);
}
const size = moduleList.length;
if (size === 0) return new CycleGraph(new Set());
/** @type {number[][]} */
const edges = Array.from({ length: size });
/** @type {boolean[]} */
const selfLoops = Array.from({ length: size }, () => false);
for (let i = 0; i < size; i++) {
const module = moduleList[i];
/** @type {number[]} */
const deps = [];
for (const connection of moduleGraph.getOutgoingConnections(module)) {
const dep = connection.dependency;
if (!dep) continue;
const target = connection.module;
if (!target) continue;
// Weak references don't synchronously evaluate the target.
if (connection.weak) continue;
// Async edges (dynamic import & friends) live in AsyncDependenciesBlock,
// so a synchronous dep's parent block is the module itself.
if (moduleGraph.getParentBlock(dep) !== module) continue;
if (target === module) {
selfLoops[i] = true;
continue;
}
const targetIdx = moduleToIndex.get(target);
if (targetIdx !== undefined) {
deps.push(targetIdx);
}
}
edges[i] = deps;
}
// Iterative SCC algorithm
/** @type {Set<Module>} */
const circular = new Set();
let nextIndex = 0;
const nodeIndex = new Int32Array(size).fill(-1);
const nodeLowLink = new Int32Array(size);
const nodeOnStack = new Uint8Array(size);
/** @type {number[]} */
const sccStack = [];
/**
* @typedef {object} Frame
* @property {number} node
* @property {number} edgeIdx
* @property {number} parent
*/
for (let root = 0; root < size; root++) {
if (nodeIndex[root] !== -1) continue;
nodeIndex[root] = nextIndex;
nodeLowLink[root] = nextIndex;
nextIndex++;
nodeOnStack[root] = 1;
sccStack.push(root);
/** @type {Frame[]} */
const callStack = [{ node: root, edgeIdx: 0, parent: -1 }];
while (callStack.length > 0) {
const frame = /** @type {Frame} */ (callStack[callStack.length - 1]);
const v = frame.node;
const vEdges = edges[v];
if (frame.edgeIdx < vEdges.length) {
const w = vEdges[frame.edgeIdx++];
if (nodeIndex[w] === -1) {
nodeIndex[w] = nextIndex;
nodeLowLink[w] = nextIndex;
nextIndex++;
nodeOnStack[w] = 1;
sccStack.push(w);
callStack.push({ node: w, edgeIdx: 0, parent: v });
} else if (nodeOnStack[w] && nodeIndex[w] < nodeLowLink[v]) {
nodeLowLink[v] = nodeIndex[w];
}
} else {
if (nodeLowLink[v] === nodeIndex[v]) {
/** @type {number[]} */
const component = [];
let w;
do {
w = /** @type {number} */ (sccStack.pop());
nodeOnStack[w] = 0;
component.push(w);
} while (w !== v);
if (component.length > 1 || selfLoops[v]) {
for (const idx of component) {
circular.add(moduleList[idx]);
}
}
}
callStack.pop();
if (
frame.parent !== -1 &&
nodeLowLink[v] < nodeLowLink[frame.parent]
) {
nodeLowLink[frame.parent] = nodeLowLink[v];
}
}
}
}
return new CycleGraph(circular);
}
}
/**
* Detects circular dependencies and marks each circular module
* via buildInfo.isCircular for downstream consumers.
*/
class CircularModulesPlugin {
/**
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
compilation.hooks.optimizeModules.tap(PLUGIN_NAME, (modules) => {
const { circularModules } = CycleGraph.build(
modules,
compilation.moduleGraph
);
for (const m of modules) {
/** @type {BuildInfo} */
(m.buildInfo).isCircular = circularModules.has(m);
}
});
});
}
}
module.exports = CircularModulesPlugin;