In this post, we look into how to keep our Gruntfile clean and tidy. By keeping our Gruntfile clean and tidy, it is easier for us to refine and improve the Grunt build process with its numerous plugins.
Starting point
Let’s say you start a new Node project.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
At the end of these steps, you have a basic package.json
and Gruntfile
.
The basic Gruntfile would appear like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
|
load-grunt-tasks
plugin
In the original basic Gruntfile, we have to manually load our Grunt plugins, as
1 2 3 |
|
If you now uninstall the plugin via npm
and update your package.json
, but forget to update your Gruntfile
, your build will break.
With load-grunt-tasks
plugin, you can collapse that down to the following one-liner:
1
|
|
After requiring the plugin, it will analyze your package.json file, determine which of the dependencies are Grunt plugins and load them all automatically.
load-grunt-config
plugin
load-grunt-tasks
shrunk your Gruntfile in code and complexity a little, but task configurations still remain in the Gruntfile (defined in grunt.initConfig
).
As you configure a large application, it will still become a very large file.
This is when load-grunt-config
comes into play.
load-grunt-config
lets you break up your Gruntfile config by task.
With load-grunt-config
, your Gruntfile
may look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Note that load-grunt-config
also includes load-grunt-tasks
’s functionality.
The task configurations live in files in folder ./grunt/tasks
.
By default, ./grunt
folder is used but, in this example, using a custom path is demonstrated.
In other words, our directory structure should be like this:
1 2 3 4 5 6 |
|
The task configuration for each task is defined in respective file name.
For example, task concat
is defined in “grunt/tasks/concat.js”:
1 2 3 4 5 6 7 8 9 10 |
|
The list of registered task aliases such as default
is defined in aliases.js
file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
References
- Safari: Introducing Grunt: the JavaScript task runner
- Common Grunt plugins:
- load-grunt-config: key plugin to keep Gruntfile organized.
- concat
- Unit Testing: qunit
- Image optimization: imagemin
- Deploying: deploy
- Chaining: concurrent
- Project scaffolding with
grunt-init
- grunt-init-commonjs - Create a commonjs module, including Nodeunit unit tests.
- grunt-init-gruntfile - Create a basic Gruntfile.
- grunt-init-gruntplugin - Create a Grunt plugin, including Nodeunit unit tests.
- grunt-init-jquery - Create a jQuery plugin, including QUnit unit tests.
- grunt-init-node - Create a Node.js module, including Nodeunit unit tests.