@@ -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 ;
0 commit comments