Grunto
Organize Your Grunt config
This plugin requires Grunt ~0.4.0
If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
$ npm install grunto --save-dev
Expanding the capabilities of Grunt. Maintenance of large config fles.
You can continue as you used to adding syntactical sugar to boring unmainteined Grunt configs.
- You can cut grunt build projects into litte pieces simplifying the maintenance.
- You can add runtime generation of task configs. That can help in some complex cases (e.g. with r.js).
- You can also use utilities to create grunt tasks.
History
We're using grunt for more than a year. It's a superb file handling, build and deployment tool. It's done a lot work for us so far and we're happy with it. But at some point we're facing issues with maintenance of task configs. We end up with 35 tasks (including custom ones), 180 subtasks, 20 aliases to maintain everything. Gruntfle.js s as fat as 2500 liines (not including custom tasks). We try arranging the task configs based on task names e.g. moving "copy" or "clean" to separate files. That helps a bit but is still a make-do. It doesn't solve the maintenance of the lines. Worse, it complicates search.
So we come up with purpose-based arrangement - the one employed in usual programming.
- cut config into files and put them in the "grunt" folder in the project root.
- split tasks based on their purpose The final structure looks something like
.├──grunt/| ├──database| | └──install.js| || ├──api-tester.js| | ├──compile| | | ├──templates.js| | | ├──styles.js| | | ├──scripts.js| | | └──environment.js| | ├──default.js| | ├──build.js| | ├──install.js| | └──watcher.js| || ├──opt| | ├──install.js| | ├──deploy.js| | └──watcher.js| || ├──client| | ├──compile| | | ├──templates.js| | | ├──styles.js| | | ├──scripts.js| | | ├──router.js| | | ├──locale.js| | | ├──images.js| | | └──default.js| | || | └──minify| | | ├──styles.js| | | ├──scripts.js| | | ├──images.js| | | └──default.js| | || | ├──install.js| | ├──deploy.js| | └──default.js| ...└──Gruntfile.js
Here you can see which parts of the system work with Grunt, which are then to be minified, deployed and processed. It's easy to choose place for your new system components.
To start the task grunt/client/compile/templates
you should only execute:
$ grunt client/compile/template
To start compiling all the the client's components, minify and prepare everything for deployment:
$ grunt client/compile client/minify
First all tasks from grunt/client/compile/default.js
and then all tasks from grunt/client/minify/default.js
will be fired.
Predictable, you must admit.
We aggreed that all tasks named **/default.js
, fire all tasks in it's folder in the right order.
We don't insist on that so it's up to your team but it worked fine for us.
Getting Started
Gruntfile.js
var grunto = ;moduleexports = ;
Init configs
Usually used to init general config for all similar task (with eq names, such as copy or clean)
// first case (default for grunt)moduleexports = ; // second casemoduleexports = ; // third casemoduleexports = ;
You should not use multiple config inits. It usually adds dissonance to configuration.
Advanced Usage
You have grunt modules fs
.├──grunt/├──default.js├──build/| ├──default.js| ├──styles.js| └── ...├── ...└── Gruntfile.js
Gruntfile.js:
moduleexports = ;
grunt/build/styles.js
module { var buildDir = thisBUILD_DIR; // eq contextOptions.BUILD_DIR (from params) thisgrunt; // eq grunt (from params) - grunt object thisCURRENT_PREFIX; // build/styles - (absolute) name of alias. // all inside task targets named with this prefix, example "copy:build/styles/1", "clean:build/styles/nameOfTarget" // files with "default" name in nested dir have prefix such as dirpath from scan cwd (in this case "grunt/") // init config this // 1. run copy task with params // 2. run "clean:build/styles/nameOfTargetForClean" task // 3. run function on this runtime moment // 4. run copy task with params, that was compiled after execute function in runtime // 5. run "copy:build/styles/nameOfTarget" task with params, that was compiled after execute function in runtime // run other tasks (in other dirs) ; // global config initializing return jshint: options: config: ".jshintrc" ;};
to run this task
$ grunt build/styles
Sum up
You can create complex setups avoiding code duplication and config confusion. This approach helps you to arrange appropriate structure for building complex projects that is clear, predictable and simple.