@@ -79,6 +79,8 @@ class ModuleGraphModule {
79
79
this . profile = undefined ;
80
80
/** @type {boolean } */
81
81
this . async = false ;
82
+ /** @type {ModuleGraphConnection[] } */
83
+ this . _unassignedConnections = undefined ;
82
84
}
83
85
}
84
86
@@ -169,14 +171,21 @@ class ModuleGraph {
169
171
dependency . weak ,
170
172
dependency . getCondition ( this )
171
173
) ;
172
- this . _dependencyMap . set ( dependency , connection ) ;
173
174
const connections = this . _getModuleGraphModule ( module ) . incomingConnections ;
174
175
connections . add ( connection ) ;
175
- const mgm = this . _getModuleGraphModule ( originModule ) ;
176
- if ( mgm . outgoingConnections === undefined ) {
177
- mgm . outgoingConnections = new Set ( ) ;
176
+ if ( originModule ) {
177
+ const mgm = this . _getModuleGraphModule ( originModule ) ;
178
+ if ( mgm . _unassignedConnections === undefined ) {
179
+ mgm . _unassignedConnections = [ ] ;
180
+ }
181
+ mgm . _unassignedConnections . push ( connection ) ;
182
+ if ( mgm . outgoingConnections === undefined ) {
183
+ mgm . outgoingConnections = new Set ( ) ;
184
+ }
185
+ mgm . outgoingConnections . add ( connection ) ;
186
+ } else {
187
+ this . _dependencyMap . set ( dependency , connection ) ;
178
188
}
179
- mgm . outgoingConnections . add ( connection ) ;
180
189
}
181
190
182
191
/**
@@ -185,7 +194,7 @@ class ModuleGraph {
185
194
* @returns {void }
186
195
*/
187
196
updateModule ( dependency , module ) {
188
- const connection = this . _dependencyMap . get ( dependency ) ;
197
+ const connection = this . getConnection ( dependency ) ;
189
198
if ( connection . module === module ) return ;
190
199
const newConnection = connection . clone ( ) ;
191
200
newConnection . module = module ;
@@ -202,12 +211,12 @@ class ModuleGraph {
202
211
* @returns {void }
203
212
*/
204
213
removeConnection ( dependency ) {
205
- const connection = this . _dependencyMap . get ( dependency ) ;
214
+ const connection = this . getConnection ( dependency ) ;
206
215
const targetMgm = this . _getModuleGraphModule ( connection . module ) ;
207
216
targetMgm . incomingConnections . delete ( connection ) ;
208
217
const originMgm = this . _getModuleGraphModule ( connection . originModule ) ;
209
218
originMgm . outgoingConnections . delete ( connection ) ;
210
- this . _dependencyMap . delete ( dependency ) ;
219
+ this . _dependencyMap . set ( dependency , null ) ;
211
220
}
212
221
213
222
/**
@@ -216,7 +225,7 @@ class ModuleGraph {
216
225
* @returns {void }
217
226
*/
218
227
addExplanation ( dependency , explanation ) {
219
- const connection = this . _dependencyMap . get ( dependency ) ;
228
+ const connection = this . getConnection ( dependency ) ;
220
229
connection . addExplanation ( explanation ) ;
221
230
}
222
231
@@ -342,7 +351,7 @@ class ModuleGraph {
342
351
* @returns {Module } the referenced module
343
352
*/
344
353
getResolvedModule ( dependency ) {
345
- const connection = this . _dependencyMap . get ( dependency ) ;
354
+ const connection = this . getConnection ( dependency ) ;
346
355
return connection !== undefined ? connection . resolvedModule : null ;
347
356
}
348
357
@@ -352,15 +361,38 @@ class ModuleGraph {
352
361
*/
353
362
getConnection ( dependency ) {
354
363
const connection = this . _dependencyMap . get ( dependency ) ;
355
- return connection ;
364
+ if ( connection === undefined ) {
365
+ const module = this . getParentModule ( dependency ) ;
366
+ if ( module !== undefined ) {
367
+ const mgm = this . _getModuleGraphModule ( module ) ;
368
+ if (
369
+ mgm . _unassignedConnections &&
370
+ mgm . _unassignedConnections . length !== 0
371
+ ) {
372
+ let foundConnection ;
373
+ for ( const connection of mgm . _unassignedConnections ) {
374
+ this . _dependencyMap . set ( connection . dependency , connection ) ;
375
+ if ( connection . dependency === dependency )
376
+ foundConnection = connection ;
377
+ }
378
+ mgm . _unassignedConnections . length = 0 ;
379
+ if ( foundConnection !== undefined ) {
380
+ return foundConnection ;
381
+ }
382
+ }
383
+ }
384
+ this . _dependencyMap . set ( dependency , null ) ;
385
+ return undefined ;
386
+ }
387
+ return connection === null ? undefined : connection ;
356
388
}
357
389
358
390
/**
359
391
* @param {Dependency } dependency the dependency to look for a referenced module
360
392
* @returns {Module } the referenced module
361
393
*/
362
394
getModule ( dependency ) {
363
- const connection = this . _dependencyMap . get ( dependency ) ;
395
+ const connection = this . getConnection ( dependency ) ;
364
396
return connection !== undefined ? connection . module : null ;
365
397
}
366
398
@@ -369,7 +401,7 @@ class ModuleGraph {
369
401
* @returns {Module } the referencing module
370
402
*/
371
403
getOrigin ( dependency ) {
372
- const connection = this . _dependencyMap . get ( dependency ) ;
404
+ const connection = this . getConnection ( dependency ) ;
373
405
return connection !== undefined ? connection . originModule : null ;
374
406
}
375
407
@@ -378,7 +410,7 @@ class ModuleGraph {
378
410
* @returns {Module } the original referencing module
379
411
*/
380
412
getResolvedOrigin ( dependency ) {
381
- const connection = this . _dependencyMap . get ( dependency ) ;
413
+ const connection = this . getConnection ( dependency ) ;
382
414
return connection !== undefined ? connection . resolvedOriginModule : null ;
383
415
}
384
416
0 commit comments