Skip to content

Commit f3f2d9f

Browse files
janiceilenephated
authored andcommitted
Docs: Add "Watching Files" documentation
1 parent 233c3f9 commit f3f2d9f

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed
+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<!-- front-matter
2+
id: watching-files
3+
title: Watching Files
4+
hide_title: true
5+
sidebar_label: Watching Files
6+
-->
7+
8+
# Watching Files
9+
10+
The `watch()` API connects [globs][globs-docs] to [tasks][creating-tasks-docs] using a file system watcher. It watches for changes to files that match the globs and executes the task when a change occurs. If the task doesn't signal [Async Completion][async-completion-doc], it will never be run a second time.
11+
12+
This API provides built-in delay and queueing based on most-common-use defaults.
13+
14+
```js
15+
const { watch, series } = require('gulp');
16+
17+
function clean(cb) {
18+
// Body omitted
19+
cb();
20+
}
21+
22+
function javascript(cb) {
23+
// Body omitted
24+
cb();
25+
}
26+
27+
function css(cb) {
28+
// Body omitted
29+
cb();
30+
}
31+
32+
// You can use a single task
33+
watch('src/*.css', css);
34+
// Or a composed task
35+
watch('src/*.js', series(clean, javascript));
36+
```
37+
38+
## Warning: Avoid synchronous
39+
40+
A watcher's task cannot be synchronous, like tasks registered into the task system. If you pass a sync task, the completion can't be determined and the task won't run again - it is assumed to still be running.
41+
42+
There is no error or warning message provided because the file watcher keeps your Node process running. Since the process doesn't exit, it cannot be determined whether the task is done or just taking a really, really long time to run.
43+
44+
## Watched events
45+
46+
By default, the watcher executes tasks whenever a file is created, changed, or deleted.
47+
If you need to use different events, you can use the `events` option when calling `watch()`. The available events are `'add'`, `'addDir'`, `'change'`, `'unlink'`, `'unlinkDir'`, `'ready'`, `'error'`. Additionally `'all'` is available, which represents all events other than `'ready'` and `'error'`.
48+
49+
```js
50+
const { watch } = require('gulp');
51+
52+
// All events will be watched
53+
watch('src/*.js', { events: 'all' }, function(cb) {
54+
// Body omitted
55+
cb();
56+
});
57+
```
58+
59+
## Initial execution
60+
61+
Upon calling `watch()`, the tasks won't be executed, instead they'll wait for the first file change.
62+
63+
To execute tasks before the first file change, set the `ignoreInitial` option to `false`.
64+
65+
```js
66+
const { watch } = require('gulp');
67+
68+
// The task will be executed upon startup
69+
watch('src/*.js', { ignoreInitial: false }, function(cb) {
70+
// Body omitted
71+
cb();
72+
});
73+
```
74+
75+
## Queueing
76+
77+
Each `watch()` guarantees that its currently running task won't execute again concurrently. When a file change is made while a watcher task is running, another execution will queue up to run when the task finishes. Only one run can be queued up at a time.
78+
79+
To disable queueing, set the `queue` option to `false`.
80+
81+
```js
82+
const { watch } = require('gulp');
83+
84+
// The task will be run (concurrently) for every change made
85+
watch('src/*.js', { queue: false }, function(cb) {
86+
// Body omitted
87+
cb();
88+
});
89+
```
90+
91+
## Delay
92+
93+
Upon file change, a watcher task won't run until a 200ms delay has elapsed. This is to avoid starting a task too early when many files are being changed at once - like find-and-replace.
94+
95+
To adjust the delay duration, set the `delay` option to a positive integer.
96+
97+
```js
98+
const { watch } = require('gulp');
99+
100+
// The task won't be run until 500ms have elapsed since the first change
101+
watch('src/*.js', { delay: 500 }, function(cb) {
102+
// Body omitted
103+
cb();
104+
});
105+
```
106+
107+
## Using the watcher instance
108+
109+
You likely won't use this feature, but if you need full control over changed files - like access to paths or metadata - use the [chokidar][chokidar-module-package] instance returned from `watch()`.
110+
111+
__Be careful:__ The returned chokidar instance doesn't have queueing, delay, or async completion features.
112+
113+
## Optional dependency
114+
115+
Gulp has an optional dependency called [fsevents][fsevents-package], which is a Mac-specific file watcher. If you see an installation warning for fsevents - _"npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents"_ - it is not an issue.
116+
If fsevents installation is skipped, a fallback watcher will be used and any errors occurring in your gulpfile aren't related to this warning.
117+
118+
[globs-docs]: 6-explaining-globs.md
119+
[creating-tasks-docs]: 3-creating-tasks.md
120+
[async-completion-doc]: 4-async-completion.md
121+
[chokidar-module-package]: https://www.npmjs.com/package/chokidar
122+
[fsevents-package]: https://www.npmjs.com/package/fsevents

0 commit comments

Comments
 (0)