Skip to content

Commit 911e8a4

Browse files
committed
refactor(cache): represent inlined imports as INACTIVE_INLINED
Extend ConnectionState instead of re-detecting inlining while hashing, matching TRANSITIVE_ONLY-style inactive variants (#22019).
1 parent 0aa7584 commit 911e8a4

7 files changed

Lines changed: 63 additions & 66 deletions

‎.changeset/021-inline-exports-cache-invalidation.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack": patch
33
---
44

5-
Invalidate consumer codegen when an inlined export value changes.
5+
Mark inlined imports INACTIVE_INLINED so consumer codegen cache invalidates.

‎lib/ChunkGraph.js‎

Lines changed: 11 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const util = require("util");
99
const Entrypoint = require("./Entrypoint");
1010
const ModuleGraphConnection = require("./ModuleGraphConnection");
1111
const { DEFAULTS } = require("./config/defaults");
12-
const { isExportInlined } = require("./optimize/InlineExports");
1312
const { first } = require("./util/SetHelpers");
1413
const SortableSet = require("./util/SortableSet");
1514
const {
@@ -22,7 +21,6 @@ const {
2221
} = require("./util/comparators");
2322
const createHash = require("./util/createHash");
2423
const findGraphRoots = require("./util/findGraphRoots");
25-
const memoize = require("./util/memoize");
2624
const {
2725
RuntimeSpecMap,
2826
RuntimeSpecSet,
@@ -31,10 +29,6 @@ const {
3129
runtimeToString
3230
} = require("./util/runtime");
3331

34-
const getHarmonyImportSpecifierDependency = memoize(() =>
35-
require("./dependencies/HarmonyImportSpecifierDependency")
36-
);
37-
3832
/** @import AsyncDependenciesBlock from "./AsyncDependenciesBlock" */
3933
/** @import Chunk, { Chunks, Entrypoints, ChunkId } from "./Chunk" */
4034
/** @import ChunkGroup from "./ChunkGroup" */
@@ -66,32 +60,16 @@ const ZERO_BIG_INT = BigInt(0);
6660
* Active state to string. Module-level (depends only on ModuleGraphConnection)
6761
* so it isn't re-allocated on every getModuleGraphHash call, incl. cache hits.
6862
* @param {ConnectionState} state state
69-
* @returns {"F" | "T" | "O"} result
63+
* @returns {"F" | "T" | "O" | "I"} result
7064
*/
7165
const activeStateToString = (state) => {
7266
if (state === false) return "F";
7367
if (state === true) return "T";
7468
if (state === ModuleGraphConnection.TRANSITIVE_ONLY) return "O";
69+
if (state === ModuleGraphConnection.INACTIVE_INLINED) return "I";
7570
throw new Error("Not implemented active state");
7671
};
7772

78-
/**
79-
* True when an inactive connection still embeds an inlined export literal
80-
* in the referencing module's generated code (must stay in its hash).
81-
* @param {ModuleGraph} moduleGraph module graph
82-
* @param {ModuleGraphConnection} connection connection
83-
* @param {RuntimeSpec} runtime runtime
84-
* @returns {boolean} true when the connection contributes an inlined literal
85-
*/
86-
const connectionInactiveDueToInlining = (moduleGraph, connection, runtime) => {
87-
const module = connection.module;
88-
if (!module) return false;
89-
const dep = connection.dependency;
90-
// Lazy: top-level require cycles Module → ChunkGraph → dependency → Module.
91-
if (!(dep instanceof getHarmonyImportSpecifierDependency())) return false;
92-
return isExportInlined(moduleGraph, module, dep.getIds(moduleGraph), runtime);
93-
};
94-
9573
const compareModuleIterables = compareIterables(compareModulesByIdentifier);
9674

9775
/** @typedef {(c: Chunk, chunkGraph: ChunkGraph) => boolean} ChunkFilterPredicate */
@@ -415,7 +393,9 @@ class ChunkGraph {
415393
for (const connection of moduleGraph.getOutgoingConnections(module)) {
416394
if (!connection.module) continue;
417395
const activeState = connection.getActiveState(undefined);
418-
if (activeState === false) continue;
396+
if (ModuleGraphConnection.isInactiveForInclusion(activeState)) {
397+
continue;
398+
}
419399
if (activeState === ModuleGraphConnection.TRANSITIVE_ONLY) {
420400
addDependencies(connection.module);
421401
continue;
@@ -1908,18 +1888,9 @@ Caller might not support runtime-dependent code generation (opt-out via optimiza
19081888
if (runtime === undefined || typeof runtime === "string") {
19091889
for (const connection of connections) {
19101890
const state = connection.getActiveState(runtime);
1911-
if (state === false) {
1912-
// Keep inlined imports in the hash: their literals are
1913-
// embedded in this module's codegen (#22019).
1914-
if (
1915-
connectionInactiveDueToInlining(
1916-
this.moduleGraph,
1917-
connection,
1918-
runtime
1919-
)
1920-
) {
1921-
processConnection(connection, "I");
1922-
}
1891+
if (state === false) continue;
1892+
if (state === ModuleGraphConnection.INACTIVE_INLINED) {
1893+
processConnection(connection, "I");
19231894
continue;
19241895
}
19251896
processConnection(connection, state === true ? "T" : "O");
@@ -1941,16 +1912,9 @@ Caller might not support runtime-dependent code generation (opt-out via optimiza
19411912
);
19421913
if (states.size === 1) {
19431914
const state = first(states);
1944-
if (state === false) {
1945-
if (
1946-
connectionInactiveDueToInlining(
1947-
this.moduleGraph,
1948-
connection,
1949-
runtime
1950-
)
1951-
) {
1952-
processConnection(connection, "I");
1953-
}
1915+
if (state === false) continue;
1916+
if (state === ModuleGraphConnection.INACTIVE_INLINED) {
1917+
processConnection(connection, "I");
19541918
continue;
19551919
}
19561920
stateInfo = activeStateToString(

‎lib/FlagDependencyUsagePlugin.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,9 @@ class FlagDependencyUsagePlugin {
268268
continue;
269269
}
270270
const activeState = connection.getActiveState(runtime);
271-
if (activeState === false) continue;
271+
if (ModuleGraphConnection.isInactiveForInclusion(activeState)) {
272+
continue;
273+
}
272274
const { module } = connection;
273275
if (activeState === ModuleGraphConnection.TRANSITIVE_ONLY) {
274276
processModule(module, runtime);

‎lib/ModuleGraphConnection.js‎

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,24 @@ const TRANSITIVE_ONLY = Symbol("transitive only");
1919
*/
2020
const CIRCULAR_CONNECTION = Symbol("circular connection");
2121

22-
/** @typedef {boolean | typeof TRANSITIVE_ONLY | typeof CIRCULAR_CONNECTION} ConnectionState */
22+
/**
23+
* Import was replaced with an inlined literal: not included in the chunk,
24+
* but the referencing module's codegen hash must still see the provider.
25+
*/
26+
const INACTIVE_INLINED = Symbol("inactive inlined");
27+
28+
/**
29+
* @typedef {boolean | typeof TRANSITIVE_ONLY | typeof CIRCULAR_CONNECTION | typeof INACTIVE_INLINED} ConnectionState
30+
*/
31+
32+
/**
33+
* Whether a connection state means the referenced module is not entered
34+
* into the chunk / side-effect edge (plain inactive or inlined-away).
35+
* @param {ConnectionState} state state
36+
* @returns {boolean} true when inclusion should skip this connection
37+
*/
38+
const isInactiveForInclusion = (state) =>
39+
state === false || state === INACTIVE_INLINED;
2340

2441
/**
2542
* Adds connection states.
@@ -46,6 +63,8 @@ const intersectConnectionStates = (a, b) => {
4663
if (a === false || b === false) return false;
4764
if (a === true) return b;
4865
if (b === true) return a;
66+
// Inlined beats transitive-only: the binding is gone from the exports object.
67+
if (a === INACTIVE_INLINED || b === INACTIVE_INLINED) return INACTIVE_INLINED;
4968
if (a === CIRCULAR_CONNECTION) return b;
5069
if (b === CIRCULAR_CONNECTION) return a;
5170
return a;
@@ -155,9 +174,8 @@ class ModuleGraphConnection {
155174
*/
156175
isActive(runtime) {
157176
if (!this.conditional) return this._active;
158-
159-
return (
160-
/** @type {GetConditionFn} */ (this.condition)(this, runtime) !== false
177+
return !isInactiveForInclusion(
178+
/** @type {GetConditionFn} */ (this.condition)(this, runtime)
161179
);
162180
}
163181

@@ -174,9 +192,11 @@ class ModuleGraphConnection {
174192
}
175193

176194
/**
177-
* Returns true: fully active, false: inactive, TRANSITIVE: direct module inactive, but transitive connection maybe active.
195+
* Returns true: fully active, false: inactive, TRANSITIVE: direct module inactive
196+
* but transitive connection maybe active, INACTIVE_INLINED: inactive because
197+
* the import was inlined into the referencing module's codegen.
178198
* @param {RuntimeSpec} runtime the runtime
179-
* @returns {ConnectionState} true: fully active, false: inactive, TRANSITIVE: direct module inactive, but transitive connection maybe active
199+
* @returns {ConnectionState} connection state
180200
*/
181201
getActiveState(runtime) {
182202
if (!this.conditional) return this._active;
@@ -196,6 +216,8 @@ class ModuleGraphConnection {
196216

197217
ModuleGraphConnection.CIRCULAR_CONNECTION = CIRCULAR_CONNECTION;
198218
ModuleGraphConnection.TRANSITIVE_ONLY = TRANSITIVE_ONLY;
219+
ModuleGraphConnection.INACTIVE_INLINED = INACTIVE_INLINED;
199220
ModuleGraphConnection.addConnectionStates = addConnectionStates;
221+
ModuleGraphConnection.isInactiveForInclusion = isInactiveForInclusion;
200222

201223
module.exports = ModuleGraphConnection;

‎lib/buildChunkGraph.js‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,9 @@ const visitModules = (
822822
);
823823
skipConnectionBuffer.push([refModule, connections]);
824824
// We skip inactive connections
825-
if (activeState === false) continue;
825+
if (ModuleGraphConnection.isInactiveForInclusion(activeState)) {
826+
continue;
827+
}
826828
} else if (
827829
// assigning ordinals lazily here keeps masks small
828830
isOrdinalSetInMask(minAvailableModules, getModuleOrdinal(refModule))
@@ -1191,7 +1193,9 @@ const visitModules = (
11911193
connections,
11921194
info.runtime
11931195
);
1194-
if (activeState === false) continue;
1196+
if (ModuleGraphConnection.isInactiveForInclusion(activeState)) {
1197+
continue;
1198+
}
11951199
if (activeState === true) {
11961200
const ordinal = getModuleOrdinal(module);
11971201
info.skippedModuleConnections.delete(entry);
@@ -1304,7 +1308,7 @@ const visitModules = (
13041308
const activeState = /** @type {ConnectionState} */ (
13051309
blockModules[i + 1]
13061310
);
1307-
if (activeState === false) {
1311+
if (ModuleGraphConnection.isInactiveForInclusion(activeState)) {
13081312
continue;
13091313
}
13101314
const refModule = /** @type {Module} */ (blockModules[i]);

‎lib/dependencies/HarmonyImportSpecifierDependency.js‎

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

88
const Dependency = require("../Dependency");
99
const InitFragment = require("../InitFragment");
10+
const ModuleGraphConnection = require("../ModuleGraphConnection");
1011
const Template = require("../Template");
1112
const {
1213
InlinedUsedName,
@@ -30,11 +31,7 @@ const { ImportPhaseUtils } = require("./ImportPhase");
3031
/** @import { DependencyTemplateContext } from "../DependencyTemplate" */
3132
/** @import Module, { BuildMeta } from "../Module" */
3233
/** @import ModuleGraph from "../ModuleGraph" */
33-
/**
34-
* @import ModuleGraphConnection, {
35-
* ConnectionState
36-
* } from "../ModuleGraphConnection"
37-
*/
34+
/** @import { ConnectionState } from "../ModuleGraphConnection" */
3835
/** @import WebpackError from "../errors/WebpackError" */
3936
/**
4037
* @import {
@@ -246,7 +243,7 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
246243
ids.length > 0 &&
247244
isExportInlined(moduleGraph, connection.module, ids, runtime)
248245
) {
249-
return false;
246+
return ModuleGraphConnection.INACTIVE_INLINED;
250247
}
251248
}
252249
const guards = dep.branchGuards;

‎types.d.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4943,7 +4943,10 @@ declare interface Configuration {
49434943
watchOptions?: WatchOptions;
49444944
}
49454945
type ConnectionState =
4946-
boolean | typeof CIRCULAR_CONNECTION | typeof TRANSITIVE_ONLY;
4946+
| boolean
4947+
| typeof CIRCULAR_CONNECTION
4948+
| typeof TRANSITIVE_ONLY
4949+
| typeof INACTIVE_INLINED;
49474950
declare class ConstDependency extends NullDependency {
49484951
/**
49494952
* Creates an instance of ConstDependency.
@@ -11109,6 +11112,7 @@ type IBigIntStatsTypes = IStatsBaseTypes<bigint> & {
1110911112
ctimeNs: bigint;
1111011113
birthtimeNs: bigint;
1111111114
};
11115+
declare const INACTIVE_INLINED: unique symbol;
1111211116

1111311117
/**
1111411118
* Returns location of targetPath relative to rootPath.
@@ -18271,7 +18275,9 @@ declare class ModuleGraphConnection {
1827118275
isTargetActive(runtime: RuntimeSpec): boolean;
1827218276

1827318277
/**
18274-
* Returns true: fully active, false: inactive, TRANSITIVE: direct module inactive, but transitive connection maybe active.
18278+
* Returns true: fully active, false: inactive, TRANSITIVE: direct module inactive
18279+
* but transitive connection maybe active, INACTIVE_INLINED: inactive because
18280+
* the import was inlined into the referencing module's codegen.
1827518281
*/
1827618282
getActiveState(runtime: RuntimeSpec): ConnectionState;
1827718283

@@ -18281,10 +18287,12 @@ declare class ModuleGraphConnection {
1828118287
setActive(value: boolean): void;
1828218288
static CIRCULAR_CONNECTION: typeof CIRCULAR_CONNECTION;
1828318289
static TRANSITIVE_ONLY: typeof TRANSITIVE_ONLY;
18290+
static INACTIVE_INLINED: typeof INACTIVE_INLINED;
1828418291
static addConnectionStates: (
1828518292
a: ConnectionState,
1828618293
b: ConnectionState
1828718294
) => ConnectionState;
18295+
static isInactiveForInclusion: (state: ConnectionState) => boolean;
1828818296
}
1828918297
type ModuleId = string | number;
1829018298
type ModuleInfo = ConcatenatedModuleInfo | ExternalModuleInfo;

0 commit comments

Comments
 (0)