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-changedandgulp-watchseems 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
gulpitself 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.cssfiles. As long asgulp.watch()is running any change to a.cssfile automatically results in the execution of thebuild-csstask.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.cssfile all of your.cssfiles will pass throughdoSomethingHere()again. Thebuild-csstask 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-changedcomes in.gulp-changedThis 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-csstask is still called for every change of a.cssfile and all.cssfiles 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-csseven if you're not watching for file changes. You can explicitly invokegulp build-csson the command-line and only those files that have changed since the last invocation ofbuild-csswill be rebuilt.gulp-watchThis plugin is an attempt to improve on the built-in
gulp.watch(). Whilegulp.watch()usesgazeto listen for file changes,gulp-watchuseschokidarwhich is generally considered to be the more mature of the two.You can use
gulp-watchto achieve the same effect as usinggulp.watch()andgulp-changedin combination:This again watches all
.cssfiles for changes. But this time whenever a.cssfile 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.