node.js - 防止错误破坏/崩溃 gulp watch

标签 node.js gulp

我正在运行 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-utillog 功能 以防止您在 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/

相关文章:

javascript - express-hbs 实例 registerAsyncHelper 奇怪的哈希

javascript - TypeError : fs. readdirSync 不是函数 React js

node.js - 为什么从 Swagger UI 发送的请求中缺少 Authorization header ?

javascript - 无法理解 gulp.src() 的行为

javascript - Babel/Typescript 别名无法正常工作

node.js - 在第一次完成之前,不允许使用相同参数调用订阅者两次

node.js - 如何使用 Google 服务帐户代替 oAuth2Client 来使用 Google Apps API?

node.js - 当 node_modules 文件夹是符号链接(symbolic link)时 browserify-shim 错误

javascript - 为什么 vinyl.isVinyl() 对于 gulp 发出的乙烯基文件返回 false?

node.js - 无法让 "npm install -g"处理任何包(AppData/Roaming/npm 始终为空)