Skip to content

Commit 45501ce

Browse files
committed
allow to suppress warnings based on string/regexp/function "warningsFilter" flag in stats
1 parent 343cdc7 commit 45501ce

1 file changed

Lines changed: 38 additions & 6 deletions

File tree

‎lib/Stats.js‎

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,34 @@ class Stats {
1616
this.hash = compilation.hash;
1717
}
1818

19+
static filterWarnings(warnings, warningsFilter) {
20+
// we dont have anything to filter so all warnings can be shown
21+
if(!warningsFilter) {
22+
return warnings;
23+
}
24+
25+
// create a chain of filters
26+
// if they return "true" a warning should be surpressed
27+
const normalizedWarningsFilters = [].concat(warningsFilter).map(filter => {
28+
if(typeof filter === "string") {
29+
return warning => warning.indexOf(filter) > -1;
30+
}
31+
32+
if(filter instanceof RegExp) {
33+
return warning => filter.test(warning);
34+
}
35+
36+
if(typeof filter === "function") {
37+
return filter;
38+
}
39+
40+
throw new Error(`Can only filter warnings with Strings or RegExps. (Given: ${filter})`);
41+
});
42+
return warnings.filter(warning => {
43+
return !normalizedWarningsFilters.some(check => check(warning));
44+
});
45+
}
46+
1947
hasWarnings() {
2048
return this.compilation.warnings.length > 0;
2149
}
@@ -70,6 +98,7 @@ class Stats {
7098
const showErrors = optionOrFallback(options.errors, true);
7199
const showErrorDetails = optionOrFallback(options.errorDetails, !forToString);
72100
const showWarnings = optionOrFallback(options.warnings, true);
101+
const warningsFilter = optionOrFallback(options.warningsFilter, null);
73102
const showPublicPath = optionOrFallback(options.publicPath, !forToString);
74103
const excludeModules = [].concat(optionOrFallback(options.exclude, [])).map(str => {
75104
if(typeof str !== "string") return str;
@@ -155,7 +184,8 @@ class Stats {
155184

156185
const obj = {
157186
errors: compilation.errors.map(formatError),
158-
warnings: compilation.warnings.map(formatError)
187+
warnings: compilation.warnings.map(formatError),
188+
warningsFilter: warningsFilter,
159189
};
160190

161191
//We just hint other renderers since actually omitting
@@ -758,12 +788,14 @@ class Stats {
758788
newline();
759789
}
760790
}
791+
761792
if(obj._showWarnings && obj.warnings) {
762-
obj.warnings.forEach(warning => {
763-
newline();
764-
colors.yellow(`WARNING in ${warning}`);
765-
newline();
766-
});
793+
Stats.filterWarnings(obj.warnings, obj.warningsFilter)
794+
.forEach(warning => {
795+
newline();
796+
colors.yellow(`WARNING in ${warning}`);
797+
newline();
798+
});
767799
}
768800
if(obj._showErrors && obj.errors) {
769801
obj.errors.forEach(error => {

0 commit comments

Comments
 (0)