Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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;

// 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) => {
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