gulp 遇到任何错误停止 gulp 进程

gulp stopping gulp process on any error

我在我的项目中使用 gulp 进行产品构建相关活动。我有如下任务

gulp.task('bundle-libs', bundlelibs);
gulp.task('bundle-modules', bundle);

/* build libs for production */
gulp.task('build:prod', function (done) {
    runSequence('clean', ['styles', 'copy-assets'], 'compile', 'systemjs_config', 'prefix_routes', 'bundle-libs', done)
});

/* create module bundles */
gulp.task('bundle', function (done) {
    runSequence('copy-required-files', 'bundle-modules', done);
}); 

但在此过程中,当任何一项任务失败时,我会在控制台中收到错误消息,但构建过程仍在继续。我想做的是,当任何任务失败时,我想在控制台中显示适当的错误并停止构建过程,即我不想执行后续任务。怎么办?我在网上搜索,但找不到任何可以解决问题的方法。请帮忙。谢谢。

这取决于您尝试处理的错误类型。如果它作为异常抛出,您可以使用 try-catch 处理它。它会进入 catch 块,如果您不想执行剩余的任务,则不会执行已完成的功能。示例:

function testTask(done) {
  try {
    return gulp.src('tmp/dist/*.html')
          .pipe(gulp.dest('tmp/dist'));
  } catch(error) {
    const error_message = 'Error:: task failed. ' + error.name + ' : ' + error.message;
  }
}

如果是错误回调,有多种处理方式。你可以试试gulp plumber