From 78ee6fb3867e42cab35d17bc90ab3e834d8aec21 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Sat, 17 Apr 2021 21:12:50 +0200 Subject: [PATCH] store parents in Dependency --- lib/Dependency.js | 5 ++++ lib/ModuleGraph.js | 68 ++++++++++++---------------------------------- 2 files changed, 22 insertions(+), 51 deletions(-) diff --git a/lib/Dependency.js b/lib/Dependency.js index 390495314..c2417e592 100644 --- a/lib/Dependency.js +++ b/lib/Dependency.js @@ -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; diff --git a/lib/ModuleGraph.js b/lib/ModuleGraph.js index 409bd7300..e3bf70592 100644 --- a/lib/ModuleGraph.js +++ b/lib/ModuleGraph.js @@ -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} */ + /** @type {Map} */ this._dependencyMap = new Map(); /** @type {Map} */ 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; }