Skip to content

Commit 6add433

Browse files
committed
Add Stats typing support
1 parent 3f183b5 commit 6add433

File tree

2 files changed

+62
-12
lines changed

2 files changed

+62
-12
lines changed

lib/Compilation.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ class Compilation extends Tapable {
263263
this.fileTimestamps = undefined;
264264
this.contextTimestamps = undefined;
265265
this.compilationDependencies = undefined;
266+
this.needAdditionalPass = false;
266267

267268
this._buildingModules = new Map();
268269
this._rebuildingModules = new Map();

lib/Stats.js

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,53 @@ const SizeFormatHelpers = require("./SizeFormatHelpers");
99
const formatLocation = require("./formatLocation");
1010
const identifierUtils = require("./util/identifier");
1111

12-
const optionsOrFallback = (...args) => {
13-
let optionValues = [];
14-
optionValues.push(...args);
12+
/** @typedef {import("./Compilation")} Compilation */
13+
14+
const optionsOrFallback = (...optionValues) => {
1515
return optionValues.find(optionValue => typeof optionValue !== "undefined");
1616
};
1717

18+
/**
19+
* @typedef {string | RegExp | ((warning: any) => any)} WarningFilterAtom
20+
* @typedef {WarningFilterAtom | WarningFilterAtom[]} WarningFilter
21+
*/
22+
1823
class Stats {
24+
/**
25+
* @param {Compilation} compilation the current compilation
26+
*/
1927
constructor(compilation) {
28+
/** @type {Compilation} */
2029
this.compilation = compilation;
30+
/** @type {string} */
2131
this.hash = compilation.hash;
32+
/** @type {undefined | number} */
2233
this.startTime = undefined;
34+
/** @type {undefined | number} */
2335
this.endTime = undefined;
2436
}
2537

38+
/**
39+
* @param {string[]} warnings the warnings to filter
40+
* @param {WarningFilter} warningsFilter the predicate used per iteration
41+
* @returns {string[]} returns the filtered warnings
42+
*/
2643
static filterWarnings(warnings, warningsFilter) {
2744
// we dont have anything to filter so all warnings can be shown
2845
if (!warningsFilter) {
2946
return warnings;
3047
}
3148

3249
// create a chain of filters
33-
// if they return "true" a warning should be suppressed
34-
const normalizedWarningsFilters = [].concat(warningsFilter).map(filter => {
50+
let warningsFilters;
51+
if (!Array.isArray(warningsFilter)) {
52+
warningsFilters = [warningsFilter];
53+
} else {
54+
warningsFilters = warningsFilter;
55+
}
56+
57+
// if a filter return "true" a warning should be suppressed
58+
const normalizedWarningsFilters = warningsFilters.map(filter => {
3559
if (typeof filter === "string") {
3660
return warning => warning.includes(filter);
3761
}
@@ -48,41 +72,63 @@ class Stats {
4872
`Can only filter warnings with Strings or RegExps. (Given: ${filter})`
4973
);
5074
});
75+
5176
return warnings.filter(warning => {
5277
return !normalizedWarningsFilters.some(check => check(warning));
5378
});
5479
}
5580

81+
// TODO: Remove from this class and convert to a function in webpack 5
82+
/**
83+
* @param {string} filePath the file path to normalize
84+
* @returns {string} returns the normalized file path
85+
*/
5686
formatFilePath(filePath) {
5787
const OPTIONS_REGEXP = /^(\s|\S)*!/;
5888
return filePath.includes("!")
5989
? `${filePath.replace(OPTIONS_REGEXP, "")} (${filePath})\n`
6090
: `${filePath}\n`;
6191
}
6292

93+
/**
94+
* @returns {boolean} returns true if the compilation contains warnings
95+
*/
6396
hasWarnings() {
6497
return (
6598
this.compilation.warnings.length > 0 ||
6699
this.compilation.children.some(child => child.getStats().hasWarnings())
67100
);
68101
}
69102

103+
/**
104+
* @returns {boolean} returns true if the compilation contains errors
105+
*/
70106
hasErrors() {
71107
return (
72108
this.compilation.errors.length > 0 ||
73109
this.compilation.children.some(child => child.getStats().hasErrors())
74110
);
75111
}
76112

77-
// remove a prefixed "!" that can be specified to reverse sort order
113+
// TODO: Remove from this class and convert to a function in webpack 5
114+
/**
115+
* Remove a prefixed "!" that can be specified to reverse sort order.
116+
* @param {string} field the field name
117+
* @returns {string} returns normalized name
118+
*/
78119
normalizeFieldKey(field) {
79120
if (field[0] === "!") {
80121
return field.substr(1);
81122
}
82123
return field;
83124
}
84125

85-
// if a field is prefixed by a "!" reverse sort order
126+
// TODO: Remove from this class and convert to a function in webpack 5
127+
/**
128+
* If a field is prefixed by a "!" reverse sort order.
129+
* @param {string} field the field name
130+
* @returns {boolean} returns true if the sort order is regular, false if the order is reversed.
131+
*/
86132
sortOrderRegular(field) {
87133
if (field[0] === "!") {
88134
return false;
@@ -392,11 +438,14 @@ class Stats {
392438
}
393439
if (chunk.name) {
394440
assetsByFile[asset].chunkNames.push(chunk.name);
395-
if (obj.assetsByChunkName[chunk.name])
396-
obj.assetsByChunkName[chunk.name] = []
397-
.concat(obj.assetsByChunkName[chunk.name])
398-
.concat([asset]);
399-
else obj.assetsByChunkName[chunk.name] = asset;
441+
if (obj.assetsByChunkName[chunk.name]) {
442+
obj.assetsByChunkName[chunk.name] = [
443+
...obj.assetsByChunkName[chunk.name],
444+
asset
445+
];
446+
} else {
447+
obj.assetsByChunkName[chunk.name] = asset;
448+
}
400449
}
401450
}
402451
}

0 commit comments

Comments
 (0)