Why do some Gulp streams “flow” by default, while others do not?

我的未来我决定 提交于 2019-12-01 09:04:33

The reason why this happens is because some streams have data to be read, and some do not.

gulp.src('docs/*') returns a readable stream with data for each file in docs. The end event only triggers for a readable stream once all the data has been read from the stream.

Normally you would pipe this to another stream which does that automatically, but since you aren't you would need to use:

gulp.task('src', function(done) {
  gulp.src('docs/*')
    .on('data', function() {})
    .on('end', function() {
      console.log('ending');
      done();
    });
});

Alternatively you can use the finish event, which (I think) waits until all data has been pushed into the stream (i.e. it's finished working):

gulp.task('src', function(done) {
  gulp.src('docs/*')
    .on('finish', function() {
      console.log('ending');
      done();
    });
});

Your second gulp task uses gulp.dest('temp') which returns a stream with no data, so end is triggered as soon as the stream is done processing.

Not an answer to why, but I threw together a stream-end module to smooth out the quirks in the short term:

end = require('stream-end')

gulp.task 'logger', (done) ->
  gulp.src 'docs/*'
    .pipe logger()
    .pipe end ->
      console.log('ending')
      done()

The callback passed to end is called whether the upstream is flowing or not.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!