为什么有些 Gulp 流会“流动"?默认情况下,而其他人没有? [英] Why do some Gulp streams "flow" by default, while others do not?

查看:13
本文介绍了为什么有些 Gulp 流会“流动"?默认情况下,而其他人没有?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这两个 gulp 任务:

Consider these two gulp tasks:

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

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

运行 gulp dest 行为符合预期,输出:

Running gulp dest behaves as expected, outputting:

[12:33:15] Using gulpfile ~/Projects/gulp-exit/gulpfile.js
[12:33:15] Starting 'dest'...
ending
[12:33:15] Finished 'dest' after 13 ms

但是,运行 gulp src 只会输出:

However, running gulp src only outputs:

[12:31:11] Using gulpfile gulpfile.js
[12:31:11] Starting 'src'...

'end' 回调永远不会被调用.经过一番调试,我认为 dest 任务中的流是 流动,而源任务中的流不是.

The 'end' callback is never called. After a bit of debugging, I think the stream in the dest task is flowing while the stream in the source task is not.

通过调用 stream.resume() 指示 src 任务显式流动:

Signaling the src task to flow explicitly by calling stream.resume():

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

给出预期的输出:

[12:46:52] Using gulpfile gulpfile.js
[12:46:52] Starting 'src'...
ending
[12:46:52] Finished 'src' after 11 ms

我已经看到了与插件相同的行为组合:gulp.destgulp-mocha 似乎返回流动的流,而 gulp-logger 和 gulp-gh-pages 没有.

I've seen this same mix of behaviors with plugins: gulp.dest and gulp-mocha seem to return flowing streams while gulp-logger and gulp-gh-pages do not.

为什么会有不同的行为?

Why the difference in behavior?

推荐答案

之所以会这样,是因为有些流有数据要读取,有些没有.

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

gulp.src('docs/*') 返回一个可读流,其中包含 docs 中每个文件的数据.end 事件仅在从流中读取所有数据后触发可读流.

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();
    });
});

您也可以使用 finish 事件,它(我认为)会等到所有数据都被推送到流中(即完成工作):

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();
    });
});

您的第二个 gulp 任务使用 gulp.dest('temp') 它返回一个没有数据的流,因此一旦流完成处理就会触发 end.

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.

这篇关于为什么有些 Gulp 流会“流动"?默认情况下,而其他人没有?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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