|
| 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