Skip to content

Commit daa2c38

Browse files
committed
lazy assign connections to dependencies
in many cases we don't need the assignment
1 parent 449f7ef commit daa2c38

File tree

1 file changed

+46
-14
lines changed

1 file changed

+46
-14
lines changed

lib/ModuleGraph.js

+46-14
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ class ModuleGraphModule {
7979
this.profile = undefined;
8080
/** @type {boolean} */
8181
this.async = false;
82+
/** @type {ModuleGraphConnection[]} */
83+
this._unassignedConnections = undefined;
8284
}
8385
}
8486

@@ -169,14 +171,21 @@ class ModuleGraph {
169171
dependency.weak,
170172
dependency.getCondition(this)
171173
);
172-
this._dependencyMap.set(dependency, connection);
173174
const connections = this._getModuleGraphModule(module).incomingConnections;
174175
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);
178188
}
179-
mgm.outgoingConnections.add(connection);
180189
}
181190

182191
/**
@@ -185,7 +194,7 @@ class ModuleGraph {
185194
* @returns {void}
186195
*/
187196
updateModule(dependency, module) {
188-
const connection = this._dependencyMap.get(dependency);
197+
const connection = this.getConnection(dependency);
189198
if (connection.module === module) return;
190199
const newConnection = connection.clone();
191200
newConnection.module = module;
@@ -202,12 +211,12 @@ class ModuleGraph {
202211
* @returns {void}
203212
*/
204213
removeConnection(dependency) {
205-
const connection = this._dependencyMap.get(dependency);
214+
const connection = this.getConnection(dependency);
206215
const targetMgm = this._getModuleGraphModule(connection.module);
207216
targetMgm.incomingConnections.delete(connection);
208217
const originMgm = this._getModuleGraphModule(connection.originModule);
209218
originMgm.outgoingConnections.delete(connection);
210-
this._dependencyMap.delete(dependency);
219+
this._dependencyMap.set(dependency, null);
211220
}
212221

213222
/**
@@ -216,7 +225,7 @@ class ModuleGraph {
216225
* @returns {void}
217226
*/
218227
addExplanation(dependency, explanation) {
219-
const connection = this._dependencyMap.get(dependency);
228+
const connection = this.getConnection(dependency);
220229
connection.addExplanation(explanation);
221230
}
222231

@@ -342,7 +351,7 @@ class ModuleGraph {
342351
* @returns {Module} the referenced module
343352
*/
344353
getResolvedModule(dependency) {
345-
const connection = this._dependencyMap.get(dependency);
354+
const connection = this.getConnection(dependency);
346355
return connection !== undefined ? connection.resolvedModule : null;
347356
}
348357

@@ -352,15 +361,38 @@ class ModuleGraph {
352361
*/
353362
getConnection(dependency) {
354363
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;
356388
}
357389

358390
/**
359391
* @param {Dependency} dependency the dependency to look for a referenced module
360392
* @returns {Module} the referenced module
361393
*/
362394
getModule(dependency) {
363-
const connection = this._dependencyMap.get(dependency);
395+
const connection = this.getConnection(dependency);
364396
return connection !== undefined ? connection.module : null;
365397
}
366398

@@ -369,7 +401,7 @@ class ModuleGraph {
369401
* @returns {Module} the referencing module
370402
*/
371403
getOrigin(dependency) {
372-
const connection = this._dependencyMap.get(dependency);
404+
const connection = this.getConnection(dependency);
373405
return connection !== undefined ? connection.originModule : null;
374406
}
375407

@@ -378,7 +410,7 @@ class ModuleGraph {
378410
* @returns {Module} the original referencing module
379411
*/
380412
getResolvedOrigin(dependency) {
381-
const connection = this._dependencyMap.get(dependency);
413+
const connection = this.getConnection(dependency);
382414
return connection !== undefined ? connection.resolvedOriginModule : null;
383415
}
384416

0 commit comments

Comments
 (0)