我需要将一个参数从另一个任务传递给gullow任务,或者用runSequence调用的函数替换一个任务

I need to pass a parameter to a gulp task from another task or replace a task with a function called by runSequence

本文关键字:任务 一个 或者 runSequence 函数 gullow 替换 调用 参数 另一个      更新时间:2023-09-26

我有一个gump文件,用于创建多种检查的Html和Js,Ex1、Ex2等

这是我用来为Ex1创建这些的任务。它已经硬编码了对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 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"answers"makeEx2Html"等特定任务,但我不确定如何做到这一点。

请注意,所有这些任务都需要按顺序运行,这就是我使用runSequence的原因。

如果有任何建议,我将不胜感激。理想情况下,我希望使Html成为一个可以向其传递参数的函数,但我不确定如何将其适应我的需求。

理想情况下,我希望使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);
}

在您的gump任务中,您可以使用上面的makeExHtml()函数并传递一个回调,该回调将执行您的runSequence():的其余部分

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