Back to Top

An Introduction to Gulp JS

gulp-tutorial

Gulp JS provides facility to deal with static resource files, usually, processes files before outputting it to the browser. In this article, You will learn how to use Gulp JS to change the development process, so that the development is more efficient and easy.

What is Gulp JS?

Gulp JS is basically an open source task runner for JavaScript application. Gulp is a task/build runner for JavaScript based architectures. Gulp is npm module which is easy to use, easy to manage. There are few ready tasks which you can find in GitHub repositories.

You can plug Gulp with any web application as it goes with all like Angular JS, React JS or any Javascript architecture.

Gulp JS is a Nodejs automatic task runner for building code in front-end development and is a builder of automation projects. Gulp is not only optimizing website resources but also help to automatically complete a lot of repetitive tasks. Also, It greatly improves work efficiency.

Gulp JS is not a library or frameworks like ReactJS or AngularJS. It is just a toolkit for building Javascript applications.

Why Gulp JS needed?

There are some issues developer find while developing an application in Javascript.Gulp is mainly used for time-consuming and repetitive tasks. Gulp take care of all annoying task by plugins and tasks very easily.

Features:

This is basic features Gulp JS provide:

  1. Easy to learn
  2. Efficient
  3. Easy to use
  4. High Quality

How to Install Gulp

You can install Gulp command line. There is one npm(Node Package Manager) command which used to install Gulp. Here I am doing global installation gulp in which gulp is globally available for all application in your server.

Prerequisite

Most important is that you need to setup Node environment and for that install node js and npm in your server to use Gulp.

After executing above command, if you want to see whether the gulp installed correctly, you can execute the following command.

This command will return the version number of gulp if installed.

Next, You need to install gulp locally into the project

Here, —-save-dev is used to update the package.json file and update the dev-dependencies value to indicate that the project needs to depend on gulp.

Directory Structure Requirement

This ideal directory structure when you are using Gulp but it is not necessary that you have to follow this structure only. You can do changes in structure as per your requirement.

Let’s look at the directory structure.

dist
|__ app.css
|__ app.js
src
|__ app.css
|__ app.js
gulpfile.js

The main idea is to use gulpfile.js to distribute the tasks in separate documents. This is the easiest way to manage each task in one file.

Src folder will have the source code of the application before it compiled. After compilation done using Gulp, all the task and code will be put into the dist directory. You need gulpfile.js file in the root directory and it is the only file you need to use Gulp. This file describes all of the tasks and everything you want gulp to do.

How Gulp works?

Gulp is built on node streams. Node stream is the continuous flow of data that can be manipulated asynchronously. These streams facilitate the connections of file operations through the pipeline. The pipeline is basically a chain to process element and they are arranged in a manner that output of one element will be the input of next element and all this done using pipe operator or pipe() function.

What are the Gulp Common Tasks?

Gulp and grunt are very similar, but to write task in Gulp is very easy that’s why people are moving from grunt to Gulp. If you check task in Grunt, it is very complex to understand and write. Let’s understand Gulp tasks:

  1. Clean
  2. Concat
  3. Uglify
  4. Watch and Browser sync

Let’s start to understand task one by one.

1. Clean

Clean task is basically used to clean the files and directory using Gulp.

Here, included two modules, one is gulp and another is gulp-clean. gulp.task allows you to define the task. Gulp clean is used to clean your directory. Clean is the task name in gulp.task. Instead of clean, You can clear as well. There is a function gulp.src in which you need to pass the directory path. Pipe return those gulp files which are in mentioned directory and run the clean command.

gulp.src is used to point the source files. There is one more function gulp.dest which is for the destination folder to output files.

2. Concat

Concat task is mainly available for concatenation of files or code.

Here, gulp.src take the file from the source directory and combine all the files in vendor.js and put it into destination folder which is in the build.

If you have changed your dependencies, you need to make changes in vendor files. You can use a concat command to attach your new dependency files in vendor.js. Otherwise, you can do gulp.clean first and then do gulp.build, it will do the same thing but it is a tedious task because takes few minutes to takes all javascript files and put it into one file. So concat command is a better option.

3. Uglify

Uglify is the uglification of your code. It is minification of scripts and styles. Uglification removes all the spaces, newlines and comments from the files and making it much lighter which internally makes your application faster. So, Gulp takes care of that very easily using gulp.uglify.

gulp.dest() is used to write stream using the src file. It also creates the file and after creating the file, it continues to transfer the stream for other operations.

Here, first define the source path using gulp.src and then pipe to uglify files and put it into the destination using gulp.dest.

4. Watch

A watch will continuously watch the files so if you change any file which is in a watch, will go and list out changes and perform the action. Serve is the task name in this. browserSync will sync the watched file to the destination if any change made.

That’s it for now!! Hopefully you guys learn something from this article about gulp and hopefully, you will use Gulp in your upcoming project.

Share:
Share on Facebook2Share on LinkedIn1Tweet about this on TwitterShare on Google+0Buffer this pageDigg thisShare on Reddit0

Leave a Reply

Your email address will not be published. Required fields are marked *

Most Popular Posts

The Readers’ Poll – June 2012

Posted on 5 years ago

Bhumi

How to Import Users in MODx from CSV file

Posted on 5 years ago

Bhumi

How to set Expire Headers using .htaccess?

Posted on 2 years ago

Bhumi

How To Send Email in MODx

Posted on 5 years ago

Bhumi

Facade implementation in Laravel

Updated 3 weeks ago

Bhumi

MySQL coalesce() function

Updated 17 years ago

Bhumi

How to use JavaScript Promise API?

Updated 17 years ago

Bhumi

What are Lambdas and Closures in PHP?

Updated 17 years ago

Bhumi

The Reader’s Poll – June 2015

Posted on 2 years ago

Bhumi

The Reader’s Poll – August 2014

Posted on 3 years ago

Bhumi

The Reader’s Poll – June 2014

Posted on 3 years ago

Bhumi

The Readers’ Poll – May 2014

Posted on 3 years ago

Bhumi

The Readers’ Poll – August 2013

Posted on 4 years ago

Bhumi