如何在gulp 4中从另一个任务重复启动任务

时间:2016-10-21 10:25:34

标签: gulp gulp-4

我最近升级到gulp 4,我正试图通过我的导出流程解决一个长期存在的问题。

简而言之,我的项目中有3个(或更多)独立文件夹。独立我的意思是他们每个人都有他们自己的bundle.js和global.css文件。我在gulpfile中设置了一个target变量,用于创建gulp对target所需的所有路径。

在我想要导出整个项目的当前情况下,我需要手动更改gulpfile中的target变量,然后运行export任务。

我需要像以下一样工作的东西(因为other_folders数组可以改变)

/*----------  Exports current target  ----------*/
gulp.task('export', gulp.series(to_prod,'export_files', 'export_scripts_and_styles', 'export_fonts', 'export_core'));

/*----------  Exports all targets  ----------*/
gulp.task('export_all', function(done){
    var needs_exporting = other_folders.concat("website");

    needs_exporting.forEach(function(export_this){
        target = export_this;
        set_paths();

        // Here it needs to fire the generic export task
        gulp.series('export');
    });

    done();
});

问题是我似乎找不到在forEach循环中调用gulp任务的方法。有没有办法做到这一点,还是我需要一个解决方法?

1 个答案:

答案 0 :(得分:1)

致电gulp.series('export')并不会立即启动export任务。它只返回一个必须调用的函数才能启动export任务。

但是,调用返回的函数也不会立即启动export任务。该函数是异步的。实际上只有export任务才开始实施。

为集合中的每个元素运行异步函数的最简单方法是使用eachSeries()包提供的async函数:

var async = require('async');

gulp.task('export_all', function(done){
    var needs_exporting = other_folders.concat("website");

    async.eachSeries(needs_exporting, function(export_this, cb) {
        target = export_this;
        set_paths();

        gulp.series('export')(cb);
    }, done);
});
相关问题