@@ -14,8 +14,7 @@ import { DevEnvironment, type DevEnvironmentContext } from '../environment'
1414import type { ResolvedConfig } from '../../config'
1515import type { ViteDevServer } from '../../server'
1616import { createDebugger } from '../../utils'
17- import { getShortName } from '../hmr'
18- import type { WebSocketClient } from '../ws'
17+ import { type NormalizedHotChannelClient , getShortName } from '../hmr'
1918import { prepareError } from '../middlewares/error'
2019
2120const debug = createDebugger ( 'vite:full-bundle-mode' )
@@ -64,7 +63,7 @@ export class FullBundleDevEnvironment extends DevEnvironment {
6463 private devEngine ! : DevEngine
6564 private clients = new Clients ( )
6665 private invalidateCalledModules = new Map <
67- /* clientId */ string ,
66+ NormalizedHotChannelClient ,
6867 Set < string >
6968 > ( )
7069 private debouncedFullReload = debounce ( 20 , ( ) => {
@@ -88,7 +87,7 @@ export class FullBundleDevEnvironment extends DevEnvironment {
8887 super ( name , config , { ...context , disableDepsOptimizer : true } )
8988 }
9089
91- override async listen ( server : ViteDevServer ) : Promise < void > {
90+ override async listen ( _server : ViteDevServer ) : Promise < void > {
9291 this . hot . listen ( )
9392
9493 debug ?.( 'INITIAL: setup bundle options' )
@@ -106,17 +105,17 @@ export class FullBundleDevEnvironment extends DevEnvironment {
106105 : rollupOptions . output
107106 ) !
108107
109- // TODO: use hot API
110- server . ws . on (
111- 'vite:module-loaded' ,
112- ( payload : { modules : string [ ] } , client : WebSocketClient ) => {
113- const clientId = this . clients . setupIfNeeded ( client , ( ) => {
114- this . devEngine . removeClient ( clientId )
115- } )
116- this . devEngine . registerModules ( clientId , payload . modules )
117- } ,
118- )
119- server . ws . on ( 'vite:invalidate' , ( payload , client : WebSocketClient ) => {
108+ this . hot . on ( 'vite:module-loaded' , ( payload , client ) => {
109+ const clientId = this . clients . setupIfNeeded ( client )
110+ this . devEngine . registerModules ( clientId , payload . modules )
111+ } )
112+ this . hot . on ( 'vite: client-disconnect' , ( _payload , client ) => {
113+ const clientId = this . clients . delete ( client )
114+ if ( clientId ) {
115+ this . devEngine . removeClient ( clientId )
116+ }
117+ } )
118+ this . hot . on ( 'vite:invalidate' , ( payload , client ) => {
120119 this . handleInvalidateModule ( client , payload )
121120 } )
122121
@@ -141,9 +140,11 @@ export class FullBundleDevEnvironment extends DevEnvironment {
141140 return
142141 }
143142 for ( const { clientId, update } of updates ) {
144- this . invalidateCalledModules . get ( clientId ) ?. clear ( )
145- const client = this . clients . get ( clientId ) !
146- this . handleHmrOutput ( client , changedFiles , update )
143+ const client = this . clients . get ( clientId )
144+ if ( client ) {
145+ this . invalidateCalledModules . get ( client ) ?. clear ( )
146+ this . handleHmrOutput ( client , changedFiles , update )
147+ }
147148 }
148149 } ,
149150 onOutput : ( result ) => {
@@ -203,18 +204,15 @@ export class FullBundleDevEnvironment extends DevEnvironment {
203204 }
204205
205206 private handleInvalidateModule (
206- client : WebSocketClient ,
207+ client : NormalizedHotChannelClient ,
207208 m : {
208209 path : string
209210 message ?: string
210211 firstInvalidatedBy : string
211212 } ,
212213 ) : void {
213214 ; ( async ( ) => {
214- const clientId = this . clients . getId ( client )
215- if ( ! clientId ) return
216-
217- const invalidateCalledModules = this . invalidateCalledModules . get ( clientId )
215+ const invalidateCalledModules = this . invalidateCalledModules . get ( client )
218216 if ( invalidateCalledModules ?. has ( m . path ) ) {
219217 debug ?.(
220218 `INVALIDATE: invalidate received from ${ m . path } , but ignored because it was already invalidated` ,
@@ -226,16 +224,18 @@ export class FullBundleDevEnvironment extends DevEnvironment {
226224 `INVALIDATE: invalidate received from ${ m . path } , re-triggering HMR` ,
227225 )
228226 if ( ! invalidateCalledModules ) {
229- this . invalidateCalledModules . set ( clientId , new Set ( [ ] ) )
227+ this . invalidateCalledModules . set ( client , new Set ( [ ] ) )
230228 }
231- this . invalidateCalledModules . get ( clientId ) ! . add ( m . path )
229+ this . invalidateCalledModules . get ( client ) ! . add ( m . path )
232230
233231 // TODO: how to handle errors?
234232 const _update = await this . devEngine . invalidate (
235233 m . path ,
236234 m . firstInvalidatedBy ,
237235 )
238- const update = _update . find ( ( u ) => u . clientId === clientId ) ?. update
236+ const update = _update . find (
237+ ( u ) => this . clients . get ( u . clientId ) === client ,
238+ ) ?. update
239239 if ( ! update ) return
240240
241241 if ( update . type === 'Patch' ) {
@@ -299,7 +299,7 @@ export class FullBundleDevEnvironment extends DevEnvironment {
299299 }
300300
301301 private handleHmrOutput (
302- client : WebSocketClient ,
302+ client : NormalizedHotChannelClient ,
303303 files : string [ ] ,
304304 hmrOutput : HmrOutput ,
305305 invalidateInformation ?: { firstInvalidatedBy : string } ,
@@ -357,41 +357,33 @@ export class FullBundleDevEnvironment extends DevEnvironment {
357357}
358358
359359class Clients {
360- private clientToId = new Map < WebSocketClient , string > ( )
361- private idToClient = new Map < string , WebSocketClient > ( )
360+ private clientToId = new Map < NormalizedHotChannelClient , string > ( )
361+ private idToClient = new Map < string , NormalizedHotChannelClient > ( )
362362
363- setupIfNeeded ( client : WebSocketClient , onClose ?: ( ) => void ) : string {
363+ setupIfNeeded ( client : NormalizedHotChannelClient ) : string {
364364 const id = this . clientToId . get ( client )
365365 if ( id ) return id
366366
367367 const newId = randomUUID ( )
368368 this . clientToId . set ( client , newId )
369369 this . idToClient . set ( newId , client )
370- client . socket . once ( 'close' , ( ) => {
371- this . clientToId . delete ( client )
372- this . idToClient . delete ( newId )
373- onClose ?.( )
374- } )
375370 return newId
376371 }
377372
378- get ( id : string ) : WebSocketClient | undefined {
373+ get ( id : string ) : NormalizedHotChannelClient | undefined {
379374 return this . idToClient . get ( id )
380375 }
381376
382- getId ( client : WebSocketClient ) : string | undefined {
383- return this . clientToId . get ( client )
384- }
385-
386- getAll ( ) : WebSocketClient [ ] {
377+ getAll ( ) : NormalizedHotChannelClient [ ] {
387378 return Array . from ( this . idToClient . values ( ) )
388379 }
389380
390- delete ( client : WebSocketClient ) : void {
381+ delete ( client : NormalizedHotChannelClient ) : string | undefined {
391382 const id = this . clientToId . get ( client )
392383 if ( id ) {
393384 this . clientToId . delete ( client )
394385 this . idToClient . delete ( id )
386+ return id
395387 }
396388 }
397389}
0 commit comments