我需要从另一个任务传递一个参数给吞噬任务,或者用runSequence调用的函数replace一个任务

我有大量的文件,我用它来创buildHtml和Js多种考试,Ex1,Ex2等

这是我用来为Ex1创build这些的任务。 它已经对makeEx1Html任务进行了硬编码,还有三个其他的任务,然后是可以传递参数的函数调用:

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

这是为Ex1硬编码的任务:

 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:

 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。

我将不胜感激任何build议。 理想情况下,我希望使Html成为一个可以传递参数的函数,但是我不确定如何将这个函数应用于我的需求。

理想情况下,我希望使Html成为一个可以传递参数的函数

你可以做到这一点,除了你不只是传递你的参数到函数,还有一个callback函数完成时将被调用的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()任务中,你可以使用上面的makeExHtml()函数并传递一个callback来执行runSequence()的其余部分:

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