I have a problem with my Gulp tasks. I use one task to create multiple html files with gulp-mustache, so that I have two files (index_de.html and index_en.html) at the end. I have a .json file, that contains the strings. It all works fine. But I always end up with either both files being de or en, instead of one file per language. I already tried creating tasks using a loop [gulp], but that doesn't work.
Edit: to clarify: Both files contain the same content. Always. Seems randomly what language it will be, but it's always the same.
My Gulp tasks looks like the following:
gulp.task('mustache', function () {
console.log('Found '+Object.keys(strings).length+' languages.');
for (var l in strings) {
var lang = strings[l];
(function(lang, l) {
gulp.src(buildpath+'/index.html')
.pipe(mustache(lang))
.pipe(rename('index_'+l+'.html'))
.pipe(compressor({
'remove-intertag-spaces': true,
'compress-js': true,
'compress-css': true
}))
.pipe(gulp.dest(destpath+'/'));
})(lang, l);
}
});
I think an option here is to merge a series of gulp.src()
streams and return the merged streams. Here is your code modified to use the merge-stream
npm package (https://www.npmjs.com/package/merge-stream).
var mergeStream = require('merge-stream');
gulp.task('mustache', function () {
console.log('Found ' + Object.keys(strings).length + ' languages.');
var tasks = [];
for (var l in strings) {
var lang = strings[l];
tasks.push(
gulp.src(buildpath + '/index.html')
.pipe(mustache(lang))
.pipe(rename('index_' + l + '.html'))
.pipe(compressor({
'remove-intertag-spaces': true,
'compress-js': true,
'compress-css': true
}))
.pipe(gulp.dest(destpath + '/'))
);
}
return mergeStream(tasks);
});
You can try async
var async = require('async');
gulp.task('build', function (done) {
var tasks = [];
for (var i = 0; i < config.length; i++) {
tasks.push(function () {
var tmp = config[i];
return function (callback) {
gulp.src(tmp.src)
.pipe( blah blah blah
))
.pipe(gulp.dest(tmp.dest))
.on("end", callback);
}
}());
}
async.parallel(tasks, done);
});
来源:https://stackoverflow.com/questions/26286412/run-gulp-tasks-with-loop