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 RequestShortener = require("../RequestShortener");
|
---|
9 |
|
---|
10 | /** @typedef {import("../../declarations/WebpackOptions").StatsOptions} StatsOptions */
|
---|
11 | /** @typedef {import("../Compilation")} Compilation */
|
---|
12 | /** @typedef {import("../Compilation").CreateStatsOptionsContext} CreateStatsOptionsContext */
|
---|
13 | /** @typedef {import("../Compiler")} Compiler */
|
---|
14 | /** @typedef {import("./DefaultStatsFactoryPlugin").StatsError} StatsError */
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * @param {StatsOptions} options options
|
---|
18 | * @param {StatsOptions} defaults default options
|
---|
19 | */
|
---|
20 | const applyDefaults = (options, defaults) => {
|
---|
21 | for (const _k of Object.keys(defaults)) {
|
---|
22 | const key = /** @type {keyof StatsOptions} */ (_k);
|
---|
23 | if (typeof options[key] === "undefined") {
|
---|
24 | /** @type {TODO} */
|
---|
25 | (options)[key] = defaults[key];
|
---|
26 | }
|
---|
27 | }
|
---|
28 | };
|
---|
29 |
|
---|
30 | /** @typedef {Record<string, StatsOptions>} NamedPresets */
|
---|
31 | /** @type {NamedPresets} */
|
---|
32 | const NAMED_PRESETS = {
|
---|
33 | verbose: {
|
---|
34 | hash: true,
|
---|
35 | builtAt: true,
|
---|
36 | relatedAssets: true,
|
---|
37 | entrypoints: true,
|
---|
38 | chunkGroups: true,
|
---|
39 | ids: true,
|
---|
40 | modules: false,
|
---|
41 | chunks: true,
|
---|
42 | chunkRelations: true,
|
---|
43 | chunkModules: true,
|
---|
44 | dependentModules: true,
|
---|
45 | chunkOrigins: true,
|
---|
46 | depth: true,
|
---|
47 | env: true,
|
---|
48 | reasons: true,
|
---|
49 | usedExports: true,
|
---|
50 | providedExports: true,
|
---|
51 | optimizationBailout: true,
|
---|
52 | errorDetails: true,
|
---|
53 | errorStack: true,
|
---|
54 | publicPath: true,
|
---|
55 | logging: "verbose",
|
---|
56 | orphanModules: true,
|
---|
57 | runtimeModules: true,
|
---|
58 | exclude: false,
|
---|
59 | errorsSpace: Infinity,
|
---|
60 | warningsSpace: Infinity,
|
---|
61 | modulesSpace: Infinity,
|
---|
62 | chunkModulesSpace: Infinity,
|
---|
63 | assetsSpace: Infinity,
|
---|
64 | reasonsSpace: Infinity,
|
---|
65 | children: true
|
---|
66 | },
|
---|
67 | detailed: {
|
---|
68 | hash: true,
|
---|
69 | builtAt: true,
|
---|
70 | relatedAssets: true,
|
---|
71 | entrypoints: true,
|
---|
72 | chunkGroups: true,
|
---|
73 | ids: true,
|
---|
74 | chunks: true,
|
---|
75 | chunkRelations: true,
|
---|
76 | chunkModules: false,
|
---|
77 | chunkOrigins: true,
|
---|
78 | depth: true,
|
---|
79 | usedExports: true,
|
---|
80 | providedExports: true,
|
---|
81 | optimizationBailout: true,
|
---|
82 | errorDetails: true,
|
---|
83 | publicPath: true,
|
---|
84 | logging: true,
|
---|
85 | runtimeModules: true,
|
---|
86 | exclude: false,
|
---|
87 | errorsSpace: 1000,
|
---|
88 | warningsSpace: 1000,
|
---|
89 | modulesSpace: 1000,
|
---|
90 | assetsSpace: 1000,
|
---|
91 | reasonsSpace: 1000
|
---|
92 | },
|
---|
93 | minimal: {
|
---|
94 | all: false,
|
---|
95 | version: true,
|
---|
96 | timings: true,
|
---|
97 | modules: true,
|
---|
98 | errorsSpace: 0,
|
---|
99 | warningsSpace: 0,
|
---|
100 | modulesSpace: 0,
|
---|
101 | assets: true,
|
---|
102 | assetsSpace: 0,
|
---|
103 | errors: true,
|
---|
104 | errorsCount: true,
|
---|
105 | warnings: true,
|
---|
106 | warningsCount: true,
|
---|
107 | logging: "warn"
|
---|
108 | },
|
---|
109 | "errors-only": {
|
---|
110 | all: false,
|
---|
111 | errors: true,
|
---|
112 | errorsCount: true,
|
---|
113 | errorsSpace: Infinity,
|
---|
114 | moduleTrace: true,
|
---|
115 | logging: "error"
|
---|
116 | },
|
---|
117 | "errors-warnings": {
|
---|
118 | all: false,
|
---|
119 | errors: true,
|
---|
120 | errorsCount: true,
|
---|
121 | errorsSpace: Infinity,
|
---|
122 | warnings: true,
|
---|
123 | warningsCount: true,
|
---|
124 | warningsSpace: Infinity,
|
---|
125 | logging: "warn"
|
---|
126 | },
|
---|
127 | summary: {
|
---|
128 | all: false,
|
---|
129 | version: true,
|
---|
130 | errorsCount: true,
|
---|
131 | warningsCount: true
|
---|
132 | },
|
---|
133 | none: {
|
---|
134 | all: false
|
---|
135 | }
|
---|
136 | };
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * @param {StatsOptions} all stats option
|
---|
140 | * @returns {boolean} true when enabled, otherwise false
|
---|
141 | */
|
---|
142 | const NORMAL_ON = ({ all }) => all !== false;
|
---|
143 | /**
|
---|
144 | * @param {StatsOptions} all stats option
|
---|
145 | * @returns {boolean} true when enabled, otherwise false
|
---|
146 | */
|
---|
147 | const NORMAL_OFF = ({ all }) => all === true;
|
---|
148 | /**
|
---|
149 | * @param {StatsOptions} all stats option
|
---|
150 | * @param {CreateStatsOptionsContext} forToString stats options context
|
---|
151 | * @returns {boolean} true when enabled, otherwise false
|
---|
152 | */
|
---|
153 | const ON_FOR_TO_STRING = ({ all }, { forToString }) =>
|
---|
154 | forToString ? all !== false : all === true;
|
---|
155 | /**
|
---|
156 | * @param {StatsOptions} all stats option
|
---|
157 | * @param {CreateStatsOptionsContext} forToString stats options context
|
---|
158 | * @returns {boolean} true when enabled, otherwise false
|
---|
159 | */
|
---|
160 | const OFF_FOR_TO_STRING = ({ all }, { forToString }) =>
|
---|
161 | forToString ? all === true : all !== false;
|
---|
162 | /**
|
---|
163 | * @param {StatsOptions} all stats option
|
---|
164 | * @param {CreateStatsOptionsContext} forToString stats options context
|
---|
165 | * @returns {boolean | "auto"} true when enabled, otherwise false
|
---|
166 | */
|
---|
167 | const AUTO_FOR_TO_STRING = ({ all }, { forToString }) => {
|
---|
168 | if (all === false) return false;
|
---|
169 | if (all === true) return true;
|
---|
170 | if (forToString) return "auto";
|
---|
171 | return true;
|
---|
172 | };
|
---|
173 |
|
---|
174 | /** @typedef {Record<string, (options: StatsOptions, context: CreateStatsOptionsContext, compilation: Compilation) => StatsOptions[keyof StatsOptions] | RequestShortener>} Defaults */
|
---|
175 |
|
---|
176 | /** @type {Defaults} */
|
---|
177 | const DEFAULTS = {
|
---|
178 | context: (options, context, compilation) => compilation.compiler.context,
|
---|
179 | requestShortener: (options, context, compilation) =>
|
---|
180 | compilation.compiler.context === options.context
|
---|
181 | ? compilation.requestShortener
|
---|
182 | : new RequestShortener(
|
---|
183 | /** @type {string} */
|
---|
184 | (options.context),
|
---|
185 | compilation.compiler.root
|
---|
186 | ),
|
---|
187 | performance: NORMAL_ON,
|
---|
188 | hash: OFF_FOR_TO_STRING,
|
---|
189 | env: NORMAL_OFF,
|
---|
190 | version: NORMAL_ON,
|
---|
191 | timings: NORMAL_ON,
|
---|
192 | builtAt: OFF_FOR_TO_STRING,
|
---|
193 | assets: NORMAL_ON,
|
---|
194 | entrypoints: AUTO_FOR_TO_STRING,
|
---|
195 | chunkGroups: OFF_FOR_TO_STRING,
|
---|
196 | chunkGroupAuxiliary: OFF_FOR_TO_STRING,
|
---|
197 | chunkGroupChildren: OFF_FOR_TO_STRING,
|
---|
198 | chunkGroupMaxAssets: (o, { forToString }) => (forToString ? 5 : Infinity),
|
---|
199 | chunks: OFF_FOR_TO_STRING,
|
---|
200 | chunkRelations: OFF_FOR_TO_STRING,
|
---|
201 | chunkModules: ({ all, modules }) => {
|
---|
202 | if (all === false) return false;
|
---|
203 | if (all === true) return true;
|
---|
204 | if (modules) return false;
|
---|
205 | return true;
|
---|
206 | },
|
---|
207 | dependentModules: OFF_FOR_TO_STRING,
|
---|
208 | chunkOrigins: OFF_FOR_TO_STRING,
|
---|
209 | ids: OFF_FOR_TO_STRING,
|
---|
210 | modules: ({ all, chunks, chunkModules }, { forToString }) => {
|
---|
211 | if (all === false) return false;
|
---|
212 | if (all === true) return true;
|
---|
213 | if (forToString && chunks && chunkModules) return false;
|
---|
214 | return true;
|
---|
215 | },
|
---|
216 | nestedModules: OFF_FOR_TO_STRING,
|
---|
217 | groupModulesByType: ON_FOR_TO_STRING,
|
---|
218 | groupModulesByCacheStatus: ON_FOR_TO_STRING,
|
---|
219 | groupModulesByLayer: ON_FOR_TO_STRING,
|
---|
220 | groupModulesByAttributes: ON_FOR_TO_STRING,
|
---|
221 | groupModulesByPath: ON_FOR_TO_STRING,
|
---|
222 | groupModulesByExtension: ON_FOR_TO_STRING,
|
---|
223 | modulesSpace: (o, { forToString }) => (forToString ? 15 : Infinity),
|
---|
224 | chunkModulesSpace: (o, { forToString }) => (forToString ? 10 : Infinity),
|
---|
225 | nestedModulesSpace: (o, { forToString }) => (forToString ? 10 : Infinity),
|
---|
226 | relatedAssets: OFF_FOR_TO_STRING,
|
---|
227 | groupAssetsByEmitStatus: ON_FOR_TO_STRING,
|
---|
228 | groupAssetsByInfo: ON_FOR_TO_STRING,
|
---|
229 | groupAssetsByPath: ON_FOR_TO_STRING,
|
---|
230 | groupAssetsByExtension: ON_FOR_TO_STRING,
|
---|
231 | groupAssetsByChunk: ON_FOR_TO_STRING,
|
---|
232 | assetsSpace: (o, { forToString }) => (forToString ? 15 : Infinity),
|
---|
233 | orphanModules: OFF_FOR_TO_STRING,
|
---|
234 | runtimeModules: ({ all, runtime }, { forToString }) =>
|
---|
235 | runtime !== undefined
|
---|
236 | ? runtime
|
---|
237 | : forToString
|
---|
238 | ? all === true
|
---|
239 | : all !== false,
|
---|
240 | cachedModules: ({ all, cached }, { forToString }) =>
|
---|
241 | cached !== undefined ? cached : forToString ? all === true : all !== false,
|
---|
242 | moduleAssets: OFF_FOR_TO_STRING,
|
---|
243 | depth: OFF_FOR_TO_STRING,
|
---|
244 | cachedAssets: OFF_FOR_TO_STRING,
|
---|
245 | reasons: OFF_FOR_TO_STRING,
|
---|
246 | reasonsSpace: (o, { forToString }) => (forToString ? 15 : Infinity),
|
---|
247 | groupReasonsByOrigin: ON_FOR_TO_STRING,
|
---|
248 | usedExports: OFF_FOR_TO_STRING,
|
---|
249 | providedExports: OFF_FOR_TO_STRING,
|
---|
250 | optimizationBailout: OFF_FOR_TO_STRING,
|
---|
251 | children: OFF_FOR_TO_STRING,
|
---|
252 | source: NORMAL_OFF,
|
---|
253 | moduleTrace: NORMAL_ON,
|
---|
254 | errors: NORMAL_ON,
|
---|
255 | errorsCount: NORMAL_ON,
|
---|
256 | errorDetails: AUTO_FOR_TO_STRING,
|
---|
257 | errorStack: OFF_FOR_TO_STRING,
|
---|
258 | warnings: NORMAL_ON,
|
---|
259 | warningsCount: NORMAL_ON,
|
---|
260 | publicPath: OFF_FOR_TO_STRING,
|
---|
261 | logging: ({ all }, { forToString }) =>
|
---|
262 | forToString && all !== false ? "info" : false,
|
---|
263 | loggingDebug: () => [],
|
---|
264 | loggingTrace: OFF_FOR_TO_STRING,
|
---|
265 | excludeModules: () => [],
|
---|
266 | excludeAssets: () => [],
|
---|
267 | modulesSort: () => "depth",
|
---|
268 | chunkModulesSort: () => "name",
|
---|
269 | nestedModulesSort: () => false,
|
---|
270 | chunksSort: () => false,
|
---|
271 | assetsSort: () => "!size",
|
---|
272 | outputPath: OFF_FOR_TO_STRING,
|
---|
273 | colors: () => false
|
---|
274 | };
|
---|
275 |
|
---|
276 | /**
|
---|
277 | * @param {string | ({ test: function(string): boolean }) | (function(string): boolean) | boolean} item item to normalize
|
---|
278 | * @returns {(function(string): boolean) | undefined} normalize fn
|
---|
279 | */
|
---|
280 | const normalizeFilter = item => {
|
---|
281 | if (typeof item === "string") {
|
---|
282 | const regExp = new RegExp(
|
---|
283 | `[\\\\/]${item.replace(/[-[\]{}()*+?.\\^$|]/g, "\\$&")}([\\\\/]|$|!|\\?)`
|
---|
284 | );
|
---|
285 | return ident => regExp.test(ident);
|
---|
286 | }
|
---|
287 | if (item && typeof item === "object" && typeof item.test === "function") {
|
---|
288 | return ident => item.test(ident);
|
---|
289 | }
|
---|
290 | if (typeof item === "function") {
|
---|
291 | return item;
|
---|
292 | }
|
---|
293 | if (typeof item === "boolean") {
|
---|
294 | return () => item;
|
---|
295 | }
|
---|
296 | };
|
---|
297 |
|
---|
298 | /** @type {Record<string, function(any): any[]>} */
|
---|
299 | const NORMALIZER = {
|
---|
300 | excludeModules: value => {
|
---|
301 | if (!Array.isArray(value)) {
|
---|
302 | value = value ? [value] : [];
|
---|
303 | }
|
---|
304 | return value.map(normalizeFilter);
|
---|
305 | },
|
---|
306 | excludeAssets: value => {
|
---|
307 | if (!Array.isArray(value)) {
|
---|
308 | value = value ? [value] : [];
|
---|
309 | }
|
---|
310 | return value.map(normalizeFilter);
|
---|
311 | },
|
---|
312 | warningsFilter: value => {
|
---|
313 | if (!Array.isArray(value)) {
|
---|
314 | value = value ? [value] : [];
|
---|
315 | }
|
---|
316 | /**
|
---|
317 | * @callback WarningFilterFn
|
---|
318 | * @param {StatsError} warning warning
|
---|
319 | * @param {string} warningString warning string
|
---|
320 | * @returns {boolean} result
|
---|
321 | */
|
---|
322 | return value.map(
|
---|
323 | /**
|
---|
324 | * @param {StatsOptions["warningsFilter"]} filter a warning filter
|
---|
325 | * @returns {WarningFilterFn} result
|
---|
326 | */
|
---|
327 | filter => {
|
---|
328 | if (typeof filter === "string") {
|
---|
329 | return (warning, warningString) => warningString.includes(filter);
|
---|
330 | }
|
---|
331 | if (filter instanceof RegExp) {
|
---|
332 | return (warning, warningString) => filter.test(warningString);
|
---|
333 | }
|
---|
334 | if (typeof filter === "function") {
|
---|
335 | return filter;
|
---|
336 | }
|
---|
337 | throw new Error(
|
---|
338 | `Can only filter warnings with Strings or RegExps. (Given: ${filter})`
|
---|
339 | );
|
---|
340 | }
|
---|
341 | );
|
---|
342 | },
|
---|
343 | logging: value => {
|
---|
344 | if (value === true) value = "log";
|
---|
345 | return value;
|
---|
346 | },
|
---|
347 | loggingDebug: value => {
|
---|
348 | if (!Array.isArray(value)) {
|
---|
349 | value = value ? [value] : [];
|
---|
350 | }
|
---|
351 | return value.map(normalizeFilter);
|
---|
352 | }
|
---|
353 | };
|
---|
354 |
|
---|
355 | class DefaultStatsPresetPlugin {
|
---|
356 | /**
|
---|
357 | * Apply the plugin
|
---|
358 | * @param {Compiler} compiler the compiler instance
|
---|
359 | * @returns {void}
|
---|
360 | */
|
---|
361 | apply(compiler) {
|
---|
362 | compiler.hooks.compilation.tap("DefaultStatsPresetPlugin", compilation => {
|
---|
363 | for (const key of Object.keys(NAMED_PRESETS)) {
|
---|
364 | const defaults = NAMED_PRESETS[/** @type {keyof NamedPresets} */ (key)];
|
---|
365 | compilation.hooks.statsPreset
|
---|
366 | .for(key)
|
---|
367 | .tap("DefaultStatsPresetPlugin", (options, context) => {
|
---|
368 | applyDefaults(options, defaults);
|
---|
369 | });
|
---|
370 | }
|
---|
371 | compilation.hooks.statsNormalize.tap(
|
---|
372 | "DefaultStatsPresetPlugin",
|
---|
373 | (options, context) => {
|
---|
374 | for (const key of Object.keys(DEFAULTS)) {
|
---|
375 | if (options[key] === undefined)
|
---|
376 | options[key] = DEFAULTS[key](options, context, compilation);
|
---|
377 | }
|
---|
378 | for (const key of Object.keys(NORMALIZER)) {
|
---|
379 | options[key] = NORMALIZER[key](options[key]);
|
---|
380 | }
|
---|
381 | }
|
---|
382 | );
|
---|
383 | });
|
---|
384 | }
|
---|
385 | }
|
---|
386 | module.exports = DefaultStatsPresetPlugin;
|
---|