Skip to content

Commit

Permalink
Fix: Add support for gulp.watch usage w/o opts or callback
Browse files Browse the repository at this point in the history
  • Loading branch information
jmm authored and phated committed Dec 31, 2017
1 parent 83f5632 commit 9fc4125
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
12 changes: 7 additions & 5 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ Type: `Array`, `String` or `Function`
A task name, a function or an array of either.


### gulp.watch(glob[, opts], fn)
### gulp.watch(glob[, opts][, fn])

Watch files and do something when a file changes.
File watching is provided by the [`chokidar`][chokidar] module.
Expand All @@ -515,7 +515,7 @@ Please report any file watching problems directly to its
gulp.watch('js/**/*.js', gulp.parallel('uglify', 'reload'));
```

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

#### glob
Expand Down Expand Up @@ -548,12 +548,14 @@ Read about the full set of options in [`chokidar`'s README][chokidar].
#### fn
Type: `Function`

An [async](#async-support) function to run when a file changes.
An [async](#async-support) function to run when a file changes. Does not provide
access to the `path` parameter.

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

```js
var watcher = gulp.watch('js/**/*.js', gulp.parallel('uglify', 'reload'));
Expand All @@ -569,7 +571,7 @@ watcher.on('unlink', function(path) {
##### path
Type: `String`

The relative path of the document.
Path to the file. If `opts.cwd` is set, `path` is relative to it.

##### stats
Type: `Object`
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Gulp.prototype.watch = function(glob, opt, task) {
opt = {};
}

opt = opt || {};

var fn;
if (typeof task === 'function') {
fn = this.parallel(task);
Expand Down
4 changes: 4 additions & 0 deletions test/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ describe('gulp', function() {
updateTempFile(tempFile);
});

it('should work without options or callback', function() {
gulp.watch('x');
});

it('should run many tasks: w/ options', function(done) {
var tempFile = path.join(outpath, 'watch-task-options.txt');
var a = 0;
Expand Down

0 comments on commit 9fc4125

Please sign in to comment.