gulp basics

Learning Gulp Basics | Javascript

In the past trimester, I was given a project of creating anything with MEAN.JS and it was the first time I used gulp, so I decided it was time to learn more about what gulp is and how does it work; with the help with a friend I understood how it worked. So now, let’s get started with gulp basics:

What is Gulp?

Gulp is a task runner and build system that uses the philosophy of code over configuration (I’m looking at you, Grunt!). At first glance, I had no idea what was happening in that gulpfile.js, but then I started reading and noticed very interesting things, like it’s simplicity for starters. You will mainly use four functions:

  • gulp.task:
    Defines a task and it’s purpose.

    This example is taken from a chat I made for the class. I’m creating a task to compile SASS into CSS using gulp-sass dependancy. The function .pipe allows gulp to connect to other plugins/functions as you can see in the example.

  • gulp.dest:
    Sets the output file.
  • gulp.watch:
    Listes for any update in a file, if it, it can execute a task like recompiling sass.

    In this example, I’m watching all the javascript and sass files, so gulp can execute the task made

  • gulp.src:
    Sets the files we are going to use.

Installation

In order to install and use gulp, you need to make sure you have it globally:

The next step is add gulp into the project:

Ta-da! You are ready to use gulp! Now, you will definitely need other dependacies, which you install the same way you install gulp:

Useful Tasks

You can use browserify to bundle all the selected javascript files into one file, babelify to compile jsx and es6 to es5, and finally, uglify to compile everything into a minified file.

Compile SASS to CSS.

Configure JSHint task to check selected files.

Start the server.

Restart the server.

And so much more! Gulp seems easy to understand, but it can get complex (which I haven’t reached yet). Considering that this is the first task runner I use, I find it very useful and impressed with what we can do with it. I hope you managed to understand gulp basics. Please let me know if you have any comments, questions, or any recommendations.