Skip to content

Commit 86d2358

Browse files
fix(analyzable): rename only what an on-demand repair touched
The fill records which assets it rewrote under a name marked for repair, and an on-demand RealContentHashPlugin recomputes only their hashes and those of what names them: every other asset keeps the name it was given, instead of moving to a real content hash the moment any repair is needed. The runtime chunk of an ESM build no longer names an async chunk, so the watch case expects it to stay put.
1 parent 12c35d7 commit 86d2358

7 files changed

Lines changed: 124 additions & 12 deletions

File tree

‎lib/RuntimeTemplate.js‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,8 @@ class RuntimeTemplate {
446446
(getGlobalObject(outputOptions.globalObject));
447447
/** @type {string} */
448448
this.contentHashReplacement = "X".repeat(outputOptions.hashDigestLength);
449-
/** @type {boolean} */
450-
this._analyzableRepairNeeded = false;
449+
/** @type {Set<string>} */
450+
this._analyzableRepairedAssets = new Set();
451451
/** @type {WeakMap<Chunk, boolean>} */
452452
this._chunkNamedWithoutContent = new WeakMap();
453453
/** @type {WeakMap<Chunk, string>} */
@@ -625,7 +625,17 @@ class RuntimeTemplate {
625625
* @returns {boolean} true once the fill has marked such a chunk
626626
*/
627627
needsAnalyzableRepair() {
628-
return this._analyzableRepairNeeded;
628+
return this._analyzableRepairedAssets.size > 0;
629+
}
630+
631+
/**
632+
* The assets the deferred fill rewrote under a name that has to be repaired. Only
633+
* these, and whatever names them, are renamed by an on-demand repair — the rest
634+
* keep the names they were given.
635+
* @returns {ReadonlySet<string>} their names
636+
*/
637+
analyzableRepairedAssets() {
638+
return this._analyzableRepairedAssets;
629639
}
630640

631641
/**
@@ -4000,9 +4010,10 @@ class RuntimeTemplate {
40004010
async () => {
40014011
const { outputOptions } = compilation;
40024012
const fullHash = compilation.hash;
4003-
// Whether any stand-in landed in a chunk whose hashed name has to be
4004-
// brought back in line, which `RealContentHashPlugin` then does.
4005-
let repairNeeded = false;
4013+
// The assets a stand-in landed in whose hashed name has to be brought
4014+
// back in line, which `RealContentHashPlugin` then does.
4015+
/** @type {Set<string>} */
4016+
const repairedAssets = new Set();
40064017
/** @type {Map<string, Chunk> | undefined} */
40074018
let chunksById;
40084019
/** @type {Map<string, Chunk> | undefined} */
@@ -4058,7 +4069,7 @@ class RuntimeTemplate {
40584069
let specifier = "";
40594070
for (const [kind, value] of parts) {
40604071
if (kind === "repair") {
4061-
repairNeeded = true;
4072+
repairedAssets.add(assetName);
40624073
} else if (kind === "base") {
40634074
base = /** @type {string} */ (value);
40644075
} else if (kind === "literal") {
@@ -4183,7 +4194,8 @@ class RuntimeTemplate {
41834194

41844195
tasks.push({ name, source, replacements });
41854196
}
4186-
compilation.runtimeTemplate._analyzableRepairNeeded = repairNeeded;
4197+
compilation.runtimeTemplate._analyzableRepairedAssets =
4198+
repairedAssets;
41874199
if (tasks.length === 0) return;
41884200

41894201
await Promise.all(

‎lib/optimize/RealContentHashPlugin.js‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ class RealContentHashPlugin {
230230
// stale; only then is `[chunkhash]` repaired rather than left as named.
231231
const repairing = compilation.runtimeTemplate.needsAnalyzableRepair();
232232
if (this._onDemand && !repairing) return;
233+
// On demand, only a name the fill left stale moves, and whatever names it.
234+
const repairedAssets = this._onDemand
235+
? compilation.runtimeTemplate.analyzableRepairedAssets()
236+
: undefined;
233237
const assets = compilation.getAssets();
234238
/** @type {AssetInfoForRealContentHash[]} */
235239
const assetsWithInfo = [];
@@ -503,7 +507,44 @@ ${referencingAssets
503507
};
504508
/** @type {Comparator<AssetInfoForRealContentHash>} */
505509
const comparator = compareSelect((a) => a.name, compareStrings);
510+
/**
511+
* Whether a group's names went stale: the fill rewrote an asset of theirs,
512+
* or one names a hash that already moved. Groups arrive dependencies first.
513+
* @param {string[]} group the hashes assigned together
514+
* @returns {boolean} true when they have to be recomputed
515+
*/
516+
const staleOnDemand = (group) => {
517+
const own = new Set(group);
518+
for (const hash of group) {
519+
for (const asset of /** @type {AssetInfoForRealContentHash[]} */ (
520+
hashToAssets.get(hash)
521+
)) {
522+
if (
523+
/** @type {ReadonlySet<string>} */ (repairedAssets).has(
524+
asset.name
525+
)
526+
) {
527+
return true;
528+
}
529+
for (const referenced of /** @type {Hashes} */ (
530+
asset.referencedHashes
531+
)) {
532+
if (
533+
!own.has(referenced) &&
534+
hashToNewHash.get(referenced) !== referenced
535+
) {
536+
return true;
537+
}
538+
}
539+
}
540+
}
541+
return false;
542+
};
506543
for (const group of groupsInOrder) {
544+
if (repairedAssets !== undefined && !staleOnDemand(group)) {
545+
for (const hash of group) hashToNewHash.set(hash, hash);
546+
continue;
547+
}
507548
// A cycle is assigned as one group: members are told apart by stable
508549
// markers, so the group hashes to a fixed point instead of chasing one.
509550
if (group.length > 1) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 3;

‎test/configCases/analyzable/circular-chunk-hashes-repaired/index.js‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ import fs from "fs";
22
import path from "path";
33

44
it("should build rather than deadlock on two chunks that name each other", async () => {
5-
const [a, b] = await Promise.all([import("./a.js"), import("./b.js")]);
5+
const [a, b, c] = await Promise.all([
6+
import("./a.js"),
7+
import("./b.js"),
8+
import("./c.js")
9+
]);
610
expect(a.default).toBe(1);
711
expect(b.default).toBe(2);
12+
expect(c.default).toBe(3);
813
});
914

1015
it("should bake both directions of the cycle under repaired names", () => {
@@ -23,3 +28,21 @@ it("should bake both directions of the cycle under repaired names", () => {
2328
expect(read("b_js")).toContain(`${helper}(`);
2429
expect(read("b_js")).toContain(`"./${emitted("a_js")}"`);
2530
});
31+
32+
it("should rename only what the repair touched", () => {
33+
const dir = __STATS__.outputPath;
34+
const before = JSON.parse(
35+
Buffer.from(
36+
fs.readFileSync(path.join(dir, "names-before-repair.txt"), "utf8"),
37+
"base64"
38+
).toString()
39+
);
40+
const after = fs.readdirSync(dir);
41+
const find = (list, prefix) => list.find((n) => n.startsWith(prefix));
42+
43+
// The pair's names went stale with the fill, so both moved.
44+
expect(find(after, "a_js")).not.toBe(find(before, "a_js"));
45+
expect(find(after, "b_js")).not.toBe(find(before, "b_js"));
46+
// Nothing the fill touched reaches this one, so it keeps the name it was given.
47+
expect(find(after, "c_js")).toBe(find(before, "c_js"));
48+
});

‎test/configCases/analyzable/circular-chunk-hashes-repaired/webpack.config.js‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
"use strict";
22

3+
const { RawSource } = require("webpack-sources");
4+
const { Compilation } = require("../../../../");
5+
36
// Two chunks naming each other, with nothing asked to repair their hashes: the fill
47
// marks both, and the repair runs for them alone, so both directions bake.
58

@@ -15,5 +18,29 @@ module.exports = {
1518
chunkFilename: "[name].[contenthash].mjs",
1619
publicPath: "auto"
1720
},
18-
optimization: { realContentHash: false, chunkIds: "named" }
21+
optimization: { realContentHash: false, chunkIds: "named" },
22+
plugins: [
23+
// Writes the names out as they were before the repair ran, for the test to
24+
// compare — encoded, or the repair would rewrite the hashes in them too.
25+
(compiler) => {
26+
compiler.hooks.compilation.tap("NamesBeforeRepair", (compilation) => {
27+
compilation.hooks.processAssets.tap(
28+
{
29+
name: "NamesBeforeRepair",
30+
stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_HASH - 1
31+
},
32+
(assets) => {
33+
compilation.emitAsset(
34+
"names-before-repair.txt",
35+
new RawSource(
36+
Buffer.from(JSON.stringify(Object.keys(assets))).toString(
37+
"base64"
38+
)
39+
)
40+
);
41+
}
42+
);
43+
});
44+
}
45+
]
1946
};

‎test/watchCases/chunks/esm-async-chunks-hmr/0/index.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ it("should work where an ESM entryChunk depends on the runtimeChunk", async func
3131
expect(mainChunk.hash).not.toBe(STATE.mainChunkHash);
3232
// async dynamic1Chunk need to be updated
3333
expect(dynamic1Chunk.hash).not.toBe(STATE.dynamic1ChunkHash);
34-
// runtime runtimeChunk need to be updated
35-
expect(runtimeChunk.hash).not.toBe(STATE.runtimeChunkHash);
34+
// The entry imports each async chunk by name itself, so nothing in the runtime
35+
// chunk names one and it needn't be updated.
36+
expect(runtimeChunk.hash).toBe(STATE.runtimeChunkHash);
3637
}
3738
done()
3839
});

‎types.d.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25519,6 +25519,13 @@ declare abstract class RuntimeTemplate {
2551925519
*/
2552025520
needsAnalyzableRepair(): boolean;
2552125521

25522+
/**
25523+
* The assets the deferred fill rewrote under a name that has to be repaired. Only
25524+
* these, and whatever names them, are renamed by an on-demand repair — the rest
25525+
* keep the names they were given.
25526+
*/
25527+
analyzableRepairedAssets(): ReadonlySet<string>;
25528+
2552225529
/**
2552325530
* Whether a reference a foreign bundler can follow without running webpack's runtime
2552425531
* may be emitted — the one question every caller asks, in the form it is asking for:

0 commit comments

Comments
 (0)