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-context supports arguments. The arguments are broken down as follows:
context: name : task : arguments
- name: The name of the context to use
- task: A task for the context
- arguments: The arguments to apply to the task if it's a function
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 contentssometask: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.
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.
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:
- From the command line
> grunt context:jshintoptionseqeqeq=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.
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:
- Enable code profiling whilst actively developing but remove it when doing a build for release
> grunt context:development requirejs// ... grunt file contentscontext:development:requirejs:js:pragmas:devExclude: falseprofileExclude: falseremoteExclude: false// ... grunt file contents> grunt context:production requirejs// ... grunt file contentscontext:production:requirejs:js:pragmas:devExclude: trueprofileExclude: trueremoteExclude: 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.
- Depricated config alias due to conflicts with core grunt
- Fixed defect introduced in 0.3.1
- Override bug && lint fix
- Tweaked helper so that it is standalone.
- Config alias added for context task and context helper
- Support for config options to be passed via the command line
- Updated tests
- Added support to helper to allow multiple overrides
- Config overriding now based on the keys defined under context.name.options
- Can now be run in conjuction with other tasks and still override configs
- Added typical examples section to documentation
- Initial Release
Copyright (c) 2012 "indieisaconcept" Jonathan Barnett
Licensed under the MIT license.