Give grunt some some context, provide named configs for common options and override individual options via the command line
MIT License
Grunt as of version 0.4.0 now itself provides support to pass environment options (http://gruntjs.com/api/grunt.option). It's recommended that this is now used instead of this plugin.
The aim of this plugin is to allow the grunt config to be overridden based on the current context selected. This is particularly useful if builds need to be tailored based upon the deployment environment.
Install this grunt plugin next to your project's grunt.js gruntfile with: npm install grunt-context
Then add this line to your project's grunt.js
gruntfile:
grunt.loadNpmTasks('grunt-context');
grunt-context supports arguments. The arguments are broken down as follows:
context: name : task : arguments
Tasks you wish to provide a context for should be defined inside the context object and under the options key inside your main config. Properties for a specific task will then be combined with any defined outside of the context object.
Example grunt file with contexts defined:
// ... grunt file contents
sometask: {
prop1: 'some original value',
prop2: 'some original value'
},
context: {
development: {
options: {
sometask: {
prop1: 'some development value',
prop3: 'some development value',
prop4: 'some development value'
}
},
tasks: {
'default': 'sometask'
}
}
}
// ... even more grunt file contents
The above configuration would result in the following config when grunt-context is run with the context set to "development".
sometask: {
prop1: 'some development value',
prop2: 'some original value'
prop3: 'some development value',
prop4: 'some development value'
},
Using the approach above you could also define the following tasks inside your grunt.js and when run the default task list for the context will run if it exists.
grunt.registerTask('default', 'context:development');
grunt.registerTask('release', 'context:production');
Tasks can also be defined against a context, in the same way in which you would normally use grunt.registerTask you can now directly associate tasks to a specific context.
tasks: {
'default': 'sometask'
},
When run any config options for the current context will be overriden. If a default task is found this will also be run.
To run the "default" task for a context or another task list, this is possible using the following syntax:
> grunt context:development // run default task for development context
> grunt context:development:test // run test task for development context
Config options can also be overriden using the following:
> grunt context:jshint.options.eqeqeq=false lint
> grunt.registerTask('release', 'context:jshint.options.eqeqeq=false lint');
A helper is also available which will allow other tasks to support overriding config properties via the command line or for general config overriding.
grunt.helper('context', 'some.plugin.value=1234', {
some: {
plugin: {
value2: 5678
}
}
}, 'someother.plugin.value=910');
The helper also has an alias of "config"
Depending upon the tasks you are using and the options provided you could structure your grunt.js file so that you can:
> grunt context:development requirejs
// ... grunt file contents
context: {
development: {
requirejs: {
js: {
pragmas: {
devExclude: false,
profileExclude: false,
remoteExclude: false
}
}
}
}
}
// ... grunt file contents
> grunt context:production requirejs
// ... grunt file contents
context: {
production: {
requirejs: {
js: {
pragmas: {
devExclude: true,
profileExclude: true,
remoteExclude: true
}
}
}
}
}
// ... grunt file contents
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using grunt.
Copyright (c) 2012 "indieisaconcept" Jonathan Barnett Licensed under the MIT license.