Skip to content Skip to sidebar Skip to footer

Run Gulp Tasks With Loop

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

Solution 1:

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 + '/'))
        );
    }
    returnmergeStream(tasks);
});

Solution 2:

You can try async

varasync = require('async');

gulp.task('build', function (done) {
 var tasks = [];
    for (var i = 0; i < config.length; i++) {
        tasks.push(function () {
            var tmp = config[i];
            returnfunction (callback) {
                gulp.src(tmp.src)
                  .pipe( blah blah blah
                  ))
                  .pipe(gulp.dest(tmp.dest))
                  .on("end", callback);
            }
        }());
    }
    async.parallel(tasks, done);
});

Post a Comment for "Run Gulp Tasks With Loop"