gulp.paralell()或gulp.series()中的条件gulp任务 [英] Conditional gulp task inside gulp.paralell() or gulp.series()

查看:265
本文介绍了gulp.paralell()或gulp.series()中的条件gulp任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关管道内部条件任务的信息过多(例如"gulp-if"插件).但是,实际上它不是条件任务":它是条件插件用法",并且一个任务可以使用多个插件.这是有条件地在管道内(例如,在gulp.paralell()内)运行任务的方法

Too much information about conditions tasks inside pipes (e. g. "gulp-if" plugin). However, actually it is not "conditional tasks": it is the "conditional plugin usage", and one task can use multiple plugins. Here is how to conditionally run task NOT inside pipe (for example, inside gulp.paralell())

假设任务名称可以包含用于提供易于理解的任务含义的空格.

Suppose that task name can contain spaces for providing easy-to-understand task meaning.

gulp.task('Build', gulp.paralell(
    'Preprocess HTML',
    'Prepeocess styles',
    done => {
        if(checkSomeCondition()){
            runTask('some Task') // but how?
        }
        else {
            done();
        }
    }
))

推荐答案

对于gulp 4,首先创建此辅助函数:

For gulp 4, first create this helper function:

function gulpTaskIf(condition, task) {
  task = gulp.series(task) // make sure we have a function that takes callback as first argument
  return function (cb) {
    if (condition()) {
      task(cb)
    } else {
      cb()
    }
  }
}

  • 作为第一个参数,此帮助器采用函数形式的条件.条件函数在任务执行开始时运行,因此您可以检查条件函数中先前步骤的输出.
  • 第二个参数指定要运行的任务,并且可以与gulp.parallel()gulp.series()接受的参数相同,即字符串,函数引用或来自另一个gulp.parallel()gulp.series()的返回值.
  • 返回一个函数,该函数可以作为第二个参数或作为gulp.parallel()gulp.series()调用的参数传递给gulp.task().
    • As its first argument, this helper takes a condition in the form of a function. The condition function is run at the time when the task execution is to start, so you can i.e. check output of previous steps in the condition function.
    • The second arguments specifies the task to be run and can be the same values that gulp.parallel() or gulp.series() accept as arguments, i.e. string, function reference, or a return value from another gulp.parallel() or gulp.series().
    • Returns a function that can be passedto gulp.task() as second argument or as an argument to gulp.parallel() or gulp.series() call.
    • 示例(第一个匹配问题)

      Examples (first one matches question):

        嵌入在gulp.parallel()或gulp.series(),按名称调用任务
      • embedded in e.g. gulp.parallel() or gulp.series(), calling task by name
      gulp.task('Build', gulp.parallel(
          'Preprocess HTML',
          'Prepeocess styles',
          runTaskIf(checkSomeCondition, 'some Task')
      ))
      

      • 作为任务,按名称调用任务
      • function myTask() {
          return gulp.src(...)
            ...
            .dest(...)
        }
        gulp.task('my-task', myTask)
        gulp.task('default', gulpTaskIf(
          function () {
            return Math.random() < 0.5; // example condition
          },
          'my-task')
        

        • 作为独立任务,按功能引用调用任务
        • function myTask() {
            return gulp.src(...)
              ...
              .dest(...)
          }
          gulp.task('default', gulpTaskIf(() => Math.random() < 0.5, myTask)
          

          • 作为独立任务,调用gulp.parallel()或gulp.series()参考
          • const manyTasks = gulp.parallel(task1, task2, task3)
            gulp.task('default', gulpTaskIf(
              function () {
                return Math.random() < 0.5;
              },
              manyTasks)
            

            这篇关于gulp.paralell()或gulp.series()中的条件gulp任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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