[6a3a178] | 1 | # watchpack
|
---|
| 2 |
|
---|
| 3 | Wrapper library for directory and file watching.
|
---|
| 4 |
|
---|
| 5 | [![Build Status][build-status]][build-status-url]
|
---|
| 6 | [![Build status][build-status-veyor]][build-status-veyor-url]
|
---|
| 7 | [![Test coverage][coveralls-image]][coveralls-url]
|
---|
| 8 | [![codecov][codecov]][codecov-url]
|
---|
| 9 | [![downloads][downloads]][downloads-url]
|
---|
| 10 | [![Github contributors][contributors]][contributors-url]
|
---|
| 11 |
|
---|
| 12 | ## Concept
|
---|
| 13 |
|
---|
| 14 | 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.
|
---|
| 15 |
|
---|
| 16 | - The high level API requests `DirectoryWatchers` from a `WatcherManager`, which ensures that only a single `DirectoryWatcher` per directory is created.
|
---|
| 17 | - A user-faced `Watcher` can be obtained from a `DirectoryWatcher` and provides a filtered view on the `DirectoryWatcher`.
|
---|
| 18 | - Reference-counting is used on the `DirectoryWatcher` and `Watcher` to decide when to close them.
|
---|
| 19 | - The real watchers are created by the `DirectoryWatcher`.
|
---|
| 20 | - Files are never watched directly. This should keep the watcher count low.
|
---|
| 21 | - Watching can be started in the past. This way watching can start after file reading.
|
---|
| 22 | - Symlinks are not followed, instead the symlink is watched.
|
---|
| 23 |
|
---|
| 24 | ## API
|
---|
| 25 |
|
---|
| 26 | ```javascript
|
---|
| 27 | var Watchpack = require("watchpack");
|
---|
| 28 |
|
---|
| 29 | var wp = new Watchpack({
|
---|
| 30 | // options:
|
---|
| 31 | aggregateTimeout: 1000,
|
---|
| 32 | // fire "aggregated" event when after a change for 1000ms no additional change occurred
|
---|
| 33 | // aggregated defaults to undefined, which doesn't fire an "aggregated" event
|
---|
| 34 |
|
---|
| 35 | poll: true,
|
---|
| 36 | // poll: true - use polling with the default interval
|
---|
| 37 | // poll: 10000 - use polling with an interval of 10s
|
---|
| 38 | // poll defaults to undefined, which prefer native watching methods
|
---|
| 39 | // Note: enable polling when watching on a network path
|
---|
| 40 | // When WATCHPACK_POLLING environment variable is set it will override this option
|
---|
| 41 |
|
---|
| 42 | followSymlinks: true,
|
---|
| 43 | // true: follows symlinks and watches symlinks and real files
|
---|
| 44 | // (This makes sense when symlinks has not been resolved yet, comes with a performance hit)
|
---|
| 45 | // false (default): watches only specified item they may be real files or symlinks
|
---|
| 46 | // (This makes sense when symlinks has already been resolved)
|
---|
| 47 |
|
---|
| 48 | ignored: "**/.git"
|
---|
| 49 | // ignored: "string" - a glob pattern for files or folders that should not be watched
|
---|
| 50 | // ignored: ["string", "string"] - multiple glob patterns that should be ignored
|
---|
| 51 | // ignored: /regexp/ - a regular expression for files or folders that should not be watched
|
---|
| 52 | // All subdirectories are ignored too
|
---|
| 53 | });
|
---|
| 54 |
|
---|
| 55 | // Watchpack.prototype.watch({
|
---|
| 56 | // files: Iterable<string>,
|
---|
| 57 | // directories: Iterable<string>,
|
---|
| 58 | // missing: Iterable<string>,
|
---|
| 59 | // startTime?: number
|
---|
| 60 | // })
|
---|
| 61 | wp.watch({
|
---|
| 62 | files: listOfFiles,
|
---|
| 63 | directories: listOfDirectories,
|
---|
| 64 | missing: listOfNotExistingItems,
|
---|
| 65 | startTime: Date.now() - 10000
|
---|
| 66 | });
|
---|
| 67 | // starts watching these files and directories
|
---|
| 68 | // calling this again will override the files and directories
|
---|
| 69 | // files: can be files or directories, for files: content and existence changes are tracked
|
---|
| 70 | // for directories: only existence and timestamp changes are tracked
|
---|
| 71 | // directories: only directories, directory content (and content of children, ...) and
|
---|
| 72 | // existence changes are tracked.
|
---|
| 73 | // assumed to exist, when directory is not found without further information a remove event is emitted
|
---|
| 74 | // missing: can be files or directorees,
|
---|
| 75 | // only existence changes are tracked
|
---|
| 76 | // expected to not exist, no remove event is emitted when not found initially
|
---|
| 77 | // files and directories are assumed to exist, when they are not found without further information a remove event is emitted
|
---|
| 78 | // missing is assumed to not exist and no remove event is emitted
|
---|
| 79 |
|
---|
| 80 | wp.on("change", function(filePath, mtime, explanation) {
|
---|
| 81 | // filePath: the changed file
|
---|
| 82 | // mtime: last modified time for the changed file
|
---|
| 83 | // explanation: textual information how this change was detected
|
---|
| 84 | });
|
---|
| 85 |
|
---|
| 86 | wp.on("remove", function(filePath, explanation) {
|
---|
| 87 | // filePath: the removed file or directory
|
---|
| 88 | // explanation: textual information how this change was detected
|
---|
| 89 | });
|
---|
| 90 |
|
---|
| 91 | wp.on("aggregated", function(changes, removals) {
|
---|
| 92 | // changes: a Set of all changed files
|
---|
| 93 | // removals: a Set of all removed files
|
---|
| 94 | // watchpack gives up ownership on these Sets.
|
---|
| 95 | });
|
---|
| 96 |
|
---|
| 97 | // Watchpack.prototype.pause()
|
---|
| 98 | wp.pause();
|
---|
| 99 | // stops emitting events, but keeps watchers open
|
---|
| 100 | // next "watch" call can reuse the watchers
|
---|
| 101 | // The watcher will keep aggregating events
|
---|
| 102 | // which can be received with getAggregated()
|
---|
| 103 |
|
---|
| 104 | // Watchpack.prototype.close()
|
---|
| 105 | wp.close();
|
---|
| 106 | // stops emitting events and closes all watchers
|
---|
| 107 |
|
---|
| 108 | // Watchpack.prototype.getAggregated(): { changes: Set<string>, removals: Set<string> }
|
---|
| 109 | const { changes, removals } = wp.getAggregated();
|
---|
| 110 | // returns the current aggregated info and removes that from the watcher
|
---|
| 111 | // The next aggregated event won't include that info and will only emitted
|
---|
| 112 | // when futher changes happen
|
---|
| 113 | // Can also be used when paused.
|
---|
| 114 |
|
---|
| 115 | // Watchpack.prototype.getTimeInfoEntries()
|
---|
| 116 | var fileTimes = wp.getTimeInfoEntries();
|
---|
| 117 | // returns a Map with all known time info objects for files and directories
|
---|
| 118 | // this include info from files not directly watched
|
---|
| 119 | // key: absolute path, value: object with { safeTime, timestamp }
|
---|
| 120 | // safeTime: a point in time at which it is safe to say all changes happened before that
|
---|
| 121 | // timestamp: only for files, the mtime timestamp of the file
|
---|
| 122 |
|
---|
| 123 | // (deprecated)
|
---|
| 124 | // Watchpack.prototype.getTimes()
|
---|
| 125 | var fileTimes = wp.getTimes();
|
---|
| 126 | // returns an object with all known change times for files
|
---|
| 127 | // this include timestamps from files not directly watched
|
---|
| 128 | // key: absolute path, value: timestamp as number
|
---|
| 129 | ```
|
---|
| 130 |
|
---|
| 131 | [build-status]: https://travis-ci.org/webpack/watchpack.svg?branch=master
|
---|
| 132 | [build-status-url]: https://travis-ci.org/webpack/watchpack
|
---|
| 133 | [build-status-veyor]: https://ci.appveyor.com/api/projects/status/e5u2qvmugtv0r647/branch/master?svg=true
|
---|
| 134 | [build-status-veyor-url]: https://ci.appveyor.com/project/sokra/watchpack/branch/master
|
---|
| 135 | [coveralls-url]: https://coveralls.io/r/webpack/watchpack/
|
---|
| 136 | [coveralls-image]: https://img.shields.io/coveralls/webpack/watchpack.svg
|
---|
| 137 | [codecov]: https://codecov.io/gh/webpack/watchpack/branch/master/graph/badge.svg
|
---|
| 138 | [codecov-url]: https://codecov.io/gh/webpack/watchpack
|
---|
| 139 | [downloads]: https://img.shields.io/npm/dm/watchpack.svg
|
---|
| 140 | [downloads-url]: https://www.npmjs.com/package/watchpack
|
---|
| 141 | [contributors]: https://img.shields.io/github/contributors/webpack/watchpack.svg
|
---|
| 142 | [contributors-url]: https://github.com/webpack/watchpack/graphs/contributors
|
---|