gulp watch のエラーによる破損/クラッシュを防ぐ 質問する

gulp watch のエラーによる破損/クラッシュを防ぐ 質問する

私は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 が停止します。明らかに、これは望んでいないことです。

他のところで勧められたのでこれを試してみた

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)

ベストアンサー1

関数swallowErrorは次のようになります。

function swallowError (error) {

  // If you want details of the error in the console
  console.log(error.toString())

  this.emit('end')
}

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余分な関数を宣言しないようにするためgulpfile:

.on('error', gutil.log)

しかし、素晴らしいものを見ることをお勧めしますgulp-配管工プラグインは、ストリームの中断を引き起こすイベントonerrorのハンドラーを削除するために使用されますerror。使い方は非常に簡単で、失敗する可能性のあるすべてのタスクをキャッチするのを防ぎます。

gulp.src('./app/script/*.coffee')
  .pipe(plumber())
  .pipe(coffee({ bare: true }))
  .pipe(gulp.dest('./public/js'))

これに関する詳細はこの記事関係するプラグインの作成者によって。

おすすめ記事