Skip to content

Commit 0d5efa3

Browse files
committed
refactor(graph): add ModuleGraph primitives for incremental rebuilds
Internal connection removal by stale dependency set, module removal with connection detachment, by-origin flush of unassigned connections, and a mutation journal that records and reverses seal-time connection moves. No behavior change; groundwork for incremental rebuilds.
1 parent d22a4d9 commit 0d5efa3

2 files changed

Lines changed: 324 additions & 3 deletions

File tree

‎lib/ModuleGraph.js‎

Lines changed: 166 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,15 @@ class ModuleGraph {
171171
*/
172172
this._cacheStage = undefined;
173173

174+
// incremental rebuilds: when set, move/copyOutgoingModuleConnections record
175+
// their seal-time graph mutations (concatenation) here so they can be reversed,
176+
// keeping this durable graph reusable across rebuilds
177+
/**
178+
* @type {{ oldModule?: Module, newModule: Module, movedOut?: ModuleGraphConnection[] | undefined, movedIn?: ModuleGraphConnection[] | undefined, copied?: ModuleGraphConnection[] }[] | undefined}
179+
* @private
180+
*/
181+
this._mutationJournal = undefined;
182+
174183
/**
175184
* @type {WeakMap<Dependency, DependencySourceOrder>}
176185
* @private
@@ -395,6 +404,59 @@ class ModuleGraph {
395404
this._dependencyMap.set(dependency, null);
396405
}
397406

407+
/**
408+
* Removes every outgoing connection of `module` whose dependency is in
409+
* `staleDependencies`, including inactive ones left by `updateModule`
410+
* re-targeting (which the dependency-keyed `removeConnection` misses). Needed
411+
* when a module is rebuilt against a persisted graph so stale connections don't
412+
* linger in the old target's incoming set and crash seal-time plugins.
413+
* @param {Module} module the rebuilt origin module
414+
* @param {Set<Dependency>} staleDependencies dependencies replaced by the rebuild
415+
* @returns {void}
416+
*/
417+
_removeStaleOutgoingConnections(module, staleDependencies) {
418+
const mgm = this._getModuleGraphModule(module);
419+
const outgoing = mgm.outgoingConnections;
420+
if (outgoing === undefined) return;
421+
for (const connection of outgoing) {
422+
if (
423+
!staleDependencies.has(
424+
/** @type {Dependency} */ (connection.dependency)
425+
)
426+
) {
427+
continue;
428+
}
429+
outgoing.delete(connection);
430+
if (connection.module !== null) {
431+
this._getModuleGraphModule(
432+
connection.module
433+
).incomingConnections.delete(connection);
434+
}
435+
}
436+
}
437+
438+
/**
439+
* Assigns `module`'s pending outgoing connections into the dependency map by
440+
* origin. `getConnection` normally does this lazily keyed by a dependency's
441+
* PARENT module; an incremental barrel un-lazy wires a re-export target from
442+
* the importer's walk, so the export dep's parent differs from its origin
443+
* barrel and the lazy flush never reaches it. Flush by origin to fix that.
444+
* @param {Module} module the origin module
445+
* @returns {void}
446+
*/
447+
_flushUnassignedConnections(module) {
448+
const mgm = this._getModuleGraphModule(module);
449+
const pending = mgm._unassignedConnections;
450+
if (pending === undefined || pending.length === 0) return;
451+
for (const connection of pending) {
452+
this._dependencyMap.set(
453+
/** @type {Dependency} */ (connection.dependency),
454+
connection
455+
);
456+
}
457+
pending.length = 0;
458+
}
459+
398460
/**
399461
* Adds the provided dependency to the module graph.
400462
* @param {Dependency} dependency the referencing dependency
@@ -437,6 +499,35 @@ class ModuleGraph {
437499
mgm.async = false;
438500
}
439501

502+
/**
503+
* Removes a module from the graph entirely, detaching its connections. Used to
504+
* drop stale runtime modules on an incremental rebuild so the persisted graph
505+
* doesn't accumulate them.
506+
* @param {Module} module the module to remove
507+
* @returns {void}
508+
*/
509+
_removeModule(module) {
510+
const mgm = this._moduleMap.get(module);
511+
if (mgm === undefined) return;
512+
if (mgm.outgoingConnections !== undefined) {
513+
for (const connection of mgm.outgoingConnections) {
514+
if (connection.module && connection.module !== module) {
515+
const other = this._moduleMap.get(connection.module);
516+
if (other !== undefined) other.incomingConnections.delete(connection);
517+
}
518+
}
519+
}
520+
for (const connection of mgm.incomingConnections) {
521+
if (connection.originModule && connection.originModule !== module) {
522+
const other = this._moduleMap.get(connection.originModule);
523+
if (other !== undefined && other.outgoingConnections !== undefined) {
524+
other.outgoingConnections.delete(connection);
525+
}
526+
}
527+
}
528+
this._moduleMap.delete(module);
529+
}
530+
440531
/**
441532
* Removes all module attributes.
442533
* @returns {void}
@@ -461,6 +552,11 @@ class ModuleGraph {
461552
if (oldModule === newModule) return;
462553
const oldMgm = this._getModuleGraphModule(oldModule);
463554
const newMgm = this._getModuleGraphModule(newModule);
555+
const journal = this._mutationJournal;
556+
/** @type {ModuleGraphConnection[] | undefined} */
557+
const movedOut = journal ? [] : undefined;
558+
/** @type {ModuleGraphConnection[] | undefined} */
559+
const movedIn = journal ? [] : undefined;
464560
// Outgoing connections
465561
const oldConnections = oldMgm.outgoingConnections;
466562
if (oldConnections !== undefined) {
@@ -473,6 +569,7 @@ class ModuleGraph {
473569
connection.originModule = newModule;
474570
newConnections.add(connection);
475571
oldConnections.delete(connection);
572+
if (movedOut) movedOut.push(connection);
476573
}
477574
}
478575
}
@@ -484,8 +581,69 @@ class ModuleGraph {
484581
connection.module = newModule;
485582
newConnections2.add(connection);
486583
oldConnections2.delete(connection);
584+
if (movedIn) movedIn.push(connection);
585+
}
586+
}
587+
if (journal) journal.push({ oldModule, newModule, movedOut, movedIn });
588+
}
589+
590+
/**
591+
* Start recording connection moves so they can later be reversed for incremental rebuilds.
592+
* @returns {void}
593+
*/
594+
_startMutationJournal() {
595+
this._mutationJournal = [];
596+
}
597+
598+
/**
599+
* Reverse all recorded connection moves, restoring the graph to its pre-seal
600+
* (end-of-make) state, then stop recording (incremental rebuilds).
601+
* @returns {void}
602+
*/
603+
_restoreFromMutationJournal() {
604+
const journal = this._mutationJournal;
605+
if (journal === undefined) return;
606+
for (let i = journal.length - 1; i >= 0; i--) {
607+
const { oldModule, newModule, movedOut, movedIn, copied } = journal[i];
608+
const newMgm = this._getModuleGraphModule(newModule);
609+
// copyOutgoingModuleConnections clones connections onto newModule and the
610+
// external targets; undo by deleting the clones (there is no oldModule)
611+
if (copied !== undefined) {
612+
for (const connection of copied) {
613+
if (newMgm.outgoingConnections !== undefined) {
614+
newMgm.outgoingConnections.delete(connection);
615+
}
616+
if (connection.module !== null && connection.module !== undefined) {
617+
this._getModuleGraphModule(
618+
connection.module
619+
).incomingConnections.delete(connection);
620+
}
621+
}
622+
continue;
623+
}
624+
const oldModuleT = /** @type {Module} */ (oldModule);
625+
const oldMgm = this._getModuleGraphModule(oldModuleT);
626+
if (movedOut !== undefined) {
627+
if (oldMgm.outgoingConnections === undefined) {
628+
oldMgm.outgoingConnections = new SortableSet();
629+
}
630+
for (const connection of movedOut) {
631+
connection.originModule = oldModuleT;
632+
oldMgm.outgoingConnections.add(connection);
633+
if (newMgm.outgoingConnections !== undefined) {
634+
newMgm.outgoingConnections.delete(connection);
635+
}
636+
}
637+
}
638+
if (movedIn !== undefined) {
639+
for (const connection of movedIn) {
640+
connection.module = oldModuleT;
641+
oldMgm.incomingConnections.add(connection);
642+
newMgm.incomingConnections.delete(connection);
643+
}
487644
}
488645
}
646+
this._mutationJournal = undefined;
489647
}
490648

