Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run some gulp task based on a conditional

Tags:

gulp

Suppose I have this in my gulpfile:

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

gulp.task('bar', function () {
  if (something) {
    // how do I run task 'foo' here?
  }
});
like image 624
1800 INFORMATION Avatar asked Oct 05 '15 01:10

1800 INFORMATION


People also ask

How do you define a task in gulp?

A private task looks and acts like any other task, but an end-user can't ever execute it independently. To register a task publicly, export it from your gulpfile. const { series } = require('gulp'); // The `clean` function is not exported so it can be considered a private task.

How do I run Gulpfile in Visual Studio?

Run a Gulp Task in Visual Studio CodeType "Run Task" and select it, which will bring up a list of tasks configured in Gulp. Choose the Gulp Task you want to run! Most of your Gulp Tasks will probably be automated using gulp watch, but manual Gulp Tasks can easily be run within Visual Studio Code.

What is gulp Gulpfile?

Gulpfile explained A gulpfile is a file in your project directory titled gulpfile. js (or capitalized as Gulpfile. js , like Makefile), that automatically loads when you run the gulp command.


1 Answers

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()
  }
}
like image 152
revelt Avatar answered Sep 28 '22 07:09

revelt