Skip to content

Commit 5dc3b07

Browse files
committed
Docs: Update gulp.watch API to align with glob-watcher
1 parent 0c66069 commit 5dc3b07

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

docs/API.md

+24-16
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* [gulp.lastRun](#gulplastruntaskname-timeresolution) - Get timestamp of last successful run
88
* [gulp.parallel](#gulpparalleltasks) - Run tasks in parallel
99
* [gulp.series](#gulpseriestasks) - Run tasks in series
10-
* [gulp.watch](#gulpwatchglob-opts-fn) - Do something when a file changes
10+
* [gulp.watch](#gulpwatchglobs-opts-fn) - Do something when a file changes
1111
* [gulp.tree](#gulptreeoptions) - Get the tree of tasks
1212
* [gulp.registry](#gulpregistryregistry) - Get or set the task registry
1313

@@ -504,28 +504,31 @@ Type: `Array`, `String` or `Function`
504504
A task name, a function or an array of either.
505505

506506

507-
### gulp.watch(glob[, opts][, fn])
507+
### gulp.watch(globs[, opts][, fn])
508508

509-
Watch files and do something when a file changes.
510-
File watching is provided by the [`chokidar`][chokidar] module.
511-
Please report any file watching problems directly to its
512-
[issue tracker](https://github.com/paulmillr/chokidar/issues).
509+
Takes a path string, an array of path strings, a [glob][node-glob] string or an array of [glob][node-glob] strings as `globs` to watch on the filesystem. Also optionally takes `options` to configure the watcher and a `fn` to execute when a file changes.
510+
511+
Returns an instance of [`chokidar`][chokidar].
513512

514513
```js
515-
gulp.watch('js/**/*.js', gulp.parallel('uglify', 'reload'));
514+
gulp.watch('js/**/*.js', gulp.parallel('concat', 'uglify'));
516515
```
517516

518517
In the example, `gulp.watch` runs the function returned by `gulp.parallel` each
519518
time a file with the `js` extension in `js/` is updated.
520519

521-
#### glob
520+
#### globs
522521
Type: `String` or `Array`
523522

524-
A single glob or array of globs that indicate which files to watch for changes.
523+
A path string, an array of path strings, a [glob][node-glob] string or an array of [glob][node-glob] strings that indicate which files to watch for changes.
525524

526525
#### opts
527526
Type: `Object`
528527

528+
* `delay` (milliseconds, default: `200`). The delay to wait before triggering the fn. Useful for waiting on many changes before doing the work on changed files, e.g. find-and-replace on many files.
529+
* `queue` (boolean, default: `true`). Whether or not a file change should queue the fn execution if the fn is already running. Useful for a long running fn.
530+
* `ignoreInitial` (boolean, default: `true`). If set to `false` the `fn` is called during [chokidar][chokidar] instantiation as it discovers the file paths. Useful if it is desirable to trigger the `fn` during startup. __Passed through to [chokidar][chokidar], but defaulted to `true` instead of `false`.__
531+
529532
Options that are passed to [`chokidar`][chokidar].
530533

531534
Commonly used options:
@@ -548,17 +551,21 @@ Read about the full set of options in [`chokidar`'s README][chokidar].
548551
#### fn
549552
Type: `Function`
550553

551-
An [async](#async-support) function to run when a file changes. Does not provide
552-
access to the `path` parameter.
554+
If the `fn` is passed, it will be called when the watcher emits a `change`, `add` or `unlink` event. It is automatically debounced with a default delay of 200 milliseconds and subsequent calls will be queued and called upon completion. These defaults can be changed using the `options`.
555+
556+
The `fn` is passed a single argument, `callback`, which is a function that must be called when work in the `fn` is complete. Instead of calling the `callback` function, [async completion][async-completion] can be signalled by:
557+
* Returning a `Stream` or `EventEmitter`
558+
* Returning a `Child Process`
559+
* Returning a `Promise`
560+
* Returning an `Observable`
561+
562+
Once async completion is signalled, if another run is queued, it will be executed.
553563

554-
`gulp.watch` returns a wrapped [chokidar] FSWatcher object. If provided,
555-
the callback will be triggered upon any `add`, `change`, or `unlink` event.
556-
Listeners can also be set directly for any of [chokidar]'s events, such as
557-
`addDir`, `unlinkDir`, and `error`. You must set listeners directly to get
564+
`gulp.watch` returns a wrapped [chokidar] FSWatcher object. Listeners can also be set directly for any of [chokidar]'s events, such as `addDir`, `unlinkDir`, and `error`. You must set listeners directly to get
558565
access to chokidar's callback parameters, such as `path`.
559566

560567
```js
561-
var watcher = gulp.watch('js/**/*.js', gulp.parallel('uglify', 'reload'));
568+
var watcher = gulp.watch('js/**/*.js', gulp.parallel('concat', 'uglify'));
562569
watcher.on('change', function(path, stats) {
563570
console.log('File ' + path + ' was changed');
564571
});
@@ -815,3 +822,4 @@ module.exports = new MyCompanyTasksRegistry();
815822
[vinyl File instance]: https://github.com/gulpjs/vinyl
816823
[Vinyl files]: https://github.com/gulpjs/vinyl-fs
817824
[fs stats]: https://nodejs.org/api/fs.html#fs_class_fs_stats
825+
[async-completion]: https://github.com/gulpjs/async-done#completion-and-error-resolution

0 commit comments

Comments
 (0)