1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Sean Larkin @thelarkinn
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const { find } = require("../util/SetHelpers");
|
---|
9 | const AssetsOverSizeLimitWarning = require("./AssetsOverSizeLimitWarning");
|
---|
10 | const EntrypointsOverSizeLimitWarning = require("./EntrypointsOverSizeLimitWarning");
|
---|
11 | const NoAsyncChunksWarning = require("./NoAsyncChunksWarning");
|
---|
12 |
|
---|
13 | /** @typedef {import("webpack-sources").Source} Source */
|
---|
14 | /** @typedef {import("../../declarations/WebpackOptions").PerformanceOptions} PerformanceOptions */
|
---|
15 | /** @typedef {import("../ChunkGroup")} ChunkGroup */
|
---|
16 | /** @typedef {import("../Compilation").Asset} Asset */
|
---|
17 | /** @typedef {import("../Compiler")} Compiler */
|
---|
18 | /** @typedef {import("../Entrypoint")} Entrypoint */
|
---|
19 | /** @typedef {import("../WebpackError")} WebpackError */
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * @typedef {object} AssetDetails
|
---|
23 | * @property {string} name
|
---|
24 | * @property {number} size
|
---|
25 | */
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * @typedef {object} EntrypointDetails
|
---|
29 | * @property {string} name
|
---|
30 | * @property {number} size
|
---|
31 | * @property {string[]} files
|
---|
32 | */
|
---|
33 |
|
---|
34 | const isOverSizeLimitSet = new WeakSet();
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * @param {Asset["name"]} name the name
|
---|
38 | * @param {Asset["source"]} source the source
|
---|
39 | * @param {Asset["info"]} info the info
|
---|
40 | * @returns {boolean} result
|
---|
41 | */
|
---|
42 | const excludeSourceMap = (name, source, info) => !info.development;
|
---|
43 |
|
---|
44 | module.exports = class SizeLimitsPlugin {
|
---|
45 | /**
|
---|
46 | * @param {PerformanceOptions} options the plugin options
|
---|
47 | */
|
---|
48 | constructor(options) {
|
---|
49 | this.hints = options.hints;
|
---|
50 | this.maxAssetSize = options.maxAssetSize;
|
---|
51 | this.maxEntrypointSize = options.maxEntrypointSize;
|
---|
52 | this.assetFilter = options.assetFilter;
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * @param {ChunkGroup | Source} thing the resource to test
|
---|
57 | * @returns {boolean} true if over the limit
|
---|
58 | */
|
---|
59 | static isOverSizeLimit(thing) {
|
---|
60 | return isOverSizeLimitSet.has(thing);
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Apply the plugin
|
---|
65 | * @param {Compiler} compiler the compiler instance
|
---|
66 | * @returns {void}
|
---|
67 | */
|
---|
68 | apply(compiler) {
|
---|
69 | const entrypointSizeLimit = this.maxEntrypointSize;
|
---|
70 | const assetSizeLimit = this.maxAssetSize;
|
---|
71 | const hints = this.hints;
|
---|
72 | const assetFilter = this.assetFilter || excludeSourceMap;
|
---|
73 |
|
---|
74 | compiler.hooks.afterEmit.tap("SizeLimitsPlugin", compilation => {
|
---|
75 | /** @type {WebpackError[]} */
|
---|
76 | const warnings = [];
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * @param {Entrypoint} entrypoint an entrypoint
|
---|
80 | * @returns {number} the size of the entrypoint
|
---|
81 | */
|
---|
82 | const getEntrypointSize = entrypoint => {
|
---|
83 | let size = 0;
|
---|
84 | for (const file of entrypoint.getFiles()) {
|
---|
85 | const asset = compilation.getAsset(file);
|
---|
86 | if (
|
---|
87 | asset &&
|
---|
88 | assetFilter(asset.name, asset.source, asset.info) &&
|
---|
89 | asset.source
|
---|
90 | ) {
|
---|
91 | size += asset.info.size || asset.source.size();
|
---|
92 | }
|
---|
93 | }
|
---|
94 | return size;
|
---|
95 | };
|
---|
96 |
|
---|
97 | /** @type {AssetDetails[]} */
|
---|
98 | const assetsOverSizeLimit = [];
|
---|
99 | for (const { name, source, info } of compilation.getAssets()) {
|
---|
100 | if (!assetFilter(name, source, info) || !source) {
|
---|
101 | continue;
|
---|
102 | }
|
---|
103 |
|
---|
104 | const size = info.size || source.size();
|
---|
105 | if (size > /** @type {number} */ (assetSizeLimit)) {
|
---|
106 | assetsOverSizeLimit.push({
|
---|
107 | name,
|
---|
108 | size
|
---|
109 | });
|
---|
110 | isOverSizeLimitSet.add(source);
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * @param {Asset["name"]} name the name
|
---|
116 | * @returns {boolean | undefined} result
|
---|
117 | */
|
---|
118 | const fileFilter = name => {
|
---|
119 | const asset = compilation.getAsset(name);
|
---|
120 | return asset && assetFilter(asset.name, asset.source, asset.info);
|
---|
121 | };
|
---|
122 |
|
---|
123 | /** @type {EntrypointDetails[]} */
|
---|
124 | const entrypointsOverLimit = [];
|
---|
125 | for (const [name, entry] of compilation.entrypoints) {
|
---|
126 | const size = getEntrypointSize(entry);
|
---|
127 |
|
---|
128 | if (size > /** @type {number} */ (entrypointSizeLimit)) {
|
---|
129 | entrypointsOverLimit.push({
|
---|
130 | name,
|
---|
131 | size,
|
---|
132 | files: entry.getFiles().filter(fileFilter)
|
---|
133 | });
|
---|
134 | isOverSizeLimitSet.add(entry);
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | if (hints) {
|
---|
139 | // 1. Individual Chunk: Size < 250kb
|
---|
140 | // 2. Collective Initial Chunks [entrypoint] (Each Set?): Size < 250kb
|
---|
141 | // 3. No Async Chunks
|
---|
142 | // if !1, then 2, if !2 return
|
---|
143 | if (assetsOverSizeLimit.length > 0) {
|
---|
144 | warnings.push(
|
---|
145 | new AssetsOverSizeLimitWarning(
|
---|
146 | assetsOverSizeLimit,
|
---|
147 | /** @type {number} */ (assetSizeLimit)
|
---|
148 | )
|
---|
149 | );
|
---|
150 | }
|
---|
151 | if (entrypointsOverLimit.length > 0) {
|
---|
152 | warnings.push(
|
---|
153 | new EntrypointsOverSizeLimitWarning(
|
---|
154 | entrypointsOverLimit,
|
---|
155 | /** @type {number} */ (entrypointSizeLimit)
|
---|
156 | )
|
---|
157 | );
|
---|
158 | }
|
---|
159 |
|
---|
160 | if (warnings.length > 0) {
|
---|
161 | const someAsyncChunk = find(
|
---|
162 | compilation.chunks,
|
---|
163 | chunk => !chunk.canBeInitial()
|
---|
164 | );
|
---|
165 |
|
---|
166 | if (!someAsyncChunk) {
|
---|
167 | warnings.push(new NoAsyncChunksWarning());
|
---|
168 | }
|
---|
169 |
|
---|
170 | if (hints === "error") {
|
---|
171 | compilation.errors.push(...warnings);
|
---|
172 | } else {
|
---|
173 | compilation.warnings.push(...warnings);
|
---|
174 | }
|
---|
175 | }
|
---|
176 | }
|
---|
177 | });
|
---|
178 | }
|
---|
179 | };
|
---|