Skip to content

Commit 26d315a

Browse files
refactor: ask the dependency and module kind instead of flags and type strings (#22705)
* refactor: ask the dependency kind instead of import specifier flag getters Claude-Session: https://claude.ai/code/session_01FuxKwZepsFsatQwWC2XFQy * refactor: ask the dependency and module kind instead of comparing type strings Claude-Session: https://claude.ai/code/session_01FuxKwZepsFsatQwWC2XFQy * fix: walk each visited chunk group and honor ignoreChunk in Module.isAccessibleInChunk Claude-Session: https://claude.ai/code/session_01FuxKwZepsFsatQwWC2XFQy
1 parent 3020447 commit 26d315a

13 files changed

Lines changed: 124 additions & 31 deletions
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Keep a module its chunk still needs after `compilation.rebuildModule()`.

‎lib/Compilation.js‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ const { LogType, Logger } = require("./logging/Logger");
3636
const CodeGenerationResults = require("./module/CodeGenerationResults");
3737
const Module = require("./module/Module");
3838
const ModuleProfile = require("./module/ModuleProfile");
39-
const { WEBPACK_MODULE_TYPE_RUNTIME } = require("./module/ModuleTypeConstants");
4039
const LazyBarrels = require("./optimize/LazyBarrels");
4140
const RuntimeGlobals = require("./runtime/RuntimeGlobals");
4241
const Stats = require("./stats/Stats");
@@ -6661,7 +6660,7 @@ This prevents using hashes of each other and should be avoided.`);
66616660
const usedIds = new Set();
66626661

66636662
for (const module of this.modules) {
6664-
if (module.type === WEBPACK_MODULE_TYPE_RUNTIME) continue;
6663+
if (module.is("runtime")) continue;
66656664
const moduleId = chunkGraph.getModuleId(module);
66666665
if (moduleId === null) continue;
66676666
if (usedIds.has(moduleId)) {

‎lib/container/HoistContainerReferencesPlugin.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
"use strict";
77

8+
const isDependencyKind = require("../graph/isDependencyKind");
89
const { STAGE_ADVANCED } = require("../optimize/OptimizationStages");
910
const { forEachRuntime } = require("../util/runtime");
1011
const getModuleFederationCompilationHooks = require("./moduleFederationHooks");
@@ -40,7 +41,7 @@ class HoistContainerReferencesPlugin {
4041
hooks.addFederationRuntimeDependency.tap(PLUGIN_NAME, traceDep);
4142

4243
compilation.hooks.addEntry.tap(PLUGIN_NAME, (entryDep) => {
43-
if (entryDep.type === "entry") {
44+
if (isDependencyKind(entryDep, "entry")) {
4445
entryExternalsToHoist.add(entryDep);
4546
}
4647
});

‎lib/dependencies/esm/ESMEvaluatedImportSpecifierDependency.js‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const { ExportPresenceModes } = require("./ESMImportDependency");
1515
const ESMImportSpecifierDependency = require("./ESMImportSpecifierDependency");
1616

1717
/** @import { ReplaceSource } from "webpack-sources" */
18-
/** @import Dependency, { GetConditionFn } from "../../graph/Dependency" */
18+
/** @import Dependency, { GetConditionFn, DependencyKinds } from "../../graph/Dependency" */
1919
/** @import { DependencyTemplateContext } from "../../template/DependencyTemplate" */
2020
/** @import { BuildMeta } from "../../module/Module" */
2121
/** @import ModuleGraph from "../../graph/ModuleGraph" */
@@ -33,11 +33,6 @@ const ESMImportSpecifierDependency = require("./ESMImportSpecifierDependency");
3333
* a.x !== undefined; // if x value statically analyzable
3434
*/
3535
class ESMEvaluatedImportSpecifierDependency extends ESMImportSpecifierDependency {
36-
// read by `ESMImportGuard` in place of `instanceof`, which would cycle
37-
get isHarmonyEvaluatedImportSpecifier() {
38-
return true;
39-
}
40-
4136
/**
4237
* Creates an instance of ESMEvaluatedImportSpecifierDependency.
4338
* @param {string} request the request string
@@ -204,4 +199,10 @@ ESMEvaluatedImportSpecifierDependency.Template = class ESMEvaluatedImportSpecifi
204199
}
205200
};
206201

202+
/** @type {Set<keyof DependencyKinds>} */
203+
ESMEvaluatedImportSpecifierDependency.KINDS = new Set([
204+
...ESMImportSpecifierDependency.KINDS,
205+
"esm-evaluated-import-specifier"
206+
]);
207+
207208
module.exports = ESMEvaluatedImportSpecifierDependency;

‎lib/dependencies/esm/ESMImportGuard.js‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
"use strict";
77

8+
const isDependencyKind = require("../../graph/isDependencyKind");
89
const { isInlinedUsedName } = require("../../optimize/InlineExports");
910

1011
/** @import { Expression } from "estree" */
@@ -210,11 +211,11 @@ const buildFrameLivenessFormula = (parser, frame) => {
210211
/** @type {Map<number, ESMImportSpecifierDependency>} */
211212
const depByRangeStart = new Map();
212213
for (let i = /** @type {number} */ (frame.depStart); i < deps.length; i++) {
213-
const dep = /** @type {ESMEvaluatedImportSpecifierDependency} */ (deps[i]);
214+
const dep = deps[i];
214215
// Exclude `"x" in ns` evaluated deps; those feed the presence formula only.
215216
if (
216-
dep.isHarmonyImportSpecifier === true &&
217-
dep.isHarmonyEvaluatedImportSpecifier !== true &&
217+
isDependencyKind(dep, "esm-import-specifier") &&
218+
!dep.is("esm-evaluated-import-specifier") &&
218219
dep.range
219220
) {
220221
depByRangeStart.set(dep.range[0], dep);
@@ -333,8 +334,8 @@ const buildFramePresenceFormula = (parser, frame) => {
333334
/** @type {Map<number, ESMEvaluatedImportSpecifierDependency>} */
334335
const depByRangeStart = new Map();
335336
for (let i = /** @type {number} */ (frame.depStart); i < deps.length; i++) {
336-
const dep = /** @type {ESMEvaluatedImportSpecifierDependency} */ (deps[i]);
337-
if (dep.isHarmonyEvaluatedImportSpecifier === true && dep.range) {
337+
const dep = deps[i];
338+
if (isDependencyKind(dep, "esm-evaluated-import-specifier") && dep.range) {
338339
depByRangeStart.set(dep.range[0], dep);
339340
}
340341
}

‎lib/dependencies/esm/ESMImportSpecifierDependency.js‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,6 @@ const { ExportPresenceModes } = ESMImportDependency;
8080
const conditionsByModuleGraph = new WeakMap();
8181

8282
class ESMImportSpecifierDependency extends ESMImportDependency {
83-
// read by `ESMImportGuard` in place of `instanceof`, which would cycle
84-
get isHarmonyImportSpecifier() {
85-
return true;
86-
}
87-
8883
/**
8984
* Creates an instance of ESMImportSpecifierDependency.
9085
* @param {string} request request

‎lib/graph/Dependency.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const memoize = require("../util/memoize");
1111
/** @import ChunkGraph from "./ChunkGraph" */
1212
/** @import ESMImportBareSideEffectDependency from "../dependencies/esm/ESMImportBareSideEffectDependency" */
1313
/** @import ESMImportSpecifierDependency from "../dependencies/esm/ESMImportSpecifierDependency" */
14+
/** @import ESMEvaluatedImportSpecifierDependency from "../dependencies/esm/ESMEvaluatedImportSpecifierDependency" */
1415
/** @import ESMCompatibilityDependency from "../dependencies/esm/ESMCompatibilityDependency" */
1516
/** @import CommonJsRequireDependency from "../dependencies/commonjs/CommonJsRequireDependency" */
1617
/** @import CommonJsExportRequireDependency from "../dependencies/commonjs/CommonJsExportRequireDependency" */
@@ -178,6 +179,7 @@ const getIgnoredModule = memoize(() => {
178179
* "context-element": ContextElementDependency,
179180
* "esm-bare-side-effect-import": ESMImportBareSideEffectDependency,
180181
* "esm-import-specifier": ESMImportSpecifierDependency,
182+
* "esm-evaluated-import-specifier": ESMEvaluatedImportSpecifierDependency,
181183
* "esm-compatibility": ESMCompatibilityDependency,
182184
* "commonjs-require": CommonJsRequireDependency | CommonJsFullRequireDependency,
183185
* "commonjs-export-require": CommonJsExportRequireDependency,

‎lib/hmr/HotModuleReplacementPlugin.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ const {
2828
const {
2929
JAVASCRIPT_MODULE_TYPE_AUTO,
3030
JAVASCRIPT_MODULE_TYPE_DYNAMIC,
31-
JAVASCRIPT_MODULE_TYPE_ESM,
32-
WEBPACK_MODULE_TYPE_RUNTIME
31+
JAVASCRIPT_MODULE_TYPE_ESM
3332
} = require("../module/ModuleTypeConstants");
3433
const NormalModule = require("../module/NormalModule");
3534
const RuntimeGlobals = require("../runtime/RuntimeGlobals");
@@ -683,7 +682,7 @@ class HotModuleReplacementPlugin {
683682
? chunkGraph.getModuleHash(module, newRuntime)
684683
: codeGenerationResults.getHash(module, newRuntime);
685684
if (hash !== oldHash) {
686-
if (module.type === WEBPACK_MODULE_TYPE_RUNTIME) {
685+
if (module.is("runtime")) {
687686
newRuntimeModules = newRuntimeModules || [];
688687
newRuntimeModules.push(
689688
/** @type {RuntimeModule} */ (module)

‎lib/hmr/LazyCompilationPlugin.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const { RawSource } = require("webpack-sources");
99
const CommonJsRequireDependency = require("../dependencies/commonjs/CommonJsRequireDependency");
1010
const AsyncDependenciesBlock = require("../graph/AsyncDependenciesBlock");
1111
const Dependency = require("../graph/Dependency");
12+
const isDependencyKind = require("../graph/isDependencyKind");
1213
const Module = require("../module/Module");
1314
const ModuleFactory = require("../module/ModuleFactory");
1415
const { JAVASCRIPT_TYPES } = require("../module/ModuleSourceTypeConstants");
@@ -606,7 +607,7 @@ class LazyCompilationPlugin {
606607
(this.imports &&
607608
(dep.type === "import()" ||
608609
dep.type === "import() context element")) ||
609-
(this.entries && dep.type === "entry")
610+
(this.entries && isDependencyKind(dep, "entry"))
610611
)
611612
) {
612613
return module;

‎lib/html/HtmlModulesPlugin.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ class HtmlModulesPlugin {
878878
return;
879879
}
880880
}
881-
if (entryModule && entryModule.type === HTML_MODULE_TYPE) {
881+
if (entryModule && entryModule.is("html")) {
882882
linkedPages.set(spec.name, entryModule);
883883
await processEntry(entryModule);
884884
}
@@ -924,7 +924,7 @@ class HtmlModulesPlugin {
924924
* @returns {void}
925925
*/
926926
const trackHtmlModule = (module) => {
927-
if (module.type !== HTML_MODULE_TYPE) return;
927+
if (!module.is("html")) return;
928928
// enabled here, not on the compilation hook: an integrity build
929929
// with no html module must not load the generator
930930
if (anyIntegrity) {
@@ -1242,7 +1242,7 @@ class HtmlModulesPlugin {
12421242
NormalModule.getCompilationHooks(compilation).processResult.tap(
12431243
PLUGIN_NAME,
12441244
(result, module) => {
1245-
if (module.type === HTML_MODULE_TYPE) {
1245+
if (module.is("html")) {
12461246
const [source, ...rest] = removeBOMFromResult(result);
12471247
// Run here, where the returned source becomes the module's stored
12481248
// source, so the parser's dependency offsets and the generator's

0 commit comments

Comments
 (0)