From 1110cb111c54675ef75caea14e239b18284be53e Mon Sep 17 00:00:00 2001 From: neverland Date: Tue, 24 Dec 2024 19:27:39 +0800 Subject: [PATCH] perf: format stats errors in the Node side --- packages/core/src/client/hmr.ts | 25 ++++++------------- .../core/src/{client => helpers}/format.ts | 0 packages/core/src/helpers/stats.ts | 2 +- packages/core/src/server/socketServer.ts | 17 +++++++++++-- 4 files changed, 23 insertions(+), 21 deletions(-) rename packages/core/src/{client => helpers}/format.ts (100%) diff --git a/packages/core/src/client/hmr.ts b/packages/core/src/client/hmr.ts index 2892e177e9..9451d8deec 100644 --- a/packages/core/src/client/hmr.ts +++ b/packages/core/src/client/hmr.ts @@ -1,5 +1,4 @@ -import type { NormalizedClientConfig, Rspack } from '../types'; -import { formatStatsMessages } from './format'; +import type { NormalizedClientConfig } from '../types'; const compilationId = RSBUILD_COMPILATION_NAME; const config: NormalizedClientConfig = RSBUILD_CLIENT_CONFIG; @@ -68,26 +67,21 @@ function handleSuccess() { } // Compilation with warnings (e.g. ESLint). -function handleWarnings(warnings: Rspack.StatsError[]) { +function handleWarnings(warnings: string[]) { clearOutdatedErrors(); const isHotUpdate = !isFirstCompilation; isFirstCompilation = false; hasCompileErrors = false; - const formatted = formatStatsMessages({ - warnings, - errors: [], - }); - - for (let i = 0; i < formatted.warnings.length; i++) { + for (let i = 0; i < warnings.length; i++) { if (i === 5) { console.warn( 'There were more warnings in other files, you can find a complete log in the terminal.', ); break; } - console.warn(formatted.warnings[i]); + console.warn(warnings[i]); } // Attempt to apply hot updates or reload. @@ -97,24 +91,19 @@ function handleWarnings(warnings: Rspack.StatsError[]) { } // Compilation with errors (e.g. syntax error or missing modules). -function handleErrors(errors: Rspack.StatsError[]) { +function handleErrors(errors: string[]) { clearOutdatedErrors(); isFirstCompilation = false; hasCompileErrors = true; - const formatted = formatStatsMessages({ - errors, - warnings: [], - }); - // Also log them to the console. - for (const error of formatted.errors) { + for (const error of errors) { console.error(error); } if (createOverlay) { - createOverlay(formatted.errors); + createOverlay(errors); } } diff --git a/packages/core/src/client/format.ts b/packages/core/src/helpers/format.ts similarity index 100% rename from packages/core/src/client/format.ts rename to packages/core/src/helpers/format.ts diff --git a/packages/core/src/helpers/stats.ts b/packages/core/src/helpers/stats.ts index d27a7f3b2c..9ae460a2a5 100644 --- a/packages/core/src/helpers/stats.ts +++ b/packages/core/src/helpers/stats.ts @@ -1,9 +1,9 @@ import type { StatsCompilation } from '@rspack/core'; import color from '../../compiled/picocolors/index.js'; -import { formatStatsMessages } from '../client/format'; import { logger } from '../logger'; import type { Rspack } from '../types'; import { isMultiCompiler } from './'; +import { formatStatsMessages } from './format.js'; /** * Add node polyfill tip when failed to resolve node built-in modules. diff --git a/packages/core/src/server/socketServer.ts b/packages/core/src/server/socketServer.ts index c49ba8a960..718244a216 100644 --- a/packages/core/src/server/socketServer.ts +++ b/packages/core/src/server/socketServer.ts @@ -7,6 +7,7 @@ import { getAllStatsWarnings, getStatsOptions, } from '../helpers'; +import { formatStatsMessages } from '../helpers/format'; import { logger } from '../logger'; import type { DevConfig, Rspack } from '../types'; import { getCompilationId } from './helper'; @@ -284,19 +285,31 @@ export class SocketServer { }); if (stats.errorsCount) { + const errors = getAllStatsErrors(stats); + const { errors: formattedErrors } = formatStatsMessages({ + errors, + warnings: [], + }); return this.sockWrite({ type: 'errors', compilationId, - data: getAllStatsErrors(stats), + data: formattedErrors, }); } + if (stats.warningsCount) { + const warnings = getAllStatsWarnings(stats); + const { warnings: formattedWarnings } = formatStatsMessages({ + warnings, + errors: [], + }); return this.sockWrite({ type: 'warnings', compilationId, - data: getAllStatsWarnings(stats), + data: formattedWarnings, }); } + return this.sockWrite({ type: 'ok', compilationId,