Skip to content

Commit c50713c

Browse files
authored
fix: keep code generation data per result instead of per NormalModule (#21900)
1 parent ebd3be4 commit c50713c

5 files changed

Lines changed: 47 additions & 20 deletions

File tree

‎lib/NormalModule.js‎

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ const getModuleBuildError = memoize(() => require("./errors/ModuleBuildError"));
109109
* BuildMeta,
110110
* CodeGenerationContext,
111111
* CodeGenerationResult,
112-
* CodeGenerationResultData,
113112
* ConcatenationBailoutReasonContext,
114113
* KnownBuildInfo,
115114
* LibIdentOptions,
@@ -119,7 +118,6 @@ const getModuleBuildError = memoize(() => require("./errors/ModuleBuildError"));
119118
* NeedBuildCallback,
120119
* BuildCallback,
121120
* RuntimeRequirements,
122-
* Sources,
123121
* SourceType,
124122
* SourceTypes
125123
* } from "./Module"
@@ -728,11 +726,6 @@ class NormalModule extends Module {
728726
this._sideEffectsStateGraph = undefined;
729727
/** @type {ConnectionState | undefined} */
730728
this._sideEffectsStateValue = undefined;
731-
/**
732-
* @private
733-
* @type {CodeGenerationResultData}
734-
*/
735-
this._codeGeneratorData = new Map();
736729
}
737730

738731
/**
@@ -1892,10 +1885,19 @@ class NormalModule extends Module {
18921885
runtimeRequirements.add(RuntimeGlobals.thisAsExports);
18931886
}
18941887

1895-
const getData = () => this._codeGeneratorData;
1888+
/** @type {CodeGenerationResult} */
1889+
const resultEntry = {
1890+
sources: new Map(),
1891+
runtimeRequirements,
1892+
data: undefined,
1893+
hash: undefined
1894+
};
1895+
1896+
const getData = () => {
1897+
if (resultEntry.data === undefined) resultEntry.data = new Map();
1898+
return resultEntry.data;
1899+
};
18961900

1897-
/** @type {Sources} */
1898-
const sources = new Map();
18991901
for (const type of sourceTypes || chunkGraph.getModuleSourceTypes(this)) {
19001902
// TODO webpack@6 make generateError required
19011903
const generator =
@@ -1930,17 +1932,10 @@ class NormalModule extends Module {
19301932
});
19311933

19321934
if (source) {
1933-
sources.set(type, new CachedSource(source));
1935+
resultEntry.sources.set(type, new CachedSource(source));
19341936
}
19351937
}
19361938

1937-
/** @type {CodeGenerationResult} */
1938-
const resultEntry = {
1939-
sources,
1940-
runtimeRequirements,
1941-
data: this._codeGeneratorData,
1942-
hash: undefined
1943-
};
19441939
return resultEntry;
19451940
}
19461941

@@ -2116,7 +2111,6 @@ class NormalModule extends Module {
21162111
write(this.error);
21172112
write(this._lastSuccessfulBuildMeta);
21182113
write(this._forceBuild);
2119-
write(this._codeGeneratorData);
21202114
write(this.hot);
21212115
super.serialize(context);
21222116
}
@@ -2160,7 +2154,6 @@ class NormalModule extends Module {
21602154
this.error = read();
21612155
this._lastSuccessfulBuildMeta = read();
21622156
this._forceBuild = read();
2163-
this._codeGeneratorData = read();
21642157
this.hot = read();
21652158
super.deserialize(context);
21662159
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import fs from "fs";
2+
import path from "path";
3+
import { value } from "./shared";
4+
5+
// The import the `__dirname` replacement pulls in; line-anchored so this file's own source never matches.
6+
const FILE_URL_TO_PATH_IMPORT =
7+
/^import \{[^}]*fileURLToPath[^}]*\} from "(?:node:)?url";/m;
8+
9+
it("should drop the init fragments of a removed dependency", () => {
10+
const source = fs.readFileSync(
11+
path.join(STATS_JSON.outputPath, "bundle.mjs"),
12+
"utf8"
13+
);
14+
if (WATCH_STEP === "0") {
15+
expect(typeof value).toBe("string");
16+
expect(source).toMatch(FILE_URL_TO_PATH_IMPORT);
17+
} else {
18+
expect(value).toBe("static");
19+
expect(source).not.toMatch(FILE_URL_TO_PATH_IMPORT);
20+
}
21+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const value = __dirname;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const value = "static";
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"use strict";
2+
3+
// A module rebuilt in place must not carry the previous build's init fragments.
4+
/** @type {import("../../../../").Configuration} */
5+
module.exports = {
6+
target: "node",
7+
devtool: false,
8+
experiments: { outputModule: true },
9+
output: { module: true },
10+
node: { __dirname: "node-module" }
11+
};

0 commit comments

Comments
 (0)