我在我的项目中使用gulp进行prod构建相关活动。我有像下面这样的任务
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);
});
但是在这种情况下,当任何一个任务失败时,我在控制台中出现错误,但构建过程仍在继续。我想要做的是,当任何任务失败时,我想在控制台中显示适当的错误并停止构建过程,即我不想执行后续任务。怎么去这个?我在网上搜索,但无法得到任何可以解决问题的东西。请帮忙。谢谢。
答案 0 :(得分:1)
这取决于您尝试处理的错误类型。它被抛出作为一个例外,您可以使用try-catch处理它。如果你不想执行剩余的任务,它将进入catch块并且不执行done函数。例如:
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