* Applies the plugin by registering its hooks on the compiler. * @param {Compiler} compiler the compiler instance * @returns {void}
(compiler)
| 42 | * @returns {void} |
| 43 | */ |
| 44 | apply(compiler) { |
| 45 | const options = this.options; |
| 46 | const minSizeReduce = options.minSizeReduce || 1.5; |
| 47 | |
| 48 | compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => { |
| 49 | compilation.hooks.optimizeChunks.tap( |
| 50 | { |
| 51 | name: PLUGIN_NAME, |
| 52 | stage: STAGE_ADVANCED |
| 53 | }, |
| 54 | (chunks) => { |
| 55 | const chunkGraph = compilation.chunkGraph; |
| 56 | /** @type {{ a: Chunk, b: Chunk, improvement: number }[]} */ |
| 57 | const combinations = []; |
| 58 | for (const a of chunks) { |
| 59 | if (a.canBeInitial()) continue; |
| 60 | for (const b of chunks) { |
| 61 | if (b.canBeInitial()) continue; |
| 62 | if (b === a) break; |
| 63 | if (!chunkGraph.canChunksBeIntegrated(a, b)) { |
| 64 | continue; |
| 65 | } |
| 66 | const aSize = chunkGraph.getChunkSize(b, { |
| 67 | chunkOverhead: 0 |
| 68 | }); |
| 69 | const bSize = chunkGraph.getChunkSize(a, { |
| 70 | chunkOverhead: 0 |
| 71 | }); |
| 72 | const abSize = chunkGraph.getIntegratedChunksSize(b, a, { |
| 73 | chunkOverhead: 0 |
| 74 | }); |
| 75 | const improvement = (aSize + bSize) / abSize; |
| 76 | combinations.push({ |
| 77 | a, |
| 78 | b, |
| 79 | improvement |
| 80 | }); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | combinations.sort((a, b) => b.improvement - a.improvement); |
| 85 | |
| 86 | const pair = combinations[0]; |
| 87 | |
| 88 | if (!pair) return; |
| 89 | if (pair.improvement < minSizeReduce) return; |
| 90 | |
| 91 | chunkGraph.integrateChunks(pair.b, pair.a); |
| 92 | compilation.chunks.delete(pair.a); |
| 93 | return true; |
| 94 | } |
| 95 | ); |
| 96 | }); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | module.exports = AggressiveMergingPlugin; |
nothing calls this directly
no test coverage detected