store parents in Dependency

This commit is contained in:
Tobias Koppers 2021-04-17 21:12:50 +02:00
parent 88d90bea14
commit 78ee6fb386
2 changed files with 22 additions and 51 deletions

View File

@ -9,6 +9,7 @@ const memoize = require("./util/memoize");
/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("./ChunkGraph")} ChunkGraph */
/** @typedef {import("./DependenciesBlock")} DependenciesBlock */
/** @typedef {import("./DependencyTemplates")} DependencyTemplates */
/** @typedef {import("./Module")} Module */
/** @typedef {import("./ModuleGraph")} ModuleGraph */
@ -84,6 +85,10 @@ const getIgnoredModule = memoize(() => {
class Dependency {
constructor() {
/** @type {Module} */
this._parentModule = undefined;
/** @type {DependenciesBlock} */
this._parentDependenciesBlock = undefined;
// TODO check if this can be moved into ModuleDependency
/** @type {boolean} */
this.weak = false;

View File

@ -82,20 +82,9 @@ class ModuleGraphModule {
}
}
class ModuleGraphDependency {
constructor() {
/** @type {ModuleGraphConnection} */
this.connection = undefined;
/** @type {Module} */
this.parentModule = undefined;
/** @type {DependenciesBlock} */
this.parentBlock = undefined;
}
}
class ModuleGraph {
constructor() {
/** @type {Map<Dependency, ModuleGraphDependency>} */
/** @type {Map<Dependency, ModuleGraphConnection>} */
this._dependencyMap = new Map();
/** @type {Map<Module, ModuleGraphModule>} */
this._moduleMap = new Map();
@ -137,23 +126,6 @@ class ModuleGraph {
return mgm;
}
/**
* @param {Dependency} dependency the dependency
* @returns {ModuleGraphDependency} the internal dependency
*/
_getModuleGraphDependency(dependency) {
if (this._cacheModuleGraphDependencyKey === dependency)
return this._cacheModuleGraphDependencyValue;
let mgd = this._dependencyMap.get(dependency);
if (mgd === undefined) {
mgd = new ModuleGraphDependency();
this._dependencyMap.set(dependency, mgd);
}
this._cacheModuleGraphDependencyKey = dependency;
this._cacheModuleGraphDependencyValue = mgd;
return mgd;
}
/**
* @param {Dependency} dependency the dependency
* @param {DependenciesBlock} block parent block
@ -161,9 +133,8 @@ class ModuleGraph {
* @returns {void}
*/
setParents(dependency, block, module) {
const mgd = this._getModuleGraphDependency(dependency);
mgd.parentBlock = block;
mgd.parentModule = module;
dependency._parentDependenciesBlock = block;
dependency._parentModule = module;
}
/**
@ -171,8 +142,7 @@ class ModuleGraph {
* @returns {Module} parent module
*/
getParentModule(dependency) {
const mgd = this._getModuleGraphDependency(dependency);
return mgd.parentModule;
return dependency._parentModule;
}
/**
@ -180,8 +150,7 @@ class ModuleGraph {
* @returns {DependenciesBlock} parent block
*/
getParentBlock(dependency) {
const mgd = this._getModuleGraphDependency(dependency);
return mgd.parentBlock;
return dependency._parentDependenciesBlock;
}
/**
@ -199,8 +168,7 @@ class ModuleGraph {
dependency.weak,
dependency.getCondition(this)
);
const mgd = this._getModuleGraphDependency(dependency);
mgd.connection = connection;
this._dependencyMap.set(dependency, connection);
const connections = this._getModuleGraphModule(module).incomingConnections;
connections.add(connection);
const mgm = this._getModuleGraphModule(originModule);
@ -216,12 +184,11 @@ class ModuleGraph {
* @returns {void}
*/
updateModule(dependency, module) {
const mgd = this._getModuleGraphDependency(dependency);
if (mgd.connection.module === module) return;
const { connection } = mgd;
const connection = this._dependencyMap.get(dependency);
if (connection.module === module) return;
const newConnection = connection.clone();
newConnection.module = module;
mgd.connection = newConnection;
this._dependencyMap.set(dependency, newConnection);
connection.setActive(false);
const originMgm = this._getModuleGraphModule(connection.originModule);
originMgm.outgoingConnections.add(newConnection);
@ -234,13 +201,12 @@ class ModuleGraph {
* @returns {void}
*/
removeConnection(dependency) {
const mgd = this._getModuleGraphDependency(dependency);
const { connection } = mgd;
const connection = this._dependencyMap.get(dependency);
const targetMgm = this._getModuleGraphModule(connection.module);
targetMgm.incomingConnections.delete(connection);
const originMgm = this._getModuleGraphModule(connection.originModule);
originMgm.outgoingConnections.delete(connection);
mgd.connection = undefined;
this._dependencyMap.delete(dependency);
}
/**
@ -249,7 +215,7 @@ class ModuleGraph {
* @returns {void}
*/
addExplanation(dependency, explanation) {
const { connection } = this._getModuleGraphDependency(dependency);
const connection = this._dependencyMap.get(dependency);
connection.addExplanation(explanation);
}
@ -375,7 +341,7 @@ class ModuleGraph {
* @returns {Module} the referenced module
*/
getResolvedModule(dependency) {
const { connection } = this._getModuleGraphDependency(dependency);
const connection = this._dependencyMap.get(dependency);
return connection !== undefined ? connection.resolvedModule : null;
}
@ -384,7 +350,7 @@ class ModuleGraph {
* @returns {ModuleGraphConnection | undefined} the connection
*/
getConnection(dependency) {
const { connection } = this._getModuleGraphDependency(dependency);
const connection = this._dependencyMap.get(dependency);
return connection;
}
@ -393,7 +359,7 @@ class ModuleGraph {
* @returns {Module} the referenced module
*/
getModule(dependency) {
const { connection } = this._getModuleGraphDependency(dependency);
const connection = this._dependencyMap.get(dependency);
return connection !== undefined ? connection.module : null;
}
@ -402,7 +368,7 @@ class ModuleGraph {
* @returns {Module} the referencing module
*/
getOrigin(dependency) {
const { connection } = this._getModuleGraphDependency(dependency);
const connection = this._dependencyMap.get(dependency);
return connection !== undefined ? connection.originModule : null;
}
@ -411,7 +377,7 @@ class ModuleGraph {
* @returns {Module} the original referencing module
*/
getResolvedOrigin(dependency) {
const { connection } = this._getModuleGraphDependency(dependency);
const connection = this._dependencyMap.get(dependency);
return connection !== undefined ? connection.resolvedOriginModule : null;
}