Skip to content

Commit 2cd0e1e

Browse files
Stefan Baumgartnerphated
authored andcommitted
Breaking: Replace vinyl-fs watch/gaze with chokidar
1 parent 646044b commit 2cd0e1e

File tree

5 files changed

+69
-40
lines changed

5 files changed

+69
-40
lines changed

docs/API.md

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -509,31 +509,42 @@ A single glob or array of globs that indicate which files to watch for changes.
509509
#### opts
510510
Type: `Object`
511511

512-
Options, that are passed to [`gaze`][gaze].
512+
Options, that are passed to [`chokidar`][chokidar].
513513

514514
#### fn
515515
Type: `Function`
516516

517517
An [async](#async-support) function to run when a file changes.
518518

519-
`gulp.watch` returns an `EventEmitter` object which emits `change` events with
520-
the [gaze] `event`:
519+
`gulp.watch` returns a wrapped [chokidar] FSWatcher object. If provided,
520+
the callback will be triggered upon any `add`, `change`, or `unlink` event.
521+
Listeners can also be set directly for any of [chokidar]'s events.
522+
521523
```js
522524
var watcher = gulp.watch('js/**/*.js', gulp.parallel('uglify', 'reload'));
523-
watcher.on('change', function(event) {
524-
console.log('File ' + event.path + ' was ' + event.type);
525+
watcher.on('change', function(path, stats) {
526+
console.log('File ' + path + ' was changed');
527+
if (stats) {
528+
console.log('changed size to ' + stats.size);
529+
}
530+
});
531+
532+
watcher.on('unlink', function(path) {
533+
console.log('File ' + path + ' was removed');
525534
});
526535
```
527536

528-
##### event.type
537+
##### path
529538
Type: String
530539

531-
The type of change that occurred, either "added", "changed" or "deleted".
540+
The relative path of the document.
532541

533-
##### event.path
534-
Type: String
542+
##### stats
543+
Type: Object
535544

536-
The path to the file that triggered the event.
545+
[File stats](http://nodejs.org/api/fs.html#fs_class_fs_stats) object when available.
546+
Setting the `alwaysStat` option to true will ensure that a file stat object will be
547+
available.
537548

538549
### gulp.tree(options)
539550

@@ -737,7 +748,7 @@ module.exports = new MyCompanyTasksRegistry();
737748
```
738749

739750
[Function.name]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name
740-
[gaze]: https://github.com/shama/gaze
751+
[chokidar]: https://github.com/paulmillr/chokidar
741752
[glob-stream]: https://github.com/gulpjs/glob-stream
742753
[glob-parent]: https://github.com/es128/glob-parent
743754
[gulp-if]: https://github.com/robrich/gulp-if
Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
# Handling the Delete Event on Watch
22

3-
You can listen for `'change'` events to fire on the watcher returned from `gulp.watch`.
4-
5-
Each change event has a `type` property. If `type` is `'deleted'`, you can delete the file
6-
from your destination directory, using something like:
3+
You can listen for `'unlink'` events to fire on the watcher returned from `gulp.watch`.
4+
This gets fired when files are removed, so you can delete the file from your destination
5+
directory, using something like:
76

87
```js
98
'use strict';
@@ -24,16 +23,11 @@ gulp.task('scripts', function() {
2423
gulp.task('watch', function () {
2524
var watcher = gulp.watch('src/**/*.js', ['scripts']);
2625

27-
watcher.on('change', function (event) {
28-
if (event.type === 'deleted') {
29-
// Simulating the {base: 'src'} used with gulp.src in the scripts task
30-
var filePathFromSrc = path.relative(path.resolve('src'), event.path);
31-
32-
// Concatenating the 'build' absolute path used by gulp.dest in the scripts task
33-
var destFilePath = path.resolve('build', filePathFromSrc);
34-
35-
del.sync(destFilePath);
36-
}
26+
watcher.on('unlink', function (filepath) {
27+
var filePathFromSrc = path.relative(path.resolve('src'), filepath);
28+
// Concatenating the 'build' absolute path used by gulp.dest in the scripts task
29+
var destFilePath = path.resolve('build', filePathFromSrc);
30+
del.sync(destFilePath);
3731
});
3832
});
3933
```

index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var util = require('util');
44
var Undertaker = require('undertaker');
55
var vfs = require('vinyl-fs');
6+
var chokidar = require('chokidar');
67

78
function Gulp() {
89
Undertaker.call(this);
@@ -30,7 +31,15 @@ Gulp.prototype.watch = function(glob, opt, task) {
3031
fn = this.parallel(task);
3132
}
3233

33-
return vfs.watch(glob, opt, fn);
34+
var watcher = chokidar.watch(glob, opt);
35+
if (fn) {
36+
watcher
37+
.on('change', fn)
38+
.on('unlink', fn)
39+
.on('add', fn);
40+
}
41+
42+
return watcher;
3443
};
3544

3645
// Let people use this class from our instance

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
},
2424
"man": "gulp.1",
2525
"dependencies": {
26+
"chokidar": "^1.0.5",
2627
"gulp-cli": "gulpjs/gulp-cli#4.0",
2728
"undertaker": "^0.12.0",
2829
"vinyl-fs": "^1.0.0"

test/watch.js

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,38 @@ describe('gulp', function() {
3636
createTempFile(tempFile);
3737

3838
var watcher = gulp.watch(tempFile, function(cb) {
39-
watcher.end();
39+
watcher.close();
4040
cb();
4141
done();
4242
});
4343

4444
updateTempFile(tempFile);
4545
});
4646

47+
it('should execute the gulp.parallel tasks', function(done) {
48+
var tempFile = path.join(outpath, 'watch-func.txt');
49+
50+
createTempFile(tempFile);
51+
52+
gulp.task('test', function(cb) {
53+
watcher.close();
54+
cb();
55+
done();
56+
});
57+
58+
var watcher = gulp.watch(tempFile, gulp.parallel('test'));
59+
60+
updateTempFile(tempFile);
61+
});
62+
63+
4764
it('should call the function when file changes: w/ options', function(done) {
4865
var tempFile = path.join(outpath, 'watch-func-options.txt');
4966

5067
createTempFile(tempFile);
5168

52-
var watcher = gulp.watch(tempFile, {debounceDelay: 5}, function(cb) {
53-
watcher.end();
69+
var watcher = gulp.watch(tempFile, function(cb) {
70+
watcher.close();
5471
cb();
5572
done();
5673
});
@@ -66,14 +83,11 @@ describe('gulp', function() {
6683

6784
createTempFile(tempFile);
6885

69-
var watcher = gulp.watch(relFile, {debounceDelay: 5, cwd: cwd})
70-
.on('change', function(evt) {
71-
should.exist(evt);
72-
should.exist(evt.path);
73-
should.exist(evt.type);
74-
evt.type.should.equal('changed');
75-
evt.path.should.equal(path.resolve(tempFile));
76-
watcher.end();
86+
var watcher = gulp.watch(relFile, {cwd: cwd})
87+
.on('change', function(filepath) {
88+
should.exist(filepath);
89+
path.resolve(cwd, filepath).should.equal(path.resolve(tempFile));
90+
watcher.close();
7791
done();
7892
});
7993

@@ -93,12 +107,12 @@ describe('gulp', function() {
93107
gulp.task('task2', function(cb) {
94108
a += 10;
95109
a.should.equal(11);
96-
watcher.end();
110+
watcher.close();
97111
cb();
98112
done();
99113
});
100114

101-
var watcher = gulp.watch(tempFile, {debounceDelay: 25}, gulp.series('task1', 'task2'));
115+
var watcher = gulp.watch(tempFile, gulp.series('task1', 'task2'));
102116

103117
updateTempFile(tempFile);
104118
});
@@ -116,7 +130,7 @@ describe('gulp', function() {
116130
gulp.task('task2', function(cb) {
117131
a += 10;
118132
a.should.equal(11);
119-
watcher.end();
133+
watcher.close();
120134
cb();
121135
done();
122136
});

0 commit comments

Comments
 (0)