Skip to content

Commit 1068a0b

Browse files
fix: walk each visited chunk group and honor ignoreChunk in Module.isAccessibleInChunk
Claude-Session: https://claude.ai/code/session_01FuxKwZepsFsatQwWC2XFQy
1 parent 26d7332 commit 1068a0b

3 files changed

Lines changed: 93 additions & 3 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+
Keep a module its chunk still needs after `compilation.rebuildModule()`.

‎lib/module/Module.js‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,9 @@ class Module extends DependenciesBlock {
905905
isAccessibleInChunk(chunkGraph, chunk, ignoreChunk) {
906906
// Check if module is accessible in ALL chunk groups
907907
for (const chunkGroup of chunk.groupsIterable) {
908-
if (!this.isAccessibleInChunkGroup(chunkGraph, chunkGroup)) return false;
908+
if (!this.isAccessibleInChunkGroup(chunkGraph, chunkGroup, ignoreChunk)) {
909+
return false;
910+
}
909911
}
910912
return true;
911913
}
@@ -930,9 +932,9 @@ class Module extends DependenciesBlock {
930932
}
931933
}
932934
// 2. If the chunk group is initial, we can break here because it's not accessible.
933-
if (chunkGroup.isInitial()) return false;
935+
if (cg.isInitial()) return false;
934936
// 3. Enqueue all parents because it must be accessible from ALL parents
935-
for (const parent of chunkGroup.parentsIterable) queue.add(parent);
937+
for (const parent of cg.parentsIterable) queue.add(parent);
936938
}
937939
// When we processed through the whole list and we didn't bailout, the module is accessible
938940
return true;
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"use strict";
2+
3+
const Chunk = require("../../lib/graph/Chunk");
4+
const ChunkGraph = require("../../lib/graph/ChunkGraph");
5+
const ChunkGroup = require("../../lib/graph/ChunkGroup");
6+
const Entrypoint = require("../../lib/graph/Entrypoint");
7+
const ModuleGraph = require("../../lib/graph/ModuleGraph");
8+
const RawModule = require("../../lib/module/RawModule");
9+
10+
// `Module.hasReasonForChunk` reaches these only from `Compilation.rebuildModule`
11+
// after seal, which no build case drives with an existing chunk graph.
12+
13+
/**
14+
* @template {ChunkGroup} T
15+
* @param {T} group chunk group
16+
* @param {string} name chunk name
17+
* @returns {Chunk} the chunk now in the group
18+
*/
19+
const addChunk = (group, name) => {
20+
const chunk = new Chunk(name, false);
21+
group.pushChunk(chunk);
22+
chunk.addGroup(group);
23+
return chunk;
24+
};
25+
26+
/**
27+
* @param {ChunkGroup} parent parent group
28+
* @param {ChunkGroup} child child group
29+
* @returns {void}
30+
*/
31+
const connect = (parent, child) => {
32+
parent.addChild(child);
33+
child.addParent(parent);
34+
};
35+
36+
/**
37+
* Builds the chain entry → async → nested async with one chunk per group.
38+
* @returns {{ entryChunk: Chunk, asyncChunk: Chunk, nestedChunk: Chunk, nested: ChunkGroup }} the chain
39+
*/
40+
const createChain = () => {
41+
const entry = new Entrypoint("main");
42+
const entryChunk = addChunk(entry, "main");
43+
entry.setEntrypointChunk(entryChunk);
44+
const asyncGroup = new ChunkGroup("async");
45+
const asyncChunk = addChunk(asyncGroup, "async");
46+
const nested = new ChunkGroup("nested");
47+
const nestedChunk = addChunk(nested, "nested");
48+
connect(entry, asyncGroup);
49+
connect(asyncGroup, nested);
50+
return { entryChunk, asyncChunk, nestedChunk, nested };
51+
};
52+
53+
describe("Module.isAccessibleInChunk", () => {
54+
it("is accessible when an ancestor group's chunk holds the module", () => {
55+
const { entryChunk, nested } = createChain();
56+
const chunkGraph = new ChunkGraph(new ModuleGraph());
57+
const module = new RawModule("", "m", "m");
58+
chunkGraph.connectChunkAndModule(entryChunk, module);
59+
60+
expect(module.isAccessibleInChunkGroup(chunkGraph, nested)).toBe(true);
61+
});
62+
63+
it("walks up to the initial group when no ancestor holds the module", () => {
64+
const { asyncChunk, nested } = createChain();
65+
const chunkGraph = new ChunkGraph(new ModuleGraph());
66+
const module = new RawModule("", "m", "m");
67+
chunkGraph.connectChunkAndModule(asyncChunk, new RawModule("", "o", "o"));
68+
69+
expect(module.isAccessibleInChunkGroup(chunkGraph, nested)).toBe(false);
70+
});
71+
72+
it("ignores the given chunk", () => {
73+
const { asyncChunk, nestedChunk } = createChain();
74+
const chunkGraph = new ChunkGraph(new ModuleGraph());
75+
const module = new RawModule("", "m", "m");
76+
chunkGraph.connectChunkAndModule(asyncChunk, module);
77+
78+
expect(module.isAccessibleInChunk(chunkGraph, nestedChunk)).toBe(true);
79+
expect(module.isAccessibleInChunk(chunkGraph, nestedChunk, asyncChunk)).toBe(
80+
false
81+
);
82+
});
83+
});

0 commit comments

Comments
 (0)