fedd31504e1d675a30a4ebc402ba9ed7730f95d50f8416d558acab040a690f69aec3bf7652ea4002c62d0d1fec87fa190c2458ac12c45d301246a8c0c04673 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # watchpack
  2. Wrapper library for directory and file watching.
  3. [![Build Status](https://travis-ci.org/webpack/watchpack.svg?branch=master)](https://travis-ci.org/webpack/watchpack) [![Build status](https://ci.appveyor.com/api/projects/status/e5u2qvmugtv0r647/branch/master?svg=true)](https://ci.appveyor.com/project/sokra/watchpack/branch/master) [![Test coverage][coveralls-image]][coveralls-url]
  4. ## Concept
  5. 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.
  6. * The high level API requests `DirectoryWatchers` from a `WatcherManager`, which ensures that only a single `DirectoryWatcher` per directory is created.
  7. * A user-faced `Watcher` can be obtained from a `DirectoryWatcher` and provides a filtered view on the `DirectoryWatcher`.
  8. * Reference-counting is used on the `DirectoryWatcher` and `Watcher` to decide when to close them.
  9. * The real watchers (currently chokidar) are created by the `DirectoryWatcher`.
  10. * Files are never watched directly. This should keep the watcher count low.
  11. * Watching can be started in the past. This way watching can start after file reading.
  12. * Symlinks are not followed, instead the symlink is watched.
  13. ## API
  14. ``` javascript
  15. var Watchpack = require("watchpack");
  16. var wp = new Watchpack({
  17. // options:
  18. aggregateTimeout: 1000
  19. // fire "aggregated" event when after a change for 1000ms no additional change occurred
  20. // aggregated defaults to undefined, which doesn't fire an "aggregated" event
  21. poll: true
  22. // poll: true - use polling with the default interval
  23. // poll: 10000 - use polling with an interval of 10s
  24. // poll defaults to undefined, which prefer native watching methods
  25. // Note: enable polling when watching on a network path
  26. ignored: /node_modules/,
  27. // anymatch-compatible definition of files/paths to be ignored
  28. // see https://github.com/paulmillr/chokidar#path-filtering
  29. });
  30. // Watchpack.prototype.watch(string[] files, string[] directories, [number startTime])
  31. wp.watch(listOfFiles, listOfDirectories, Date.now() - 10000);
  32. // starts watching these files and directories
  33. // calling this again will override the files and directories
  34. wp.on("change", function(filePath, mtime) {
  35. // filePath: the changed file
  36. // mtime: last modified time for the changed file
  37. });
  38. wp.on("aggregated", function(changes) {
  39. // changes: an array of all changed files
  40. });
  41. // Watchpack.prototype.pause()
  42. wp.pause();
  43. // stops emitting events, but keeps watchers open
  44. // next "watch" call can reuse the watchers
  45. // Watchpack.prototype.close()
  46. wp.close();
  47. // stops emitting events and closes all watchers
  48. // Watchpack.prototype.getTimes()
  49. var fileTimes = wp.getTimes();
  50. // returns an object with all know change times for files
  51. // this include timestamps from files not directly watched
  52. // key: absolute path, value: timestamp as number
  53. ```
  54. [coveralls-url]: https://coveralls.io/r/webpack/watchpack/
  55. [coveralls-image]: https://img.shields.io/coveralls/webpack/watchpack.svg