* Applies the plugin by registering its hooks on the compiler. * @param {Compiler} compiler the webpack compiler * @returns {void}
(compiler)
| 61 | * @returns {void} |
| 62 | */ |
| 63 | apply(compiler) { |
| 64 | compiler.hooks.validate.tap(PLUGIN_NAME, () => { |
| 65 | compiler.validate( |
| 66 | () => |
| 67 | require("../../schemas/plugins/optimize/LimitChunkCountPlugin.json"), |
| 68 | this.options, |
| 69 | { |
| 70 | name: "Limit Chunk Count Plugin", |
| 71 | baseDataPath: "options" |
| 72 | }, |
| 73 | (options) => |
| 74 | require("../../schemas/plugins/optimize/LimitChunkCountPlugin.check")( |
| 75 | options |
| 76 | ) |
| 77 | ); |
| 78 | }); |
| 79 | |
| 80 | compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => { |
| 81 | compilation.hooks.optimizeChunks.tap( |
| 82 | { |
| 83 | name: PLUGIN_NAME, |
| 84 | stage: STAGE_ADVANCED |
| 85 | }, |
| 86 | (chunks) => { |
| 87 | const chunkGraph = compilation.chunkGraph; |
| 88 | const maxChunks = this.options.maxChunks; |
| 89 | if (!maxChunks) return; |
| 90 | if (maxChunks < 1) return; |
| 91 | if (compilation.chunks.size <= maxChunks) return; |
| 92 | |
| 93 | let remainingChunksToMerge = compilation.chunks.size - maxChunks; |
| 94 | |
| 95 | // order chunks in a deterministic way |
| 96 | const compareChunksWithGraph = compareChunks(chunkGraph); |
| 97 | /** @type {Chunk[]} */ |
| 98 | const orderedChunks = [...chunks].sort(compareChunksWithGraph); |
| 99 | |
| 100 | // create a lazy sorted data structure to keep all combinations |
| 101 | // this is large. Size = chunks * (chunks - 1) / 2 |
| 102 | // It uses a multi layer bucket sort plus normal sort in the last layer |
| 103 | // It's also lazy so only accessed buckets are sorted |
| 104 | /** @type {LazyBucketSortedSet<ChunkCombination, number>} */ |
| 105 | const combinations = new LazyBucketSortedSet( |
| 106 | // Layer 1: ordered by largest size benefit |
| 107 | (c) => c.sizeDiff, |
| 108 | (a, b) => b - a, |
| 109 | |
| 110 | // Layer 2: ordered by smallest combined size |
| 111 | /** |
| 112 | * Handles the stage callback for this hook. |
| 113 | * @param {ChunkCombination} c combination |
| 114 | * @returns {number} integrated size |
| 115 | */ |
| 116 | (c) => c.integratedSize, |
| 117 | /** |
| 118 | * Handles the callback logic for this hook. |
| 119 | * @param {number} a a |
| 120 | * @param {number} b b |
nothing calls this directly
no test coverage detected