[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_ADVANCED } = require("../OptimizationStages");
|
---|
| 9 |
|
---|
| 10 | /** @typedef {import("../Chunk")} Chunk */
|
---|
| 11 | /** @typedef {import("../Compiler")} Compiler */
|
---|
| 12 |
|
---|
| 13 | /**
|
---|
| 14 | * @typedef {object} AggressiveMergingPluginOptions
|
---|
| 15 | * @property {number=} minSizeReduce minimal size reduction to trigger merging
|
---|
| 16 | */
|
---|
| 17 |
|
---|
| 18 | class AggressiveMergingPlugin {
|
---|
| 19 | /**
|
---|
| 20 | * @param {AggressiveMergingPluginOptions=} [options] options object
|
---|
| 21 | */
|
---|
| 22 | constructor(options) {
|
---|
| 23 | if (
|
---|
| 24 | (options !== undefined && typeof options !== "object") ||
|
---|
| 25 | Array.isArray(options)
|
---|
| 26 | ) {
|
---|
| 27 | throw new Error(
|
---|
| 28 | "Argument should be an options object. To use defaults, pass in nothing.\nFor more info on options, see https://webpack.js.org/plugins/"
|
---|
| 29 | );
|
---|
| 30 | }
|
---|
| 31 | this.options = options || {};
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | /**
|
---|
| 35 | * Apply the plugin
|
---|
| 36 | * @param {Compiler} compiler the compiler instance
|
---|
| 37 | * @returns {void}
|
---|
| 38 | */
|
---|
| 39 | apply(compiler) {
|
---|
| 40 | const options = this.options;
|
---|
| 41 | const minSizeReduce = options.minSizeReduce || 1.5;
|
---|
| 42 |
|
---|
| 43 | compiler.hooks.thisCompilation.tap(
|
---|
| 44 | "AggressiveMergingPlugin",
|
---|
| 45 | compilation => {
|
---|
| 46 | compilation.hooks.optimizeChunks.tap(
|
---|
| 47 | {
|
---|
| 48 | name: "AggressiveMergingPlugin",
|
---|
| 49 | stage: STAGE_ADVANCED
|
---|
| 50 | },
|
---|
| 51 | chunks => {
|
---|
| 52 | const chunkGraph = compilation.chunkGraph;
|
---|
| 53 | /** @type {{a: Chunk, b: Chunk, improvement: number}[]} */
|
---|
| 54 | const combinations = [];
|
---|
| 55 | for (const a of chunks) {
|
---|
| 56 | if (a.canBeInitial()) continue;
|
---|
| 57 | for (const b of chunks) {
|
---|
| 58 | if (b.canBeInitial()) continue;
|
---|
| 59 | if (b === a) break;
|
---|
| 60 | if (!chunkGraph.canChunksBeIntegrated(a, b)) {
|
---|
| 61 | continue;
|
---|
| 62 | }
|
---|
| 63 | const aSize = chunkGraph.getChunkSize(b, {
|
---|
| 64 | chunkOverhead: 0
|
---|
| 65 | });
|
---|
| 66 | const bSize = chunkGraph.getChunkSize(a, {
|
---|
| 67 | chunkOverhead: 0
|
---|
| 68 | });
|
---|
| 69 | const abSize = chunkGraph.getIntegratedChunksSize(b, a, {
|
---|
| 70 | chunkOverhead: 0
|
---|
| 71 | });
|
---|
| 72 | const improvement = (aSize + bSize) / abSize;
|
---|
| 73 | combinations.push({
|
---|
| 74 | a,
|
---|
| 75 | b,
|
---|
| 76 | improvement
|
---|
| 77 | });
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | combinations.sort((a, b) => b.improvement - a.improvement);
|
---|
| 82 |
|
---|
| 83 | const pair = combinations[0];
|
---|
| 84 |
|
---|
| 85 | if (!pair) return;
|
---|
| 86 | if (pair.improvement < minSizeReduce) return;
|
---|
| 87 |
|
---|
| 88 | chunkGraph.integrateChunks(pair.b, pair.a);
|
---|
| 89 | compilation.chunks.delete(pair.a);
|
---|
| 90 | return true;
|
---|
| 91 | }
|
---|
| 92 | );
|
---|
| 93 | }
|
---|
| 94 | );
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | module.exports = AggressiveMergingPlugin;
|
---|