Skip to content

Commit

Permalink
fix: avoid recursive rebuilds on webpack 5 (#532)
Browse files Browse the repository at this point in the history
Closes: #527
  • Loading branch information
piotr-oles committed Nov 15, 2020
1 parent ca4dce0 commit 8cd7304
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions src/watch/InclusiveNodeWatchFileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ import chokidar, { FSWatcher } from 'chokidar';
import { extname } from 'path';
import { Watcher, WatchFileSystem, WatchFileSystemOptions } from './WatchFileSystem';

const IGNORED_DIRS = ['node_modules', '.git', '.yarn', '.pnp'];

function isIgnored(path: string) {
return IGNORED_DIRS.some((ignoredDir) => path.includes(`/${ignoredDir}/`));
}

class InclusiveNodeWatchFileSystem implements WatchFileSystem {
get watcher() {
return this.watchFileSystem.watcher || this.watchFileSystem.wfs?.watcher;
}

readonly changedFiles: Set<string>;
readonly removedFiles: Set<string>;
readonly dirsWatchers: Map<string, FSWatcher | undefined>;

constructor(
private watchFileSystem: WatchFileSystem,
Expand All @@ -21,8 +28,6 @@ class InclusiveNodeWatchFileSystem implements WatchFileSystem {
}

private paused = true;
private fileWatcher: Watcher | undefined;
private dirsWatchers: Map<string, FSWatcher | undefined>;

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

// cleanup old standard watchers
if (this.fileWatcher) {
this.fileWatcher.close();
}

// use standard watch file system for files and missing
this.fileWatcher = this.watchFileSystem.watch(
const fileWatcher = this.watchFileSystem.watch(
files,
[],
missing,
Expand All @@ -53,19 +53,25 @@ class InclusiveNodeWatchFileSystem implements WatchFileSystem {
);

this.watcher?.on('change', (file: string) => {
this.changedFiles.add(file);
this.removedFiles.delete(file);
if (!isIgnored(file)) {
this.changedFiles.add(file);
this.removedFiles.delete(file);
}
});
this.watcher?.on('remove', (file: string) => {
this.removedFiles.add(file);
this.changedFiles.delete(file);
if (!isIgnored(file)) {
this.removedFiles.add(file);
this.changedFiles.delete(file);
}
});

// calculate what to change
const prevDirs = Array.from(this.dirsWatchers.keys());
const nextDirs = Array.from(dirs);
const dirsToUnwatch = prevDirs.filter((prevDir) => !nextDirs.includes(prevDir));
const dirsToWatch = nextDirs.filter((nextDir) => !prevDirs.includes(nextDir));
const dirsToWatch = nextDirs.filter(
(nextDir) => !prevDirs.includes(nextDir) && !isIgnored(nextDir)
);

// update dirs watcher
dirsToUnwatch.forEach((dirToUnwatch) => {
Expand All @@ -78,7 +84,7 @@ class InclusiveNodeWatchFileSystem implements WatchFileSystem {
const dirWatcher = chokidar.watch(dirToWatch, {
ignoreInitial: true,
ignorePermissionErrors: true,
ignored: ['**/node_modules/**', '**/.git/**'],
ignored: (path: string) => isIgnored(path),
usePolling: options?.poll ? true : undefined,
interval: interval,
binaryInterval: interval,
Expand Down Expand Up @@ -129,14 +135,13 @@ class InclusiveNodeWatchFileSystem implements WatchFileSystem {
this.paused = false;

return {
...this.fileWatcher,
...fileWatcher,
close: () => {
this.changedFiles.clear();
this.removedFiles.clear();

if (this.fileWatcher) {
this.fileWatcher.close();
this.fileWatcher = undefined;
if (fileWatcher) {
fileWatcher.close();
}
this.dirsWatchers.forEach((dirWatcher) => {
dirWatcher?.close();
Expand All @@ -146,8 +151,8 @@ class InclusiveNodeWatchFileSystem implements WatchFileSystem {
this.paused = true;
},
pause: () => {
if (this.fileWatcher) {
this.fileWatcher.pause();
if (fileWatcher) {
fileWatcher.pause();
}
this.paused = true;
},
Expand Down

0 comments on commit 8cd7304

Please sign in to comment.