我使用以下gulp任务将较少内容转换为CSS。我一经启动,一旦文件更改少,它就会更新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);
    }
  });
});


问题:有时候,less失败并停止,并显示错误消息,例如

events.js:72
    throw er; // Unhandled 'error' event
          ^
Error: Unrecognised input in file


显然,我的try catch机制是错误的,我认为它可以保护管道构建步骤,但不能保护管道运行时间。

问:如何编写gulp-watch任务,以便在更少失败时继续工作?

最佳答案

要么去参加活动

gulp.src(filename)
    .pipe(sourcemaps.init())
    .pipe(less())
    .on('error', console.log)
    .pipe(sourcemaps.write())
    .pipe(gulp.dest('app/styles/'));


或使用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/'));

关于javascript - 当由gulp-watch触发的任务失败时,如何捕获异常?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30185304/

10-11 23:04
查看更多