Conditional gulp task inside gulp.paralell() or gu

2019-09-01 18:39发布

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 gulp-if
1条回答
可以哭但决不认输i
2楼-- · 2019-09-01 19:02

The beauty of gulp4.0 is that your tasks can just be functions, so the following works:

gulp.task('Preprocess HTML', function () {
  console.log("in Preprocess HTML");
  return gulp.src('./');
});

You can use either the above version (the 'old way') or the newer way below.

I show two tasks here that use both versions but I personally wouldn't mix them.

// function PreprocessHTML() {
//   console.log("in Preprocess HTML");
//   return gulp.src('./');
// }

function PreprocessStyles() {
  console.log("in Preprocess styles");
  return gulp.src('./');
}

function testTaskTrue() {
  console.log("in testTaskTrue");
  return gulp.src('./');
}

function testTaskFalse() {
  console.log("in testTaskFalse");
  return gulp.src('./');
}

function checkSomeCondition() {
  console.log("in checkSomeCondition");
  return false;
}

//    Again, I definitely wouldn't mix the two versions of tasks as shown below.
//    Just here for demonstration purposes.

gulp.task('test', gulp.parallel( 'Preprocess HTML',  PreprocessStyles,
  done => {
    if (checkSomeCondition()) {

        // so testTaskTrue can be any gulp4.0 task, easy to call since it just a function

      testTaskTrue();
    }
    else {
      testTaskFalse();
    }
    done();
  }
));
查看更多
登录 后发表回答