From 42167db4affe764aa0d9a0e6c19e46bd4e24ca8b Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Tue, 21 Aug 2018 16:12:00 +0200 Subject: [PATCH] move Module.index and index2 into ModuleGraph --- lib/Compilation.js | 25 +++- lib/Module.js | 16 +-- lib/ModuleGraph.js | 108 +++++++++++++++++- lib/Stats.js | 6 +- lib/optimize/ConcatenatedModule.js | 2 - lib/optimize/ModuleConcatenationPlugin.js | 5 +- lib/util/comparators.js | 24 ++-- .../order-multiple-entries/webpack.config.js | 17 ++- 8 files changed, 170 insertions(+), 33 deletions(-) diff --git a/lib/Compilation.js b/lib/Compilation.js index 9dc71ebae..a56f55a23 100644 --- a/lib/Compilation.js +++ b/lib/Compilation.js @@ -1056,6 +1056,7 @@ class Compilation { for (const module of this.modules) { module.unseal(); } + this.moduleGraph.removeAllModuleAttributes(); } /** @@ -1381,6 +1382,8 @@ class Compilation { // eachother and Blocks with Chunks. It stops traversing when all modules // for a chunk are already available. So it doesn't connect unneeded chunks. + const moduleGraph = this.moduleGraph; + /** @type {Map} */ const chunkDependencies = new Map(); /** @type {Set} */ @@ -1467,8 +1470,8 @@ class Compilation { chunkGroupCounters.set(chunkGroup, { index: 0, index2: 0 }); } - let nextFreeModuleIndex = 0; - let nextFreeModuleIndex2 = 0; + let nextFreeModulePreOrderIndex = 0; + let nextFreeModulePostOrderIndex = 0; /** @type {Map} */ const blockChunkGroups = new Map(); @@ -1605,8 +1608,13 @@ class Compilation { } } - if (module.index === null) { - module.index = nextFreeModuleIndex++; + if ( + moduleGraph.setPreOrderIndexIfUnset( + module, + nextFreeModulePreOrderIndex + ) + ) { + nextFreeModulePreOrderIndex++; } queue.push({ @@ -1659,8 +1667,13 @@ class Compilation { } } - if (module.index2 === null) { - module.index2 = nextFreeModuleIndex2++; + if ( + moduleGraph.setPostOrderIndexIfUnset( + module, + nextFreeModulePostOrderIndex + ) + ) { + nextFreeModulePostOrderIndex++; } break; } diff --git a/lib/Module.js b/lib/Module.js index 8ef718202..0373fe434 100644 --- a/lib/Module.js +++ b/lib/Module.js @@ -103,10 +103,6 @@ class Module extends DependenciesBlock { /** @type {number|string} */ this.id = null; /** @type {number} */ - this.index = null; - /** @type {number} */ - this.index2 = null; - /** @type {number} */ this.depth = null; /** @type {undefined | object} */ this.profile = undefined; @@ -119,6 +115,14 @@ class Module extends DependenciesBlock { this.useSourceMap = false; } + get index() { + throw new Error(); + } + + get index2() { + throw new Error(); + } + /** * @deprecated moved to .buildInfo.exportsArgument * @returns {string} name of the exports argument @@ -144,8 +148,6 @@ class Module extends DependenciesBlock { this.renderedHash = undefined; this.id = null; - this.index = null; - this.index2 = null; this.depth = null; this.profile = undefined; this.prefetched = false; @@ -159,8 +161,6 @@ class Module extends DependenciesBlock { */ unseal() { this.id = null; - this.index = null; - this.index2 = null; this.depth = null; super.unseal(); } diff --git a/lib/ModuleGraph.js b/lib/ModuleGraph.js index aa26c5960..bf46247b6 100644 --- a/lib/ModuleGraph.js +++ b/lib/ModuleGraph.js @@ -27,6 +27,10 @@ class ModuleGraphModule { this.optimizationBailout = []; /** @type {false | true | SortableSet | null} */ this.usedExports = null; + /** @type {number} */ + this.preOrderIndex = null; + /** @type {number} */ + this.postOrderIndex = null; } } @@ -155,16 +159,51 @@ class ModuleGraph { connection.addExplanation(explanation); } + /** + * @param {Module} oldModule the old module + * @param {Module} newModule the new module + * @returns {void} + */ + moveModuleAttributes(oldModule, newModule) { + const oldMgm = this._getModuleGraphModule(oldModule); + const newMgm = this._getModuleGraphModule(newModule); + newMgm.postOrderIndex = oldMgm.postOrderIndex; + newMgm.preOrderIndex = oldMgm.preOrderIndex; + oldMgm.postOrderIndex = null; + oldMgm.preOrderIndex = null; + } + + /** + * @param {Module} module the module + * @returns {void} + */ + removeModuleAttributes(module) { + const mgm = this._getModuleGraphModule(module); + mgm.postOrderIndex = null; + mgm.preOrderIndex = null; + } + + /** + * @returns {void} + */ + removeAllModuleAttributes() { + for (const mgm of this._moduleMap.values()) { + mgm.postOrderIndex = null; + mgm.preOrderIndex = null; + } + } + /** * @param {Module} oldModule the old referencing module * @param {Module} newModule the new referencing module * @param {function(ModuleGraphConnection): boolean} filterConnection filter predicate for replacement * @returns {void} */ - replaceModule(oldModule, newModule, filterConnection) { + moveModuleConnections(oldModule, newModule, filterConnection) { if (oldModule === newModule) return; const oldMgm = this._getModuleGraphModule(oldModule); const newMgm = this._getModuleGraphModule(newModule); + // Outgoing connections const oldConnections = oldMgm.outgoingConnections; const newConnections = newMgm.outgoingConnections; for (const connection of oldConnections) { @@ -174,6 +213,7 @@ class ModuleGraph { oldConnections.delete(connection); } } + // Incoming connections const oldConnections2 = oldMgm.incomingConnections; const newConnections2 = newMgm.incomingConnections; for (const connection of oldConnections2) { @@ -310,6 +350,72 @@ class ModuleGraph { mgm.usedExports = usedExports; } + /** + * @param {Module} module the module + * @returns {number} the index of the module + */ + getPreOrderIndex(module) { + const mgm = this._getModuleGraphModule(module); + return mgm.preOrderIndex; + } + + /** + * @param {Module} module the module + * @returns {number} the index of the module + */ + getPostOrderIndex(module) { + const mgm = this._getModuleGraphModule(module); + return mgm.postOrderIndex; + } + + /** + * @param {Module} module the module + * @param {number} index the index of the module + * @returns {void} + */ + setPreOrderIndex(module, index) { + const mgm = this._getModuleGraphModule(module); + mgm.preOrderIndex = index; + } + + /** + * @param {Module} module the module + * @param {number} index the index of the module + * @returns {boolean} true, if the index was set + */ + setPreOrderIndexIfUnset(module, index) { + const mgm = this._getModuleGraphModule(module); + if (mgm.preOrderIndex === null) { + mgm.preOrderIndex = index; + return true; + } + return false; + } + + /** + * @param {Module} module the module + * @param {number} index the index of the module + * @returns {void} + */ + setPostOrderIndex(module, index) { + const mgm = this._getModuleGraphModule(module); + mgm.postOrderIndex = index; + } + + /** + * @param {Module} module the module + * @param {number} index the index of the module + * @returns {boolean} true, if the index was set + */ + setPostOrderIndexIfUnset(module, index) { + const mgm = this._getModuleGraphModule(module); + if (mgm.postOrderIndex === null) { + mgm.postOrderIndex = index; + return true; + } + return false; + } + /** * @param {any} thing any thing * @returns {Object} metadata diff --git a/lib/Stats.js b/lib/Stats.js index dc52b6b0d..4aa7d8bde 100644 --- a/lib/Stats.js +++ b/lib/Stats.js @@ -518,8 +518,10 @@ class Stats { id: module.id, identifier: module.identifier(), name: module.readableIdentifier(requestShortener), - index: module.index, - index2: module.index2, + index: moduleGraph.getPreOrderIndex(module), + preOrderIndex: moduleGraph.getPreOrderIndex(module), + index2: moduleGraph.getPostOrderIndex(module), + postOrderIndex: moduleGraph.getPostOrderIndex(module), size: module.size(), cacheable: module.buildInfo.cacheable, built: !!module.built, diff --git a/lib/optimize/ConcatenatedModule.js b/lib/optimize/ConcatenatedModule.js index 9d5ae5893..0997d98e3 100644 --- a/lib/optimize/ConcatenatedModule.js +++ b/lib/optimize/ConcatenatedModule.js @@ -321,8 +321,6 @@ class ConcatenatedModule extends Module { this.factoryMeta = rootModule.factoryMeta; // Info from Compilation - this.index = rootModule.index; - this.index2 = rootModule.index2; this.depth = rootModule.depth; // Info from Optimization diff --git a/lib/optimize/ModuleConcatenationPlugin.js b/lib/optimize/ModuleConcatenationPlugin.js index ee0ae0b7b..d44555a63 100644 --- a/lib/optimize/ModuleConcatenationPlugin.js +++ b/lib/optimize/ModuleConcatenationPlugin.js @@ -336,12 +336,15 @@ class ModuleConcatenationPlugin { .getOptimizationBailout(newModule) .push(formatBailoutWarning(warning[0], warning[1])); } + moduleGraph.moveModuleAttributes(rootModule, newModule); for (const m of modules) { usedModules.add(m); + // remove attributes from module + moduleGraph.removeModuleAttributes(m); // remove module from chunk chunkGraph.replaceModule(m, newModule); // replace module references with the concatenated module - moduleGraph.replaceModule(m, newModule, c => { + moduleGraph.moveModuleConnections(m, newModule, c => { return !( c.dependency instanceof HarmonyImportDependency && modules.has(c.originModule) && diff --git a/lib/util/comparators.js b/lib/util/comparators.js index 4de848913..23590724b 100644 --- a/lib/util/comparators.js +++ b/lib/util/comparators.js @@ -81,7 +81,10 @@ const compareNumbers = (a, b) => { * @returns {-1|0|1} compare result */ const compareModulesByIndex = (moduleGraph, a, b) => { - return compareNumbers(a.index, b.index); + return compareNumbers( + moduleGraph.getPreOrderIndex(a), + moduleGraph.getPreOrderIndex(b) + ); }; /** @type {ParamizedComparator} */ exports.compareModulesByIndex = createCachedParamizedComparator( @@ -95,7 +98,10 @@ exports.compareModulesByIndex = createCachedParamizedComparator( * @returns {-1|0|1} compare result */ const compareModulesByIndex2 = (moduleGraph, a, b) => { - return compareNumbers(a.index2, b.index2); + return compareNumbers( + moduleGraph.getPostOrderIndex(a), + moduleGraph.getPostOrderIndex(b) + ); }; /** @type {ParamizedComparator} */ exports.compareModulesByIndex2 = createCachedParamizedComparator( @@ -109,13 +115,13 @@ exports.compareModulesByIndex2 = createCachedParamizedComparator( * @returns {-1|0|1} compare result */ const compareModulesByIndexOrIdentifier = (moduleGraph, a, b) => { - if (a.index < b.index) return -1; - if (a.index > b.index) return 1; - const identA = a.identifier(); - const identB = b.identifier(); - if (identA < identB) return -1; - if (identA > identB) return 1; - return 0; + const cmp1 = compareNumbers( + moduleGraph.getPreOrderIndex(a), + moduleGraph.getPreOrderIndex(b) + ); + if (cmp1 !== 0) return cmp1; + const cmp2 = compareIds(a.identifier(), b.identifier()); + return cmp2; }; /** @type {ParamizedComparator} */ exports.compareModulesByIndexOrIdentifier = createCachedParamizedComparator( diff --git a/test/configCases/chunk-index/order-multiple-entries/webpack.config.js b/test/configCases/chunk-index/order-multiple-entries/webpack.config.js index fbac66a06..e3a3a5109 100644 --- a/test/configCases/chunk-index/order-multiple-entries/webpack.config.js +++ b/test/configCases/chunk-index/order-multiple-entries/webpack.config.js @@ -16,6 +16,7 @@ module.exports = { * @returns {void} */ const handler = compilation => { + const moduleGraph = compilation.moduleGraph; compilation.hooks.afterSeal.tap("testcase", () => { const data = {}; for (const [name, group] of compilation.namedChunkGroups) { @@ -69,20 +70,28 @@ module.exports = { }); const indicies = compilation.modules .slice() - .sort((a, b) => a.index - b.index) + .sort( + (a, b) => + moduleGraph.getPreOrderIndex(a) - + moduleGraph.getPreOrderIndex(b) + ) .map( m => - `${m.index}: ${m.readableIdentifier( + `${moduleGraph.getPreOrderIndex(m)}: ${m.readableIdentifier( compilation.requestShortener )}` ) .join(", "); const indicies2 = compilation.modules .slice() - .sort((a, b) => a.index2 - b.index2) + .sort( + (a, b) => + moduleGraph.getPostOrderIndex(a) - + moduleGraph.getPostOrderIndex(b) + ) .map( m => - `${m.index2}: ${m.readableIdentifier( + `${moduleGraph.getPostOrderIndex(m)}: ${m.readableIdentifier( compilation.requestShortener )}` )