gulp任务异步问题,没有在哪里使用'返回'

时间:2015-07-23 07:29:42

标签: gulp

我有一个循环将我的所有htmls插入到template.html中,然后输出

gulp.task('inject', ()=>{
t.data.forEach((node) => {
    var url = node.url;
    gulp.src('./templates/template.html')
        .pipe($.inject(bowers, {
            name: 'bower'
        }))
        .pipe($.inject(styles))
        .pipe($.inject(js))
        .pipe($.inject(header, {
            starttag: '<!-- inject:header:html -->',
            transform: function(filePath, file) {
                // return file contents as string
                return file.contents.toString('utf8')
            }
        }))
        .pipe(gulp.dest(path.join(conf.paths.tmp, '/serve')));
});})

那么如何写返回来同步,否则,我的&#39; html&#39;任务将在“注入”之前执行。任务完成目标

我已尝试cb(null)return truereturn cb(null),无效。
还是有更好的方式来编写return gulp.src.....来重构我的forEach吗?,我真的不喜欢forEach in gulp

1 个答案:

答案 0 :(得分:0)

我有类似的问题。 github issue中的解决方案。 我使用插件'async-each-series'进行串行/异步工作forEach。

'use strict';
var gulp = require('gulp');
var plumber = require('gulp-plumber');
var inject = require('gulp-inject');
var browserSync = require('browser-sync');
var reload = browserSync.reload;
var src = {};
var config = require('../utils/config');
var each = require('async-each-series');

gulp.task('injectDev', ['jade'], function(done) {
    each(config.bundles, function(bundle, next) {
        gulp.src(bundle.pages)
        .pipe(plumber({errorHandler: onError}))
        .pipe(inject(gulp.src(bundle.css.concat(bundle.js), {read: false}), {
            name: bundle.name,
            ignorePath: 'public'
        }))
        .pipe(gulp.dest('public'))
        .pipe(browserSync.reload({stream: true}))
        .on('finish', next);
    }, done());
});

其中../utils/config包含包:

module.exports = {
    bundles: [
        {
            name: 'libs',
            css: [
                'bower_components/normalize.css/normalize.css',
                'bower_components/bootstrap/dist/css/bootstrap.min.css'
            ],
            js: [
                'bower_components/jquery/dist/jquery.min.js',
                'bower_components/jquery.browser/dist/jquery.browser.min.js',
                'bower_components/picturefill/dist/picturefill.min.js'
            ],
            pages: [
                'public/*.html',
                '!public/i.html'
            ],
            build: [
                './public/app/libs.min.js',
                './public/app/libs.min.css'
            ]
        },
        {
            name: 'app',
            css: [
                'public/font/**/*.css',
                'public/app/**/*.css',
                'public/blocks/**/*.css'
            ],
            js: [
                'public/app/**/*.js',
                'public/blocks/**/*.js'
            ],
            pages: [
                'public/*.html',
                '!public/i.html'
            ],
            build: [
                './public/app/libs.min.js',
                './public/app/libs.min.css'
            ]
        }
    ]
}