Michael Joseph Aubry Michael Joseph Aubry - 6 months ago 199
Javascript Question

gulp concat scripts in order?

Say for example you are building a project on backbone or whatever and you need to load scripts in a certain order, ex "underscore.js" needs to be loaded first, before "backbone.js".

How do I get it to concat the scripts so that it's in order?

// JS concat, strip debugging and minify
gulp.task('scripts', function() {
gulp.src(['./source/js/*.js', './source/js/**/*.js'])

I have the right order of scripts in my source/index.html, but since files are organized by alphabetic order, gulp will concat underscore after backbone.js, and the order of the scripts in my source/index.html does not matter, it looks at the files in the directory.

So does anyone have an idea on this?

Best idea I have is to rename the vendor scripts with 1,2,3 to give them the proper order, but I am not sure if I like this.

Edit: As I learned more I found browserify is a great solution, it can be a pain at first but it's great.


I had a similar problem recently with Grunt when building my AngularJS app. Here's a question I posted.

What I ended up doing is to explicitly list the files in order in the grunt config. So it looked like:


And things worked. And Grunt is apparently smart enough to not include the same file twice.

This works the same with Gulp.