From 9fc412590747b5e20b4c5b52f59446aaf84632f9 Mon Sep 17 00:00:00 2001 From: Jesse McCarthy Date: Tue, 26 Jan 2016 14:04:10 -0500 Subject: [PATCH] Fix: Add support for gulp.watch usage w/o opts or callback --- docs/API.md | 12 +++++++----- index.js | 2 ++ test/watch.js | 4 ++++ 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/docs/API.md b/docs/API.md index 201114b6d..e95cc52f7 100644 --- a/docs/API.md +++ b/docs/API.md @@ -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. @@ -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 @@ -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')); @@ -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` diff --git a/index.js b/index.js index e3b5b4fe4..2058ce0b6 100644 --- a/index.js +++ b/index.js @@ -35,6 +35,8 @@ Gulp.prototype.watch = function(glob, opt, task) { opt = {}; } + opt = opt || {}; + var fn; if (typeof task === 'function') { fn = this.parallel(task); diff --git a/test/watch.js b/test/watch.js index 787b6543d..d2d15b482 100644 --- a/test/watch.js +++ b/test/watch.js @@ -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;