1- import { randomUUID } from 'node:crypto'
21import { setTimeout } from 'node:timers/promises'
32import {
43 type BindingClientHmrUpdate ,
@@ -61,6 +60,7 @@ export class MemoryFiles {
6160
6261export class FullBundleDevEnvironment extends DevEnvironment {
6362 private devEngine ! : DevEngine
63+ private initialBuildCompleted = false
6464 private clients = new Clients ( )
6565 private invalidateCalledModules = new Map <
6666 NormalizedHotChannelClient ,
@@ -106,8 +106,8 @@ export class FullBundleDevEnvironment extends DevEnvironment {
106106 ) !
107107
108108 this . hot . on ( 'vite:module-loaded' , ( payload , client ) => {
109- const clientId = this . clients . setupIfNeeded ( client )
110- this . devEngine . registerModules ( clientId , payload . modules )
109+ this . clients . setupIfNeeded ( client , payload . clientId )
110+ this . devEngine . registerModules ( payload . clientId , payload . modules )
111111 } )
112112 this . hot . on ( 'vite:client:disconnect' , ( _payload , client ) => {
113113 const clientId = this . clients . delete ( client )
@@ -184,6 +184,7 @@ export class FullBundleDevEnvironment extends DevEnvironment {
184184 this . waitForInitialBuildFinish ( ) . then ( ( ) => {
185185 debug ?.( 'INITIAL: build done' )
186186 this . hot . send ( { type : 'full-reload' , path : '*' } )
187+ this . initialBuildCompleted = true
187188 } )
188189 }
189190
@@ -260,7 +261,9 @@ export class FullBundleDevEnvironment extends DevEnvironment {
260261 async triggerBundleRegenerationIfStale ( ) : Promise < boolean > {
261262 const bundleState = await this . devEngine . getBundleState ( )
262263 const shouldTrigger =
263- bundleState . hasStaleOutput && ! bundleState . lastFullBuildFailed
264+ bundleState . hasStaleOutput &&
265+ ! bundleState . lastFullBuildFailed &&
266+ this . initialBuildCompleted
264267 if ( shouldTrigger ) {
265268 this . devEngine . ensureLatestBuildOutput ( ) . then ( ( ) => {
266269 this . debouncedFullReload ( )
@@ -270,16 +273,34 @@ export class FullBundleDevEnvironment extends DevEnvironment {
270273 return shouldTrigger
271274 }
272275
276+ async triggerLazyBundling (
277+ moduleId : string | null ,
278+ clientId : string | null ,
279+ ) : Promise < string | undefined > {
280+ if ( ! moduleId || ! clientId ) {
281+ return
282+ }
283+ debug ?.(
284+ `TRIGGER-LAZY: trigger lazy bundling for module ${ moduleId } for client ${ clientId } ` ,
285+ )
286+ return await this . devEngine . compileEntry ( moduleId , clientId )
287+ }
288+
273289 override async close ( ) : Promise < void > {
274290 this . memoryFiles . clear ( )
275291 await Promise . all ( [ super . close ( ) , this . devEngine . close ( ) ] )
292+ this . initialBuildCompleted = false
276293 }
277294
278295 private async getRolldownOptions ( ) {
279296 const chunkMetadataMap = new ChunkMetadataMap ( )
280297 const rolldownOptions = resolveRolldownOptions ( this , chunkMetadataMap )
281298 rolldownOptions . experimental ??= { }
282299 rolldownOptions . experimental . devMode = {
300+ lazy : true ,
301+ ...( typeof rolldownOptions . experimental . devMode === 'object'
302+ ? rolldownOptions . experimental . devMode
303+ : { } ) ,
283304 implement : await getHmrImplementation ( this . getTopLevelConfig ( ) ) ,
284305 }
285306
@@ -382,14 +403,15 @@ class Clients {
382403 private clientToId = new Map < NormalizedHotChannelClient , string > ( )
383404 private idToClient = new Map < string , NormalizedHotChannelClient > ( )
384405
385- setupIfNeeded ( client : NormalizedHotChannelClient ) : string {
406+ setupIfNeeded ( client : NormalizedHotChannelClient , clientId : string ) {
386407 const id = this . clientToId . get ( client )
387- if ( id ) return id
388-
389- const newId = randomUUID ( )
390- this . clientToId . set ( client , newId )
391- this . idToClient . set ( newId , client )
392- return newId
408+ if ( id && id !== clientId ) {
409+ throw new Error (
410+ 'client ID conflict detected. Please restart the dev server.' ,
411+ )
412+ }
413+ this . clientToId . set ( client , clientId )
414+ this . idToClient . set ( clientId , client )
393415 }
394416
395417 get ( id : string ) : NormalizedHotChannelClient | undefined {
0 commit comments