@@ -3,13 +3,20 @@ import chokidar, { FSWatcher } from 'chokidar';
3
3
import { extname } from 'path' ;
4
4
import { Watcher , WatchFileSystem , WatchFileSystemOptions } from './WatchFileSystem' ;
5
5
6
+ const IGNORED_DIRS = [ 'node_modules' , '.git' , '.yarn' , '.pnp' ] ;
7
+
8
+ function isIgnored ( path : string ) {
9
+ return IGNORED_DIRS . some ( ( ignoredDir ) => path . includes ( `/${ ignoredDir } /` ) ) ;
10
+ }
11
+
6
12
class InclusiveNodeWatchFileSystem implements WatchFileSystem {
7
13
get watcher ( ) {
8
14
return this . watchFileSystem . watcher || this . watchFileSystem . wfs ?. watcher ;
9
15
}
10
16
11
17
readonly changedFiles : Set < string > ;
12
18
readonly removedFiles : Set < string > ;
19
+ readonly dirsWatchers : Map < string , FSWatcher | undefined > ;
13
20
14
21
constructor (
15
22
private watchFileSystem : WatchFileSystem ,
@@ -21,8 +28,6 @@ class InclusiveNodeWatchFileSystem implements WatchFileSystem {
21
28
}
22
29
23
30
private paused = true ;
24
- private fileWatcher : Watcher | undefined ;
25
- private dirsWatchers : Map < string , FSWatcher | undefined > ;
26
31
27
32
watch (
28
33
files : Iterable < string > ,
@@ -36,13 +41,8 @@ class InclusiveNodeWatchFileSystem implements WatchFileSystem {
36
41
this . changedFiles . clear ( ) ;
37
42
this . removedFiles . clear ( ) ;
38
43
39
- // cleanup old standard watchers
40
- if ( this . fileWatcher ) {
41
- this . fileWatcher . close ( ) ;
42
- }
43
-
44
44
// use standard watch file system for files and missing
45
- this . fileWatcher = this . watchFileSystem . watch (
45
+ const fileWatcher = this . watchFileSystem . watch (
46
46
files ,
47
47
[ ] ,
48
48
missing ,
@@ -53,19 +53,25 @@ class InclusiveNodeWatchFileSystem implements WatchFileSystem {
53
53
) ;
54
54
55
55
this . watcher ?. on ( 'change' , ( file : string ) => {
56
- this . changedFiles . add ( file ) ;
57
- this . removedFiles . delete ( file ) ;
56
+ if ( ! isIgnored ( file ) ) {
57
+ this . changedFiles . add ( file ) ;
58
+ this . removedFiles . delete ( file ) ;
59
+ }
58
60
} ) ;
59
61
this . watcher ?. on ( 'remove' , ( file : string ) => {
60
- this . removedFiles . add ( file ) ;
61
- this . changedFiles . delete ( file ) ;
62
+ if ( ! isIgnored ( file ) ) {
63
+ this . removedFiles . add ( file ) ;
64
+ this . changedFiles . delete ( file ) ;
65
+ }
62
66
} ) ;
63
67
64
68
// calculate what to change
65
69
const prevDirs = Array . from ( this . dirsWatchers . keys ( ) ) ;
66
70
const nextDirs = Array . from ( dirs ) ;
67
71
const dirsToUnwatch = prevDirs . filter ( ( prevDir ) => ! nextDirs . includes ( prevDir ) ) ;
68
- const dirsToWatch = nextDirs . filter ( ( nextDir ) => ! prevDirs . includes ( nextDir ) ) ;
72
+ const dirsToWatch = nextDirs . filter (
73
+ ( nextDir ) => ! prevDirs . includes ( nextDir ) && ! isIgnored ( nextDir )
74
+ ) ;
69
75
70
76
// update dirs watcher
71
77
dirsToUnwatch . forEach ( ( dirToUnwatch ) => {
@@ -78,7 +84,7 @@ class InclusiveNodeWatchFileSystem implements WatchFileSystem {
78
84
const dirWatcher = chokidar . watch ( dirToWatch , {
79
85
ignoreInitial : true ,
80
86
ignorePermissionErrors : true ,
81
- ignored : [ '**/node_modules/**' , '**/.git/**' ] ,
87
+ ignored : ( path : string ) => isIgnored ( path ) ,
82
88
usePolling : options ?. poll ? true : undefined ,
83
89
interval : interval ,
84
90
binaryInterval : interval ,
@@ -129,14 +135,13 @@ class InclusiveNodeWatchFileSystem implements WatchFileSystem {
129
135
this . paused = false ;
130
136
131
137
return {
132
- ...this . fileWatcher ,
138
+ ...fileWatcher ,
133
139
close : ( ) => {
134
140
this . changedFiles . clear ( ) ;
135
141
this . removedFiles . clear ( ) ;
136
142
137
- if ( this . fileWatcher ) {
138
- this . fileWatcher . close ( ) ;
139
- this . fileWatcher = undefined ;
143
+ if ( fileWatcher ) {
144
+ fileWatcher . close ( ) ;
140
145
}
141
146
this . dirsWatchers . forEach ( ( dirWatcher ) => {
142
147
dirWatcher ?. close ( ) ;
@@ -146,8 +151,8 @@ class InclusiveNodeWatchFileSystem implements WatchFileSystem {
146
151
this . paused = true ;
147
152
} ,
148
153
pause : ( ) => {
149
- if ( this . fileWatcher ) {
150
- this . fileWatcher . pause ( ) ;
154
+ if ( fileWatcher ) {
155
+ fileWatcher . pause ( ) ;
151
156
}
152
157
this . paused = true ;
153
158
} ,
0 commit comments