为什么一些Gulp流“流动”默认情况下,其他人不会? [英] Why do some Gulp streams "flow" by default, while others do not?
问题描述
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
的行为与预期相同, :
[12:33:15]使用gulpfile〜/ Projects / gulp-exit / gulpfile.js
$然而,运行
[ 12:33:15]开始'dest'...
结束
[12:33:15] 13 ms后完成'dest'
gulp src
只能输出:
<$>
<$> p $ p>[12:31:11]使用gulpfile gulpfile.js
[12:31:11]开始'src'...
从不调用'end'
回调。经过一些调试后,我认为 dest
任务中的流是 流动 ,而源任务中的流不是。
发信 src
任务通过调用 stream.resume()
显式流动:
gulp.task('src',function(done){
gulp.src('docs / *')
.on('end',function(){
console.log('ending');
done();
})
.resume();
});
给出预期的输出:
[12:46:52]使用gulpfile gulpfile.js
[12:46:52]开始'src'...
结束
[12: 46:52] 11毫秒后完成'src'
我已经看到了这种相同的行为组合插件: gulp.dest 和 gulp-mocha 似乎会返回流动的流,而 gulp-logger 和 gulp-gh-pages 不要。
为什么行为上的差异?
这种情况发生的原因是因为一些流有数据需要读取,有些则不需要。 b
$ b
gulp.src('docs / *')
返回一个可读流,其中包含 docs中每个文件的数据
。一旦从流中读取所有数据, end
事件仅触发可读流。
通常情况下,您会自动将它传递给另一个流,但由于您不是,您需要使用:
gulp.task ('src',function(done){
gulp.src('docs / *')
.on('data',function(){})
.on('end ',function(){
console.log('ending');
done();
});
});
您也可以使用完成
事件, (我认为)等待,直到所有数据已被推入流(即它已完成工作):
gulp.task( 'src',function(done){
gulp.src('docs / *')
.on('finish',function(){
console.log('ending') ;
done();
});
});
你的第二个gulp任务使用 gulp.dest('temp')
,它返回一个没有数据的流,所以只要流完成处理,就会触发
end
。
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();
});
});
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
However, running gulp src
only outputs:
[12:31:11] Using gulpfile gulpfile.js
[12:31:11] Starting 'src'...
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.
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();
});
Gives the expected output:
[12:46:52] Using gulpfile gulpfile.js
[12:46:52] Starting 'src'...
ending
[12:46:52] Finished 'src' after 11 ms
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/*')
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.
这篇关于为什么一些Gulp流“流动”默认情况下,其他人不会?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!