Skip to content

Commit

Permalink
refactor errorDetails logic
Browse files Browse the repository at this point in the history
  • Loading branch information
vankop committed Mar 3, 2022
1 parent 8667b68 commit 38bffd1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 50 deletions.
79 changes: 34 additions & 45 deletions lib/stats/DefaultStatsFactoryPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -811,24 +811,32 @@ const SIMPLE_EXTRACTORS = {
},
errors: (object, compilation, context, options, factory) => {
const { type, cachedGetErrors } = context;
const rawErrors = 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 = rawErrors;
object.errors = factorizedErrors;
if (filtered) object.filteredErrorDetailsCount = filtered;
return;
}
const [errors, filtered] = errorsSpaceLimit(
rawErrors,
const [errors, filteredBySpace] = errorsSpaceLimit(
factorizedErrors,
options.errorsSpace
);
object.filteredErrorDetailsCount = filtered + filteredBySpace;
object.errors = errors;
if (filtered) object.filteredErrorDetailsCount = filtered;
},
errorsCount: (object, compilation, { cachedGetErrors }) => {
object.errorsCount = countWithChildren(compilation, c =>
Expand All @@ -842,19 +850,26 @@ const SIMPLE_EXTRACTORS = {
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, filtered] = errorsSpaceLimit(
const [warnings, filteredBySpace] = errorsSpaceLimit(
rawWarnings,
options.warningsSpace
);
object.filteredWarningDetailsCount = filtered + filteredBySpace;
object.warnings = warnings;
if (filtered) object.filteredWarningDetailsCount = filtered;
},
warningsCount: (
object,
Expand All @@ -879,35 +894,6 @@ const SIMPLE_EXTRACTORS = {
});
});
},
errorDetails: (
object,
compilation,
{ cachedGetErrors, cachedGetWarnings },
{ errorDetails, errors, warnings }
) => {
if (errorDetails === "auto") {
if (warnings) {
const warnings = cachedGetWarnings(compilation);
const filtered = object.filteredWarningDetailsCount || 0;
object.filteredWarningDetailsCount =
filtered +
warnings
.map(e => typeof e !== "string" && e.details)
.filter(Boolean).length;
}
if (errors) {
const errors = cachedGetErrors(compilation);
if (errors.length >= 3) {
const filtered = object.filteredErrorDetailsCount || 0;
object.filteredErrorDetailsCount =
filtered +
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 @@ -1815,10 +1801,10 @@ const errorsSpaceLimit = (errors, max) => {
let filtered = 0;
// Can not fit into limit
// print only messages
if (errors.length >= max)
if (errors.length + 1 >= max)
return [
errors.map(error => {
if (error.details === undefined) return error;
if (typeof error === "string" || !error.details) return error;
filtered++;
return { ...error, details: "" };
}),
Expand All @@ -1829,13 +1815,14 @@ const errorsSpaceLimit = (errors, max) => {

let i = 0;
for (; i < errors.length; i++) {
if (errors[i].details) {
const splitted = errors[i].details.split("\n");
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;
const overlimit = fullLength - max + 1;
result.push({
...errors[i++],
details: `${splitted
Expand All @@ -1846,16 +1833,18 @@ const errorsSpaceLimit = (errors, max) => {
});
filtered = errors.length - i;
for (; i < errors.length; i++) {
if (errors[i].details === undefined) result.push(errors[i]);
result.push({ ...errors[i], details: "" });
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++) {
if (errors[i].details === undefined) result.push(errors[i]);
result.push({ ...errors[i], details: "" });
const error = errors[i];
if (typeof error === "string" || !error.details) result.push(error);
result.push({ ...error, details: "" });
}
break;
}
Expand Down
7 changes: 2 additions & 5 deletions test/__snapshots__/StatsTestCases.basictest.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1026,14 +1026,12 @@ assets by status 84 bytes [cached] 1 asset
ERROR in ./index.js (./loader.js!./index.js)
Module Error (from ./loader.js):
loader error1
stack1
+2 hidden lines
ERROR in ./index.js (./loader.js!./index.js)
Module Error (from ./loader.js):
loader error2
1 error has detailed information that is not shown.
2 errors have detailed information that is not shown.
Use 'stats.errorDetails: true' resp. '--stats-error-details' to show it.
webpack x.x.x compiled with 2 errors in X ms
Expand All @@ -1045,8 +1043,7 @@ ERROR in ./index.js (./loader.js!./index.js)
Module Error (from ./loader.js):
loader error1
stack1
stack2
+1 hidden line
+2 hidden lines
ERROR in ./index.js (./loader.js!./index.js)
Module Error (from ./loader.js):
Expand Down

0 comments on commit 38bffd1

Please sign in to comment.