[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const { STAGE_BASIC } = require("../OptimizationStages");
|
---|
| 9 |
|
---|
| 10 | /** @typedef {import("../Chunk")} Chunk */
|
---|
| 11 | /** @typedef {import("../ChunkGroup")} ChunkGroup */
|
---|
| 12 | /** @typedef {import("../Compiler")} Compiler */
|
---|
| 13 |
|
---|
| 14 | class EnsureChunkConditionsPlugin {
|
---|
| 15 | /**
|
---|
| 16 | * Apply the plugin
|
---|
| 17 | * @param {Compiler} compiler the compiler instance
|
---|
| 18 | * @returns {void}
|
---|
| 19 | */
|
---|
| 20 | apply(compiler) {
|
---|
| 21 | compiler.hooks.compilation.tap(
|
---|
| 22 | "EnsureChunkConditionsPlugin",
|
---|
| 23 | compilation => {
|
---|
| 24 | /**
|
---|
| 25 | * @param {Iterable<Chunk>} chunks the chunks
|
---|
| 26 | */
|
---|
| 27 | const handler = chunks => {
|
---|
| 28 | const chunkGraph = compilation.chunkGraph;
|
---|
| 29 | // These sets are hoisted here to save memory
|
---|
| 30 | // They are cleared at the end of every loop
|
---|
| 31 | /** @type {Set<Chunk>} */
|
---|
| 32 | const sourceChunks = new Set();
|
---|
| 33 | /** @type {Set<ChunkGroup>} */
|
---|
| 34 | const chunkGroups = new Set();
|
---|
| 35 | for (const module of compilation.modules) {
|
---|
| 36 | if (!module.hasChunkCondition()) continue;
|
---|
| 37 | for (const chunk of chunkGraph.getModuleChunksIterable(module)) {
|
---|
| 38 | if (!module.chunkCondition(chunk, compilation)) {
|
---|
| 39 | sourceChunks.add(chunk);
|
---|
| 40 | for (const group of chunk.groupsIterable) {
|
---|
| 41 | chunkGroups.add(group);
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 | if (sourceChunks.size === 0) continue;
|
---|
| 46 | /** @type {Set<Chunk>} */
|
---|
| 47 | const targetChunks = new Set();
|
---|
| 48 | chunkGroupLoop: for (const chunkGroup of chunkGroups) {
|
---|
| 49 | // Can module be placed in a chunk of this group?
|
---|
| 50 | for (const chunk of chunkGroup.chunks) {
|
---|
| 51 | if (module.chunkCondition(chunk, compilation)) {
|
---|
| 52 | targetChunks.add(chunk);
|
---|
| 53 | continue chunkGroupLoop;
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 | // We reached the entrypoint: fail
|
---|
| 57 | if (chunkGroup.isInitial()) {
|
---|
| 58 | throw new Error(
|
---|
| 59 | `Cannot fulfil chunk condition of ${module.identifier()}`
|
---|
| 60 | );
|
---|
| 61 | }
|
---|
| 62 | // Try placing in all parents
|
---|
| 63 | for (const group of chunkGroup.parentsIterable) {
|
---|
| 64 | chunkGroups.add(group);
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 | for (const sourceChunk of sourceChunks) {
|
---|
| 68 | chunkGraph.disconnectChunkAndModule(sourceChunk, module);
|
---|
| 69 | }
|
---|
| 70 | for (const targetChunk of targetChunks) {
|
---|
| 71 | chunkGraph.connectChunkAndModule(targetChunk, module);
|
---|
| 72 | }
|
---|
| 73 | sourceChunks.clear();
|
---|
| 74 | chunkGroups.clear();
|
---|
| 75 | }
|
---|
| 76 | };
|
---|
| 77 | compilation.hooks.optimizeChunks.tap(
|
---|
| 78 | {
|
---|
| 79 | name: "EnsureChunkConditionsPlugin",
|
---|
| 80 | stage: STAGE_BASIC
|
---|
| 81 | },
|
---|
| 82 | handler
|
---|
| 83 | );
|
---|
| 84 | }
|
---|
| 85 | );
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 | module.exports = EnsureChunkConditionsPlugin;
|
---|