@@ -71,6 +71,10 @@ export class BundledDev {
7171 } )
7272 } )
7373
74+ private fullReloadPending = false
75+
76+ private lastBuildError : Error | null = null
77+
7478 memoryFiles : MemoryFiles = new MemoryFiles ( )
7579
7680 constructor ( private environment : DevEnvironment ) {
@@ -108,6 +112,17 @@ export class BundledDev {
108112 this . clients . setupIfNeeded ( client , payload . clientId )
109113 this . devEngine . registerModules ( payload . clientId , payload . modules )
110114 } )
115+ this . environment . hot . on ( 'vite:client:connect' , ( _payload , client ) => {
116+ // Replay the cached build error to freshly connected clients.
117+ if ( this . lastBuildError ) {
118+ debug ?.( 'REPLAY: replaying last build error to newly connected client' )
119+
120+ client . send ( {
121+ type : 'error' ,
122+ err : prepareError ( this . lastBuildError ) ,
123+ } )
124+ }
125+ } )
111126 this . environment . hot . on ( 'vite:client:disconnect' , ( _payload , client ) => {
112127 const clientId = this . clients . delete ( client )
113128 if ( clientId ) {
@@ -151,12 +166,15 @@ export class BundledDev {
151166 error : result ,
152167 } ,
153168 )
169+ this . lastBuildError = result
170+ this . fullReloadPending = false
154171 this . environment . hot . send ( {
155172 type : 'error' ,
156173 err : prepareError ( result ) ,
157174 } )
158175 return
159176 }
177+ this . lastBuildError = null
160178
161179 // NOTE: don't clear memoryFiles here as incremental build reuses the files
162180 for ( const outputFile of result . output ) {
@@ -169,6 +187,12 @@ export class BundledDev {
169187 }
170188 } )
171189 }
190+
191+ // Trigger a full reload if there's no error in the result and a reload is pending from HMR.
192+ if ( this . fullReloadPending ) {
193+ this . fullReloadPending = false
194+ this . debouncedFullReload ( )
195+ }
172196 } ,
173197 watch : {
174198 skipWrite : true ,
@@ -192,9 +216,11 @@ export class BundledDev {
192216
193217 private async waitForInitialBuildFinish ( ) : Promise < void > {
194218 await this . devEngine . ensureCurrentBuildFinish ( )
195- while ( this . memoryFiles . size === 0 ) {
219+ let state = await this . devEngine . getBundleState ( )
220+ while ( this . memoryFiles . size === 0 && ! state . lastBuildErrored ) {
196221 await setTimeout ( 10 )
197222 await this . devEngine . ensureCurrentBuildFinish ( )
223+ state = await this . devEngine . getBundleState ( )
198224 }
199225 }
200226
@@ -254,6 +280,23 @@ export class BundledDev {
254280
255281 async triggerBundleRegenerationIfStale ( ) : Promise < boolean > {
256282 const bundleState = await this . devEngine . getBundleState ( )
283+
284+ // Trigger full build if the HMR errors,
285+ // this is to make it easier to recover if the HMR generation is broken for some reason.
286+ if (
287+ this . initialBuildCompleted &&
288+ bundleState . lastBuildErrored &&
289+ bundleState . lastErrorStage === 'Hmr'
290+ ) {
291+ debug ?.( `TRIGGER: access after HMR-stage failure, forcing full rebuild` )
292+
293+ this . devEngine . triggerFullBuild ( )
294+ this . devEngine . ensureLatestBuildOutput ( ) . then ( ( ) => {
295+ this . debouncedFullReload ( )
296+ } )
297+ return true
298+ }
299+
257300 const shouldTrigger =
258301 bundleState . hasStaleOutput &&
259302 ! bundleState . lastBuildErrored &&
@@ -348,9 +391,16 @@ export class BundledDev {
348391 colors . green ( `trigger page reload ` ) + colors . dim ( shortFile ) + reason ,
349392 { clear : ! invalidateInformation , timestamp : true } ,
350393 )
351- this . devEngine . ensureLatestBuildOutput ( ) . then ( ( ) => {
352- this . debouncedFullReload ( )
353- } )
394+ if ( invalidateInformation ) {
395+ // Invalidate does not get upgraded to `rebuild`,
396+ // so `onOutput` will not be triggered and thus the reload needs to be triggered here.
397+ this . devEngine . ensureLatestBuildOutput ( ) . then ( async ( ) => {
398+ this . debouncedFullReload ( )
399+ } )
400+ } else {
401+ // Use a flag to defer the reload until the `onOutput` callback to avoid error lay flashes.
402+ this . fullReloadPending = true
403+ }
354404 return
355405 }
356406
0 commit comments