0
gulp.task('build', function () {
  // Build, generate files...
});

gulp.task('clear', function () {
  // Delete generated files.
});

gulp.task('rebuild', function () {
  // First: clear
  // Then: build
});

In most case, "build" task doesn't need previous generated files to be removed (which slows the build process).

But there is times when I want to run "clear" and "build" task in one command:

Of course "rebuild" task depends on "clear" and "build", but if I use dependency hint like

gulp.task('rebuild', ['clear', 'build']);

the two tasks run asynchronously, which results in unexpected problem!


The easy way to solve this problem is run:

gulp clear
gulp build

but a single command

gulp rebuild

is easier, right? :P

By the way, gulp.series() has been deprecated...

1
  • What about gulp <task> --rebuild? Commented Mar 19, 2023 at 16:16

2 Answers 2

1

It's not perfect but should work. Extract build task into a function. Then excute in the build and rebuild tasks.

var build = function() {
    return gulp.src().concat();
}

gulp.task('build', function () {
  return build();
});

gulp.task('clear', function () {
  // Delete generated files.
});

gulp.task('rebuild', ['clear'], function () {
  return build();
});
Sign up to request clarification or add additional context in comments.

Comments

0

My solution isn't via Gulp as you requested, but NPM may be a better choice since you probably have NPM right there handy. All you're trying to do is save a few keystrokes.

Add an NPM script "rg" (rebuild-gulp) which does what you need:

"scripts": {
  "rg": "gulp clear && gulp build"
}

Invoke with npm run rg

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.