@@ -88,6 +88,8 @@ export async function createCompiler(options: InitConfigsOptions): Promise<{
8888 rspackConfigs : Rspack . Configuration [ ] ;
8989} > {
9090 logger . debug ( 'creating compiler' ) ;
91+
92+ const HOOK_NAME = 'rsbuild:compiler' ;
9193 const { context } = options ;
9294 const { rspackConfigs } = await initConfigs ( options ) ;
9395
@@ -122,7 +124,7 @@ export async function createCompiler(options: InitConfigsOptions): Promise<{
122124 const lazyModules : Set < string > = new Set ( ) ;
123125
124126 // Collect lazy compiled modules from the infrastructure logs
125- compiler . hooks . infrastructureLog . tap ( 'rsbuild:compiling' , ( name , _ , args ) => {
127+ compiler . hooks . infrastructureLog . tap ( HOOK_NAME , ( name , _ , args ) => {
126128 const log = args [ 0 ] ;
127129 if (
128130 name === 'LazyCompilation' &&
@@ -144,7 +146,12 @@ export async function createCompiler(options: InitConfigsOptions): Promise<{
144146 }
145147 } ) ;
146148
147- compiler . hooks . watchRun . tap ( 'rsbuild:compiling' , ( compiler ) => {
149+ compiler . hooks . run . tap ( HOOK_NAME , ( ) => {
150+ context . buildState . status = 'building' ;
151+ } ) ;
152+
153+ compiler . hooks . watchRun . tap ( HOOK_NAME , ( compiler ) => {
154+ context . buildState . status = 'building' ;
148155 logRspackVersion ( ) ;
149156
150157 if ( ! isCompiling ) {
@@ -158,68 +165,72 @@ export async function createCompiler(options: InitConfigsOptions): Promise<{
158165 isCompiling = true ;
159166 } ) ;
160167
168+ compiler . hooks . invalid . tap ( HOOK_NAME , ( ) => {
169+ context . buildState . status = 'idle' ;
170+ context . buildState . hasErrors = false ;
171+ } ) ;
172+
161173 if ( context . action === 'build' ) {
162174 // When there are multiple compilers, we only need to print the start log once
163175 const firstCompiler = isMultiCompiler
164176 ? ( compiler as Rspack . MultiCompiler ) . compilers [ 0 ]
165177 : compiler ;
166- firstCompiler . hooks . run . tap ( 'rsbuild:run' , ( ) => {
178+
179+ firstCompiler . hooks . run . tap ( HOOK_NAME , ( ) => {
167180 logger . info ( 'build started...' ) ;
168181 logRspackVersion ( ) ;
169182 } ) ;
170183 }
171184
172- const done = ( stats : Rspack . Stats | Rspack . MultiStats ) => {
173- const statsOptions = getStatsOptions ( compiler ) ;
174- const statsJson = stats . toJson ( {
175- children : true ,
176- moduleTrace : true ,
177- // get the compilation time
178- timings : true ,
179- preset : 'errors-warnings' ,
180- ...statsOptions ,
181- } ) ;
185+ compiler . hooks . done . tap (
186+ HOOK_NAME ,
187+ ( stats : Rspack . Stats | Rspack . MultiStats ) => {
188+ const hasErrors = stats . hasErrors ( ) ;
189+ context . buildState . hasErrors = hasErrors ;
190+ context . buildState . status = 'done' ;
191+
192+ const statsOptions = getStatsOptions ( compiler ) ;
193+ const statsJson = stats . toJson ( {
194+ children : true ,
195+ moduleTrace : true ,
196+ // get the compilation time
197+ timings : true ,
198+ preset : 'errors-warnings' ,
199+ ...statsOptions ,
200+ } ) ;
201+
202+ const printTime = ( c : StatsCompilation , index : number ) => {
203+ if ( c . time ) {
204+ const time = prettyTime ( c . time / 1000 ) ;
205+ const { name } = rspackConfigs [ index ] ;
206+
207+ // When using multi compiler, print name to distinguish different compilers
208+ const suffix = name && isMultiCompiler ? color . dim ( ` (${ name } )` ) : '' ;
209+ logger . ready ( `built in ${ time } ${ suffix } ` ) ;
210+ }
211+ } ;
212+
213+ if ( ! hasErrors ) {
214+ // only print children compiler time when multi compiler
215+ if ( isMultiCompiler && statsJson . children ?. length ) {
216+ statsJson . children . forEach ( ( c , index ) => {
217+ printTime ( c , index ) ;
218+ } ) ;
219+ } else {
220+ printTime ( statsJson , 0 ) ;
221+ }
222+ }
182223
183- const printTime = ( c : StatsCompilation , index : number ) => {
184- if ( c . time ) {
185- const time = prettyTime ( c . time / 1000 ) ;
186- const { name } = rspackConfigs [ index ] ;
224+ const { message, level } = formatStats ( statsJson , hasErrors ) ;
187225
188- // When using multi compiler, print name to distinguish different compilers
189- const suffix = name && isMultiCompiler ? color . dim ( ` (${ name } )` ) : '' ;
190- logger . ready ( `built in ${ time } ${ suffix } ` ) ;
226+ if ( level === 'error' ) {
227+ logger . error ( message ) ;
191228 }
192- } ;
193-
194- const hasErrors = stats . hasErrors ( ) ;
195-
196- if ( ! hasErrors ) {
197- // only print children compiler time when multi compiler
198- if ( isMultiCompiler && statsJson . children ?. length ) {
199- statsJson . children . forEach ( ( c , index ) => {
200- printTime ( c , index ) ;
201- } ) ;
202- } else {
203- printTime ( statsJson , 0 ) ;
229+ if ( level === 'warning' ) {
230+ logger . warn ( message ) ;
204231 }
205- }
206-
207- const { message, level } = formatStats ( statsJson , hasErrors ) ;
208-
209- if ( level === 'error' ) {
210- logger . error ( message ) ;
211- }
212- if ( level === 'warning' ) {
213- logger . warn ( message ) ;
214- }
215232
216- isCompiling = false ;
217- } ;
218-
219- compiler . hooks . done . tap (
220- 'rsbuild:done' ,
221- ( stats : Rspack . Stats | Rspack . MultiStats ) => {
222- done ( stats ) ;
233+ isCompiling = false ;
223234 } ,
224235 ) ;
225236
0 commit comments