source: imaps-frontend/node_modules/@parcel/watcher/README.md

main
Last change on this file was 0c6b92a, checked in by stefan toskovski <stefantoska84@…>, 5 weeks ago

Pred finalna verzija

  • Property mode set to 100644
File size: 7.5 KB
Line 
1# @parcel/watcher
2
3A native C++ Node module for querying and subscribing to filesystem events. Used by [Parcel 2](https://github.com/parcel-bundler/parcel).
4
5## Features
6
7- **Watch** - subscribe to realtime recursive directory change notifications when files or directories are created, updated, or deleted.
8- **Query** - performantly query for historical change events in a directory, even when your program is not running.
9- **Native** - implemented in C++ for performance and low-level integration with the operating system.
10- **Cross platform** - includes backends for macOS, Linux, Windows, FreeBSD, and Watchman.
11- **Performant** - events are throttled in C++ so the JavaScript thread is not overwhelmed during large filesystem changes (e.g. `git checkout` or `npm install`).
12- **Scalable** - tens of thousands of files can be watched or queried at once with good performance.
13
14## Example
15
16```javascript
17const watcher = require('@parcel/watcher');
18const path = require('path');
19
20// Subscribe to events
21let subscription = await watcher.subscribe(process.cwd(), (err, events) => {
22 console.log(events);
23});
24
25// later on...
26await subscription.unsubscribe();
27
28// Get events since some saved snapshot in the past
29let snapshotPath = path.join(process.cwd(), 'snapshot.txt');
30let events = await watcher.getEventsSince(process.cwd(), snapshotPath);
31
32// Save a snapshot for later
33await watcher.writeSnapshot(process.cwd(), snapshotPath);
34```
35
36## Watching
37
38`@parcel/watcher` supports subscribing to realtime notifications of changes in a directory. It works recursively, so changes in sub-directories will also be emitted.
39
40Events are throttled and coalesced for performance during large changes like `git checkout` or `npm install`, and a single notification will be emitted with all of the events at the end.
41
42Only one notification will be emitted per file. For example, if a file was both created and updated since the last event, you'll get only a `create` event. If a file is both created and deleted, you will not be notifed of that file. Renames cause two events: a `delete` for the old name, and a `create` for the new name.
43
44```javascript
45let subscription = await watcher.subscribe(process.cwd(), (err, events) => {
46 console.log(events);
47});
48```
49
50Events have two properties:
51
52- `type` - the event type: `create`, `update`, or `delete`.
53- `path` - the absolute path to the file or directory.
54
55To unsubscribe from change notifications, call the `unsubscribe` method on the returned subscription object.
56
57```javascript
58await subscription.unsubscribe();
59```
60
61`@parcel/watcher` has the following watcher backends, listed in priority order:
62
63- [FSEvents](https://developer.apple.com/documentation/coreservices/file_system_events) on macOS
64- [Watchman](https://facebook.github.io/watchman/) if installed
65- [inotify](http://man7.org/linux/man-pages/man7/inotify.7.html) on Linux
66- [ReadDirectoryChangesW](https://msdn.microsoft.com/en-us/library/windows/desktop/aa365465%28v%3Dvs.85%29.aspx) on Windows
67- [kqueue](https://man.freebsd.org/cgi/man.cgi?kqueue) on FreeBSD, or as an alternative to FSEvents on macOS
68
69You can specify the exact backend you wish to use by passing the `backend` option. If that backend is not available on the current platform, the default backend will be used instead. See below for the list of backend names that can be passed to the options.
70
71## Querying
72
73`@parcel/watcher` also supports querying for historical changes made in a directory, even when your program is not running. This makes it easy to invalidate a cache and re-build only the files that have changed, for example. It can be **significantly** faster than traversing the entire filesystem to determine what files changed, depending on the platform.
74
75In order to query for historical changes, you first need a previous snapshot to compare to. This can be saved to a file with the `writeSnapshot` function, e.g. just before your program exits.
76
77```javascript
78await watcher.writeSnapshot(dirPath, snapshotPath);
79```
80
81When your program starts up, you can query for changes that have occurred since that snapshot using the `getEventsSince` function.
82
83```javascript
84let events = await watcher.getEventsSince(dirPath, snapshotPath);
85```
86
87The events returned are exactly the same as the events that would be passed to the `subscribe` callback (see above).
88
89`@parcel/watcher` has the following watcher backends, listed in priority order:
90
91- [FSEvents](https://developer.apple.com/documentation/coreservices/file_system_events) on macOS
92- [Watchman](https://facebook.github.io/watchman/) if installed
93- [fts](http://man7.org/linux/man-pages/man3/fts.3.html) (brute force) on Linux and FreeBSD
94- [FindFirstFile](https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-findfirstfilea) (brute force) on Windows
95
96The FSEvents (macOS) and Watchman backends are significantly more performant than the brute force backends used by default on Linux and Windows, for example returning results in miliseconds instead of seconds for large directory trees. This is because a background daemon monitoring filesystem changes on those platforms allows us to query cached data rather than traversing the filesystem manually (brute force).
97
98macOS has good performance with FSEvents by default. For the best performance on other platforms, install [Watchman](https://facebook.github.io/watchman/) and it will be used by `@parcel/watcher` automatically.
99
100You can specify the exact backend you wish to use by passing the `backend` option. If that backend is not available on the current platform, the default backend will be used instead. See below for the list of backend names that can be passed to the options.
101
102## Options
103
104All of the APIs in `@parcel/watcher` support the following options, which are passed as an object as the last function argument.
105
106- `ignore` - an array of paths or glob patterns to ignore. uses [`is-glob`](https://github.com/micromatch/is-glob) to distinguish paths from globs. glob patterns are parsed with [`micromatch`](https://github.com/micromatch/micromatch) (see [features](https://github.com/micromatch/micromatch#matching-features)).
107 - paths can be relative or absolute and can either be files or directories. No events will be emitted about these files or directories or their children.
108 - glob patterns match on relative paths from the root that is watched. No events will be emitted for matching paths.
109- `backend` - the name of an explicitly chosen backend to use. Allowed options are `"fs-events"`, `"watchman"`, `"inotify"`, `"kqueue"`, `"windows"`, or `"brute-force"` (only for querying). If the specified backend is not available on the current platform, the default backend will be used instead.
110
111## WASM
112
113The `@parcel/watcher-wasm` package can be used in place of `@parcel/watcher` on unsupported platforms. It relies on the Node `fs` module, so in non-Node environments such as browsers, an `fs` polyfill will be needed.
114
115**Note**: the WASM implementation is significantly less efficient than the native implementations because it must crawl the file system to watch each directory individually. Use the native `@parcel/watcher` package wherever possible.
116
117```js
118import {subscribe} from '@parcel/watcher-wasm';
119
120// Use the module as documented above.
121subscribe(/* ... */);
122```
123
124## Who is using this?
125
126- [Parcel 2](https://parceljs.org/)
127- [VSCode](https://code.visualstudio.com/updates/v1_62#_file-watching-changes)
128- [Tailwind CSS Intellisense](https://github.com/tailwindlabs/tailwindcss-intellisense)
129- [Gatsby Cloud](https://twitter.com/chatsidhartha/status/1435647412828196867)
130- [Nx](https://nx.dev)
131- [Nuxt](https://nuxt.com)
132
133## License
134
135MIT
Note: See TracBrowser for help on using the repository browser.