11import type { RolldownBuild , RolldownOptions } from 'rolldown'
22import type { Update } from 'types/hmrPayload'
33import colors from 'picocolors'
4+ import type { FSWatcher } from 'chokidar'
45import {
56 ChunkMetadataMap ,
67 clearLine ,
@@ -11,7 +12,7 @@ import { getHmrImplementation } from '../../plugins/clientInjections'
1112import { DevEnvironment , type DevEnvironmentContext } from '../environment'
1213import type { ResolvedConfig } from '../../config'
1314import type { ViteDevServer } from '../../server'
14- import { arraify , createDebugger } from '../../utils'
15+ import { arraify , createDebugger , tryStatSync } from '../../utils'
1516import { prepareError } from '../middlewares/error'
1617import { getShortName } from '../hmr'
1718
@@ -65,6 +66,7 @@ export class FullBundleDevEnvironment extends DevEnvironment {
6566 private state : BundleState = { type : 'initial' }
6667 private invalidateCalledModules = new Set < string > ( )
6768
69+ watcher ! : FSWatcher
6870 watchFiles = new Set < string > ( )
6971 memoryFiles = new MemoryFiles ( )
7072
@@ -82,8 +84,9 @@ export class FullBundleDevEnvironment extends DevEnvironment {
8284 super ( name , config , { ...context , disableDepsOptimizer : true } )
8385 }
8486
85- override async listen ( _server : ViteDevServer ) : Promise < void > {
87+ override async listen ( server : ViteDevServer ) : Promise < void > {
8688 this . hot . listen ( )
89+ this . watcher = server . watcher
8790
8891 debug ?.( 'INITIAL: setup bundle options' )
8992 const rollupOptions = await this . getRolldownOptions ( )
@@ -368,8 +371,19 @@ export class FullBundleDevEnvironment extends DevEnvironment {
368371 }
369372
370373 // TODO: should this be done for hmr patch file generation?
371- for ( const file of await bundle . watchFiles ) {
372- this . watchFiles . add ( file )
374+ const bundleWatchFiles = new Set ( await bundle . watchFiles )
375+ for ( const file of this . watchFiles ) {
376+ if ( ! bundleWatchFiles . has ( file ) ) {
377+ this . watcher . unwatch ( file )
378+ }
379+ }
380+ for ( const file of bundleWatchFiles ) {
381+ if ( ! this . watchFiles . has ( file ) ) {
382+ if ( tryStatSync ( file ) ) {
383+ this . watcher . add ( file )
384+ }
385+ this . watchFiles . add ( file )
386+ }
373387 }
374388 if ( signal . aborted ) return
375389 const postGenerateTime = Date . now ( )
0 commit comments