Basic Error Handling with gulp.watch
One of the most annoying thing when using the gulp.watch API is that it crashes whenever errors happen and you will have to start it again manually. Gulp system uses Nodejs Stream for automation, so you can use the events system of Nodejs Stream to handle error.
For example, I have the following Gulp task
var gulp = require('gulp');
var less = require('gulp-less');
var minifyCSS = require('gulp-minify-css');
gulp.task('transform-less', function(){
return gulp.src('./*.less')
.pipe(less())
.pipe(minifyCSS())
.pipe(gulp.dest('./dist'));
});
gulp.task('watch', function(){
gulp.watch('./*.less',
['transform-less']);
});
If one of the source .less file contains syntax error, gulp will crash while
watching these file. To handle it, simply add on('error')
event after each
time you pipe the stream to a plugin.