| [9af201e] | 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 | const PLUGIN_NAME = "EnsureChunkConditionsPlugin";
|
|---|
| 15 |
|
|---|
| 16 | class EnsureChunkConditionsPlugin {
|
|---|
| 17 | /**
|
|---|
| 18 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 19 | * @param {Compiler} compiler the compiler instance
|
|---|
| 20 | * @returns {void}
|
|---|
| 21 | */
|
|---|
| 22 | apply(compiler) {
|
|---|
| 23 | compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
|
|---|
| 24 | /**
|
|---|
| 25 | * Handles the hook callback for this code path.
|
|---|
| 26 | * @param {Iterable<Chunk>} chunks the chunks
|
|---|
| 27 | */
|
|---|
| 28 | const handler = (chunks) => {
|
|---|
| 29 | const chunkGraph = compilation.chunkGraph;
|
|---|
| 30 | // These sets are hoisted here to save memory
|
|---|
| 31 | // They are cleared at the end of every loop
|
|---|
| 32 | /** @type {Set<Chunk>} */
|
|---|
| 33 | const sourceChunks = new Set();
|
|---|
| 34 | /** @type {Set<ChunkGroup>} */
|
|---|
| 35 | const chunkGroups = new Set();
|
|---|
| 36 | for (const module of compilation.modules) {
|
|---|
| 37 | if (!module.hasChunkCondition()) continue;
|
|---|
| 38 | for (const chunk of chunkGraph.getModuleChunksIterable(module)) {
|
|---|
| 39 | if (!module.chunkCondition(chunk, compilation)) {
|
|---|
| 40 | sourceChunks.add(chunk);
|
|---|
| 41 | for (const group of chunk.groupsIterable) {
|
|---|
| 42 | chunkGroups.add(group);
|
|---|
| 43 | }
|
|---|
| 44 | }
|
|---|
| 45 | }
|
|---|
| 46 | if (sourceChunks.size === 0) continue;
|
|---|
| 47 | /** @type {Set<Chunk>} */
|
|---|
| 48 | const targetChunks = new Set();
|
|---|
| 49 | chunkGroupLoop: for (const chunkGroup of chunkGroups) {
|
|---|
| 50 | // Can module be placed in a chunk of this group?
|
|---|
| 51 | for (const chunk of chunkGroup.chunks) {
|
|---|
| 52 | if (module.chunkCondition(chunk, compilation)) {
|
|---|
| 53 | targetChunks.add(chunk);
|
|---|
| 54 | continue chunkGroupLoop;
|
|---|
| 55 | }
|
|---|
| 56 | }
|
|---|
| 57 | // We reached the entrypoint: fail
|
|---|
| 58 | if (chunkGroup.isInitial()) {
|
|---|
| 59 | throw new Error(
|
|---|
| 60 | `Cannot fulfil chunk condition of ${module.identifier()}`
|
|---|
| 61 | );
|
|---|
| 62 | }
|
|---|
| 63 | // Try placing in all parents
|
|---|
| 64 | for (const group of chunkGroup.parentsIterable) {
|
|---|
| 65 | chunkGroups.add(group);
|
|---|
| 66 | }
|
|---|
| 67 | }
|
|---|
| 68 | for (const sourceChunk of sourceChunks) {
|
|---|
| 69 | chunkGraph.disconnectChunkAndModule(sourceChunk, module);
|
|---|
| 70 | }
|
|---|
| 71 | for (const targetChunk of targetChunks) {
|
|---|
| 72 | chunkGraph.connectChunkAndModule(targetChunk, module);
|
|---|
| 73 | }
|
|---|
| 74 | sourceChunks.clear();
|
|---|
| 75 | chunkGroups.clear();
|
|---|
| 76 | }
|
|---|
| 77 | };
|
|---|
| 78 | compilation.hooks.optimizeChunks.tap(
|
|---|
| 79 | {
|
|---|
| 80 | name: PLUGIN_NAME,
|
|---|
| 81 | stage: STAGE_BASIC
|
|---|
| 82 | },
|
|---|
| 83 | handler
|
|---|
| 84 | );
|
|---|
| 85 | });
|
|---|
| 86 | }
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | module.exports = EnsureChunkConditionsPlugin;
|
|---|