watchpack high level API doesn't map directly to watchers. Instead a three level architecture ensures that for each directory only a single watcher exists.
The high level API requests DirectoryWatchers from a WatcherManager, which ensures that only a single DirectoryWatcher per directory is created.
A user-faced Watcher can be obtained from a DirectoryWatcher and provides a filtered view on the DirectoryWatcher.
Reference-counting is used on the DirectoryWatcher and Watcher to decide when to close them.
The real watchers are created by the DirectoryWatcher.
Files are never watched directly. This should keep the watcher count low.
Watching can be started in the past. This way watching can start after file reading.
Symlinks are not followed, instead the symlink is watched.
API
const Watchpack = require("watchpack");const wp = new Watchpack({ // options: aggregateTimeout: 1000, // fire "aggregated" event when after a change for 1000ms no additional change occurred // aggregated defaults to undefined, which doesn't fire an "aggregated" event poll: true, // poll: true - use polling with the default interval // poll: 10000 - use polling with an interval of 10s // poll defaults to undefined, which prefer native watching methods // Note: enable polling when watching on a network path // When WATCHPACK_POLLING environment variable is set it will override this option followSymlinks: true, // true: follows symlinks and watches symlinks and real files // (This makes sense when symlinks has not been resolved yet, comes with a performance hit) // false (default): watches only specified item they may be real files or symlinks // (This makes sense when symlinks has already been resolved)
Environment variables
WATCHPACK_POLLING: when set, overrides the poll option (see above).
WATCHPACK_RETRIES: number of times to retry fs.lstat when it returns
EBUSY (default: 3). Useful on Windows where anti-virus scanners,
indexers or editors briefly lock files — without retries watchpack would
see a spurious remove and stop tracking the file. Set to 0 or
"false" to disable retrying.