1+ import isGlob from 'is-glob' ;
12import type { FSWatcher } from '../../compiled/chokidar/index.js' ;
23import { normalizePublicDirs } from '../config' ;
34import { castArray } from '../helpers' ;
45import type {
5- ChokidarWatchOptions ,
6+ ChokidarOptions ,
67 DevConfig ,
78 ServerConfig ,
89 WatchFiles ,
@@ -13,6 +14,7 @@ type WatchFilesOptions = {
1314 dev : DevConfig ;
1415 server : ServerConfig ;
1516 compileMiddlewareAPI ?: CompileMiddlewareAPI ;
17+ root : string ;
1618} ;
1719
1820export async function setupWatchFiles ( options : WatchFilesOptions ) : Promise <
@@ -21,17 +23,22 @@ export async function setupWatchFiles(options: WatchFilesOptions): Promise<
2123 }
2224 | undefined
2325> {
24- const { dev, server, compileMiddlewareAPI } = options ;
26+ const { dev, server, root , compileMiddlewareAPI } = options ;
2527
2628 const { hmr, liveReload } = dev ;
2729 if ( ( ! hmr && ! liveReload ) || ! compileMiddlewareAPI ) {
2830 return ;
2931 }
3032
31- const closeDevFilesWatcher = await watchDevFiles ( dev , compileMiddlewareAPI ) ;
33+ const closeDevFilesWatcher = await watchDevFiles (
34+ dev ,
35+ compileMiddlewareAPI ,
36+ root ,
37+ ) ;
3238 const serverFilesWatcher = await watchServerFiles (
3339 server ,
3440 compileMiddlewareAPI ,
41+ root ,
3542 ) ;
3643
3744 return {
@@ -47,6 +54,7 @@ export async function setupWatchFiles(options: WatchFilesOptions): Promise<
4754async function watchDevFiles (
4855 devConfig : DevConfig ,
4956 compileMiddlewareAPI : CompileMiddlewareAPI ,
57+ root : string ,
5058) {
5159 const { watchFiles } = devConfig ;
5260 if ( ! watchFiles ) {
@@ -57,7 +65,11 @@ async function watchDevFiles(
5765
5866 for ( const { paths, options, type } of castArray ( watchFiles ) ) {
5967 const watchOptions = prepareWatchOptions ( paths , options , type ) ;
60- const watcher = await startWatchFiles ( watchOptions , compileMiddlewareAPI ) ;
68+ const watcher = await startWatchFiles (
69+ watchOptions ,
70+ compileMiddlewareAPI ,
71+ root ,
72+ ) ;
6173 if ( watcher ) {
6274 watchers . push ( watcher ) ;
6375 }
@@ -73,6 +85,7 @@ async function watchDevFiles(
7385function watchServerFiles (
7486 serverConfig : ServerConfig ,
7587 compileMiddlewareAPI : CompileMiddlewareAPI ,
88+ root : string ,
7689) {
7790 const publicDirs = normalizePublicDirs ( serverConfig . publicDir ) ;
7891 if ( ! publicDirs . length ) {
@@ -88,12 +101,12 @@ function watchServerFiles(
88101 }
89102
90103 const watchOptions = prepareWatchOptions ( watchPaths ) ;
91- return startWatchFiles ( watchOptions , compileMiddlewareAPI ) ;
104+ return startWatchFiles ( watchOptions , compileMiddlewareAPI , root ) ;
92105}
93106
94107function prepareWatchOptions (
95108 paths : string | string [ ] ,
96- options : ChokidarWatchOptions = { } ,
109+ options : ChokidarOptions = { } ,
97110 type ?: WatchFiles [ 'type' ] ,
98111) {
99112 return {
@@ -103,20 +116,53 @@ function prepareWatchOptions(
103116 } ;
104117}
105118
119+ export async function createChokidar (
120+ pathOrGlobs : string [ ] ,
121+ root : string ,
122+ options : ChokidarOptions ,
123+ ) : Promise < FSWatcher > {
124+ const chokidar = await import ( '../../compiled/chokidar/index.js' ) ;
125+
126+ const watchFiles : Set < string > = new Set ( ) ;
127+
128+ const globPatterns = pathOrGlobs . filter ( ( pathOrGlob ) => {
129+ if ( isGlob ( pathOrGlob ) ) {
130+ return true ;
131+ }
132+ watchFiles . add ( pathOrGlob ) ;
133+ return false ;
134+ } ) ;
135+
136+ if ( globPatterns . length ) {
137+ const tinyglobby = await import ( '../../compiled/tinyglobby/index.js' ) ;
138+ // interop default to make both CJS and ESM work
139+ const { glob } = tinyglobby . default || tinyglobby ;
140+ const files = await glob ( globPatterns , {
141+ cwd : root ,
142+ absolute : true ,
143+ } ) ;
144+ for ( const file of files ) {
145+ watchFiles . add ( file ) ;
146+ }
147+ }
148+
149+ return chokidar . watch ( Array . from ( watchFiles ) , options ) ;
150+ }
151+
106152async function startWatchFiles (
107153 {
108154 paths,
109155 options,
110156 type = 'reload-page' ,
111157 } : ReturnType < typeof prepareWatchOptions > ,
112158 compileMiddlewareAPI : CompileMiddlewareAPI ,
159+ root : string ,
113160) {
114161 if ( type !== 'reload-page' ) {
115162 return ;
116163 }
117164
118- const chokidar = await import ( '../../compiled/chokidar/index.js' ) ;
119- const watcher = chokidar . watch ( paths , options ) ;
165+ const watcher = await createChokidar ( paths , root , options ) ;
120166
121167 watcher . on ( 'change' , ( ) => {
122168 compileMiddlewareAPI . sockWrite ( 'static-changed' ) ;
0 commit comments