diff --git a/packages/core/src/createContext.ts b/packages/core/src/createContext.ts index a927d3a8c2..faa45cf53e 100644 --- a/packages/core/src/createContext.ts +++ b/packages/core/src/createContext.ts @@ -215,6 +215,7 @@ export async function createContext( stats: null, status: 'idle', hasErrors: false, + time: {}, }, }; } diff --git a/packages/core/src/helpers/stats.ts b/packages/core/src/helpers/stats.ts index 76b29854fb..8437edfb88 100644 --- a/packages/core/src/helpers/stats.ts +++ b/packages/core/src/helpers/stats.ts @@ -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 diff --git a/packages/core/src/provider/createCompiler.ts b/packages/core/src/provider/createCompiler.ts index f06db98dd2..56e0df99d8 100644 --- a/packages/core/src/provider/createCompiler.ts +++ b/packages/core/src/provider/createCompiler.ts @@ -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 @@ -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(); @@ -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; + + // When using multiple compilers, print the 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); + }); + }); + } + compiler.hooks.done.tap( HOOK_NAME, (statsInstance: Rspack.Stats | Rspack.MultiStats) => { @@ -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); diff --git a/packages/core/src/types/context.ts b/packages/core/src/types/context.ts index 3767d0e586..60a9e643d9 100644 --- a/packages/core/src/types/context.ts +++ b/packages/core/src/types/context.ts @@ -78,6 +78,8 @@ export type BuildState = { status: BuildStatus; /** Whether there are build errors */ hasErrors: boolean; + /** The build time of each environment */ + time: Record; }; /** The inner context. */ diff --git a/packages/core/src/types/rsbuild.ts b/packages/core/src/types/rsbuild.ts index 77cf038c96..13c288cc73 100644 --- a/packages/core/src/types/rsbuild.ts +++ b/packages/core/src/types/rsbuild.ts @@ -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' >; /**