Skip to content

Commit 4f4ba91

Browse files
feat(esm): bake analyzable urls where the name settles late (#21828)
* feat(esm): bake analyzable names into content-named chunks A chunk named by [chunkhash], or by [contenthash] with realContentHash off, kept the runtime form: the deferred pass rewrites the asset after its name was taken, and nothing repairs that name afterwards. Fold what each stand-in resolves to into the consuming chunk's own hash before the name is taken, so the two move together — the same thing RuntimeModule.dependentHash already does for __webpack_require__.u. The bake-order predicate now follows createHash's real rounds (async, then initial, then runtime, then entry) instead of comparing ids alone, since a folded name has to be settled first; one direction of a chunk cycle bakes on that order too. A stand-in whose prefix carries a public path, a template or an undo path still needs the repair: those resolve against the compilation hash or the consuming asset's own name, neither settled while it is hashed. * perf(esm): cache what the analyzable name fold walks The fold is asked twice per chunk — once for chunk.hash, once for contentHash — and each ask walked everything the chunk reaches and re-resolved every name through getPath. Memoize the folded text per chunk and the resolved name per chunk and type, and take the second walk over initial chunks only for a runtime chunk, which is the only one that writes out a name it does not reference. Both caches are dead once createHash returns, so afterHash drops them rather than holding a name per chunk edge until seal. Counted over a 400-chunk dynamic-import tree: the fold falls from 31.6ms to 6.1ms, and its share of build cpu from 5.6% to 1.3%. The default production path (contenthash with realContentHash) stays at 0.1% and makes no call into the new predicates at all; a build without output.module never registers the tap. * feat(esm): bake references a per-asset path used to block Two more analyzable bailouts, both about text that varies per asset rather than per build: A module duplicated into chunks at two output depths needs each copy to carry its own `../` path to the chunk they share, which only a stand-in resolved per asset can do. That was refused wherever those chunks were named by their own content — but the depth is read off the filename template with the hashes neutralized, exactly as `_chunkPlacement` already does during code generation, so it reaches the hash before the name is taken like any other part. A module in a chunk the host fetched and in one the loader fetched through `output.publicPath` sits at two urls one public path apart. That only makes the two disagree where the public path has a depth of its own — where it has none, both answers put the same text in front, so the literal is right either way and only a public path that climbs still keeps the runtime form. Asking a chunk for its depth during hashing also surfaced a latent throw: a `chunkFilename` function was handed to `getPath`, which then had to resolve a `[contenthash]` in what it returned against a hash that does not exist yet. It is now asked for its template first, and one that builds a name out of a hash itself gets a stand-in to build from. * fix(esm): read the base uri off the chunk that sets it `entryBaseUri` polled every chunk carrying entry options and gave up when two of them named different bases. But the value a literal has to agree with is the one `BaseUriRuntimeModule` writes out, and it reads the entry options off the chunk it is emitted into — so entries sharing a runtime chunk already share the base it sets, whatever their own descriptors say. Under `runtimeChunk: "single"` webpack emits one `__webpack_require__.b` taken from the first entry, and every entry resolves against it. The runtime form already did exactly that; now the baked literal spells the same url instead of refusing, and the runtime module that set the base drops out because nothing reads it any more. Entries that really are generated for separate runtimes keep their own bases, and one source shared across runtimes that disagree still bails. * feat(esm): bake names built from the compilation hash A public path or a wasm filename naming `[fullhash]` was refused wherever the chunk holding the reference is named by its own content with nothing to repair that name — so a library shipping either could not be followed statically. The refusal was self-defeating. Such a chunk normally reaches for `__webpack_require__.p`, whose runtime module is flagged `fullHash`, which already puts the chunk in `createHash`'s full-hash round: its name is taken again once the compilation hash exists. Baking the same text drops that runtime module and with it the very property that made the name sound. So put the chunk there on purpose. After code generation, the chunks whose bytes will carry that hash are read back out of what was generated — the stand-in says so itself, which a module restored from the persistent cache still carries — and each is attached to the full-hash round with no module to re-render. Its `[chunkhash]` is remixed with the compilation hash by the existing loop, and its `[contenthash]` is folded with it in the round that follows. Nothing may read such a chunk's name, so a content-named chunk in a build that bakes the hash is not one whose name is baked elsewhere. Asked of the shape of the build rather than of the marks, which are not made until code generation is over, so both phases give one answer. Two wasm cases documented the old refusal and now bake; the case covering the runtime interpolation keeps it with `devtool: "eval"`, a reason unrelated to hashing. * feat(esm): order bakes by when a chunk settles, not by its runtime Two refusals were standing in for one question. A runtime chunk was refused outright, because only such a chunk can carry a full-hash module and so be re-hashed after every round; and a content-named chunk was refused in any build that bakes the compilation hash, because it might be moved into that round. Both were guesses at the same thing: when does this chunk's name settle. Ask it directly instead. The chunk graph says exactly which chunks reach that last round, so the order is early-before-late first and the four rounds after — and a chunk baking a name that settles late is marked to settle later still, computed to a fixed point since the chunk it names may be baked somewhere further up in turn. During code generation, before the marks exist, the answer is the one the marking will bring about. That leaves the runtime round, which is ordered by references between its chunks rather than by id. Mirror the relation `createHash` uses: an async entrypoint the target reaches is settled before it — which is a worker's chunk against the chunk spawning it. A worker's chunk is reached by neither the referenced-chunk walk nor the initial-chunk one, so the fold now walks the async entrypoints too. Without that, a chunk baked the worker's name while its own hash stood still — the watch case added here fails exactly that way. * feat(esm): bake the last two urls that varied per asset A protocol-relative entry `baseUri` names a host but no scheme, and was refused because `new URL(name, "//host/")` cannot be resolved here. But the runtime does not resolve it here either: it writes `new URL("//host/", import.meta.url)` and reads the scheme off the chunk's own url — the very url the literal is resolved against. So the literal keeps the base protocol-relative and settles the same way at load time, and the runtime module that set the base drops out. A module in a chunk the host fetched and in one the loader fetched through a public path with a depth of its own sits at two urls one directory apart. Neither literal is right for both, so each asset now gets its own: a `"unserved"` part the deferred pass resolves against the chunk it is writing into, putting the public path in front only where the asset is not already under it. A chunk that is itself served both ways has no answer of its own and still keeps the runtime form. `asset-url-mixed-serving` documented the old fallback and now asserts the two literals — `"./media/asset.txt"` where the host fetched the chunk and `"../asset.txt"` where webpack did, both naming the same file. * test(wasm): keep the runtime name without inflating the case `full-hash-digest-filename` needs a reason to keep the runtime form that has nothing to do with hashing, and `devtool: "eval"` supplied one by wrapping every module in an escaped string — which grew each of the three bundles by ~877 B and each chunk by ~749 B, most of the size the report attributes to this change. Reassign `__webpack_public_path__` instead. It says the same thing more directly — the loader has to build the name at runtime because the path is chosen there — and the value assigned is the one `auto` computes, so nothing else moves. The chunks go back to the bytes they had before. * test(esm): cover the transitive marking and tighten the prose Review findings: Comments in `lib/` and `test/` must be at most two short lines; several of the new ones ran to three or four. Trimmed each to the fact it carries, with the longer reasoning already in the enclosing JSDoc. The changeset description is back under 80 characters. `findBundle` scanned the output directory with `.find`, which yields `"./undefined"` and an unclear failure if nothing matches. It now takes the whole match list and names what it found, so a directory holding other than one bundle fails loudly instead of executing the wrong file. `fullhash-public-path` reached the fixed-point loop that marks a chunk late for baking a late name, but never its body: every chunk that baked one already carried the compilation hash itself and was marked in the first pass. A middle chunk that only names the one below it — no url of its own — is the shape that needs the second pass, and it now covers that loop. * test(wasm): shorten the public path comment Three lines where two carry the same fact. * fix(esm): fold only the names a chunk actually bakes The fold walked everything the chunk's group reaches, so a split chunk that references nothing still had its neighbours' names folded in: an entrypoint's async children are reachable from every chunk sharing that group. Changing a lazy chunk therefore moved the `splitChunks` vendor chunk's hash — the opposite of what splitting it out is for. Use the set the marking pass already reads back off the generated code, which names exactly the chunks a stand-in here resolves to. It needs no graph walk, and the worker case it used to take a third walk for is covered because the payload names that chunk directly. * fix(runtime): hash a chunk loader by its code under a content-named filename A runtime module is generated once to be hashed, before any hash exists, so a chunk loader resolving its own `[contenthash]` name there threw — and `RuntimeModule.updateHash` swallows that, pinning the module's hash to the message. The code generation cache then answered a later build with the first one's source, so a loader that had to grow never reached the asset. Read the name through `RuntimeTemplate.chunkRootOutputDir`, which neutralizes the hashes it does not need: they sit in the basename and the `../` depth is all these five call sites want. * test(wasm): cover the baked wasm url alongside the runtime-read one The public-path reassignment that keeps a config on the runtime naming path was applied to the whole case, so all three configs grew a `get javascript chunk filename` module the analyzable form had made unnecessary. Gate it per config and let the first one bake, asserting the literal url in the chunk that holds the reference — one config back to what it costs on `main`, and a fourth behaviour covered rather than a third one lost. Also correct the `base-uri-relative` note that says a protocol-relative base cannot be spelled as a literal; it bakes. * test(esm): shorten the fold comment to two lines * refactor(esm): drop the unused build-bakes-full-hash predicate Left over from an earlier shape of the full-hash work, where a build-wide answer gated the stand-ins; marking the chunks that settle late replaced it and nothing has called it since.
1 parent 7651c03 commit 4f4ba91

82 files changed

Lines changed: 1352 additions & 203 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.changeset/020-analyzable-esm-output.md‎

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

5-
Emit analyzable ESM urls: chunks, assets, styles, workers, wasm, eval included.
5+
Emit analyzable ESM urls for chunks, assets, styles, workers and wasm.
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+
Rehash runtime modules whose code changes under a content-named filename.

‎lib/RuntimeTemplate.js‎

Lines changed: 610 additions & 50 deletions
Large diffs are not rendered by default.

‎lib/esm/ModuleChunkLoadingRuntimeModule.js‎

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,11 @@ const Template = require("../Template");
1212
const {
1313
generateJavascriptHMR
1414
} = require("../hmr/JavascriptHotModuleReplacementHelper");
15-
const {
16-
chunkHasJs,
17-
getChunkFilenameTemplate
18-
} = require("../javascript/JavascriptModulesPlugin");
15+
const { chunkHasJs } = require("../javascript/JavascriptModulesPlugin");
1916
const { getInitialChunkIds } = require("../javascript/StartupHelpers");
2017
const { renderBaseUri } = require("../runtime/baseUri");
2118
const compileBooleanMatcher = require("../util/compileBooleanMatcher");
2219
const createHooksRegistry = require("../util/createHooksRegistry");
23-
const { getUndoPath } = require("../util/identifier");
2420
const memoize = require("../util/memoize");
2521

2622
const getAPIPlugin = memoize(() => require("../APIPlugin"));
@@ -146,18 +142,7 @@ class ModuleChunkLoadingRuntimeModule extends RuntimeModule {
146142
const hasJsMatcher = compileBooleanMatcher(conditionMap);
147143
const initialChunkIds = getInitialChunkIds(chunk, chunkGraph, chunkHasJs);
148144

149-
const outputName = compilation.getPath(
150-
getChunkFilenameTemplate(chunk, compilation.outputOptions),
151-
{
152-
chunk,
153-
contentHashType: "javascript"
154-
}
155-
);
156-
const rootOutputDir = getUndoPath(
157-
outputName,
158-
compilation.outputOptions.path,
159-
true
160-
);
145+
const rootOutputDir = runtimeTemplate.chunkRootOutputDir(chunk, true);
161146

162147
const { publicPath } = compilation.outputOptions;
163148
// `import()` resolves a bare specifier as a package name, so a public path that

‎lib/node/ReadFileChunkLoadingRuntimeModule.js‎

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,10 @@ const Template = require("../Template");
1010
const {
1111
generateJavascriptHMR
1212
} = require("../hmr/JavascriptHotModuleReplacementHelper");
13-
const {
14-
chunkHasJs,
15-
getChunkFilenameTemplate
16-
} = require("../javascript/JavascriptModulesPlugin");
13+
const { chunkHasJs } = require("../javascript/JavascriptModulesPlugin");
1714
const { getInitialChunkIds } = require("../javascript/StartupHelpers");
1815
const { renderBaseUri } = require("../runtime/baseUri");
1916
const compileBooleanMatcher = require("../util/compileBooleanMatcher");
20-
const { getUndoPath } = require("../util/identifier");
2117

2218
/** @import Chunk from "../Chunk" */
2319
/** @import ChunkGraph from "../ChunkGraph" */
@@ -86,18 +82,7 @@ class ReadFileChunkLoadingRuntimeModule extends RuntimeModule {
8682
const hasJsMatcher = compileBooleanMatcher(conditionMap);
8783
const initialChunkIds = getInitialChunkIds(chunk, chunkGraph, chunkHasJs);
8884

89-
const outputName = compilation.getPath(
90-
getChunkFilenameTemplate(chunk, compilation.outputOptions),
91-
{
92-
chunk,
93-
contentHashType: "javascript"
94-
}
95-
);
96-
const rootOutputDir = getUndoPath(
97-
outputName,
98-
compilation.outputOptions.path,
99-
false
100-
);
85+
const rootOutputDir = runtimeTemplate.chunkRootOutputDir(chunk, false);
10186

10287
const stateExpression = withHmr
10388
? `${RuntimeGlobals.hmrRuntimeStatePrefix}_readFileVm`

‎lib/node/RequireChunkLoadingRuntimeModule.js‎

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,10 @@ const Template = require("../Template");
1010
const {
1111
generateJavascriptHMR
1212
} = require("../hmr/JavascriptHotModuleReplacementHelper");
13-
const {
14-
chunkHasJs,
15-
getChunkFilenameTemplate
16-
} = require("../javascript/JavascriptModulesPlugin");
13+
const { chunkHasJs } = require("../javascript/JavascriptModulesPlugin");
1714
const { getInitialChunkIds } = require("../javascript/StartupHelpers");
1815
const { renderBaseUri } = require("../runtime/baseUri");
1916
const compileBooleanMatcher = require("../util/compileBooleanMatcher");
20-
const { getUndoPath } = require("../util/identifier");
2117

2218
/** @import Chunk from "../Chunk" */
2319
/** @import ChunkGraph from "../ChunkGraph" */
@@ -86,18 +82,7 @@ class RequireChunkLoadingRuntimeModule extends RuntimeModule {
8682
const hasJsMatcher = compileBooleanMatcher(conditionMap);
8783
const initialChunkIds = getInitialChunkIds(chunk, chunkGraph, chunkHasJs);
8884

89-
const outputName = compilation.getPath(
90-
getChunkFilenameTemplate(chunk, compilation.outputOptions),
91-
{
92-
chunk,
93-
contentHashType: "javascript"
94-
}
95-
);
96-
const rootOutputDir = getUndoPath(
97-
outputName,
98-
compilation.outputOptions.path,
99-
true
100-
);
85+
const rootOutputDir = runtimeTemplate.chunkRootOutputDir(chunk, true);
10186

10287
const stateExpression = withHmr
10388
? `${RuntimeGlobals.hmrRuntimeStatePrefix}_require`

‎lib/runtime/AutoPublicPathRuntimeModule.js‎

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
const RuntimeGlobals = require("../RuntimeGlobals");
88
const RuntimeModule = require("../RuntimeModule");
99
const Template = require("../Template");
10-
const JavascriptModulesPlugin = require("../javascript/JavascriptModulesPlugin");
11-
const { getUndoPath } = require("../util/identifier");
1210

1311
/** @import Chunk from "../Chunk" */
1412
/** @import ChunkGraph from "../ChunkGraph" */
@@ -30,22 +28,11 @@ class AutoPublicPathRuntimeModule extends RuntimeModule {
3028
if (/** @type {ChunkGraph} */ (this.chunkGraph).buildTimeExecution) {
3129
return `${RuntimeGlobals.publicPath} = "";`;
3230
}
33-
const { scriptType, importMetaName, path, environment } =
31+
const { scriptType, importMetaName, environment } =
3432
compilation.outputOptions;
3533
const chunk = /** @type {Chunk} */ (this.chunk);
36-
const chunkName = compilation.getPath(
37-
JavascriptModulesPlugin.getChunkFilenameTemplate(
38-
chunk,
39-
compilation.outputOptions
40-
),
41-
{
42-
chunk,
43-
contentHashType: "javascript"
44-
}
45-
);
46-
const undoPath = getUndoPath(
47-
chunkName,
48-
/** @type {string} */ (path),
34+
const undoPath = compilation.runtimeTemplate.chunkRootOutputDir(
35+
chunk,
4936
false
5037
);
5138

‎lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js‎

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,10 @@ const Template = require("../Template");
1010
const {
1111
generateJavascriptHMR
1212
} = require("../hmr/JavascriptHotModuleReplacementHelper");
13-
const {
14-
chunkHasJs,
15-
getChunkFilenameTemplate
16-
} = require("../javascript/JavascriptModulesPlugin");
13+
const { chunkHasJs } = require("../javascript/JavascriptModulesPlugin");
1714
const { getInitialChunkIds } = require("../javascript/StartupHelpers");
1815
const { renderBaseUri } = require("../runtime/baseUri");
1916
const compileBooleanMatcher = require("../util/compileBooleanMatcher");
20-
const { getUndoPath } = require("../util/identifier");
2117

2218
/** @import Chunk from "../Chunk" */
2319
/** @import ChunkGraph from "../ChunkGraph" */
@@ -45,16 +41,8 @@ class ImportScriptsChunkLoadingRuntimeModule extends RuntimeModule {
4541
_generateBaseUri(chunk) {
4642
const options = chunk.getEntryOptions();
4743
const compilation = /** @type {Compilation} */ (this.compilation);
48-
const outputName = compilation.getPath(
49-
getChunkFilenameTemplate(chunk, compilation.outputOptions),
50-
{
51-
chunk,
52-
contentHashType: "javascript"
53-
}
54-
);
55-
const rootOutputDir = getUndoPath(
56-
outputName,
57-
compilation.outputOptions.path,
44+
const rootOutputDir = compilation.runtimeTemplate.chunkRootOutputDir(
45+
chunk,
5846
false
5947
);
6048
return renderBaseUri(

‎test/configCases/analyzable/asset-url-mixed-serving/index.js‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ it("should point at the asset through the public path", () => {
1111
expect(url.href.endsWith("/media/asset.txt")).toBe(true);
1212
});
1313

14-
it("should keep the runtime form where the two chunks disagree", () => {
14+
it("should give each chunk the path right where it is read from", () => {
1515
// Built at runtime so the needle is not a source string literal here.
1616
const runtimeForm = `${"__webpack_require__"}.p + ${JSON.stringify("asset.txt")}`;
1717

18-
expect(read("bundle0.mjs")).toContain(runtimeForm);
19-
expect(read("c/on-demand.mjs")).toContain(runtimeForm);
18+
// The host fetched this one, so it is not under the public path yet.
19+
expect(read("bundle0.mjs")).toContain('"./media/asset.txt"');
20+
// Webpack fetched this one through the public path, and it sits a directory down.
21+
expect(read("c/on-demand.mjs")).toContain('"../asset.txt"');
22+
expect(read("bundle0.mjs")).not.toContain(runtimeForm);
23+
expect(read("c/on-demand.mjs")).not.toContain(runtimeForm);
2024
});

‎test/configCases/analyzable/asset-url-mixed-serving/webpack.config.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22

33
// A public path needing a base is spelled from a chunk the host loads and left out of
4-
// one webpack loads through it. A module in both has no one answer, so it falls back.
4+
// one webpack loads through it, so each asset gets its own answer.
55

66
/** @type {import("../../../../").Configuration} */
77
module.exports = {

0 commit comments

Comments
 (0)