Front end developers or UI developers often do certain kind of things like:
- Changing small codes of CSS and JS and combine them for production.
- compiling CSS/JS/HTML preprocessor.
- Compressing images, css, js, html etc.
- Switching and reloading browser for changes.
Now for compiling, minifying a preprocessor you will need some paid or free software like Prepros, Koala, Codekit etc. Some of them support windows or some mac.
For image compressor we have PunyPNG or other software and for auto update we can use some editors like brackets visual studio etc.
Now there are some preprocessors or templating language or operating system that these software editors doesn’t support then what will you do? One option is Google and install different softwares for that or another option is using GRUNT.
What is Grunt?
Grunt is a task runner that can improve the speed of front-end development. It can help to do repetitive task like minification, compilation, compression, auto reload etc.
To install grunt one need to install node and npm first.
Installing node and npm
- Download and install node.js from https://nodejs.org/en/
- Follow the prompts.
- Test node by opening cmd(command prompt) and write node -v. This must give you the version of node installed.
- Test npm by opening cmd and type npm -v. This must give you the version of node installed.
- Set up a new package.json file in your project and add value. Something like-
or just create 1 new one by command npm init.
- Install Grunt CLI(command line interface) by typing command npm install -g grunt-cli in cmd in your project.
- Install Grunt in the project you are working by typing npm install grunt –save-dev
- Test your grunt by typing command grunt –version and you must get the version of grunt and grunt cli installed.
Important node modules or plugins and Installing them
There are two ways to install a node module.
- By typing the respective command for the module or plugin.
- Mentioning the plugin details in package.json and running npm install command in cmd. By this way you can install multiple plugin at the same time.
There are lot of modules that are important for a front end developers. I am naming some of them and their installation command.
- autoprefixer – it add prefixes to CSS properties (npm install grunt-autoprefixer –save-dev)
- grunt-browser-sync – reload the browser on each saved changes in project on all browsers.
- (npm install grunt-browser-sync –save-dev)
- grunt-contrib-htmlmin – minifying html.
- (npm install grunt-contrib-htmlmin –save-dev)
- grunt-contrib-preprocessor – for preprocessor compilation depends on preprocessor
- (npm install grunt-contrib-less –save-dev)
- “grunt-contrib-uglify” – for formation of js,
- (npm install grunt-contrib-uglify –save-dev)
- grunt-contrib-watch – it watches the changes and runt grunt on each change
- (npm install grunt-contrib-watch –save-dev)
Setting up all modules for grunt
For this you have to create a gruntfile.js like this
Different plugins we initialize in the grunt file may have different ways to initialize and may have different options.
Few things that are must in a grunt file are:
pkg: By this we tell the grunt the plugin we are using are defined in package.json file.
grunt.loadNpmTasks: This function load the tasks from specified plugin in package.json installed via npm.
grunt.registerTask: Only those task will run that are specified in that function.