Gulp is a task runner written in Javascript and run on Nodejs (similar to Grunt). It helps you to automate your daily boring, repetitive and time consuming development tasks when working with Javascript. Some examples of those tasks are compiling LESS file to CSS, browserify modules, compiling and uglify JS files. Simply defining those tasks in a task file and Gulp will take care of the rest for you. This post shows how to install and config Gulp as well as the basic usage.

# Installation

You can install Gulp using Nodejs packages managing system

Also, you will need to install Gulp locally inside your project to in order to load the module

Next, install some required Gulp plugins, I will explain about these plugins later

# A simple gulp file

Now you have all the dependencies installed, create a file named gulpfile.js in the root directory of your project with the content like this

First I will discuss about Gulp. There are only 5 main Gulp methods that you need to know

• task: defines a new task
• src: specifies the source files
• dest: specifies the source folder
• watch: monitor file changes
• start: invoke a task

Next, back to the above code, first I defined a task named default. Just skip it for now and I will come back to it later. The next task (lint) is used for syntax checking. The syntax is very simple, just set the source files using gulp.src. You can pass array of file names (can be regex) as the input. After that, pass the function that you want to apply on those files (jshint()) using pipe.

The third task (uglify) is a bit different and more complicated. It uses 2 modules uglify and rename at the same time. pipe is similar to operations chaining. The source files will be uglified first, append a suffix in file name and then saved in the destination folder (specified with gulp.dest(), in this case the public folder). Usually, the last pipe function in the pipe line indicates the output. In the previous task, the output is the default jshint reporter while the output of this command will be export to a directory using gulp.dest(). Also, I put ['lint'] in the task definition. It’s an array indicating the tasks that need to be completed before this task can run.

The last one you can guess from its name. It monitors all the changes to the source files, performs the lint and uglify tasks as the changes happens. The default is just combination of all the other 3 tasks.

# Running Gulp

Open Terminal and change to your project’s root directory. Run gulp with the input argument is the task that you want to run

Starting Gulp with no argument will invoke the default task. As a result, these two commands are the same

Now you have successfully created a simple gulp file. You can use Gulp Plugins Website to search for the plugins that you need.

# Avoid errors when watching files

The default Gulp’s watch method is really dumb. It makes Gulp quit unexpectedly whenever an error happens while watching. For example, when there is a syntax error in your code that causes the uglify module fail to parse the source file, the watch process will quit eventually and you have to restart it manually. To avoid this, use gulp-plumber (can be install through npm) to keep the pipes working after error event. In order to include it in your code, pipe it before the other operations like this