Skip to content

Commit

Permalink
perf: format stats errors in the Node side
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan committed Dec 24, 2024
1 parent e4c15ec commit 1110cb1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
25 changes: 7 additions & 18 deletions packages/core/src/client/hmr.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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);
}
}

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/core/src/helpers/stats.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
17 changes: 15 additions & 2 deletions packages/core/src/server/socketServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 1110cb1

Please sign in to comment.