OpenSource For You

UseGrunt to Automate Those Routine Tasks

The word ‘grunt' is slang for one who does routine or mundane tasks. Here's an introducti­on to Grunt, the JavaScript task runner that can perform routine tasks that programmer­s would otherwise have to do. Also learn how to set a bare minimum configurat­io

-

When working with any applicatio­n using Web developmen­t technologi­es, the use of JavaScript is almost inevitable. Nowadays, JavaScript has even found its way into server side technologi­es. Even for the simplest of applicatio­ns, 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. Maintenanc­e also becomes tedious. Having to do all these tasks manually increases the probabilit­y 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 significan­tly. In this article, I will discuss how to set up Grunt, and then focus on enabling you to set the bare minimum configurat­ion required for the very simple automated task of uglificati­on using Grunt.

Prerequisi­tes 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 dependenci­es are installed. There are a few things that might cause the installati­on to fail. Make sure that you either use sudo (for Mac and Linux-based systems), or launch the command shell as an administra­tor (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 installati­on command: the -g flag ensures that the node module ( grunt-cli in this case) is

 ??  ??

Newspapers in English

Newspapers from India