javascript - How do I catch exceptions when a task triggered by gulp-watch fails? -
i use following gulp task transform less css. start once , upon less-file changes updates css.
gulp.task('watch-less', function () { gulp.watch('app/styles/*.less', function (event) { try { var filename = event.path; gulp.src(filename) .pipe(sourcemaps.init()) .pipe(less()) .pipe(sourcemaps.write()) .pipe(gulp.dest('app/styles/')); } catch (e) { console.log(e); } }); });
the problem: sometimes, less fails , stops error message like
events.js:72 throw er; // unhandled 'error' event ^ error: unrecognised input in file
obviously try catch mechanism wrong, think protects pipe-building step not pipe-running time.
q: how can write gulp-watch task, continues working when less fails?
either go events
gulp.src(filename) .pipe(sourcemaps.init()) .pipe(less()) .on('error', console.log) .pipe(sourcemaps.write()) .pipe(gulp.dest('app/styles/'));
or use gulp-plumber
:
var plumber = require('gulp-plumber') ... gulp.src(filename) .pipe(sourcemaps.init()) .pipe(plumber()) .pipe(less()) .pipe(sourcemaps.write()) .pipe(gulp.dest('app/styles/'));
Comments
Post a Comment