@@ -4,17 +4,22 @@ import { logger } from '../utils/logger'
4
4
import type { ResolvedOptions } from '../options'
5
5
import type { FSWatcher } from 'chokidar'
6
6
7
- // TODO watch config files
7
+ const endsWithPackageJson = / [ \\ / ] p a c k a g e \. j s o n $ /
8
+
8
9
export async function watchBuild (
9
10
options : ResolvedOptions ,
11
+ configFile : string | undefined ,
10
12
rebuild : ( ) => void ,
13
+ restart : ( ) => void ,
11
14
) : Promise < FSWatcher > {
12
15
const { watch } = await import ( 'chokidar' )
13
16
const debouncedRebuild = debounce ( rebuild , 100 )
14
17
15
- const files =
16
- typeof options . watch === 'boolean' ? process . cwd ( ) : options . watch
17
- logger . info ( `Watching for changes in ${ toArray ( files ) . join ( ', ' ) } ` )
18
+ const files = toArray (
19
+ typeof options . watch === 'boolean' ? process . cwd ( ) : options . watch ,
20
+ )
21
+ logger . info ( `Watching for changes in ${ files . join ( ', ' ) } ` )
22
+ if ( configFile ) files . push ( configFile )
18
23
19
24
const watcher = watch ( files , {
20
25
ignoreInitial : true ,
@@ -25,7 +30,13 @@ export async function watchBuild(
25
30
} ,
26
31
} )
27
32
28
- watcher . on ( 'all' , ( type , file ) => {
33
+ watcher . on ( 'all' , ( type : string , file : string ) => {
34
+ if ( endsWithPackageJson . test ( file ) || configFile === file ) {
35
+ logger . info ( `Reload config: ${ file } ` )
36
+ restart ( )
37
+ return
38
+ }
39
+
29
40
logger . info ( `Change detected: ${ type } ${ file } ` )
30
41
debouncedRebuild ( )
31
42
} )
0 commit comments