for循环中的Gulp流不能正常工作 [英] Gulp stream inside a for loop not working properly
问题描述
gulp.task('loop',function(){
for(i = 0; i< 10; i ++){
gulp.src('./ build / ross / templates / pages / _page_home.liquid')
.pipe(plugins.insert.append(' ('./ src / page / _page'+ i +'.liquid'))
(这里是循环'+ i))
//真正的例子有这里的concat
.pipe(plugins.addSrc .pipe('plugins.concat('_ page_home.liquid'))
.pipe(gulp.dest('./ build / ross / templates / pages /'));
}
});
期望的输出可能类似于
循环1
// _page1.liquid内容
循环2
// _page2.liquid内容
循环3
// _page3 .liquid contents
等等...
实际输出看起来更像
//此处的原始页面内容
循环9
// _page9.liquid内容
我在循环中的原始文件上做了一个 fs.readFile
并输出数据,它只输出原始页面内容,而不是连接文件或我试图追加的字符串。
我也做了一个 console.log(i)
里面,它显示正确的数字 1,2,3,4,5,6,7,8,9
。我想知道这是否与gulp.src()有关。更新:有时看起来输出内容实际上会包含_page#.liquid内容的一部分。这有点不一致。
有没有更好的方法来做到这一点与吞噬或我只是做错了什么?
它使用 stream- to-promise
和 bluebird
var s2p = require('stream-to-promise');
var Promise = require('bluebird');
gulp.task('loop2',function(){
//不要忘记你的`var`s!
var buildIt = function(i){
return gulp.src('./ build / ross / templates / pages / _page_home.liquid')
.pipe(plugins.insert.append('Loop'+ i))
.pipe( gulp.dest('./ build / ross / templates / pages /'))
.on('end',function(){console.log(i +' - done')}); $ b ($ i = 0; i <10; i ++){
indices.push(i)
$ b var indexes = [];
;
}
// //序列它们
indexes.reduce(function(p,index){
return p.then(function(){
return s2p(buildIt(index));
});
},Promise.resolve());
});
I have a gulp task similar to the following. I am trying to loop through and append a string to the page and also concatenate a corresponding file.
gulp.task('loop', function() {
for(i =0; i < 10; i++) {
gulp.src('./build/ross/templates/pages/_page_home.liquid')
.pipe(plugins.insert.append('Loop ' + i))
// real example has concat here
.pipe(plugins.addSrc('./src/page/_page' + i + '.liquid'))
.pipe(plugins.concat('_page_home.liquid'))
.pipe(gulp.dest('./build/ross/templates/pages/'));
}
});
Expected output would be something like
Loop 1
// _page1.liquid contents
Loop 2
// _page2.liquid contents
Loop 3
// _page3.liquid contents
and so on...
Actual output appears more like
// Original Page Contents here
Loop 9
// _page9.liquid contents
I did a fs.readFile
on the original file in the loop and outputted the data and it is only outputting the original page contents and not the concatenated file or the string I'm trying to append.
I also did a console.log(i)
inside and it's showing the proper numbers 1,2,3,4,5,6,7,8,9
. I wonder if this has something to do with the gulp.src()
Update: Sometimes it appears that the output contents will actually have parts of the _page#.liquid contents. It is a bit inconsistent.
Is there a better way to do this with gulp or am I just doing something wrong?
Got it to work with some help from the people at #gulpjs on freenode irc.
This uses stream-to-promise
and bluebird
var s2p = require('stream-to-promise');
var Promise = require('bluebird');
gulp.task('loop2', function() {
// don't forget your `var`s!
var buildIt = function(i){
return gulp.src('./build/ross/templates/pages/_page_home.liquid')
.pipe(plugins.insert.append('Loop ' + i))
.pipe(gulp.dest('./build/ross/templates/pages/'))
.on('end', function(){ console.log(i + ' -- done')});
}
var indexes = [];
for(var i = 0; i < 10; i++) {
indexes.push(i);
}
// // sequence them
indexes.reduce(function(p, index){
return p.then(function(){
return s2p(buildIt(index));
});
}, Promise.resolve());
});
这篇关于for循环中的Gulp流不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!