问题描述
我正在运行gulp 3.6.2,并通过在线样本建立了以下任务:
您的 swallowError 函数应该看起来像这样:
函数swallowError(错误){
//如果您想了解错误在控制台
console.log(error.toString())
this.emit('end')
}
我想你必须在正在下降的任务的错误事件上绑定这个函数,而不是 watch 任务,因为这不是问题所在,你应该在每个可能失败的任务上设置这个错误回调,比如当你错过了一个; 或别的东西,以防止 watch
<$ c $ 任务停止。 c> 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'))
})
或者,不要介意添加另一个模块,您可以使用 不让你在 gulpfile 中声明一个额外的函数:
但我可能会建议查看一下令人敬畏的 插件,它用于删除错误的 onerror / code>事件,导致流的中断。使用起来非常简单,它可以阻止您捕捉所有可能失败的任务。
gulp.src('./ app /script/*.coffee')
.pipe(plumber())
.pipe(coffee({bare:true}))
.pipe(gulp.dest('./ public / js'))
有关由相关插件的创建者提供。
I'm running gulp 3.6.2 and have the following task that was set up from a sample online
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']); });Any time there's an error in my CoffeeScript gulp watch stops - obviously not what I want.
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(); }but it doesn't seem to work.
What am I doing wrong?
In response to @Aperçu's answer I modified my swallowError method and tried the following instead:
gulp.task('scripts', function () { gulp.src('./app/script/*.coffee') .pipe(coffee({ bare: true })) .pipe(gulp.dest('./public/js')) .on('error', swallowError); });Restarted, and then created a syntax error in my coffee file. Same issue:
[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)解决方案Your swallowError function should look like this:
function swallowError (error) { // If you want details of the error in the console console.log(error.toString()) this.emit('end') }I think you have to bind this function on the error event of the task that was falling, not the watch task, because that's not where comes the problem, you should set this error callback on each task that may fail, like plugins that breaks when you have missed a ; or something else, to prevent watch task to stop.
Examples :
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')) })Alternately, if you don't mind to include another module, you can use the log function of gulp-util to keep you from declare an extra function in your gulpfile:
.on('error', gutil.log)But I may recommend having a look at the awesome gulp-plumber plugin, which is used to remove the onerror handler of the error event, causing the break of the streams. It's very simple to use and it stops you from catch all the tasks that may fail.
gulp.src('./app/script/*.coffee') .pipe(plumber()) .pipe(coffee({ bare: true })) .pipe(gulp.dest('./public/js'))More info about this on this article by the creator of the concerned plugin.
这篇关于防止错误打破/碰撞吞咽手表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!