1
1
import type { RolldownBuild , RolldownOptions } from 'rolldown'
2
2
import type { Update } from 'types/hmrPayload'
3
3
import colors from 'picocolors'
4
+ import type { FSWatcher } from 'chokidar'
4
5
import {
5
6
ChunkMetadataMap ,
6
7
clearLine ,
@@ -11,7 +12,7 @@ import { getHmrImplementation } from '../../plugins/clientInjections'
11
12
import { DevEnvironment , type DevEnvironmentContext } from '../environment'
12
13
import type { ResolvedConfig } from '../../config'
13
14
import type { ViteDevServer } from '../../server'
14
- import { arraify , createDebugger } from '../../utils'
15
+ import { arraify , createDebugger , tryStatSync } from '../../utils'
15
16
import { prepareError } from '../middlewares/error'
16
17
import { getShortName } from '../hmr'
17
18
@@ -65,6 +66,7 @@ export class FullBundleDevEnvironment extends DevEnvironment {
65
66
private state : BundleState = { type : 'initial' }
66
67
private invalidateCalledModules = new Set < string > ( )
67
68
69
+ watcher ! : FSWatcher
68
70
watchFiles = new Set < string > ( )
69
71
memoryFiles = new MemoryFiles ( )
70
72
@@ -82,8 +84,9 @@ export class FullBundleDevEnvironment extends DevEnvironment {
82
84
super ( name , config , { ...context , disableDepsOptimizer : true } )
83
85
}
84
86
85
- override async listen ( _server : ViteDevServer ) : Promise < void > {
87
+ override async listen ( server : ViteDevServer ) : Promise < void > {
86
88
this . hot . listen ( )
89
+ this . watcher = server . watcher
87
90
88
91
debug ?.( 'INITIAL: setup bundle options' )
89
92
const rollupOptions = await this . getRolldownOptions ( )
@@ -368,8 +371,19 @@ export class FullBundleDevEnvironment extends DevEnvironment {
368
371
}
369
372
370
373
// 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
+ }
373
387
}
374
388
if ( signal . aborted ) return
375
389
const postGenerateTime = Date . now ( )
0 commit comments