Search code examples
javascriptgruntjsgrunt-contrib-concat

How to overwrite grunt task options in grunt-cli?


I am using grunt-contrib-concat and I have a simple concat task / config like below

concat: {
    options: {
        sourceMap: true
    },
    vendor: {
        src:['lib/**/*.js'],
        dest: 'dist/scripts/vendor.js'
    },
    app: {
        src:['app/**/*.js'],
        dest: 'dist/scripts/app.js'
    }
}

So when I am running above task through console I would like to be able to specify enable / disable sourceMap generation. Source map generation can take forever.

I tried below but none worked.

grunt concat:vendor --sourceMap=false
grunt concat --sourceMap=false

Thanks.


Solution

  • I know one way to do this, it requires you to write a custom task, it's easy.

    // Leave your `concat` task as above
    concat: ...
    
    
    // and then define a custom task as below (out of `grunt.config.init` call)
    grunt.registerTask('TASK_NAME', 'OPTIONAL_DESCRIPTION', function (arg) {
    
        // CLI can pass an argument which will be passed in this function as `arg` parameter
        // We use this parameter to alter the `sourceMap` option
        if (arg) {
            grunt.config.set('concat.options.sourceMap', false);
        }
    
        // Just run `concat` with modified options or pass in an array as tasks list
        grunt.task.run('concat');
    
    });
    

    This is simple, you can customize this template as your wishes.

    To use it, just use a ":" to pass extra parameter(s) in CLI like below:

    $ grunt concat:noSrcMap

    Basically you can pass anything as the parameter, it will be treated like a string (or undefined if no parameter passed).