Skip to content

Commit 5f9e60a

Browse files
perf(javascript): release a child compilation's rendered sources too
A child compiler never inherits `afterEmit`, and `runAsChild` copies the child's assets into the parent, which keeps the child reachable through `compilation.children`. Its rendered sources were therefore tracked but never released. The parent's emit now walks its children as well. Measured on a production build with one child compilation: 8 sources holding 1.34 MiB of flattened text stayed cached, and now none do. The test case builds a child compilation, so both paths are covered.
1 parent 3dc80ef commit 5f9e60a

3 files changed

Lines changed: 37 additions & 6 deletions

File tree

‎lib/javascript/JavascriptModulesPlugin.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,12 @@ class JavascriptModulesPlugin {
896896
* @returns {void}
897897
*/
898898
_releaseRenderedModuleFactorySources(compilation) {
899+
// A child compiler never inherits `afterEmit` and its assets are copied
900+
// into this compilation, which keeps the child reachable through
901+
// `children`, so the parent's emit is where its sources are released too.
902+
for (const child of compilation.children) {
903+
this._releaseRenderedModuleFactorySources(child);
904+
}
899905
const { codeGenerationResults } = compilation;
900906
if (codeGenerationResults === undefined) return;
901907
const renderedSources = this._renderedModuleFactorySources.get(
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { value } from "./dep";
2+
3+
export const childValue = value();

‎test/configCases/code-generation/release-rendered-module-sources/webpack.config.js‎

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,26 @@ const observeRenderedSourceRelease = {
4040
}
4141
}
4242
);
43-
compiler.hooks.done.tap("Observe", () => {
43+
// Reports on the root compilation, since a child's own errors do not
44+
// reach the test.
45+
compiler.hooks.done.tap("Observe", (stats) => {
46+
const { errors } = stats.compilation;
4447
// A run restoring its assets from the persistent cache renders
4548
// nothing, so there is nothing to assert; the uncached run asserts it.
4649
if (textBefore.size === 0) return;
4750
if (cleared.size !== textBefore.size) {
48-
compilation.errors.push(
51+
errors.push(
4952
new Error(
50-
`released ${cleared.size} of ${textBefore.size} rendered module sources`
53+
`${compilation.name || "main"}: released ${cleared.size} of ${textBefore.size} rendered module sources`
5154
)
5255
);
5356
}
5457
for (const [source, before] of textBefore) {
5558
if (String(source.source()) !== before) {
56-
compilation.errors.push(
57-
new Error("a released source rendered different text")
59+
errors.push(
60+
new Error(
61+
`${compilation.name || "main"}: a released source rendered different text`
62+
)
5863
);
5964
}
6065
}
@@ -63,10 +68,27 @@ const observeRenderedSourceRelease = {
6368
}
6469
};
6570

71+
// A child compiler renders its own modules but never emits, and it does not
72+
// inherit `afterEmit`, so its sources are released by the parent's.
73+
/** @type {import("../../../../").WebpackPluginInstance} */
74+
const buildChildCompilation = {
75+
apply(compiler) {
76+
const { EntryPlugin } = compiler.webpack;
77+
compiler.hooks.make.tapAsync("Child", (compilation, callback) => {
78+
const child = compilation.createChildCompiler(
79+
"child",
80+
{ filename: "child-bundle.js" },
81+
[new EntryPlugin(compiler.context, "./child.js", { name: "child" })]
82+
);
83+
child.runAsChild((err) => callback(err));
84+
});
85+
}
86+
};
87+
6688
/** @type {import("../../../../").Configuration} */
6789
module.exports = {
6890
// Keeps the two modules apart, so the chunk renders module factories rather
6991
// than inlining one module into its startup.
7092
optimization: { concatenateModules: false },
71-
plugins: [observeRenderedSourceRelease]
93+
plugins: [observeRenderedSourceRelease, buildChildCompilation]
7294
};

0 commit comments

Comments
 (0)