WXL
4 天以前 2cc85c64f1c64a2dbaeae276a3e2ca8420de76b7
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
    MIT License http://www.opensource.org/licenses/mit-license.php
    Author Sean Larkin @thelarkinn
*/
 
"use strict";
 
const { find } = require("../util/SetHelpers");
const AssetsOverSizeLimitWarning = require("./AssetsOverSizeLimitWarning");
const EntrypointsOverSizeLimitWarning = require("./EntrypointsOverSizeLimitWarning");
const NoAsyncChunksWarning = require("./NoAsyncChunksWarning");
 
/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("../../declarations/WebpackOptions").PerformanceOptions} PerformanceOptions */
/** @typedef {import("../ChunkGroup")} ChunkGroup */
/** @typedef {import("../Compilation").Asset} Asset */
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../Entrypoint")} Entrypoint */
/** @typedef {import("../WebpackError")} WebpackError */
 
/**
 * Defines the asset details type used by this module.
 * @typedef {object} AssetDetails
 * @property {string} name
 * @property {number} size
 */
 
/**
 * Defines the entrypoint details type used by this module.
 * @typedef {object} EntrypointDetails
 * @property {string} name
 * @property {number} size
 * @property {string[]} files
 */
 
/** @type {WeakSet<Entrypoint | ChunkGroup | Source>} */
const isOverSizeLimitSet = new WeakSet();
 
/** @typedef {(name: Asset["name"], source: Asset["source"], assetInfo: Asset["info"]) => boolean} AssetFilter */
 
/** @type {AssetFilter} */
const excludeSourceMap = (name, source, info) => !info.development;
 
const PLUGIN_NAME = "SizeLimitsPlugin";
 
module.exports = class SizeLimitsPlugin {
    /**
     * Creates an instance of SizeLimitsPlugin.
     * @param {PerformanceOptions} options the plugin options
     */
    constructor(options) {
        /** @type {PerformanceOptions["hints"]} */
        this.hints = options.hints;
        /** @type {number | undefined} */
        this.maxAssetSize = options.maxAssetSize;
        /** @type {number | undefined} */
        this.maxEntrypointSize = options.maxEntrypointSize;
        /** @type {AssetFilter | undefined} */
        this.assetFilter = options.assetFilter;
    }
 
    /**
     * Checks whether this size limits plugin is over size limit.
     * @param {Entrypoint | ChunkGroup | Source} thing the resource to test
     * @returns {boolean} true if over the limit
     */
    static isOverSizeLimit(thing) {
        return isOverSizeLimitSet.has(thing);
    }
 
    /**
     * Applies the plugin by registering its hooks on the compiler.
     * @param {Compiler} compiler the compiler instance
     * @returns {void}
     */
    apply(compiler) {
        const entrypointSizeLimit = this.maxEntrypointSize;
        const assetSizeLimit = this.maxAssetSize;
        const hints = this.hints;
        const assetFilter = this.assetFilter || excludeSourceMap;
 
        compiler.hooks.afterEmit.tap(PLUGIN_NAME, (compilation) => {
            /** @type {WebpackError[]} */
            const warnings = [];
 
            /**
             * Gets entrypoint size.
             * @param {Entrypoint} entrypoint an entrypoint
             * @returns {number} the size of the entrypoint
             */
            const getEntrypointSize = (entrypoint) => {
                let size = 0;
                for (const file of entrypoint.getFiles()) {
                    const asset = compilation.getAsset(file);
                    if (
                        asset &&
                        assetFilter(asset.name, asset.source, asset.info) &&
                        asset.source
                    ) {
                        size += asset.info.size || asset.source.size();
                    }
                }
                return size;
            };
 
            /** @type {AssetDetails[]} */
            const assetsOverSizeLimit = [];
            for (const { name, source, info } of compilation.getAssets()) {
                if (!assetFilter(name, source, info) || !source) {
                    continue;
                }
 
                const size = info.size || source.size();
                if (size > /** @type {number} */ (assetSizeLimit)) {
                    assetsOverSizeLimit.push({
                        name,
                        size
                    });
                    isOverSizeLimitSet.add(source);
                }
            }
 
            /**
             * Returns result.
             * @param {Asset["name"]} name the name
             * @returns {boolean | undefined} result
             */
            const fileFilter = (name) => {
                const asset = compilation.getAsset(name);
                return asset && assetFilter(asset.name, asset.source, asset.info);
            };
 
            /** @type {EntrypointDetails[]} */
            const entrypointsOverLimit = [];
            for (const [name, entry] of compilation.entrypoints) {
                const size = getEntrypointSize(entry);
 
                if (size > /** @type {number} */ (entrypointSizeLimit)) {
                    entrypointsOverLimit.push({
                        name,
                        size,
                        files: entry.getFiles().filter(fileFilter)
                    });
                    isOverSizeLimitSet.add(entry);
                }
            }
 
            if (hints) {
                // 1. Individual Chunk: Size < 250kb
                // 2. Collective Initial Chunks [entrypoint] (Each Set?): Size < 250kb
                // 3. No Async Chunks
                // if !1, then 2, if !2 return
                if (assetsOverSizeLimit.length > 0) {
                    warnings.push(
                        new AssetsOverSizeLimitWarning(
                            assetsOverSizeLimit,
                            /** @type {number} */ (assetSizeLimit)
                        )
                    );
                }
                if (entrypointsOverLimit.length > 0) {
                    warnings.push(
                        new EntrypointsOverSizeLimitWarning(
                            entrypointsOverLimit,
                            /** @type {number} */ (entrypointSizeLimit)
                        )
                    );
                }
 
                if (warnings.length > 0) {
                    const someAsyncChunk = find(
                        compilation.chunks,
                        (chunk) => !chunk.canBeInitial()
                    );
 
                    if (!someAsyncChunk) {
                        warnings.push(new NoAsyncChunksWarning());
                    }
 
                    if (hints === "error") {
                        compilation.errors.push(...warnings);
                    } else {
                        compilation.warnings.push(...warnings);
                    }
                }
            }
        });
    }
};