Skip to content

Commit 51c1b2b

Browse files
perf(hmr): build the module id map once per build (#23019)
* perf(hmr): build the module id map once per build The fullHash and processAssets taps each built their own id to module map; share one built on first use and released after seal. * perf(hmr): never let a runtime module hide a module in the id map A custom module id plugin may give a normal module the empty id that all runtime modules share; retention must still find that module.
1 parent a08aa19 commit 51c1b2b

1 file changed

Lines changed: 32 additions & 23 deletions

File tree

‎lib/hmr/HotModuleReplacementPlugin.js‎

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -560,11 +560,35 @@ class HotModuleReplacementPlugin {
560560
const nonCodeGeneratedModules = new TupleSet();
561561
/** @type {Map<Chunk, Module[]>} */
562562
const retainedModules = new Map();
563+
/** @type {Map<ModuleId, Module> | undefined} */
564+
let modulesById;
565+
/**
566+
* Maps each module id of this build to its module, built once on first use.
567+
* Runtime modules all share the empty id; one never hides another module.
568+
* @returns {Map<ModuleId, Module>} modules by id
569+
*/
570+
const getModulesById = () => {
571+
if (modulesById === undefined) {
572+
const chunkGraph = compilation.chunkGraph;
573+
modulesById = new Map();
574+
for (const module of compilation.modules) {
575+
const id = /** @type {ModuleId} */ (
576+
chunkGraph.getModuleId(module)
577+
);
578+
if (module.is("runtime") && modulesById.has(id)) continue;
579+
modulesById.set(id, module);
580+
}
581+
}
582+
return modulesById;
583+
};
584+
compilation.hooks.afterSeal.tap(PLUGIN_NAME, () => {
585+
modulesById = undefined;
586+
});
563587
compilation.hooks.fullHash.tap(PLUGIN_NAME, (hash) => {
564588
const chunkGraph = compilation.chunkGraph;
565589
const records = /** @type {Records} */ (compilation.records);
566-
/** @type {Map<ModuleId, Module> | undefined} */
567-
let modulesById;
590+
// A reseal assigns module ids again.
591+
modulesById = undefined;
568592
for (const chunk of compilation.chunks) {
569593
/**
570594
* Returns module hash.
@@ -652,25 +676,17 @@ class HotModuleReplacementPlugin {
652676
records.chunkModuleIds &&
653677
records.chunkModuleIds[/** @type {ChunkId} */ (chunk.id)];
654678
if (oldModuleIds === undefined) continue;
655-
if (modulesById === undefined) {
656-
modulesById = new Map();
657-
for (const module of compilation.modules) {
658-
// Runtime modules all share the empty id.
659-
if (module.is("runtime")) continue;
660-
modulesById.set(
661-
/** @type {ModuleId} */ (chunkGraph.getModuleId(module)),
662-
module
663-
);
664-
}
665-
}
679+
const modulesById = getModulesById();
666680
// A module that left this chunk but not its runtime stays cached in the
667681
// chunk's clients, which load no other chunk holding it: keep it (and
668682
// what it now imports synchronously) here so its updates reach them.
669683
/** @type {Module[]} */
670684
const queue = [];
671685
for (const id of oldModuleIds) {
672686
const module = modulesById.get(id);
673-
if (module !== undefined) queue.push(module);
687+
if (module !== undefined && !module.is("runtime")) {
688+
queue.push(module);
689+
}
674690
}
675691
/** @type {Set<Module>} */
676692
const retained = new Set();
@@ -815,15 +831,8 @@ class HotModuleReplacementPlugin {
815831
});
816832
if (hotUpdateMainContentByRuntime.size === 0) return;
817833

818-
// Create a list of all active modules to verify which modules are removed completely
819-
/** @type {Map<ModuleId, Module>} */
820-
const allModules = new Map();
821-
for (const module of compilation.modules) {
822-
const id =
823-
/** @type {ModuleId} */
824-
(chunkGraph.getModuleId(module));
825-
allModules.set(id, module);
826-
}
834+
// All active modules, to verify which modules are removed completely
835+
const allModules = getModulesById();
827836
/** @type {Map<string, Chunk>} */
828837
const chunksById = new Map();
829838
for (const chunk of compilation.chunks) {

0 commit comments

Comments
 (0)