Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c70cd01
commit 13012e4
Showing
6 changed files
with
298 additions
and
310 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import chokidar, { FSWatcher } from 'chokidar'; | ||
import { ChokidarOptions } from '../rollup/types'; | ||
import { Task } from './watch'; | ||
|
||
export class FileWatcher { | ||
private chokidarOptions: ChokidarOptions; | ||
private task: Task; | ||
private transformWatchers = new Map<string, FSWatcher>(); | ||
private watcher: FSWatcher; | ||
|
||
constructor(task: Task, chokidarOptions: ChokidarOptions) { | ||
this.chokidarOptions = chokidarOptions; | ||
this.task = task; | ||
this.watcher = this.createWatcher(null); | ||
} | ||
|
||
close() { | ||
this.watcher.close(); | ||
for (const watcher of this.transformWatchers.values()) { | ||
watcher.close(); | ||
} | ||
} | ||
|
||
unwatch(id: string) { | ||
this.watcher.unwatch(id); | ||
const transformWatcher = this.transformWatchers.get(id); | ||
if (transformWatcher) { | ||
this.transformWatchers.delete(id); | ||
transformWatcher.close(); | ||
} | ||
} | ||
|
||
watch(id: string, isTransformDependency: boolean) { | ||
if (isTransformDependency) { | ||
const watcher = this.transformWatchers.get(id) || this.createWatcher(id); | ||
watcher.add(id); | ||
this.transformWatchers.set(id, watcher); | ||
} else { | ||
this.watcher.add(id); | ||
} | ||
} | ||
|
||
private createWatcher(transformWatcherId: string | null): FSWatcher { | ||
const handleChange = transformWatcherId | ||
? () => { | ||
// unwatching and watching fixes an issue with chokidar where on certain systems, | ||
// a file that was unlinked and immediately recreated would create a change event | ||
// but then no longer any further events | ||
watcher.unwatch(transformWatcherId); | ||
watcher.add(transformWatcherId); | ||
this.task.invalidate(transformWatcherId, true); | ||
} | ||
: (id: string) => { | ||
watcher.unwatch(id); | ||
watcher.add(id); | ||
this.task.invalidate(id, false); | ||
}; | ||
const watcher = chokidar | ||
.watch([], this.chokidarOptions) | ||
.on('change', handleChange) | ||
.on('unlink', handleChange); | ||
return watcher; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.