Skip to content

Commit 8cd7304

Browse files
authored
fix: avoid recursive rebuilds on webpack 5 (#532)
Closes: #527
1 parent ca4dce0 commit 8cd7304

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

src/watch/InclusiveNodeWatchFileSystem.ts

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@ import chokidar, { FSWatcher } from 'chokidar';
33
import { extname } from 'path';
44
import { Watcher, WatchFileSystem, WatchFileSystemOptions } from './WatchFileSystem';
55

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+
612
class InclusiveNodeWatchFileSystem implements WatchFileSystem {
713
get watcher() {
814
return this.watchFileSystem.watcher || this.watchFileSystem.wfs?.watcher;
915
}
1016

1117
readonly changedFiles: Set<string>;
1218
readonly removedFiles: Set<string>;
19+
readonly dirsWatchers: Map<string, FSWatcher | undefined>;
1320

1421
constructor(
1522
private watchFileSystem: WatchFileSystem,
@@ -21,8 +28,6 @@ class InclusiveNodeWatchFileSystem implements WatchFileSystem {
2128
}
2229

2330
private paused = true;
24-
private fileWatcher: Watcher | undefined;
25-
private dirsWatchers: Map<string, FSWatcher | undefined>;
2631

2732
watch(
2833
files: Iterable<string>,
@@ -36,13 +41,8 @@ class InclusiveNodeWatchFileSystem implements WatchFileSystem {
3641
this.changedFiles.clear();
3742
this.removedFiles.clear();
3843

39-
// cleanup old standard watchers
40-
if (this.fileWatcher) {
41-
this.fileWatcher.close();
42-
}
43-
4444
// use standard watch file system for files and missing
45-
this.fileWatcher = this.watchFileSystem.watch(
45+
const fileWatcher = this.watchFileSystem.watch(
4646
files,
4747
[],
4848
missing,
@@ -53,19 +53,25 @@ class InclusiveNodeWatchFileSystem implements WatchFileSystem {
5353
);
5454

5555
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+
}
5860
});
5961
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+
}
6266
});
6367

6468
// calculate what to change
6569
const prevDirs = Array.from(this.dirsWatchers.keys());
6670
const nextDirs = Array.from(dirs);
6771
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+
);
6975

7076
// update dirs watcher
7177
dirsToUnwatch.forEach((dirToUnwatch) => {
@@ -78,7 +84,7 @@ class InclusiveNodeWatchFileSystem implements WatchFileSystem {
7884
const dirWatcher = chokidar.watch(dirToWatch, {
7985
ignoreInitial: true,
8086
ignorePermissionErrors: true,
81-
ignored: ['**/node_modules/**', '**/.git/**'],
87+
ignored: (path: string) => isIgnored(path),
8288
usePolling: options?.poll ? true : undefined,
8389
interval: interval,
8490
binaryInterval: interval,
@@ -129,14 +135,13 @@ class InclusiveNodeWatchFileSystem implements WatchFileSystem {
129135
this.paused = false;
130136

131137
return {
132-
...this.fileWatcher,
138+
...fileWatcher,
133139
close: () => {
134140
this.changedFiles.clear();
135141
this.removedFiles.clear();
136142

137-
if (this.fileWatcher) {
138-
this.fileWatcher.close();
139-
this.fileWatcher = undefined;
143+
if (fileWatcher) {
144+
fileWatcher.close();
140145
}
141146
this.dirsWatchers.forEach((dirWatcher) => {
142147
dirWatcher?.close();
@@ -146,8 +151,8 @@ class InclusiveNodeWatchFileSystem implements WatchFileSystem {
146151
this.paused = true;
147152
},
148153
pause: () => {
149-
if (this.fileWatcher) {
150-
this.fileWatcher.pause();
154+
if (fileWatcher) {
155+
fileWatcher.pause();
151156
}
152157
this.paused = true;
153158
},

0 commit comments

Comments
 (0)