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