When we are developing any application there are lot of common repetitive tasks which we are required to do.Tasks such as using separate configurations for different environments,removing debug statements from production code and minifying JavaScript files are required very frequently.Most of the time we as developers perform these tasks manually.But this is not the best approach.Doing all these tasks manually can cause several problems such as:
- forgetting to perform some of the tasks.
- using the tasks in wrong sequence.
- time is wasted in performing these tasks.
This is where task runners such as gulp are useful.Gulp lets you automate tasks.Instead of you taking care of all these repetitive tasks you define these tasks once and ask gulp to run these tasks.
Getting started with Gulp
To use gulp you need to install Node.js which can be installed from the following url:
https://nodejs.org/en/download/
Install gulp globally
Initialize the directory where you want to use gulp
To initialize the directory just run the following command:
npm init
This command will create a package.json file.
It will ask you for some basic information.Once you enter the required information it will create package.json file.
If you see in the above folder you can see the following json file::
run the following command
npm install gulp –save-dev
this command adds gulp as dependency in the project package.json file
also if you see in the folder you can see the node_modules folder after you execute the above command:
Now you are ready to define your first Gulp task.
Define the following task in a file with name gulpfile.js.
var gulp = require('gulp'); gulp.task('sampletask', function() { console.log('My first task'); });
You have defined your first gulp task.You can execute the above task by using the following command:
Leave a Reply