防止错误打破/碰撞吞咽手表 [英] Prevent errors from breaking / crashing gulp watch

查看:89
本文介绍了防止错误打破/碰撞吞咽手表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行gulp 3.6.2,并通过在线样本建立了以下任务:

  gulp。任务('watch',['default'],function(){
gulp.watch([
'views / ** / *。html',
'public / ** / * .js',
'public / ** / *。css'
],function(event){
return gulp.src(event.path)
.pipe刷新(lrserver));
});

gulp.watch(['./app/ ** / *。coffee'],['scripts']);
gulp.watch('./ app / ** / *。scss',['scss']);
});

任何时候我的CoffeeScript gulp手表出现错误都会停止 - 显然不是我想要的。



正如其他地方所建议的,我试过这个

  gulp.watch(['./app/ ** / *。coffee'],['scripts'])。on ('error',swallowError); $('error',swallowError); 
gulp.watch('./app/ ** / *。scss',['scss'])。
函数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] 306μs后完成'scripts'

stream.js:94
扔呃; //管道中未处理的流错误。
^
错误:W:\bariokart\app\script\trishell.coffee:5:1:error:unexpected *
*
^
在Stream.modifyFile(W:\bariokart\\\
ode_modules\gulp-coffee\index.js:37:33)
在Stream.stream.write(W:\bariokart\\\
ode_modules\gulp -coffee\\\
ode_modules\event-stream\\\
ode_modules\through\index.js:26:11)
在Stream.ondata(stream.js:51:26)
在Stream。 EventEmitter.emit(events.js:95:17)在queueData(W:\ bariokart\\\
ode_modules\gulp\\\
ode_modules\vinyl-fs\\\
ode_modules\map-stream\index)处
。 js:43:21)
at next(W:\bariokart\\\
ode_modules\gulp\\\
ode_modules\vinyl-fs\\\
ode_modules\map-stream\index.js:71:7)
at W:\bariokart\\\
ode_modules\gulp\\\
ode_modules\vinyl-fs\\\
ode_modules\map-stream\index.js:85:7
at W:\ bariokart\\\
ode_modules\gulp \\ nb_ode_modules \ vinyl-fs\lib\src\bufferFile.js:8:5
在fs.js:266:14
在W:\bariokart\\\
ode_modules\gulp \\\
ode_modules\vinyl-fs\\\
ode_modules\graceful -fs\graceful-fs.js:104:5
在Object.oncomplete(fs.js:107:15)
code>


解决方案您的 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'))
})

或者,不要介意添加另一个模块,您可以使用 gulp的日志功能-util 不让你在 gulpfile 中声明一个额外的函数:

 .on('error',gutil.log)






但我可能会建议查看一下令人敬畏的 gulp-plumber 插件,它用于删除错误 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.

这篇关于防止错误打破/碰撞吞咽手表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