Skip to content

Commit 7e30b50

Browse files
fix: resolve the cycle finder's dependencies at load time
Running the finder in development put its lazy requires on the seal path, where a compilation still in flight when jest tears its environment down resolves them to undefined and fails an instanceof. Give the finder its own module, so the two dependency classes it tests against are required once at load time.
1 parent 0b0db6b commit 7e30b50

4 files changed

Lines changed: 133 additions & 123 deletions

File tree

‎lib/optimize/ConstExportsPlugin.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ const { VariableInfoFlags } = require("../javascript/JavascriptParser");
1515
const {
1616
InlinedUsedName,
1717
enableInlineExports,
18-
findModulesInCycles,
1918
toInlinedValue
2019
} = require("./InlineExports");
20+
const findModulesInCycles = require("./findModulesInCycles");
2121

2222
/** @import { InlinedValue } from "./InlineExports" */
2323
/** @import Compiler from "../Compiler" */

‎lib/optimize/InlineExports.js‎

Lines changed: 0 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -172,131 +172,10 @@ const isExportInlined = (moduleGraph, module, ids, runtime) => {
172172
return moduleGraph.getExportsInfo(module).hasInlinedUsedName(ids, runtime);
173173
};
174174

175-
/**
176-
* Collects, by an iterative Tarjan pass, the modules a cycle peer may read in
177-
* their dead zone. A cycle of bare side-effect imports still inlines.
178-
* @param {ModuleGraph} moduleGraph module graph
179-
* @param {Iterable<Module>} modules all modules of the compilation
180-
* @returns {Set<Module>} modules whose exports must not be inlined
181-
*/
182-
const findModulesInCycles = (moduleGraph, modules) => {
183-
const HarmonyImportDependency = require("../dependencies/HarmonyImportDependency");
184-
const HarmonyImportSideEffectDependency = require("../dependencies/HarmonyImportSideEffectDependency");
185-
186-
/** @type {Set<Module>} */
187-
const inCycle = new Set();
188-
/** @type {[Module, Module][]} */
189-
const bindingReads = [];
190-
/** @type {Map<Module, number>} */
191-
const componentOf = new Map();
192-
let nextComponent = 0;
193-
/** @type {Map<Module, number>} */
194-
const index = new Map();
195-
/** @type {Map<Module, number>} */
196-
const low = new Map();
197-
/** @type {Set<Module>} */
198-
const onStack = new Set();
199-
/** @type {Module[]} */
200-
const componentStack = [];
201-
let counter = 0;
202-
/**
203-
* @param {Module} module the module
204-
* @returns {Module[]} modules it imports, self included when self-importing
205-
*/
206-
const targetsOf = (module) => {
207-
const connections = moduleGraph.getOutgoingConnectionsByModule(module);
208-
if (!connections) return [];
209-
/** @type {Module[]} */
210-
const targets = [];
211-
for (const [target, moduleConnections] of connections) {
212-
if (
213-
!target ||
214-
!moduleConnections.some(
215-
(c) => c.dependency instanceof HarmonyImportDependency
216-
)
217-
) {
218-
continue;
219-
}
220-
// Anything but a bare side-effect import may observe the binding.
221-
if (
222-
moduleConnections.some(
223-
(c) =>
224-
c.dependency instanceof HarmonyImportDependency &&
225-
!(c.dependency instanceof HarmonyImportSideEffectDependency)
226-
)
227-
) {
228-
bindingReads.push([module, target]);
229-
}
230-
targets.push(target);
231-
}
232-
return targets;
233-
};
234-
/** @type {{ node: Module, targets: Module[], i: number }[]} */
235-
const work = [];
236-
/**
237-
* @param {Module} node node to open
238-
*/
239-
const open = (node) => {
240-
index.set(node, counter);
241-
low.set(node, counter);
242-
counter++;
243-
componentStack.push(node);
244-
onStack.add(node);
245-
work.push({ node, targets: targetsOf(node), i: 0 });
246-
};
247-
for (const seed of modules) {
248-
if (index.has(seed)) continue;
249-
open(seed);
250-
while (work.length > 0) {
251-
const frame = work[work.length - 1];
252-
if (frame.i < frame.targets.length) {
253-
const next = frame.targets[frame.i++];
254-
if (!index.has(next)) {
255-
open(next);
256-
} else if (onStack.has(next)) {
257-
const nodeLow = /** @type {number} */ (low.get(frame.node));
258-
const nextIndex = /** @type {number} */ (index.get(next));
259-
if (nextIndex < nodeLow) low.set(frame.node, nextIndex);
260-
}
261-
continue;
262-
}
263-
work.pop();
264-
const node = frame.node;
265-
const nodeLow = /** @type {number} */ (low.get(node));
266-
if (work.length > 0) {
267-
const parent = work[work.length - 1].node;
268-
if (nodeLow < /** @type {number} */ (low.get(parent))) {
269-
low.set(parent, nodeLow);
270-
}
271-
}
272-
if (nodeLow === index.get(node)) {
273-
const id = nextComponent++;
274-
let member;
275-
do {
276-
member = /** @type {Module} */ (componentStack.pop());
277-
onStack.delete(member);
278-
componentOf.set(member, id);
279-
} while (member !== node);
280-
}
281-
}
282-
}
283-
// Reader and target evaluate together exactly when they share a component.
284-
for (const [origin, target] of bindingReads) {
285-
if (
286-
origin === target ||
287-
componentOf.get(origin) === componentOf.get(target)
288-
) {
289-
inCycle.add(target);
290-
}
291-
}
292-
return inCycle;
293-
};
294-
295175
module.exports.InlinedUsedName = InlinedUsedName;
296176
module.exports.InlinedValue = InlinedValue;
297177
module.exports.MAX_INLINE_BYTES = MAX_INLINE_BYTES;
298178
module.exports.enableInlineExports = enableInlineExports;
299-
module.exports.findModulesInCycles = findModulesInCycles;
300179
module.exports.isExportInlined = isExportInlined;
301180
module.exports.isInlineEnabled = isInlineEnabled;
302181
module.exports.isInlineExportsEnabled = isInlineExportsEnabled;

