I cannot get my head around this.
This is supposed to be a gulp task that is executed every time a watched file is modified. Can anyone explain why it is required to pipe the watched files through the changed
plugin?
gulp.task('build-css', function () {
return gulp.src(paths.css)
.pipe(changed(paths.output, {extension: '.css'}))
.pipe(gulp.dest(paths.output));
});
gulp.task('watch', ['serve'], function() {
gulp.watch(paths.css, ['build-css']);
});
Disclaimer: this is not my code. Just trying to understand what is going on, in order to create my own custom watch task.
The difference between
gulp.watch()
,gulp-changed
andgulp-watch
seems to cause a lot of confusion, so here's my attempt at untangling the mess:gulp.watch()
This is the only one of the three that is part of
gulp
itself and not a plugin. This is important, because it means that unlike the other two it is not passed to thepipe()
function of a gulp stream.Instead it is usually called directly from inside a gulp task:
In the above
gulp.watch()
is used to listen for changes in.css
files. As long asgulp.watch()
is running any change to a.css
file automatically results in the execution of thebuild-css
task.This is where the trouble starts. Notice how no information about which files where changed is passed to
build-css
? That means even if you change just a single.css
file all of your.css
files will pass throughdoSomethingHere()
again. Thebuild-css
task has no clue which of them changed. This may be fine as long as you have only a hand full of files, but can slow down your build as the number of files grows.That's where
gulp-changed
comes in.gulp-changed
This plugin was written to act as a filter stage in a gulp stream. Its purpose is to remove all those files from a stream that haven't changed since the last build. It does this by comparing the files in the source directory with the resulting files in the destination directory:
In the above the
build-css
task is still called for every change of a.css
file and all.css
files are read in. However only those files that were actually changed now reach the expensivedoSomethingHere()
stage. The rest is filtered out bygulp-changed
.This approach has the benefit of speeding up
build-css
even if you're not watching for file changes. You can explicitly invokegulp build-css
on the command-line and only those files that have changed since the last invocation ofbuild-css
will be rebuilt.gulp-watch
This plugin is an attempt to improve on the built-in
gulp.watch()
. Whilegulp.watch()
usesgaze
to listen for file changes,gulp-watch
useschokidar
which is generally considered to be the more mature of the two.You can use
gulp-watch
to achieve the same effect as usinggulp.watch()
andgulp-changed
in combination:This again watches all
.css
files for changes. But this time whenever a.css
file is changed, that file (and only that file) is read in again and reemitted to the stream where it passes throughdoSomethingHere()
on its way to the destination directory.Note that this comparison paints all three of the alternatives in rather broad strokes and leaves out certain details and features (e.g. I haven't talked about the callback functions that you can pass to both
gulp.watch()
andgulp-watch
), but I think this should be enough to get the major differences between the three across.