When you play the Web Developer role, sometimes you may have to endure some repetitive tasks like minification, unit testing, compilation, linting, beautify or unpack Javascript code and so on. To solve this problems, and in the meantime, try to keep your mental health in a good shape, you desperately need to find a way to automate this tasks. Grunt offers you an easy way to accomplish this kind of automation.
In this article I’ll try to explain how to automate some tasks with Grunt, but I recommend that you should take some time to read Grunt’s documentation and enjoy the experience by yourself.
So, in the following sections I’ll try to show you how to accomplish this tasks:
- Concatenate and create a minified version of your CSS, JavaScript and HTML files.
- Automatic generation of the documentation for JavaScript with JSDoc.
- Linting your JavaScript code.
- Reformat and reindent (prettify) your JavaScript code.
You can install Grunt via npm (Node Package Manager), so, to install Grunt you need to install Node.js first.
Now that you have Node.js and npm
installed is a good time to install
globally the Grunt CLI (Command Line Interface) package.
Once grunt-cli
is installed you need to go to the root directory of your
project and create a package.json
file, to accomplish this you can do the
following:
The previous command will ask you a series of questions in order to create the
package.json
file, package.json
basically store metadata for projects
published as npm
modules. It’s important to remember to add this file to your
source code versioning tool to ease the installation process of the development
dependencies among your partners via npm install
command.
At this point we can install Grunt and their respective plugins in the
existing package.json
with:
And the plugins that you need can be installed as follows:
Please note that the --save-dev
parameter will change your devDependencies
section in your package.json
. So, be sure to commit the updated package.json
file with your project whenever you consider appropriate.
Code documentation
If you document your code following the syntax rules defined on JSDoc 3, e.g.:
If you need more information about JSDoc, read their documentation, it’s easy to catch up.
The next step to automate the generation of the code documentation is to install first the grunt-jsdoc plugin as follows:
Once grunt-jsdoc
is installed you must create your Gruntfile.js
in the
root directory of your project and then add the jsdoc
entry to the options
of the initConfig
method.
Then, you need to load the plugin after the initConfig
method in the
Gruntfile.js
:
The resulting Gruntfile.js
until now is:
To generate the documentation, you need to call the jsdoc
task as follows:
Immediately you can see, inside the doc
directory, the available documentation
in HTML format with some beautiful styles by default.
Linting your JavaScript code
In order to find suspicious, non-portable or potential problems in JavaScript code or simply to enforce your team’s coding convention, whatever may be the reason, I recommend that you should include a static code analysis tool in your toolset.
The first step is to define your set of rules. I prefer to specify the set of
rules in an independent file called .jshintrc
located at the root directory of
the project, let’s see an example:
If you need more details about the checks that offer every rule mentioned above, please, read the page that contains a list of all options supported by JSHint
To install the grunt-contrib-jshint plugin, , please do as follows:
Next, proceed to add the jshint
entry to the options of the initConfig
method in the Gruntfile.js
as follows:
Then, as it’s done in the previous section, you need to load the plugin after
the initConfig
method in the Grunfile.js
To validate your JavaScript code against the previous set of rules you need to
call the jshint
task:
Note that if you need more information or explanations about the errors that you may receive after the previous command I suggest that you should visit JSLint Error Explanations site.
Code Style
As I mentioned in the previous section, I suggest that you should maintain an independent file where you define the set of rules about your coding styles:
Next, proceed to add the jsbeautifier
entry to the options of the initConfig
method in the Gruntfile.js
as follows:
The next step it to load the plugin after the initConfig
method:
To adjust your JS files according to the previous set of rules you need to call
the jsbeautifier
task:
Concat and minified CSS, HTML, JS
To reduce the size of your CSS, HTML and JS files do as follows:
Next, add the htmlmin
, cssmin
and uglify
entries to the options
of the initConfig
method in the Gruntfile.js
as follows:
The next step it to load the plugins after the initConfig
method:
To adjust your files according to the previous set of rules you need to call
the htmlmin
, cssmin
or uglify
tasks:
After loading all of your Grunt tasks you can create some aliases.
If you want to create an alias that runs the three previous tasks in one step do as follows:
So, to execute the three tasks in one step you can do the following:
The previous command is a wrapper around the htmlmin
, cssmin
and uglify
tasks defined before.
Wrapping all together we get the following [Grunfile.js][]
Other plugins
Grunt offers you a very large amount of plugins, they have a dedicated section to list them!
I recommend that you should take some time to check out the following plugins:
- grunt-newer lets you configure Grunt tasks to run with newer files only.
- grunt-contrib-watch lets you run predefined tasks whenever file patterns are added, changed or deleted.
- grunt-contrib-imagemin let you minify images.
Conclusion
Certainly, Grunt do a great job when we talk about task automation, also, automating this repetitive tasks is fun and reduce the errors associated with tasks that in the past we used to run manually over and over again. That been said, I definitely recommend that you should try Grunt, you will get in exchange an improvement in your development workflow, also with it you can forget to endure repetitive tasks that we hate to do, as a result you will get more free time to focus on getting things done.
Last but not least, another option for Grunt is Gulp, I recently read about it, if you look at their documentation you notice that their syntax is more clean than Grunt, also is very concise and allows chaining calls, following the concept of streams that pipes offer in *nix like systems, so, I’ll give it a try in my next project.