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 @@ -2456,6 +2456,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 @@ -2640,6 +2644,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
58 changes: 50 additions & 8 deletions lib/stats/DefaultStatsFactoryPlugin.js
Expand Up @@ -811,10 +811,9 @@ const SIMPLE_EXTRACTORS = {
},
errors: (object, compilation, context, options, factory) => {
const { type, cachedGetErrors } = context;
object.errors = factory.create(
`${type}.errors`,
cachedGetErrors(compilation),
context
object.errors = errorsSpaceLimit(
factory.create(`${type}.errors`, cachedGetErrors(compilation), context),
options.errorsSpace
);
},
errorsCount: (object, compilation, { cachedGetErrors }) => {
Expand All @@ -824,10 +823,13 @@ const SIMPLE_EXTRACTORS = {
},
warnings: (object, compilation, context, options, factory) => {
const { type, cachedGetWarnings } = context;
object.warnings = factory.create(
`${type}.warnings`,
cachedGetWarnings(compilation),
context
object.warnings = errorsSpaceLimit(
factory.create(
`${type}.warnings`,
cachedGetWarnings(compilation),
context
),
options.warningsSpace
);
},
warningsCount: (
Expand Down Expand Up @@ -1779,6 +1781,46 @@ const spaceLimited = (
};
};

const errorsSpaceLimit = (errors, max) => {
if (!Number.isFinite(max)) return errors;
// Can not fit into limit
// print only messages
if (errors.length > max)
return errors.map(error => {
if (error.details === undefined) return error;
const { details, ...rest } = error;
return { details: "", ...rest };
vankop marked this conversation as resolved.
Show resolved Hide resolved
});

let fullLength = errors.length;
let result = errors;

let i = 0;
for (; i < errors.length; i++) {
if (errors[i].details) {
const splitted = errors[i].details.split("\n");
const len = splitted.length;
fullLength += len;
if (fullLength > max) {
result = errors.slice(0, i);
const { details, ...rest } = errors[i++];
result.push({
details: splitted.slice(0, len - (fullLength - max)).join("\n"),
vankop marked this conversation as resolved.
Show resolved Hide resolved
...rest
});
vankop marked this conversation as resolved.
Show resolved Hide resolved
for (; i < errors.length; i++) {
const { details, ...rest } = errors[i];
if (details === undefined) result.push(errors[i]);
result.push({ details: "", ...rest });
vankop marked this conversation as resolved.
Show resolved Hide resolved
}
break;
}
}
}

return result;
vankop marked this conversation as resolved.
Show resolved Hide resolved
};

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
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 @@ -4615,6 +4615,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 @@ -4847,6 +4851,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 @@ -8231,6 +8231,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 @@ -8924,6 +8937,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
44 changes: 44 additions & 0 deletions test/__snapshots__/StatsTestCases.basictest.js.snap
Expand Up @@ -987,6 +987,38 @@ chunk (runtime: a) a.js (a) 22 bytes [entry] [rendered]
webpack x.x.x compiled successfully in X ms"
`;

exports[`StatsTestCases should print correct stats for errors-space-error 1`] = `
"assets by status 84 bytes [cached] 1 asset
./loader.js!./index.js 1 bytes [built] [code generated] [1 error]

ERROR in ./index.js (./loader.js!./index.js)
Module Error (from ./loader.js):
loader error
Error: loader error

webpack x.x.x compiled with 1 error in X ms

assets by status 84 bytes [cached] 1 asset
./loader.js!./index.js 1 bytes [built] [code generated] [1 error]

ERROR in ./index.js (./loader.js!./index.js)
Module Error (from ./loader.js):
loader error

webpack x.x.x compiled with 1 error in X ms

assets by status 84 bytes [cached] 1 asset
./loader.js!./index.js 1 bytes [built] [code generated] [1 error]

ERROR in ./index.js (./loader.js!./index.js)
Module Error (from ./loader.js):
loader error
Error: loader error
at Object.<anonymous>.module.exports (Xdir/errors-space-error/loader.js:4:17)

webpack x.x.x compiled with 1 error in X ms"
`;

exports[`StatsTestCases should print correct stats for exclude-with-loader 1`] = `
"hidden assets 34 bytes 1 asset
asset bundle.js 5.25 KiB [emitted] (name: main)
Expand Down Expand Up @@ -4553,6 +4585,18 @@ require.include() is deprecated and will be removed soon.
webpack x.x.x compiled with 1 warning in X ms"
`;

exports[`StatsTestCases should print correct stats for warnings-space-warning 1`] = `
"asset main.js 1010 bytes [emitted] (name: main)
orphan modules 20 bytes [orphan] 1 module
runtime modules 274 bytes 1 module
./index.js + 1 modules 64 bytes [built] [code generated]

WARNING in ./index.js 3:12-14
export 'bb' (imported as 'bb') was not found in './a' (possible exports: a)

webpack x.x.x compiled with 1 warning in X ms"
`;

exports[`StatsTestCases should print correct stats for wasm-explorer-examples-sync 1`] = `
"assets by path *.js 22.2 KiB
asset bundle.js 16.7 KiB [emitted] (name: main)
Expand Down
Empty file.
6 changes: 6 additions & 0 deletions test/statsCases/errors-space-error/loader.js
@@ -0,0 +1,6 @@
"use strict";

module.exports = function (content) {
this.emitError(new Error("loader error"));
return content;
}
27 changes: 27 additions & 0 deletions test/statsCases/errors-space-error/webpack.config.js
@@ -0,0 +1,27 @@
/** @type {import("../../../").Configuration[]} */
module.exports = [
{
entry: "./loader!./index.js",
mode: "production",
stats: {
errorsSpace: 2,
errors: true
}
},
{
entry: "./loader!./index.js",
mode: "production",
stats: {
errorsSpace: 0,
errors: true
}
},
{
entry: "./loader!./index.js",
mode: "production",
stats: {
errorsSpace: 100,
errors: true
}
}
];
1 change: 1 addition & 0 deletions test/statsCases/warnings-space-warning/a.js
@@ -0,0 +1 @@
export const a = 1;
3 changes: 3 additions & 0 deletions test/statsCases/warnings-space-warning/index.js
@@ -0,0 +1,3 @@
import { bb } from "./a";

console.log(bb);
9 changes: 9 additions & 0 deletions test/statsCases/warnings-space-warning/webpack.config.js
@@ -0,0 +1,9 @@
/** @type {import("../../../").Configuration} */
module.exports = {
entry: "./index.js",
mode: "production",
stats: {
warningsSpace: 0,
warnings: true
}
};
10 changes: 10 additions & 0 deletions types.d.ts
Expand Up @@ -11408,6 +11408,11 @@ declare interface StatsOptions {
*/
errorsCount?: boolean;

/**
* Space to display errors (value is in number of lines).
*/
errorsSpace?: number;

/**
* Please use excludeModules instead.
*/
Expand Down Expand Up @@ -11668,6 +11673,11 @@ declare interface StatsOptions {
| RegExp
| WarningFilterItemTypes[]
| ((warning: StatsError, value: string) => boolean);

/**
* Space to display warnings (value is in number of lines).
*/
warningsSpace?: number;
}
declare abstract class StatsPrinter {
hooks: Readonly<{
Expand Down