pgreen2
pgreen2

Reputation: 3651

Is it possible to have conditional dependencies in gulp?

I have two requirements for my build script:

  1. When I run gulp clean build, clean must complete before build starts.
  2. If I run gulp build, then clean shouldn't run.

So, if clean is specified, then build should wait for it, else start.

The first part is possible if I do

gulp.task('clean');
gulp.task('build', ['clean']);

However, that violates point 2

If I do

gulp.task('clean');
gulp.task('build');

That violates point 1

Is this possible with gulp?

Upvotes: 9

Views: 2070

Answers (2)

Preview
Preview

Reputation: 35806

You cannot run two gulp tasks with the same command like you did with dependency management you want.

Anyway you can pass an argument to your build task that will allow, using a little ternary, to wait for the clean one to complete before running.

So something like this:

gulp.task('build', (process.argv[3] === '--clean') ? ['clean'] : null, function () {
  ...
});

This way, you can launch your build normally with

gulp build

And when you want to call it with the clean:

gulp build --clean

There is a lot of ways to get better argument handling, like yargs or the env of gulp-util. But I found my method nice in the fact that it doesn't need any extra dependency.

Upvotes: 14

Kelly J Andrews
Kelly J Andrews

Reputation: 5111

Looks like you can use Gulp-If

gulp.task('build', function() {
  gulp.src('*.*')
    .pipe(gulpif(condition, clean()))
    .pipe(gulp.dest('./dist'));
});

Upvotes: 0

Related Questions