O
O
Oleg2015-11-11 22:09:56
Node.js
Oleg, 2015-11-11 22:09:56

How to run several identical tasks with different parameters using Gulp?

For example, a project has several themes that share common .sass files. When changing the shared file, you need to rebuild all the CSS of the theme (I use gulp-sass).
There is a build task and I can build each theme separately with a parameter (using yargs)

gulp build --theme theme1
gulp build --theme theme2
gulp build --theme theme3
...

Question - how to run a parallel assembly of ALL themes with one task? It is highly desirable that the debug messages of each task are displayed not mixed up in a common heap, but separately (for example, after the build is completed).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
fayster, 2015-11-12
@fayster

You can try using promises. Bottom line: there is a build task, which works in essence the same as it worked for you before, but at the same time, the build: themes task was added, which takes an array of topics (here either set manually or parse the themes folder and get a list of paths) and after the build all topics, you can already display debugging information.

function build(theme) {
    return new Promise(function(resolve) {
        gulp.src([theme])
        ...
        resolve();
    });
}

var themeList = []; // Список стилей
gulp.task('build:themes', function() {
    return Promise.all(themeList.map(function(theme) {
        return build(theme);
    })).then(function() {
        // Отработает после того, как завершится сборка всех стилей
    });
});

gulp.task('build', function() {
    var theme =  'src/styles/' + (util.env.theme ? util.env.theme : 'main') + '.scss';
    build(theme);
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question