我正在运行 gulp 3.6.2 并执行以下任务,该任务是根据在线示例设置的
gulp.task('watch', ['default'], function () {
gulp.watch([
'views/**/*.html',
'public/**/*.js',
'public/**/*.css'
], function (event) {
return gulp.src(event.path)
.pipe(refresh(lrserver));
});
gulp.watch(['./app/**/*.coffee'],['scripts']);
gulp.watch('./app/**/*.scss',['scss']);
});
任何时候我的 CoffeeScript gulp watch 中出现错误都会停止 - 显然不是我想要的。
As recommended elsewhere I tried this
gulp.watch(['./app/**/*.coffee'],['scripts']).on('error', swallowError);
gulp.watch('./app/**/*.scss',['scss']).on('error', swallowError);
function swallowError (error) { error.end(); }
但它似乎不起作用。
我做错了什么?
为了回应@Aperçu 的回答,我修改了我的 swallowError
方法并尝试了以下方法:
gulp.task('scripts', function () {
gulp.src('./app/script/*.coffee')
.pipe(coffee({ bare: true }))
.pipe(gulp.dest('./public/js'))
.on('error', swallowError);
});
重新启动,然后在我的咖啡文件中创建了一个语法错误。同样的问题:
[gulp] Finished 'scripts' after 306 μs
stream.js:94
throw er; // Unhandled stream error in pipe.
^
Error: W:\bariokart\app\script\trishell.coffee:5:1: error: unexpected *
*
^
at Stream.modifyFile (W:\bariokart\node_modules\gulp-coffee\index.js:37:33)
at Stream.stream.write (W:\bariokart\node_modules\gulp-coffee\node_modules\event-stream\node_modules\through\index.js:26:11)
at Stream.ondata (stream.js:51:26)
at Stream.EventEmitter.emit (events.js:95:17)
at queueData (W:\bariokart\node_modules\gulp\node_modules\vinyl-fs\node_modules\map-stream\index.js:43:21)
at next (W:\bariokart\node_modules\gulp\node_modules\vinyl-fs\node_modules\map-stream\index.js:71:7)
at W:\bariokart\node_modules\gulp\node_modules\vinyl-fs\node_modules\map-stream\index.js:85:7
at W:\bariokart\node_modules\gulp\node_modules\vinyl-fs\lib\src\bufferFile.js:8:5
at fs.js:266:14
at W:\bariokart\node_modules\gulp\node_modules\vinyl-fs\node_modules\graceful-fs\graceful-fs.js:104:5
at Object.oncomplete (fs.js:107:15)
最佳答案
您的 swallowError
函数应如下所示:
function swallowError (error) {
// If you want details of the error in the console
console.log(error.toString())
this.emit('end')
}
我认为你必须将此函数绑定(bind)到正在下降的任务的 error
事件上,而不是 watch
任务上,因为这不是问题所在,你应该在每个可能失败的任务上设置这个错误回调,比如当你错过 ;
或其他东西时中断的插件,以防止 watch
任务停止。
例子:
gulp.task('all', function () {
gulp.src('./app/script/*.coffee')
.pipe(coffee({ bare: true }))
.on('error', swallowError)
.pipe(gulp.dest('./public/js'))
gulp.src('css/*.scss')
.pipe(sass({ compass: true }))
.on('error', swallowError)
.pipe(cssmin())
.pipe(gulp.dest('dist'))
})
或者,如果您不介意包含另一个模块,您可以使用 gulp-util 的 log 功能 以防止您在 gulpfile
中声明额外的函数:
.on('error', gutil.log)
但我可能建议您看看很棒的 gulp-plumber 插件,用于移除 error
事件的 onerror
处理程序,导致流中断。它使用起来非常简单,它可以阻止您捕获所有可能失败的任务。
gulp.src('./app/script/*.coffee')
.pipe(plumber())
.pipe(coffee({ bare: true }))
.pipe(gulp.dest('./public/js'))
更多信息请访问 this article由相关插件的创建者提供。
关于node.js - 防止错误破坏/崩溃 gulp watch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23971388/