|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +/** @type {import("../../../../").Configuration} */ |
| 4 | +module.exports = { |
| 5 | + mode: "production", |
| 6 | + plugins: [ |
| 7 | + { |
| 8 | + apply: (compiler) => { |
| 9 | + compiler.hooks.compilation.tap("test", (compilation) => { |
| 10 | + compilation.hooks.optimize.tap("test", () => { |
| 11 | + const { chunkGraph } = compilation; |
| 12 | + const [chunk] = compilation.chunks; |
| 13 | + const modules = chunkGraph.getChunkModules(chunk); |
| 14 | + |
| 15 | + if (modules.length === 0) { |
| 16 | + throw new Error("No modules found in chunk"); |
| 17 | + } |
| 18 | + |
| 19 | + const m = modules[0]; |
| 20 | + const mock = { |
| 21 | + identifier: () => "mock-module", |
| 22 | + getSourceTypes: () => ["javascript"], |
| 23 | + size: () => 0 |
| 24 | + }; |
| 25 | + |
| 26 | + // @ts-expect-error for tests |
| 27 | + chunkGraph.replaceModule(m, mock); |
| 28 | + |
| 29 | + // @ts-expect-error for tests |
| 30 | + if (!chunkGraph.isModuleInChunk(mock, chunk)) { |
| 31 | + throw new Error("replaceModule failed (new module missing)"); |
| 32 | + } |
| 33 | + if (chunkGraph.isModuleInChunk(m, chunk)) { |
| 34 | + throw new Error("replaceModule failed (old module remains)"); |
| 35 | + } |
| 36 | + |
| 37 | + // Restore original state for downstream stats |
| 38 | + // @ts-expect-error for tests |
| 39 | + chunkGraph.replaceModule(mock, m); |
| 40 | + |
| 41 | + chunkGraph.disconnectChunkAndModule(chunk, m); |
| 42 | + if (chunkGraph.isModuleInChunk(m, chunk)) { |
| 43 | + throw new Error("disconnectChunkAndModule failed"); |
| 44 | + } |
| 45 | + |
| 46 | + chunkGraph.connectChunkAndModule(chunk, m); |
| 47 | + if (!chunkGraph.isModuleInChunk(m, chunk)) { |
| 48 | + throw new Error("connectChunkAndModule failed"); |
| 49 | + } |
| 50 | + |
| 51 | + const [group] = compilation.chunkGroups; |
| 52 | + if (group) { |
| 53 | + group.getChildren(); |
| 54 | + } |
| 55 | + }); |
| 56 | + }); |
| 57 | + } |
| 58 | + } |
| 59 | + ] |
| 60 | +}; |
0 commit comments