Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify file order with Grunt?

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?

like image 922
mpen Avatar asked Nov 29 '13 19:11

mpen


People also ask

Is Grunt deprecated?

grunt. util. _ is deprecated and we highly encourage you to npm install lodash and var _ = require('lodash') to use lodash .

Where do you define configuration of Grunt plugin?

Grunt Configuration Task configuration is specified in your Gruntfile via the grunt. initConfig method. This configuration will mostly be under task-named properties, but may contain any arbitrary data.

Is Grunt still used?

The Grunt community is still going strong and both tools look like they're going to be around for a while yet. I should mention that another up and coming alternative to task runners like Grunt and Gulp is simply using npm scripts with command-line tools.

What can I use instead of Grunt?

gulp, Webpack, npm, Yarn, and Gradle are the most popular alternatives and competitors to Grunt.


1 Answers

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.

like image 173
Ben Avatar answered Sep 29 '22 19:09

Ben