491649
/**
@@ -499,6 +657,9 @@ class ModuleGraph {
499657
if (oldModule === newModule) return;
500658
const oldMgm = this._getModuleGraphModule(oldModule);
501659
const newMgm = this._getModuleGraphModule(newModule);
660+
const journal = this._mutationJournal;
661+
/** @type {ModuleGraphConnection[] | undefined} */
662+
const copied = journal ? [] : undefined;
502663
// Outgoing connections
503664
const oldConnections = oldMgm.outgoingConnections;
504665
if (oldConnections !== undefined) {
@@ -515,9 +676,13 @@ class ModuleGraph {
515676
const otherMgm = this._getModuleGraphModule(newConnection.module);
516677
otherMgm.incomingConnections.add(newConnection);
517678
}
679+
if (copied) copied.push(newConnection);
518680
}
519681
}
520682
}
683+
if (journal && copied !== undefined && copied.length > 0) {
684+
journal.push({ newModule, copied });
685+
}
521686
}
522687

523688
/**
@@ -1044,9 +1209,7 @@ class ModuleGraph {
10441209
const moduleGraph = moduleGraphForModuleMap.get(module);
10451210
if (!moduleGraph) {
10461211
throw new Error(
1047-
`${
1048-
deprecateMessage
1049-
}There was no ModuleGraph assigned to the Module for backward-compat (Use the new API)`
1212+
`${deprecateMessage}There was no ModuleGraph assigned to the Module for backward-compat (Use the new API)`
10501213
);
10511214
}
10521215
return moduleGraph;

‎test/ModuleGraph.unittest.js‎

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
"use strict";
2+
3+
const Dependency = require("../lib/Dependency");
4+
const Module = require("../lib/Module");
5+
const ModuleGraph = require("../lib/ModuleGraph");
6+
7+
/**
8+
* @returns {Module} module
9+
*/
10+
const createModule = () => new Module("javascript/auto", null, null);
11+
12+
describe("ModuleGraph", () => {
13+
describe("_removeStaleOutgoingConnections", () => {
14+
it("removes only connections of the stale dependencies from both sides", () => {
15+
const moduleGraph = new ModuleGraph();
16+
const origin = createModule();
17+
const targetA = createModule();
18+
const targetB = createModule();
19+
const staleDependency = new Dependency();
20+
const freshDependency = new Dependency();
21+
moduleGraph.setResolvedModule(origin, staleDependency, targetA);
22+
moduleGraph.setResolvedModule(origin, freshDependency, targetB);
23+
24+
moduleGraph._removeStaleOutgoingConnections(
25+
origin,
26+
new Set([staleDependency])
27+
);
28+
29+
const outgoing = [...moduleGraph.getOutgoingConnections(origin)];
30+
expect(outgoing).toHaveLength(1);
31+
expect(outgoing[0].module).toBe(targetB);
32+
expect([...moduleGraph.getIncomingConnections(targetA)]).toHaveLength(0);
33+
expect([...moduleGraph.getIncomingConnections(targetB)]).toHaveLength(1);
34+
});
35+
});
36+
37+
describe("_flushUnassignedConnections", () => {
38+
it("makes connections resolvable by dependency without a parent module", () => {
39+
const moduleGraph = new ModuleGraph();
40+
const origin = createModule();
41+
const target = createModule();
42+
const dependency = new Dependency();
43+
moduleGraph.setResolvedModule(origin, dependency, target);
44+
45+
// without a parent module the lazy flush in getConnection cannot run
46+
moduleGraph._flushUnassignedConnections(origin);
47+
48+
const connection =
49+
/** @type {import("../lib/ModuleGraphConnection")} */
50+
(moduleGraph.getConnection(dependency));
51+
expect(connection).toBeDefined();
52+
expect(connection.module).toBe(target);
53+
});
54+
55+
it("without the flush the connection is not found", () => {
56+
const moduleGraph = new ModuleGraph();
57+
const origin = createModule();
58+
const target = createModule();
59+
const dependency = new Dependency();
60+
moduleGraph.setResolvedModule(origin, dependency, target);
61+
62+
expect(moduleGraph.getConnection(dependency)).toBeUndefined();
63+
});
64+
});
65+
66+
describe("_removeModule", () => {
67+
it("detaches incoming and outgoing connections of the removed module", () => {
68+
const moduleGraph = new ModuleGraph();
69+
const origin = createModule();
70+
const removed = createModule();
71+
const target = createModule();
72+
moduleGraph.setResolvedModule(origin, new Dependency(), removed);
73+
moduleGraph.setResolvedModule(removed, new Dependency(), target);
74+
75+
moduleGraph._removeModule(removed);
76+
77+
expect([...moduleGraph.getOutgoingConnections(origin)]).toHaveLength(0);
78+
expect([...moduleGraph.getIncomingConnections(target)]).toHaveLength(0);
79+
});
80+
});
81+
82+
describe("mutation journal", () => {
83+
it("reverses moved connections back to the original module", () => {
84+
const moduleGraph = new ModuleGraph();
85+
const origin = createModule();
86+
const replacement = createModule();
87+
const incomingSource = createModule();
88+
const target = createModule();
89+
moduleGraph.setResolvedModule(origin, new Dependency(), target);
90+
moduleGraph.setResolvedModule(incomingSource, new Dependency(), origin);
91+
92+
moduleGraph._startMutationJournal();
93+
moduleGraph.moveModuleConnections(origin, replacement, () => true);
94+
95+
expect([...moduleGraph.getOutgoingConnections(replacement)]).toHaveLength(
96+
1
97+
);
98+
expect([...moduleGraph.getIncomingConnections(replacement)]).toHaveLength(
99+
1
100+
);
101+
102+
moduleGraph._restoreFromMutationJournal();
103+
104+
const outgoing = [...moduleGraph.getOutgoingConnections(origin)];
105+
expect(outgoing).toHaveLength(1);
106+
expect(outgoing[0].originModule).toBe(origin);
107+
expect(outgoing[0].module).toBe(target);
108+
const incoming = [...moduleGraph.getIncomingConnections(origin)];
109+
expect(incoming).toHaveLength(1);
110+
expect(incoming[0].module).toBe(origin);
111+
expect([...moduleGraph.getOutgoingConnections(replacement)]).toHaveLength(
112+
0
113+
);
114+
expect([...moduleGraph.getIncomingConnections(replacement)]).toHaveLength(
115+
0
116+
);
117+
});
118+
119+
it("removes copied connections from the copy target and its modules", () => {
120+
const moduleGraph = new ModuleGraph();
121+
const origin = createModule();
122+
const replacement = createModule();
123+
const target = createModule();
124+
moduleGraph.setResolvedModule(origin, new Dependency(), target);
125+
126+
moduleGraph._startMutationJournal();
127+
moduleGraph.copyOutgoingModuleConnections(
128+
origin,
129+
replacement,
130+
() => true
131+
);
132+
133+
expect([...moduleGraph.getOutgoingConnections(replacement)]).toHaveLength(
134+
1
135+
);
136+
expect([...moduleGraph.getIncomingConnections(target)]).toHaveLength(2);
137+
138+
moduleGraph._restoreFromMutationJournal();
139+
140+
expect([...moduleGraph.getOutgoingConnections(replacement)]).toHaveLength(
141+
0
142+
);
143+
expect([...moduleGraph.getIncomingConnections(target)]).toHaveLength(1);
144+
expect([...moduleGraph.getOutgoingConnections(origin)]).toHaveLength(1);
145+
});
146+
147+
it("does nothing when no journal was started", () => {
148+
const moduleGraph = new ModuleGraph();
149+
const origin = createModule();
150+
const target = createModule();
151+
moduleGraph.setResolvedModule(origin, new Dependency(), target);
152+
153+
moduleGraph._restoreFromMutationJournal();
154+
155+
expect([...moduleGraph.getOutgoingConnections(origin)]).toHaveLength(1);
156+
});
157+
});
158+
});

0 commit comments

Comments
 (0)