| 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("../../declarations/plugins/optimize/MinChunkSizePlugin").MinChunkSizePluginOptions} MinChunkSizePluginOptions */
|
|---|
| 11 | /** @typedef {import("../Chunk")} Chunk */
|
|---|
| 12 | /** @typedef {import("../Compiler")} Compiler */
|
|---|
| 13 |
|
|---|
| 14 | const PLUGIN_NAME = "MinChunkSizePlugin";
|
|---|
| 15 |
|
|---|
| 16 | class MinChunkSizePlugin {
|
|---|
| 17 | /**
|
|---|
| 18 | * Creates an instance of MinChunkSizePlugin.
|
|---|
| 19 | * @param {MinChunkSizePluginOptions} options options object
|
|---|
| 20 | */
|
|---|
| 21 | constructor(options) {
|
|---|
| 22 | /** @type {MinChunkSizePluginOptions} */
|
|---|
| 23 | this.options = options;
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | /**
|
|---|
| 27 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 28 | * @param {Compiler} compiler the compiler instance
|
|---|
| 29 | * @returns {void}
|
|---|
| 30 | */
|
|---|
| 31 | apply(compiler) {
|
|---|
| 32 | compiler.hooks.validate.tap(PLUGIN_NAME, () => {
|
|---|
| 33 | compiler.validate(
|
|---|
| 34 | () => require("../../schemas/plugins/optimize/MinChunkSizePlugin.json"),
|
|---|
| 35 | this.options,
|
|---|
| 36 | {
|
|---|
| 37 | name: "Min Chunk Size Plugin",
|
|---|
| 38 | baseDataPath: "options"
|
|---|
| 39 | },
|
|---|
| 40 | (options) =>
|
|---|
| 41 | require("../../schemas/plugins/optimize/MinChunkSizePlugin.check")(
|
|---|
| 42 | options
|
|---|
| 43 | )
|
|---|
| 44 | );
|
|---|
| 45 | });
|
|---|
| 46 | compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
|
|---|
| 47 | compilation.hooks.optimizeChunks.tap(
|
|---|
| 48 | {
|
|---|
| 49 | name: PLUGIN_NAME,
|
|---|
| 50 | stage: STAGE_ADVANCED
|
|---|
| 51 | },
|
|---|
| 52 | (chunks) => {
|
|---|
| 53 | const chunkGraph = compilation.chunkGraph;
|
|---|
| 54 | const equalOptions = {
|
|---|
| 55 | chunkOverhead: 1,
|
|---|
| 56 | entryChunkMultiplicator: 1
|
|---|
| 57 | };
|
|---|
| 58 |
|
|---|
| 59 | /** @type {Map<Chunk, number>} */
|
|---|
| 60 | const chunkSizesMap = new Map();
|
|---|
| 61 | /** @type {[Chunk, Chunk][]} */
|
|---|
| 62 | const combinations = [];
|
|---|
| 63 | /** @type {Chunk[]} */
|
|---|
| 64 | const smallChunks = [];
|
|---|
| 65 | /** @type {Chunk[]} */
|
|---|
| 66 | const visitedChunks = [];
|
|---|
| 67 | for (const a of chunks) {
|
|---|
| 68 | // check if one of the chunks sizes is smaller than the minChunkSize
|
|---|
| 69 | // and filter pairs that can NOT be integrated!
|
|---|
| 70 | if (
|
|---|
| 71 | chunkGraph.getChunkSize(a, equalOptions) <
|
|---|
| 72 | this.options.minChunkSize
|
|---|
| 73 | ) {
|
|---|
| 74 | smallChunks.push(a);
|
|---|
| 75 | for (const b of visitedChunks) {
|
|---|
| 76 | if (chunkGraph.canChunksBeIntegrated(b, a)) {
|
|---|
| 77 | combinations.push([b, a]);
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 | } else {
|
|---|
| 81 | for (const b of smallChunks) {
|
|---|
| 82 | if (chunkGraph.canChunksBeIntegrated(b, a)) {
|
|---|
| 83 | combinations.push([b, a]);
|
|---|
| 84 | }
|
|---|
| 85 | }
|
|---|
| 86 | }
|
|---|
| 87 | chunkSizesMap.set(a, chunkGraph.getChunkSize(a, this.options));
|
|---|
| 88 | visitedChunks.push(a);
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | const sortedSizeFilteredExtendedPairCombinations = combinations
|
|---|
| 92 | .map((pair) => {
|
|---|
| 93 | // extend combination pairs with size and integrated size
|
|---|
| 94 | const a = /** @type {number} */ (chunkSizesMap.get(pair[0]));
|
|---|
| 95 | const b = /** @type {number} */ (chunkSizesMap.get(pair[1]));
|
|---|
| 96 | const ab = chunkGraph.getIntegratedChunksSize(
|
|---|
| 97 | pair[0],
|
|---|
| 98 | pair[1],
|
|---|
| 99 | this.options
|
|---|
| 100 | );
|
|---|
| 101 | /** @type {[number, number, Chunk, Chunk]} */
|
|---|
| 102 | const extendedPair = [a + b - ab, ab, pair[0], pair[1]];
|
|---|
| 103 | return extendedPair;
|
|---|
| 104 | })
|
|---|
| 105 | .sort((a, b) => {
|
|---|
| 106 | // sadly javascript does an in place sort here
|
|---|
| 107 | // sort by size
|
|---|
| 108 | const diff = b[0] - a[0];
|
|---|
| 109 | if (diff !== 0) return diff;
|
|---|
| 110 | return a[1] - b[1];
|
|---|
| 111 | });
|
|---|
| 112 |
|
|---|
| 113 | if (sortedSizeFilteredExtendedPairCombinations.length === 0) return;
|
|---|
| 114 |
|
|---|
| 115 | const pair = sortedSizeFilteredExtendedPairCombinations[0];
|
|---|
| 116 |
|
|---|
| 117 | chunkGraph.integrateChunks(pair[2], pair[3]);
|
|---|
| 118 | compilation.chunks.delete(pair[3]);
|
|---|
| 119 | return true;
|
|---|
| 120 | }
|
|---|
| 121 | );
|
|---|
| 122 | });
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | module.exports = MinChunkSizePlugin;
|
|---|