Skip to content

Commit 33614f5

Browse files
authored
refactor: simplify stats error formatting logic (#6288)
1 parent b08bb4c commit 33614f5

File tree

3 files changed

+24
-60
lines changed

3 files changed

+24
-60
lines changed

packages/core/src/helpers/format.ts

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { StatsCompilation, StatsError } from '@rspack/core';
1+
import type { StatsError } from '@rspack/core';
22
import color from '../../compiled/picocolors/index.js';
33

44
const formatFileName = (fileName: string) => {
@@ -191,24 +191,20 @@ const hintNodePolyfill = (message: string): string => {
191191
return message;
192192
};
193193

194-
// Cleans up Rspack error messages.
195-
function formatMessage(stats: StatsError | string, verbose?: boolean) {
194+
// Formats Rspack stats error to readable message
195+
export function formatStatsError(stats: StatsError, verbose?: boolean): string {
196196
let lines: string[] = [];
197197
let message: string;
198198

199199
// Stats error object
200-
if (typeof stats === 'object') {
201-
const fileName = resolveFileName(stats);
202-
const mainMessage = stats.message;
203-
const details =
204-
verbose && stats.details ? `\nDetails: ${stats.details}\n` : '';
205-
const stack = verbose && stats.stack ? `\n${stats.stack}` : '';
206-
const moduleTrace = resolveModuleTrace(stats);
207-
208-
message = `${fileName}${mainMessage}${details}${stack}${moduleTrace}`;
209-
} else {
210-
message = stats;
211-
}
200+
const fileName = resolveFileName(stats);
201+
const mainMessage = stats.message;
202+
const details =
203+
verbose && stats.details ? `\nDetails: ${stats.details}\n` : '';
204+
const stack = verbose && stats.stack ? `\n${stats.stack}` : '';
205+
const moduleTrace = resolveModuleTrace(stats);
206+
207+
message = `${fileName}${mainMessage}${details}${stack}${moduleTrace}`;
212208

213209
// Remove inner error message
214210
const innerError = '-- inner error --';
@@ -234,21 +230,3 @@ function formatMessage(stats: StatsError | string, verbose?: boolean) {
234230

235231
return message.trim();
236232
}
237-
238-
export function formatStatsMessages(
239-
stats: Pick<StatsCompilation, 'errors' | 'warnings'>,
240-
verbose?: boolean,
241-
): {
242-
errors: string[];
243-
warnings: string[];
244-
} {
245-
const formattedErrors =
246-
stats.errors?.map((error) => formatMessage(error, verbose)) || [];
247-
const formattedWarnings =
248-
stats.warnings?.map((warning) => formatMessage(warning, verbose)) || [];
249-
250-
return {
251-
errors: formattedErrors,
252-
warnings: formattedWarnings,
253-
};
254-
}

packages/core/src/helpers/stats.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import color from '../../compiled/picocolors/index.js';
33
import { logger } from '../logger';
44
import type { Rspack } from '../types';
55
import { isMultiCompiler } from './';
6-
import { formatStatsMessages } from './format.js';
6+
import { formatStatsError } from './format';
77

88
function formatErrorMessage(errors: string[]) {
99
const title = color.bold(
@@ -98,27 +98,16 @@ export function formatStats(
9898
const verbose = logger.level === 'verbose';
9999

100100
if (hasErrors) {
101-
const { errors } = formatStatsMessages(
102-
{
103-
errors: getAllStatsErrors(statsData),
104-
warnings: [],
105-
},
106-
verbose,
107-
);
108-
101+
const statsErrors = getAllStatsErrors(statsData) ?? [];
102+
const errors = statsErrors.map((item) => formatStatsError(item, verbose));
109103
return {
110104
message: formatErrorMessage(errors),
111105
level: 'error',
112106
};
113107
}
114108

115-
const { warnings } = formatStatsMessages(
116-
{
117-
errors: [],
118-
warnings: getAllStatsWarnings(statsData),
119-
},
120-
verbose,
121-
);
109+
const statsWarnings = getAllStatsWarnings(statsData) ?? [];
110+
const warnings = statsWarnings.map((item) => formatStatsError(item, verbose));
122111

123112
if (warnings.length) {
124113
const title = color.bold(

packages/core/src/server/socketServer.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
getAllStatsWarnings,
77
getStatsOptions,
88
} from '../helpers';
9-
import { formatStatsMessages } from '../helpers/format';
9+
import { formatStatsError } from '../helpers/format';
1010
import { logger } from '../logger';
1111
import type { DevConfig, InternalContext, Rspack } from '../types';
1212
import { formatBrowserErrorLog } from './browserLogs';
@@ -434,11 +434,8 @@ export class SocketServer {
434434
}
435435

436436
if (statsJson.errorsCount) {
437-
const errors = getAllStatsErrors(statsJson);
438-
const { errors: formattedErrors } = formatStatsMessages({
439-
errors,
440-
warnings: [],
441-
});
437+
const statsErrors = getAllStatsErrors(statsJson) ?? [];
438+
const formattedErrors = statsErrors.map((item) => formatStatsError(item));
442439

443440
this.sockWrite(
444441
{
@@ -454,11 +451,11 @@ export class SocketServer {
454451
}
455452

456453
if (statsJson.warningsCount) {
457-
const warnings = getAllStatsWarnings(statsJson);
458-
const { warnings: formattedWarnings } = formatStatsMessages({
459-
warnings,
460-
errors: [],
461-
});
454+
const statsWarnings = getAllStatsWarnings(statsJson) ?? [];
455+
const formattedWarnings = statsWarnings.map((item) =>
456+
formatStatsError(item),
457+
);
458+
462459
this.sockWrite(
463460
{
464461
type: 'warnings',

0 commit comments

Comments
 (0)