How to specify file order with Grunt?

mpen picture mpen · Nov 29, 2013 · Viewed 8.4k times · Source

I've just started using Grunt, and I'm trying to get the concat task to concat my files in a specific order. Here's what I've got:

module.exports = function(grunt) {
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        concat: {
            options: {
                separator: ';'
            },
            dist: {
                src: ['www/js/*.js','www/js/main.js','!**/*.{min,pack}.js','!<%= concat.dist.dest %>','!<%= uglify.dist %>'],
                dest: 'www/js/<%= pkg.name %>.js'
            }
        },

I was hoping that by putting www/js/main.js second, it would move the file down to the bottom of the list, but that doesn't appear to be the case.

How can I impose some order on the list of files it matches?

Answer

Ben picture Ben · Nov 29, 2013

Your problem is that main.js is matched by the first pattern so the second pattern is made redundant. This might seem like a hacky way of doing it but basically you have to exclude it from the first pattern before you include it in the second; like so:

    concat: {
        options: {
            separator: ';'
        },
        dist: {
            src: ['www/js/*.js', '!www/js/main.js', 'www/js/main.js','!**/*.{min,pack}.js','!<%= concat.dist.dest %>','!<%= uglify.dist %>'],
            dest: 'www/js/<%= pkg.name %>.js'
        }
    }

Note that when using minimatch pattern order is important.