Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature errorsSpace, warningsSpace #15450

Merged
merged 10 commits into from Apr 19, 2023
8 changes: 8 additions & 0 deletions declarations/WebpackOptions.d.ts
Expand Up @@ -2485,6 +2485,10 @@ export interface StatsOptions {
* Add errors count.
*/
errorsCount?: boolean;
/**
* Space to display errors (value is in number of lines).
*/
errorsSpace?: number;
/**
* Please use excludeModules instead.
*/
Expand Down Expand Up @@ -2669,6 +2673,10 @@ export interface StatsOptions {
* Suppress listing warnings that match the specified filters (they will still be counted). Filters can be Strings, RegExps or Functions.
*/
warningsFilter?: WarningFilterTypes;
/**
* Space to display warnings (value is in number of lines).
*/
warningsSpace?: number;
}
/**
* Options for the watcher.
Expand Down
123 changes: 98 additions & 25 deletions lib/stats/DefaultStatsFactoryPlugin.js
Expand Up @@ -811,11 +811,32 @@ const SIMPLE_EXTRACTORS = {
},
errors: (object, compilation, context, options, factory) => {
const { type, cachedGetErrors } = context;
object.errors = factory.create(
const rawErrors = cachedGetErrors(compilation);
const factorizedErrors = factory.create(
`${type}.errors`,
cachedGetErrors(compilation),
context
);
let filtered = 0;
if (options.errorDetails === "auto" && rawErrors.length >= 3) {
filtered = rawErrors
.map(e => typeof e !== "string" && e.details)
.filter(Boolean).length;
}
if (
options.errorDetails === true ||
!Number.isFinite(options.errorsSpace)
) {
object.errors = factorizedErrors;
if (filtered) object.filteredErrorDetailsCount = filtered;
return;
}
const [errors, filteredBySpace] = errorsSpaceLimit(
factorizedErrors,
options.errorsSpace
);
object.filteredErrorDetailsCount = filtered + filteredBySpace;
object.errors = errors;
},
errorsCount: (object, compilation, { cachedGetErrors }) => {
object.errorsCount = countWithChildren(compilation, c =>
Expand All @@ -824,11 +845,31 @@ const SIMPLE_EXTRACTORS = {
},
warnings: (object, compilation, context, options, factory) => {
const { type, cachedGetWarnings } = context;
object.warnings = factory.create(
const rawWarnings = factory.create(
`${type}.warnings`,
cachedGetWarnings(compilation),
context
);
let filtered = 0;
if (options.errorDetails === "auto") {
filtered = cachedGetWarnings(compilation)
.map(e => typeof e !== "string" && e.details)
.filter(Boolean).length;
}
if (
options.errorDetails === true ||
!Number.isFinite(options.warningsSpace)
) {
object.warnings = rawWarnings;
if (filtered) object.filteredWarningDetailsCount = filtered;
return;
}
const [warnings, filteredBySpace] = errorsSpaceLimit(
rawWarnings,
options.warningsSpace
);
object.filteredWarningDetailsCount = filtered + filteredBySpace;
object.warnings = warnings;
},
warningsCount: (
object,
Expand All @@ -853,29 +894,6 @@ const SIMPLE_EXTRACTORS = {
});
});
},
errorDetails: (
object,
compilation,
{ cachedGetErrors, cachedGetWarnings },
{ errorDetails, errors, warnings }
) => {
if (errorDetails === "auto") {
if (warnings) {
const warnings = cachedGetWarnings(compilation);
object.filteredWarningDetailsCount = warnings
.map(e => typeof e !== "string" && e.details)
.filter(Boolean).length;
}
if (errors) {
const errors = cachedGetErrors(compilation);
if (errors.length >= 3) {
object.filteredErrorDetailsCount = errors
.map(e => typeof e !== "string" && e.details)
.filter(Boolean).length;
}
}
}
},
children: (object, compilation, context, options, factory) => {
const { type } = context;
object.children = factory.create(
Expand Down Expand Up @@ -1779,6 +1797,61 @@ const spaceLimited = (
};
};

const errorsSpaceLimit = (errors, max) => {
let filtered = 0;
// Can not fit into limit
// print only messages
if (errors.length + 1 >= max)
return [
errors.map(error => {
if (typeof error === "string" || !error.details) return error;
filtered++;
return { ...error, details: "" };
}),
filtered
];
let fullLength = errors.length;
let result = errors;

let i = 0;
for (; i < errors.length; i++) {
const error = errors[i];
if (typeof error !== "string" && error.details) {
const splitted = error.details.split("\n");
const len = splitted.length;
fullLength += len;
if (fullLength > max) {
result = i > 0 ? errors.slice(0, i) : [];
const overLimit = fullLength - max + 1;
const error = errors[i++];
result.push({
...error,
details: error.details.split("\n").slice(0, -overLimit).join("\n"),
filteredDetails: overLimit
});
filtered = errors.length - i;
for (; i < errors.length; i++) {
const error = errors[i];
if (typeof error === "string" || !error.details) result.push(error);
result.push({ ...error, details: "" });
}
break;
} else if (fullLength === max) {
result = errors.slice(0, ++i);
filtered = errors.length - i;
for (; i < errors.length; i++) {
const error = errors[i];
if (typeof error === "string" || !error.details) result.push(error);
result.push({ ...error, details: "" });
}
break;
}
}
}

return [result, filtered];
};

const assetGroup = (children, assets) => {
let size = 0;
for (const asset of children) {
Expand Down
9 changes: 9 additions & 0 deletions lib/stats/DefaultStatsPresetPlugin.js
Expand Up @@ -47,6 +47,8 @@ const NAMED_PRESETS = {
orphanModules: true,
runtimeModules: true,
exclude: false,
errorsSpace: Infinity,
warningsSpace: Infinity,
modulesSpace: Infinity,
chunkModulesSpace: Infinity,
assetsSpace: Infinity,
Expand All @@ -73,6 +75,8 @@ const NAMED_PRESETS = {
logging: true,
runtimeModules: true,
exclude: false,
errorsSpace: 1000,
warningsSpace: 1000,
modulesSpace: 1000,
assetsSpace: 1000,
reasonsSpace: 1000
Expand All @@ -82,6 +86,8 @@ const NAMED_PRESETS = {
version: true,
timings: true,
modules: true,
errorsSpace: 0,
warningsSpace: 0,
modulesSpace: 0,
assets: true,
assetsSpace: 0,
Expand All @@ -95,15 +101,18 @@ const NAMED_PRESETS = {
all: false,
errors: true,
errorsCount: true,
errorsSpace: Infinity,
moduleTrace: true,
logging: "error"
},
"errors-warnings": {
all: false,
errors: true,
errorsCount: true,
errorsSpace: Infinity,
warnings: true,
warningsCount: true,
warningsSpace: Infinity,
logging: "warn"
},
summary: {
Expand Down
4 changes: 4 additions & 0 deletions lib/stats/DefaultStatsPrinterPlugin.js
Expand Up @@ -589,6 +589,8 @@ const SIMPLE_PRINTERS = {
"error.message": (message, { bold, formatError }) =>
message.includes("\u001b[") ? message : bold(formatError(message)),
"error.details": (details, { formatError }) => formatError(details),
"error.filteredDetails": filteredDetails =>
filteredDetails ? `+ ${filteredDetails} hidden lines` : undefined,
"error.stack": stack => stack,
"error.moduleTrace": moduleTrace => undefined,
"error.separator!": () => "\n",
Expand Down Expand Up @@ -689,6 +691,8 @@ const ERROR_PREFERRED_ORDER = [
"separator!",
"details",
"separator!",
"filteredDetails",
"separator!",
"stack",
"separator!",
"missing",
Expand Down
2 changes: 1 addition & 1 deletion schemas/WebpackOptions.check.js

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions schemas/WebpackOptions.json
Expand Up @@ -4713,6 +4713,10 @@
"description": "Add errors count.",
"type": "boolean"
},
"errorsSpace": {
"description": "Space to display errors (value is in number of lines).",
"type": "number"
},
"exclude": {
"description": "Please use excludeModules instead.",
"cli": {
Expand Down Expand Up @@ -4945,6 +4949,10 @@
"$ref": "#/definitions/WarningFilterTypes"
}
]
},
"warningsSpace": {
"description": "Space to display warnings (value is in number of lines).",
"type": "number"
}
}
},
Expand Down
26 changes: 26 additions & 0 deletions test/__snapshots__/Cli.basictest.js.snap
Expand Up @@ -8626,6 +8626,19 @@ Object {
"multiple": false,
"simpleType": "boolean",
},
"stats-errors-space": Object {
"configs": Array [
Object {
"description": "Space to display errors (value is in number of lines).",
"multiple": false,
"path": "stats.errorsSpace",
"type": "number",
},
],
"description": "Space to display errors (value is in number of lines).",
"multiple": false,
"simpleType": "number",
},
"stats-exclude-assets": Object {
"configs": Array [
Object {
Expand Down Expand Up @@ -9319,6 +9332,19 @@ Object {
"multiple": false,
"simpleType": "boolean",
},
"stats-warnings-space": Object {
"configs": Array [
Object {
"description": "Space to display warnings (value is in number of lines).",
"multiple": false,
"path": "stats.warningsSpace",
"type": "number",
},
],
"description": "Space to display warnings (value is in number of lines).",
"multiple": false,
"simpleType": "number",
},
"target": Object {
"configs": Array [
Object {
Expand Down