6

Suppose I have this in my gulpfile:

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

gulp.task('bar', function () {
  if (something) {
    // how do I run task 'foo' here?
  }
});
1800 INFORMATION
  • 131,367
  • 29
  • 160
  • 239
  • How about [gulp-if](https://github.com/robrich/gulp-if)? – Anil Oct 05 '15 at 03:16
  • @AnilNatha I guess that would work, but why is that necessary? Why can't I just use a normal `if` and then some code to run another task? – 1800 INFORMATION Oct 05 '15 at 04:18
  • I think it's a decision by the developers to avoid gulp tasks being written with lots of if statements. The gulp piping is a clean and elegant solution. If the tasks are massively different, seperate them into two seperate tasks. If there's subtle difference (eg. add sourcemaps or not with minification), gulp-if is a good solution as the overall task still does the same job (minifying the code) – Adam Botley Oct 05 '15 at 14:33
  • This is way too broad. Please specify *what* should be conditional. Using `if` is perfectly valid, in some situations (e.g. a global build flag that decides between various build modes). In other situations, you can use a conditional stream (e.g. `gulp-if`). I've also used things like [`gulp-tap`](https://github.com/geejs/gulp-tap) to effectively make a task conditional. What would work depends on the specific problem you need to solve. – Louis Oct 05 '15 at 16:11
  • 3
    @Louis, I don't think my question is all that broad. It seems quite straightforward to me, how do I run one gulp task from inside of another? The `if` barely comes into it really – 1800 INFORMATION Oct 06 '15 at 01:46
  • @1800INFORMATION you're right, I take down my accusation of duplicity suspicion. It's not within pipe, it's calling gulp tasks manually, in ad-hoc fashion. This is a very good question by the way. – revelt Feb 09 '17 at 09:27
  • I'm floored by the criticism that this question "way too broad". How could it be any more clear and focused? – Corey Jan 09 '19 at 16:24

2 Answers2

2

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()
  }
}
revelt
  • 2,312
  • 1
  • 25
  • 37
0

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.

Meir
  • 14,081
  • 4
  • 39
  • 47