2016-04-15 25 views
3

我有用于创建Html和Js的多种考试Ex1,Ex2等的gulp文件我需要从另一个任务传递一个参数给吞噬任务,或者用runSequence调用的一个函数替换一个任务

下面是我用来为Ex1创建这些的任务。它硬编码到makeEx1Html任务的调用,其他三个任务,然后是函数调用,我可以传递参数:

gulp.task('make_prod_ex1', function() { 
    runSequence(
     'makeEx1Html', 
     'makeTemplate', 
     'rename_bundle_css', 
     'rename_bundle_js', 
     function() { 
      make_prod_index('ex1'); 
     }); 
}); 

这里是一个的硬编码练习1任务:

gulp.task('makeEx1Html', function() { 
    return gulp.src(config.srcEx1Html, { base: process.cwd() }) 
      .pipe(print(function (file) { 
       return "Found file " + file; 
      })) 
      .pipe(rename({ basename: 'base' })) 
      .pipe(gulp.dest('./')); 

}); 

这里的功能,我可以传递参数:

function make_prod_index(name) { 
    return function() { 
     gulp.src('index.html') 
     .pipe(htmlreplace({ 
      'css': 'content/bundles/css.min.css', 
      'js': 'content/bundles/js.min.js' 
     })) 
     .pipe(eol()) 
     .pipe(lec({ eolc: 'CRLF' })) 
     .pipe(replace('content/bundles/css.min.css', 'content/bundles/css-' + md5File('content/bundles/css.min.css') + '.min.css.gz')) 
     .pipe(replace('content/bundles/js.min.js', 'content/bundles/js-' + md5File('content/bundles/js.min.js') + '.min.js.gz')) 
     .pipe(rename('index-' + name + '.html')) 
     .pipe(gulp.dest('./')); 
    } 
} 

我想,以避免像“makeEx1Html”和“makeEx2Html”等特定的任务,但我不知道如何做到这一点。

注意所有这些任务需要按顺序运行,这就是为什么我使用runSequence。

我将不胜感激任何建议。理想情况下,我希望使Html成为一个可以传递参数的函数,但我不确定如何将它符合我的要求。

回答

1

理想我想,使HTML来是我可以传递参数给

功能可以做到这一点的任务,除非你不只是传递参数到功能,而且回调cb当你的函数完成,将被称为:

function makeExHtml(files, cb) { 
    return gulp.src(files, { base: process.cwd() }) 
    .pipe(print(function (file) { 
     return "Found file " + file; 
    })) 
    .pipe(rename({ basename: 'base' })) 
    .pipe(gulp.dest('./')) 
    .on('end', cb); 
} 

在你大口的任务就可以使用上面makeExHtml()功能和传递一个回调将执行你的其余:

gulp.task('make_prod_ex1', function() { 
    makeExHtml(config.srcEx1Html, function() { 
    runSequence(
     'makeTemplate', 
     'rename_bundle_css', 
     'rename_bundle_js', 
     function() { 
     make_prod_index('ex1'); 
    }); 
    }); 
}); 
相关问题