Skip to content

Commit b37faa8

Browse files
chenjiahanCopilot
andauthored
feat: improve build time printing (#6607)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 38744e4 commit b37faa8

File tree

5 files changed

+33
-24
lines changed

5 files changed

+33
-24
lines changed

packages/core/src/createContext.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ export async function createContext(
215215
stats: null,
216216
status: 'idle',
217217
hasErrors: false,
218+
time: {},
218219
},
219220
};
220221
}

packages/core/src/helpers/stats.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,6 @@ function getStatsOptions(
9393
): Rspack.StatsOptions {
9494
let defaultOptions: Rspack.StatsOptions = {
9595
all: false,
96-
// for displaying the build time
97-
timings: true,
9896
// for displaying the build errors
9997
errors: true,
10098
// for displaying the build warnings

packages/core/src/provider/createCompiler.ts

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { isSatisfyRspackVersion, rspackMinVersion } from '../helpers/version';
55
import { registerDevHook } from '../hooks';
66
import { logger } from '../logger';
77
import { rspack } from '../rspack';
8-
import type { InternalContext, RsbuildStatsItem, Rspack } from '../types';
8+
import type { InternalContext, Rspack } from '../types';
99
import { type InitConfigsOptions, initConfigs } from './initConfigs';
1010

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

148+
let startTime: number | null = null;
149+
148150
compiler.hooks.run.tap(HOOK_NAME, () => {
151+
startTime = Date.now();
149152
context.buildState.status = 'building';
150153
});
151154

152155
compiler.hooks.watchRun.tap(HOOK_NAME, (compiler) => {
156+
startTime = Date.now();
153157
context.buildState.status = 'building';
154158
logRspackVersion();
155159

@@ -182,6 +186,28 @@ export async function createCompiler(options: InitConfigsOptions): Promise<{
182186
});
183187
}
184188

189+
const printTime = (index: number) => {
190+
if (startTime === null) {
191+
return;
192+
}
193+
194+
const { name } = context.environmentList[index];
195+
const time = Date.now() - startTime;
196+
context.buildState.time[name] = time;
197+
198+
// When using multiple compilers, print the name to distinguish different environments
199+
const suffix = isMultiCompiler ? color.dim(` (${name})`) : '';
200+
logger.ready(`built in ${prettyTime(time / 1000)}${suffix}`);
201+
};
202+
203+
if (isMultiCompiler) {
204+
(compiler as Rspack.MultiCompiler).compilers.forEach((item, index) => {
205+
item.hooks.done.tap(HOOK_NAME, () => {
206+
printTime(index);
207+
});
208+
});
209+
}
210+
185211
compiler.hooks.done.tap(
186212
HOOK_NAME,
187213
(statsInstance: Rspack.Stats | Rspack.MultiStats) => {
@@ -193,26 +219,8 @@ export async function createCompiler(options: InitConfigsOptions): Promise<{
193219
context.buildState.hasErrors = hasErrors;
194220
context.socketServer?.onBuildDone();
195221

196-
const printTime = (statsItem: RsbuildStatsItem, index: number) => {
197-
if (statsItem.time) {
198-
const time = prettyTime(statsItem.time / 1000);
199-
const { name } = rspackConfigs[index];
200-
201-
// When using multi compiler, print name to distinguish different compilers
202-
const suffix = name && isMultiCompiler ? color.dim(` (${name})`) : '';
203-
logger.ready(`built in ${time}${suffix}`);
204-
}
205-
};
206-
207-
if (!hasErrors) {
208-
// only print children compiler time when multi compiler
209-
if (isMultiCompiler && stats.children?.length) {
210-
stats.children.forEach((item, index) => {
211-
printTime(item, index);
212-
});
213-
} else {
214-
printTime(stats, 0);
215-
}
222+
if (!isMultiCompiler) {
223+
printTime(0);
216224
}
217225

218226
const { message, level } = formatStats(stats, hasErrors);

packages/core/src/types/context.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ export type BuildState = {
7878
status: BuildStatus;
7979
/** Whether there are build errors */
8080
hasErrors: boolean;
81+
/** The build time of each environment */
82+
time: Record<string, number>;
8183
};
8284

8385
/** The inner context. */

packages/core/src/types/rsbuild.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ export type RsbuildMode = 'development' | 'production' | 'none';
368368

369369
export type RsbuildStatsItem = Pick<
370370
Rspack.StatsCompilation,
371-
'errors' | 'warnings' | 'time' | 'entrypoints' | 'hash'
371+
'errors' | 'warnings' | 'entrypoints' | 'hash'
372372
>;
373373

374374
/**

0 commit comments

Comments
 (0)