‎lib/optimize/LocalImportBindingsPlugin.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"use strict";
77

88
const HarmonyAcceptDependency = require("../dependencies/HarmonyAcceptDependency");
9-
const { findModulesInCycles } = require("./InlineExports");
109
const { enableLocalImportBindings } = require("./LocalImportBindings");
10+
const findModulesInCycles = require("./findModulesInCycles");
1111

1212
/** @import Compiler from "../Compiler" */
1313
/** @import Module from "../Module" */
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Haijie Xie @hai-x
4+
*/
5+
6+
"use strict";
7+
8+
const HarmonyImportDependency = require("../dependencies/HarmonyImportDependency");
9+
const HarmonyImportSideEffectDependency = require("../dependencies/HarmonyImportSideEffectDependency");
10+
11+
/** @import ModuleGraph from "../ModuleGraph" */
12+
/** @import Module from "../Module" */
13+
14+
/**
15+
* Collects, by an iterative Tarjan pass, the modules a cycle peer may read in
16+
* their dead zone. A cycle of bare side-effect imports still inlines.
17+
* @param {ModuleGraph} moduleGraph module graph
18+
* @param {Iterable<Module>} modules all modules of the compilation
19+
* @returns {Set<Module>} modules whose exports must not be inlined
20+
*/
21+
const findModulesInCycles = (moduleGraph, modules) => {
22+
/** @type {Set<Module>} */
23+
const inCycle = new Set();
24+
/** @type {[Module, Module][]} */
25+
const bindingReads = [];
26+
/** @type {Map<Module, number>} */
27+
const componentOf = new Map();
28+
let nextComponent = 0;
29+
/** @type {Map<Module, number>} */
30+
const index = new Map();
31+
/** @type {Map<Module, number>} */
32+
const low = new Map();
33+
/** @type {Set<Module>} */
34+
const onStack = new Set();
35+
/** @type {Module[]} */
36+
const componentStack = [];
37+
let counter = 0;
38+
/**
39+
* @param {Module} module the module
40+
* @returns {Module[]} modules it imports, self included when self-importing
41+
*/
42+
const targetsOf = (module) => {
43+
const connections = moduleGraph.getOutgoingConnectionsByModule(module);
44+
if (!connections) return [];
45+
/** @type {Module[]} */
46+
const targets = [];
47+
for (const [target, moduleConnections] of connections) {
48+
if (
49+
!target ||
50+
!moduleConnections.some(
51+
(c) => c.dependency instanceof HarmonyImportDependency
52+
)
53+
) {
54+
continue;
55+
}
56+
// Anything but a bare side-effect import may observe the binding.
57+
if (
58+
moduleConnections.some(
59+
(c) =>
60+
c.dependency instanceof HarmonyImportDependency &&
61+
!(c.dependency instanceof HarmonyImportSideEffectDependency)
62+
)
63+
) {
64+
bindingReads.push([module, target]);
65+
}
66+
targets.push(target);
67+
}
68+
return targets;
69+
};
70+
/** @type {{ node: Module, targets: Module[], i: number }[]} */
71+
const work = [];
72+
/**
73+
* @param {Module} node node to open
74+
*/
75+
const open = (node) => {
76+
index.set(node, counter);
77+
low.set(node, counter);
78+
counter++;
79+
componentStack.push(node);
80+
onStack.add(node);
81+
work.push({ node, targets: targetsOf(node), i: 0 });
82+
};
83+
for (const seed of modules) {
84+
if (index.has(seed)) continue;
85+
open(seed);
86+
while (work.length > 0) {
87+
const frame = work[work.length - 1];
88+
if (frame.i < frame.targets.length) {
89+
const next = frame.targets[frame.i++];
90+
if (!index.has(next)) {
91+
open(next);
92+
} else if (onStack.has(next)) {
93+
const nodeLow = /** @type {number} */ (low.get(frame.node));
94+
const nextIndex = /** @type {number} */ (index.get(next));
95+
if (nextIndex < nodeLow) low.set(frame.node, nextIndex);
96+
}
97+
continue;
98+
}
99+
work.pop();
100+
const node = frame.node;
101+
const nodeLow = /** @type {number} */ (low.get(node));
102+
if (work.length > 0) {
103+
const parent = work[work.length - 1].node;
104+
if (nodeLow < /** @type {number} */ (low.get(parent))) {
105+
low.set(parent, nodeLow);
106+
}
107+
}
108+
if (nodeLow === index.get(node)) {
109+
const id = nextComponent++;
110+
let member;
111+
do {
112+
member = /** @type {Module} */ (componentStack.pop());
113+
onStack.delete(member);
114+
componentOf.set(member, id);
115+
} while (member !== node);
116+
}
117+
}
118+
}
119+
// Reader and target evaluate together exactly when they share a component.
120+
for (const [origin, target] of bindingReads) {
121+
if (
122+
origin === target ||
123+
componentOf.get(origin) === componentOf.get(target)
124+
) {
125+
inCycle.add(target);
126+
}
127+
}
128+
return inCycle;
129+
};
130+
131+
module.exports = findModulesInCycles;

0 commit comments

Comments
 (0)