clifdensprit
clifdensprit

Reputation: 4901

Run one task dependency before other dependencies

I have a build task that looks something like this:

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

The problem is that I want the dependency clean to run before js and css, but I also don't want clean to be a dependency of the js and css tasks. This is because I need them to run without clean when running them through my watch task.

How can I achieve this?

There's a long discussion here, with no real solution as far as I can tell.

Basically it seems that the dependency system is the recommended way to handle this, but I don't see anyway that the dependency system can handle this. At least not without having duplicate tasks.


The only option I can think of is to wrap the contents of the js & css tasks in functions, then run that function inside the task, making it a bit more DRY to have duplicate tasks.

example:

function jsTask() {
  return gulp.src(['dev/assets/scripts/vendor/*.js','dev/assets/scripts/*.js'])
  .pipe($.concat('scripts.js'))
  .pipe(gulp.dest('dist/assets/'));
}
gulp.task('js-build', ['clean'], jsTask);
gulp.task('js-watch', jsTask);

This is the method that I am using now.


This seems like a common use case, so this might be a duplicate question. But I couldn't find the answer through searching.

Upvotes: 1

Views: 546

Answers (1)

ddprrt
ddprrt

Reputation: 7574

This is actually something that will be solved with Gulp 4 with the introduction of series and parallel execution. So instead of having all concurrent at the same time, you can write something like this:

gulp.task('default', gulp.series('clean', gulp.parallel('js, css')))

In the meantime, you should take a look at the run-sequence plugin featured here: https://www.npmjs.com/package/run-sequence

gulp.task('default', function(callback) {
  runSequence('clean', /** the first task in sequence **/
    ['js', 'css'], /** those are run in parallel **/
    callback); /** tell the task to end **/
});

Upvotes: 2

Related Questions