我需要从另一个任务传递一个参数给一个吞噬任务,或者用一个由runSequence调用的函数替换一个任务 [英] I need to pass a parameter to a gulp task from another task or replace a task with a function called by runSequence

查看:111
本文介绍了我需要从另一个任务传递一个参数给一个吞噬任务,或者用一个由runSequence调用的函数替换一个任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用gulp文件为多种考试创建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){
returnFound file+ file;
)))
.pipe({basename:'base'}))
.pipe(gulp.dest('./'));

});

以下是我可以传递参数的函数:

 函数make_prod_index(name){
返回函数(){
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成为一个可以传递参数的函数,但我不确定如何将它符合我的要求。

解决方案


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

你可以做到这一点,除了你不只是传递参数给函数,还有一个回调函数<

 函数makeExHtml(code> cb 文件,cb){
return gulp.src(files,{base:process.cwd()})
.pipe(print(function(file){
returnFound file+文件;
)))
.pipe(重命名({basename:'base'}))
.pipe(gulp.dest('./'))
.on ('end',cb);



$ b $ p
$ b

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



pre $ gulp.task('make_prod_ex1',function(){
makeExHtml(config.srcEx1Html,function(){
runSequence(
' ()'
'rename_bundle_css',
'rename_bundle_js',
function() );
});


I have gulp file that I use to create the Html and Js for multiple kinds of exams, Ex1, Ex2 etc

Here's the task I use to create these for Ex1. It has hardcoded a call to the makeEx1Html task, three other tasks and followed by a function call where I can pass a parameter:

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

Here's the task that's hardcoded for 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('./'));

});

Here's the function where I can pass a parameter:

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('./'));
    }
}

I would like to avoid having a specific task like 'makeEx1Html' and 'makeEx2Html' etc but I am not sure how to do this.

Note all these task needs to run in order which is why I'm using runSequence.

I would appreciate any suggestions. Ideally I would like the task that makes the Html to be a function that I can pass a parameter to but I am not sure how to fit this into my requirements.

解决方案

Ideally I would like the task that makes the Html to be a function that I can pass a parameter to

You can do just that, except you don't just pass your parameters to the function, but also a callback cb that will be called when your function is done:

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);
}

In your gulp tasks you can then use the above makeExHtml() function and pass a callback that will execute the rest of your runSequence():

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

这篇关于我需要从另一个任务传递一个参数给一个吞噬任务,或者用一个由runSequence调用的函数替换一个任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