Conditional gulp task inside gulp.paralell() or gulp.series()

假装没事ソ 提交于 2019-12-11 06:05:43

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/51093666/conditional-gulp-task-inside-gulp-paralell-or-gulp-series

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!