How to run some gulp task based on a conditional

怎甘沉沦 提交于 2019-12-23 10:48:00

问题


Suppose I have this in my gulpfile:

gulp.task('foo', ...);

gulp.task('bar', function () {
  if (something) {
    // how do I run task 'foo' here?
  }
});

回答1:


Gulp v3

Use deprecated but still working gulp.run

gulp.task('foo', ...)    

gulp.task('bar', function () {
  if (something) {
    gulp.run('foo')
  }
})

Alternatively, use use any plugins that consume task names as arguments, like run-sequence for example (which you will probably need anyway for running tasks in a strict sequence). I call my tasks conditionally this way (Gulp v3):

gulp.task('bar', (callback) => {
  if (something) {
    runSequence('foo', callback)
  } else {
    runSequence('foo', 'anotherTask', callback)
  }
})

Gulp v4

Your gulpfile, that is, gulpfile.babel.js for now, would set Gulp tasks as exported functions so you would call them directly:

export function foo () {
  ...
}

export function bar () {
  if (something) {
    foo()
  }
}



回答2:


You could make 'bar' a dependency of 'foo' and put the condition inside 'foo':

gulp.task('foo', function(){
  if(something){...}
}, 'bar');

gulp.task('bar', function(){});

This way bar will always run before foo, and foo can choose if it is necessary to run its own logic.



来源:https://stackoverflow.com/questions/32940288/how-to-run-some-gulp-task-based-on-a-conditional

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