@@ -5,7 +5,7 @@ import { isSatisfyRspackVersion, rspackMinVersion } from '../helpers/version';
55import { registerDevHook } from '../hooks' ;
66import { logger } from '../logger' ;
77import { rspack } from '../rspack' ;
8- import type { InternalContext , RsbuildStatsItem , Rspack } from '../types' ;
8+ import type { InternalContext , Rspack } from '../types' ;
99import { 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 ) ;
0 commit comments