1 |
|
2 |
|
3 |
|
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 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | const isOverSizeLimitSet = new WeakSet();
|
34 |
|
35 | const excludeSourceMap = (name, source, info) => !info.development;
|
36 |
|
37 | module.exports = class SizeLimitsPlugin {
|
38 | |
39 |
|
40 |
|
41 | constructor(options) {
|
42 | this.hints = options.hints;
|
43 | this.maxAssetSize = options.maxAssetSize;
|
44 | this.maxEntrypointSize = options.maxEntrypointSize;
|
45 | this.assetFilter = options.assetFilter;
|
46 | }
|
47 |
|
48 | |
49 |
|
50 |
|
51 |
|
52 | static isOverSizeLimit(thing) {
|
53 | return isOverSizeLimitSet.has(thing);
|
54 | }
|
55 |
|
56 | |
57 |
|
58 |
|
59 |
|
60 |
|
61 | apply(compiler) {
|
62 | const entrypointSizeLimit = this.maxEntrypointSize;
|
63 | const assetSizeLimit = this.maxAssetSize;
|
64 | const hints = this.hints;
|
65 | const assetFilter = this.assetFilter || excludeSourceMap;
|
66 |
|
67 | compiler.hooks.afterEmit.tap("SizeLimitsPlugin", compilation => {
|
68 |
|
69 | const warnings = [];
|
70 |
|
71 | |
72 |
|
73 |
|
74 |
|
75 | const getEntrypointSize = entrypoint => {
|
76 | let size = 0;
|
77 | for (const file of entrypoint.getFiles()) {
|
78 | const asset = compilation.getAsset(file);
|
79 | if (
|
80 | asset &&
|
81 | assetFilter(asset.name, asset.source, asset.info) &&
|
82 | asset.source
|
83 | ) {
|
84 | size += asset.info.size || asset.source.size();
|
85 | }
|
86 | }
|
87 | return size;
|
88 | };
|
89 |
|
90 |
|
91 | const assetsOverSizeLimit = [];
|
92 | for (const { name, source, info } of compilation.getAssets()) {
|
93 | if (!assetFilter(name, source, info) || !source) {
|
94 | continue;
|
95 | }
|
96 |
|
97 | const size = info.size || source.size();
|
98 | if (size > assetSizeLimit) {
|
99 | assetsOverSizeLimit.push({
|
100 | name,
|
101 | size
|
102 | });
|
103 | isOverSizeLimitSet.add(source);
|
104 | }
|
105 | }
|
106 |
|
107 | const fileFilter = name => {
|
108 | const asset = compilation.getAsset(name);
|
109 | return asset && assetFilter(asset.name, asset.source, asset.info);
|
110 | };
|
111 |
|
112 |
|
113 | const entrypointsOverLimit = [];
|
114 | for (const [name, entry] of compilation.entrypoints) {
|
115 | const size = getEntrypointSize(entry);
|
116 |
|
117 | if (size > entrypointSizeLimit) {
|
118 | entrypointsOverLimit.push({
|
119 | name: name,
|
120 | size: size,
|
121 | files: entry.getFiles().filter(fileFilter)
|
122 | });
|
123 | isOverSizeLimitSet.add(entry);
|
124 | }
|
125 | }
|
126 |
|
127 | if (hints) {
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | if (assetsOverSizeLimit.length > 0) {
|
133 | warnings.push(
|
134 | new AssetsOverSizeLimitWarning(assetsOverSizeLimit, assetSizeLimit)
|
135 | );
|
136 | }
|
137 | if (entrypointsOverLimit.length > 0) {
|
138 | warnings.push(
|
139 | new EntrypointsOverSizeLimitWarning(
|
140 | entrypointsOverLimit,
|
141 | entrypointSizeLimit
|
142 | )
|
143 | );
|
144 | }
|
145 |
|
146 | if (warnings.length > 0) {
|
147 | const someAsyncChunk = find(
|
148 | compilation.chunks,
|
149 | chunk => !chunk.canBeInitial()
|
150 | );
|
151 |
|
152 | if (!someAsyncChunk) {
|
153 | warnings.push(new NoAsyncChunksWarning());
|
154 | }
|
155 |
|
156 | if (hints === "error") {
|
157 | compilation.errors.push(...warnings);
|
158 | } else {
|
159 | compilation.warnings.push(...warnings);
|
160 | }
|
161 | }
|
162 | }
|
163 | });
|
164 | }
|
165 | };
|