Sync Gulp Tasks with run-sequence

By  on  

gulp.js is an awesome utility for so many things.  I've recently started using gulp as a build tool for the next blog redesign, whenever that may come.  In the past I had written my own build scripts but they quickly got messy and I ran into problems with concurrent tasks and knowing when each was complete.  gulp.js has made my build code prettier but I still get caught with timing issues due to async tasks.

Of course the nature of JS is becoming async but sometimes I just want a "top down" build process -- that's where run-sequence comes in.  With run-sequence I can easily group tasks to ensure they are done before setting off other tasks!

run-sequence works by passing arguments in the form of arrays or strings; an array signifies the tasks can be run concurrently, a string signifies a single task:

var runSequence = require('run-sequence');

gulp.task('some-task', function() {
	runSequence(
		['task-1', 'task-2', 'task-3'], // These 3 can be done in parallel
		'task-4', // ...then just do this
		['task-5', 'task-5'], // ...then do these things in parallel
		'task-6', // ...then do this
		// ....
	);
});

Each successive argument waits for the previous task(s) to finish.  My future theme's working gulp build file default task looks as follows:

// Create the default run action, which should be the entire build
gulp.task('default', function() {
	runSequence(
		['copy-js-dir', 'copy-php-files', 'copy-image-files', 'compile-stylus'],
		'clone-prism',
		['minify-css', 'minify-js'],
		'replace-build-ids',
		'create-backup-zip',
		'move-to-wordpress'
	);
});

JavaScript purists will hate on me for not creating my own promises to avoid the need for sync and run-sequence, but to be honest, I don't care.  Adding my own promises would make the code messier and with little speed benefit.

Sometimes we have to make concessions for the sake of maintainability -- all developers know that.  run-sequence was a concession I've made to keep my code maintainable and logical in my own head.

Recent Features

  • By
    Responsive and Infinitely Scalable JS Animations

    Back in late 2012 it was not easy to find open source projects using requestAnimationFrame() - this is the hook that allows Javascript code to synchronize with a web browser's native paint loop. Animations using this method can run at 60 fps and deliver fantastic...

  • By
    Chris Coyier’s Favorite CodePen Demos

    David asked me if I'd be up for a guest post picking out some of my favorite Pens from CodePen. A daunting task! There are so many! I managed to pick a few though that have blown me away over the past few months. If you...

Incredible Demos

  • By
    Image Data URIs with PHP

    If you troll page markup like me, you've no doubt seen the use of data URI's within image src attributes. Instead of providing a traditional address to the image, the image file data is base64-encoded and stuffed within the src attribute. Doing so saves...

  • By
    Add Styles to Console Statements

    I was recently checking out Google Plus because they implement some awesome effects.  I opened the console and same the following message: WARNING! Using this console may allow attackers to impersonate you and steal your information using an attack called Self-XSS. Do not enter or paste code that you...

Discussion

  1. Pablo Perez

    Thanks for all your articles David!

  2. I recently came across Gulp and encountered the same issues… and found the same solution, before Gulp 4 is released !

    with run-sequence, I can even load/queue my tasks as modules and share them across different projects.

  3. Juha

    I’d add callback in tasks, at least for the sake of example – otherwise tasks depending on it would not wait until it is completed, right?

    gulp.task('some-task', function(done) {
      runSequence('task-1', ['task-2', 'task-3'], done);
    }
    
  4. Damn, that’s awesome! I remember seeing this module a while back and not thinking much of it, but the feature you demonstrate where you can define subtasks to run in parallel is very impressive.

    Took two minutes to setup, works like a charm. Thanks for sharing :)

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!