Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/src/createContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ export async function createContext(
stats: null,
status: 'idle',
hasErrors: false,
time: {},
},
};
}
2 changes: 0 additions & 2 deletions packages/core/src/helpers/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ function getStatsOptions(
): Rspack.StatsOptions {
let defaultOptions: Rspack.StatsOptions = {
all: false,
// for displaying the build time
timings: true,
// for displaying the build errors
errors: true,
// for displaying the build warnings
Expand Down
50 changes: 29 additions & 21 deletions packages/core/src/provider/createCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { isSatisfyRspackVersion, rspackMinVersion } from '../helpers/version';
import { registerDevHook } from '../hooks';
import { logger } from '../logger';
import { rspack } from '../rspack';
import type { InternalContext, RsbuildStatsItem, Rspack } from '../types';
import type { InternalContext, Rspack } from '../types';
import { type InitConfigsOptions, initConfigs } from './initConfigs';

// keep the last 3 parts of the path to make logs clean
Expand Down Expand Up @@ -145,11 +145,15 @@ export async function createCompiler(options: InitConfigsOptions): Promise<{
}
});

let startTime: number | null = null;

compiler.hooks.run.tap(HOOK_NAME, () => {
startTime = Date.now();
context.buildState.status = 'building';
});

compiler.hooks.watchRun.tap(HOOK_NAME, (compiler) => {
startTime = Date.now();
context.buildState.status = 'building';
logRspackVersion();

Expand Down Expand Up @@ -182,6 +186,28 @@ export async function createCompiler(options: InitConfigsOptions): Promise<{
});
}

const printTime = (index: number) => {
if (startTime === null) {
return;
}

const { name } = context.environmentList[index];
const time = Date.now() - startTime;
context.buildState.time[name] = time;

// For multi compiler, print name to distinguish different environments
const suffix = isMultiCompiler ? color.dim(` (${name})`) : '';
logger.ready(`built in ${prettyTime(time / 1000)}${suffix}`);
};

if (isMultiCompiler) {
(compiler as Rspack.MultiCompiler).compilers.forEach((item, index) => {
item.hooks.done.tap(HOOK_NAME, () => {
printTime(index);
});
Comment on lines 189 to 207

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Avoid logging build time when the environment fails

The new printTime helper is invoked from both multi-compiler child hooks and the top-level done hook without checking whether the corresponding compiler finished with errors. Previously, timing information was printed only when hasErrors was false. With this change the CLI will emit logger.ready('built in …') messages even when a compilation fails, which presents the build as successful and stores a duration in context.buildState.time for environments that actually errored. This makes error runs harder to diagnose and exposes misleading telemetry. Consider checking stats.hasErrors() (available in the child done hook and from statsInstance in the single-compiler path) before logging or storing the time.

Useful? React with 👍 / 👎.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will improve the time logs when having build errors.

});
}

compiler.hooks.done.tap(
HOOK_NAME,
(statsInstance: Rspack.Stats | Rspack.MultiStats) => {
Expand All @@ -193,26 +219,8 @@ export async function createCompiler(options: InitConfigsOptions): Promise<{
context.buildState.hasErrors = hasErrors;
context.socketServer?.onBuildDone();

const printTime = (statsItem: RsbuildStatsItem, index: number) => {
if (statsItem.time) {
const time = prettyTime(statsItem.time / 1000);
const { name } = rspackConfigs[index];

// When using multi compiler, print name to distinguish different compilers
const suffix = name && isMultiCompiler ? color.dim(` (${name})`) : '';
logger.ready(`built in ${time}${suffix}`);
}
};

if (!hasErrors) {
// only print children compiler time when multi compiler
if (isMultiCompiler && stats.children?.length) {
stats.children.forEach((item, index) => {
printTime(item, index);
});
} else {
printTime(stats, 0);
}
if (!isMultiCompiler) {
printTime(0);
}

const { message, level } = formatStats(stats, hasErrors);
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/types/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ export type BuildState = {
status: BuildStatus;
/** Whether there are build errors */
hasErrors: boolean;
/** The build time of each environment */
time: Record<string, number>;
};

/** The inner context. */
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types/rsbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ export type RsbuildMode = 'development' | 'production' | 'none';

export type RsbuildStatsItem = Pick<
Rspack.StatsCompilation,
'errors' | 'warnings' | 'time' | 'entrypoints' | 'hash'
'errors' | 'warnings' | 'entrypoints' | 'hash'
>;

/**
Expand Down
Loading