防止错误破坏/崩溃 gulp watch [英] Prevent errors from breaking / crashing gulp watch

查看:21
本文介绍了防止错误破坏/崩溃 gulp watch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行 gulp 3.6.2 并执行以下任务,该任务是根据在线示例设置的

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']);
});

任何时候我的 CoffeeScript gulp watch 中出现错误都会停止 - 显然不是我想要的.

Any time there's an error in my CoffeeScript gulp watch stops - obviously not what I want.

按照其他地方的建议,我试过了

gulp.watch(['./app/**/*.coffee'],['scripts']).on('error', swallowError);
gulp.watch('./app/**/*.scss',['scss']).on('error', swallowError);
function swallowError (error) { error.end(); }

但它似乎不起作用.

我做错了什么?

为了回应@Aperçu 的回答,我修改了我的 swallowError 方法并尝试了以下方法:

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:ariokartappscript	rishell.coffee:5:1: error: unexpected *
*
^
  at Stream.modifyFile (W:ariokart
ode_modulesgulp-coffeeindex.js:37:33)
  at Stream.stream.write (W:ariokart
ode_modulesgulp-coffee
ode_modulesevent-stream
ode_modules	hroughindex.js:26:11)
  at Stream.ondata (stream.js:51:26)
  at Stream.EventEmitter.emit (events.js:95:17)
  at queueData (W:ariokart
ode_modulesgulp
ode_modulesvinyl-fs
ode_modulesmap-streamindex.js:43:21)
  at next (W:ariokart
ode_modulesgulp
ode_modulesvinyl-fs
ode_modulesmap-streamindex.js:71:7)
  at W:ariokart
ode_modulesgulp
ode_modulesvinyl-fs
ode_modulesmap-streamindex.js:85:7
  at W:ariokart
ode_modulesgulp
ode_modulesvinyl-fslibsrcufferFile.js:8:5
  at fs.js:266:14
  at W:ariokart
ode_modulesgulp
ode_modulesvinyl-fs
ode_modulesgraceful-fsgraceful-fs.js:104:5
  at Object.oncomplete (fs.js:107:15)

推荐答案

你的 swallowError 函数应该如下所示:

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')
}

我认为你必须将此函数绑定到正在下降的任务的 error 事件上,而不是 watch 任务上,因为这不是问题所在,你应该在每个可能失败的任务上设置这个错误回调,比如当你错过 ; 或其他东西时中断的插件,以防止 watch 任务停止.

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.

例子:

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 中声明额外的函数:

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)

<小时>

但我可能建议您看看很棒的 gulp-plumber 插件,用于移除 error 事件的 onerror 处理程序,导致流中断.它使用起来非常简单,它可以阻止您捕获所有可能失败的任务.


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.

这篇关于防止错误破坏/崩溃 gulp watch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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