Skip to content

Commit eeecde4

Browse files
perf(devtool): release the eval devtool's cached module sources (#22358)
* perf(devtool): release the eval devtool's cached module sources `EvalDevToolModulePlugin` reads every module's source to build its eval wrapper, which leaves the source holding a flattened copy nothing reads again. Clearing it follows the same feature-detected `clearCache` pattern `NormalModule` and `SourceMapDevToolPlugin` already use. Measured on a 2201-module build with `devtool: "eval"`: retained heap 173.25 -> 150.27 MiB, emitted output byte-identical, no rebuild penalty over five watch builds. * build(deps): update enhanced-resolve to 5.26.0 Its types now ship in a `types/` directory, so `types.d.ts` imports them instead of inlining them. The public type surface is unchanged: 876 exported names before and after, with `CacheClass` and `ResolveOptions` renamed internally but still exported under their original names. * test(devtool): exclude externals from the eval source-cache assertion `EvalDevToolModulePlugin` hands an external module's source back untouched, so its cache is never cleared. Asserting over every rendered source made the case error with `cleared 2 of 3 rendered sources` wherever an external reaches `renderModuleContent`, which a development-mode build of this fixture does.
1 parent 448a912 commit eeecde4

9 files changed

Lines changed: 403 additions & 708 deletions

File tree

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+
Release the eval devtool's cached module sources after wrapping them.
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+
Update enhanced-resolve to 5.26.0.

‎lib/devtool/EvalDevToolModulePlugin.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ const ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
2323
/** @type {WeakMap<Source, Source>} */
2424
const cache = new WeakMap();
2525

26+
const CLEAR_SOURCE_COPIES = { maps: false, source: true };
27+
2628
const devtoolWarning = new RawSource(`/*
2729
* ATTENTION: The "eval" devtool has been used (maybe by default in mode: "development").
2830
* This devtool is neither made for production nor for readable output files.
@@ -67,6 +69,10 @@ class EvalDevToolModulePlugin {
6769
apply(compiler) {
6870
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
6971
const hooks = JavascriptModulesPlugin.getCompilationHooks(compilation);
72+
// Shared across this compilation's modules, so a source reachable from
73+
// several chunks is walked once rather than once per chunk.
74+
/** @type {WeakSet<Source>} */
75+
const clearCacheVisited = new WeakSet();
7076
hooks.renderModuleContent.tap(
7177
PLUGIN_NAME,
7278
(source, module, { chunk, runtimeTemplate, chunkGraph }) => {
@@ -111,6 +117,15 @@ class EvalDevToolModulePlugin {
111117
});`
112118
);
113119
cache.set(source, result);
120+
// WHY: reading `source.source()` above left this source holding the
121+
// flattened module text, which nothing reads again — the wrapper
122+
// replaces it in rendering, and hashing ran before this. Clearing is
123+
// a memory hint, so the worst case is re-flattening on a later read.
124+
// Feature-detected, since `clearCache` landed in webpack-sources 3.5
125+
// and a third-party plugin may hand us an older `Source`.
126+
if (typeof source.clearCache === "function") {
127+
source.clearCache(CLEAR_SOURCE_COPIES, clearCacheVisited);
128+
}
114129
return result;
115130
}
116131
);

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
"@webassemblyjs/wasm-parser": "^1.14.1",
128128
"browserslist": "^4.28.1",
129129
"chrome-trace-event": "^1.0.2",
130-
"enhanced-resolve": "^5.25.0",
130+
"enhanced-resolve": "^5.26.0",
131131
"es-module-lexer": "^2.1.0",
132132
"events": "^3.2.0",
133133
"graceful-fs": "^4.2.11",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const value = 42;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { value } from "./dep";
2+
3+
it("should run fine with the eval devtool", () => {
4+
expect(value).toBe(42);
5+
});
6+
7+
it("should wrap the module in eval", () => {
8+
const self = require("fs").readFileSync(__filename, "utf8");
9+
expect(self).toContain("eval(");
10+
});
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"use strict";
2+
3+
/**
4+
* Asserts the contract `EvalDevToolModulePlugin` relies on: it drops each
5+
* rendered source's cached copy, and reading that source afterwards still
6+
* returns the same text.
7+
* @type {import("../../../../").WebpackPluginInstance}
8+
*/
9+
const observeSourceCacheClearing = {
10+
apply(compiler) {
11+
const { ExternalModule } = compiler.webpack;
12+
const { JavascriptModulesPlugin } = compiler.webpack.javascript;
13+
compiler.hooks.compilation.tap("Observe", (compilation) => {
14+
/** @type {Map<import("webpack-sources").Source, string>} */
15+
const textBefore = new Map();
16+
/** @type {Set<import("webpack-sources").Source>} */
17+
const cleared = new Set();
18+
19+
// Registered before EvalDevToolModulePlugin taps, so this sees each
20+
// original source rather than the eval wrapper replacing it.
21+
JavascriptModulesPlugin.getCompilationHooks(
22+
compilation
23+
).renderModuleContent.tap("Observe", (source, module) => {
24+
// The plugin hands an external module's source back untouched, so its
25+
// cache is never cleared and it is not part of the contract tested here.
26+
if (module instanceof ExternalModule) return source;
27+
if (textBefore.has(source)) return source;
28+
textBefore.set(source, String(source.source()));
29+
const { clearCache } = source;
30+
if (typeof clearCache === "function") {
31+
source.clearCache = (...args) => {
32+
cleared.add(source);
33+
return clearCache.apply(source, args);
34+
};
35+
}
36+
return source;
37+
});
38+
39+
compilation.hooks.afterSeal.tap("Observe", () => {
40+
// A run restoring its assets from the persistent cache renders
41+
// nothing, so there is nothing to assert; the uncached run asserts it.
42+
if (textBefore.size === 0) return;
43+
if (cleared.size !== textBefore.size) {
44+
compilation.errors.push(
45+
new Error(
46+
`Observe: cleared ${cleared.size} of ${textBefore.size} rendered sources`
47+
)
48+
);
49+
}
50+
for (const [source, before] of textBefore) {
51+
if (String(source.source()) !== before) {
52+
compilation.errors.push(
53+
new Error("Observe: a source read differently after clearCache")
54+
);
55+
return;
56+
}
57+
}
58+
});
59+
});
60+
}
61+
};
62+
63+
/** @type {import("../../../../").Configuration} */
64+
module.exports = {
65+
devtool: "eval",
66+
plugins: [observeSourceCacheClearing]
67+
};

0 commit comments

Comments
 (0)