Skip to content

Commit 7fae777

Browse files
fix(analyzable): repair hashed names on demand instead of keeping the runtime form
A baked reference into a chunk whose hashed name has nothing repairing it kept the runtime form unless the fold could settle it first — which a pair naming each other, a [chunkhash] name in a cycle, or a two-depth stand-in into content-named chunks all defeat. Such a stand-in is now reserved with a repair mark: the deferred fill records it, and RealContentHashPlugin runs for those builds alone where optimization.realContentHash did not ask for it, tracking [chunkhash] values as well so a name the fill left stale is re-hashed from what it left. The fold stays the cheap first choice wherever it applies. With every hashed name repairable, a chunk that could not be named no longer exists, so the partial url maps and the per-asset runtime fallback behind them go, as do the deferral bailouts and the reachability walk that only served their messages.
1 parent 84fd011 commit 7fae777

37 files changed

Lines changed: 339 additions & 351 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+
Repair hashed names on demand so cycles and [chunkhash] names bake in ESM output.

‎lib/RuntimeTemplate.js‎

Lines changed: 102 additions & 216 deletions
Large diffs are not rendered by default.

‎lib/WebpackOptionsApply.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,9 @@ class WebpackOptionsApply extends OptionsApply {
778778

779779
new NoEmitOnErrorsPlugin().apply(compiler);
780780
}
781-
if (options.optimization.realContentHash) {
781+
// ESM output may bake a name into a chunk whose hashed name would then go stale;
782+
// the repair runs for those alone where it was not asked for outright.
783+
if (options.optimization.realContentHash || options.output.module) {
782784
const RealContentHashPlugin = require("./optimize/RealContentHashPlugin");
783785

784786
new RealContentHashPlugin({
@@ -787,7 +789,8 @@ class WebpackOptionsApply extends OptionsApply {
787789
(options.output.hashFunction),
788790
hashDigest:
789791
/** @type {NonNullable<WebpackOptions["output"]["hashDigest"]>} */
790-
(options.output.hashDigest)
792+
(options.output.hashDigest),
793+
onDemand: !options.optimization.realContentHash
791794
}).apply(compiler);
792795
}
793796
if (options.optimization.checkWasmTypes) {

‎lib/css/CssLoadingRuntimeModule.js‎

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -185,32 +185,22 @@ class CssLoadingRuntimeModule extends RuntimeModule {
185185
",\n"
186186
)
187187
)}\n}`;
188-
/**
189-
* @param {string} id expression naming the chunk whose stylesheet is wanted
190-
* @returns {string} expression building its url at runtime
191-
*/
192-
const runtimeCssUrl = (id) =>
193-
`${RuntimeGlobals.publicPath} + ${RuntimeGlobals.getChunkCssFilename}(${id})`;
194188
// The url is read through a thunk so a runtime carrying many stylesheets builds
195189
// only the one it loads.
196190
/**
197191
* @param {string} id expression naming the chunk whose stylesheet is wanted
198192
* @returns {string} expression evaluating to its url
199193
*/
200-
const cssUrl = (id) => {
201-
if (!cssUrls) return runtimeCssUrl(id);
202-
// An id the incomplete map lacks still resolves through the runtime lookup,
203-
// which the plugin kept required for exactly this case.
204-
return cssUrls.complete
205-
? `cssUrls[${id}]()`
206-
: `(cssUrls[${id}] ? cssUrls[${id}]() : ${runtimeCssUrl(id)})`;
207-
};
194+
const cssUrl = (id) =>
195+
cssUrls === null
196+
? `${RuntimeGlobals.publicPath} + ${RuntimeGlobals.getChunkCssFilename}(${id})`
197+
: `cssUrls[${id}]()`;
208198
return Template.asString([
209199
...(cssUrls
210200
? [
211201
`${cst} cssUrls = {\n${Template.indent(
212202
Array.from(
213-
cssUrls.urls,
203+
cssUrls,
214204
([id, url]) =>
215205
`${JSON.stringify(String(id))}: ${runtimeTemplate.returningFunction(url)}`
216206
).join(",\n")

‎lib/css/CssModulesPlugin.js‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -772,12 +772,13 @@ class CssModulesPlugin {
772772
// A baked url carries where the stylesheet is, so neither the public
773773
// path nor the name lookup is read for it. Asked exactly as the
774774
// loading runtime module asks, so the two always agree.
775-
const baked = compilation.runtimeTemplate.analyzableCssChunkUrls(
776-
chunk,
777-
chunkGraph,
778-
set
779-
);
780-
if (baked === null || !baked.complete) {
775+
if (
776+
compilation.runtimeTemplate.analyzableCssChunkUrls(
777+
chunk,
778+
chunkGraph,
779+
set
780+
) === null
781+
) {
781782
set.add(RuntimeGlobals.publicPath);
782783
set.add(RuntimeGlobals.getChunkCssFilename);
783784
}

‎lib/dependencies/WorkerDependency.js‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ WorkerDependency.Template = class WorkerDependencyTemplate extends (
140140
dep.options.publicPath,
141141
chunk,
142142
templateContext.module,
143-
chunkGraph,
144-
runtimeRequirements
143+
chunkGraph
145144
);
146145

147146
if (analyzableSpecifier !== null) {

‎lib/dependencies/WorkletDependency.js‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ WorkletDependency.Template = class WorkletDependencyTemplate extends (
144144
dep.options.publicPath,
145145
entryChunk,
146146
module,
147-
chunkGraph,
148-
runtimeRequirements
147+
chunkGraph
149148
);
150149
if (analyzableSpecifier !== null) {
151150
source.replace(

‎lib/esm/ModuleChunkLoadingPlugin.js‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,15 @@ class ModuleChunkLoadingPlugin {
120120
if (!isEnabledForChunk(chunk)) return;
121121
// A baked url carries where the chunk is, so neither is read for it.
122122
// Asked as the runtime module asks, so the two always agree.
123-
const baked = compilation.runtimeTemplate.analyzableChunkScriptUrls(
124-
chunk,
125-
chunkGraph,
126-
set
127-
);
128-
if (baked !== null && baked.complete) return;
123+
if (
124+
compilation.runtimeTemplate.analyzableChunkScriptUrls(
125+
chunk,
126+
chunkGraph,
127+
set
128+
) !== null
129+
) {
130+
return;
131+
}
129132
set.add(RuntimeGlobals.publicPath);
130133
set.add(RuntimeGlobals.getChunkScriptFilename);
131134
});

‎lib/esm/ModuleChunkLoadingRuntimeModule.js‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class ModuleChunkLoadingRuntimeModule extends RuntimeModule {
129129
chunk.hasChildByOrder(chunkGraph, "preload", true, chunkHasJs);
130130
// Under module output each hinted chunk is a known file, so the urls are written
131131
// out and read by id rather than built from the chunk id at runtime.
132-
const bakedChunkUrls =
132+
const chunkUrls =
133133
withPrefetch || withPreload
134134
? runtimeTemplate.analyzableChunkScriptUrls(
135135
chunk,
@@ -138,12 +138,6 @@ class ModuleChunkLoadingRuntimeModule extends RuntimeModule {
138138
this
139139
)
140140
: null;
141-
// A hint is best-effort, so an id an incomplete map lacks is not worth a per-id
142-
// fallback — only a complete map replaces the runtime name lookup.
143-
const chunkUrls =
144-
bakedChunkUrls !== null && bakedChunkUrls.complete
145-
? bakedChunkUrls.urls
146-
: null;
147141
const conditionMap = chunkGraph.getChunkConditionMap(chunk, chunkHasJs);
148142
const hasJsMatcher = compileBooleanMatcher(conditionMap);
149143
const initialChunkIds = getInitialChunkIds(chunk, chunkGraph, chunkHasJs);

‎lib/optimize/RealContentHashPlugin.js‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ const createCompilationHooks = () => ({
187187
* @typedef {object} RealContentHashPluginOptions
188188
* @property {HashFunction} hashFunction the hash function to use
189189
* @property {HashDigest} hashDigest the hash digest to use
190+
* @property {boolean=} onDemand run only where analyzable output marked a name for repair
190191
*/
191192

192193
const PLUGIN_NAME = "RealContentHashPlugin";
@@ -196,11 +197,13 @@ class RealContentHashPlugin {
196197
* Creates an instance of RealContentHashPlugin.
197198
* @param {RealContentHashPluginOptions} options options
198199
*/
199-
constructor({ hashFunction, hashDigest }) {
200+
constructor({ hashFunction, hashDigest, onDemand }) {
200201
/** @type {HashFunction} */
201202
this._hashFunction = hashFunction;
202203
/** @type {HashDigest} */
203204
this._hashDigest = hashDigest;
205+
/** @type {boolean} */
206+
this._onDemand = Boolean(onDemand);
204207
}
205208

206209
/**
@@ -223,6 +226,11 @@ class RealContentHashPlugin {
223226
stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_HASH
224227
},
225228
async () => {
229+
// Set by the deferred fill where a baked name landed in a chunk whose
230+
// hashed name would otherwise go stale. `[chunkhash]` is repaired only
231+
// then; elsewhere it keeps naming the modules, as documented.
232+
const repairing = compilation.runtimeTemplate.needsAnalyzableRepair();
233+
if (this._onDemand && !repairing) return;
226234
const assets = compilation.getAssets();
227235
/** @type {AssetInfoForRealContentHash[]} */
228236
const assetsWithInfo = [];
@@ -238,6 +246,7 @@ class RealContentHashPlugin {
238246
/** @type {Hashes} */
239247
const hashes = new Set();
240248
addToList(info.contenthash, hashes);
249+
if (repairing) addToList(info.chunkhash, hashes);
241250
if (info.contenthashDigest) {
242251
for (const hash of Object.keys(info.contenthashDigest)) {
243252
hashToDigest.set(hash, info.contenthashDigest[hash]);

0 commit comments

Comments
 (0)