UseGrunt to Automate Those Routine Tasks
The word ‘grunt' is slang for one who does routine or mundane tasks. Here's an introduction to Grunt, the JavaScript task runner that can perform routine tasks that programmers would otherwise have to do. Also learn how to set a bare minimum configuratio
When working with any application using Web development technologies, the use of JavaScript is almost inevitable. Nowadays, JavaScript has even found its way into server side technologies. Even for the simplest of applications, the JavaScript code will not be small and simple. In fact, with time, the JavaScript code will start to grow and expand over multiple lines and files. And the larger the size gets, the more the time spent to organise and test the code. Maintenance also becomes tedious. Having to do all these tasks manually increases the probability of error. It is precisely for this reason that we prefer to use an automation tool. Grunt is a JavaScript task runner that helps to manage and automate the build tasks significantly. In this article, I will discuss how to set up Grunt, and then focus on enabling you to set the bare minimum configuration required for the very simple automated task of uglification using Grunt.
Prerequisites to using Grunt are Node.js and Grunt CLI. First, let’s look into how to install Node.js, which is pretty straight forward. You may choose to build and install it manually. But there are binaries available for Windows and Mac, which can be used to directly install Node.js without having to build it. And for Linux, the tar file can be downloaded, then untarred and simply run, as follows:
./configure && make && sudo make install
Once Node.js is installed, we need to install Grunt CLI, which is used to launch Grunt. But installing Grunt CLI doesn’t install the Grunt task runner. The job of the Grunt CLI is to run the version of Grunt that has been installed next to the Grunt file in your project. This way, one can install multiple versions of Grunt on the same machine.
To install Grunt CLI, enter the following in your command shell:
npm install -g grunt-cli
npm is the Node package manager and is used to install any Node.js related packages. It ensures that the package and all its dependencies are installed. There are a few things that might cause the installation to fail. Make sure that you either use sudo (for Mac and Linux-based systems), or launch the command shell as an administrator (for Windows) if you do not have the required privileges. Also, while working behind firewalls and proxies, you will have to explicitly set the proxy for npm. Use the following command to set up the proxy for npm:
npm config set proxy http://proxy.company.com:portNumber npm config set https-proxy http://proxy.company.com:portNumber
Here’s another note about the installation command: the -g flag ensures that the node module ( grunt-cli in this case) is