Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 832 of the free egghead.io lessons, plus get JavaScript content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Just one more step!

Check your inbox for an email from us and click link to unlock your lesson.



Bundling Your JavaScript Files with Gulp

3:31 JavaScript lesson by

We will look at bundling your JavaScript files using Gulp, Gulp-Concat, and Gulp-Uglify


egghead.io comment guidelines

Avatar
egghead.io

We will look at bundling your JavaScript files using Gulp, Gulp-Concat, and Gulp-Uglify

Today, I'm going to go over bundling your JavaScript files with gulp. What I mean by bundling is taking multiple JavaScript files, concatenating them down into one file, then minifying or uglifying that one file to make it as small as possible.

The benefits to this are, rather than loading, let's say, 20 different resources on page load, you can load one resource that is a lot smaller than those 20 combined. The first thing that we need to do is install gulp in our project.

To do that, we're just going to go ahead and type npm install gulp. Now that that's been installed, we need to go into our gulp file.js and include gulp. To do that, we'll type var gulp = require('gulp').

Next thing we need to do is create our first task. We'll call this task default, which is what gulp looks for when you run the vanilla gulp command in your terminal. For this task, we're going to return a console log that says that the task was ran.

We can go ahead and test this now by moving into our terminal and typing gulp. We'll see the default task was started, our console log was spit out, and then default task was finished. Now moving on to the bundle portion of our project, we're going to first install gulp concat.

We'll type npm install gulp-concat. What this will allow us to do is specify multiple files that we want concatenated into one file, and the destination where we want that file to go. First, we'll include concat by requiring gulp-concat.

Let's go ahead and create a new task this time called scripts. This is where we'll do all of our bundling work. First, we're going to specify the source of our files. We'll do that with gulp source. I'll go ahead and type in a globbing pattern, that we'll go over in a second, of all files ending in .js.

We'll pipe the results of that into our concat method, which we will then pipe into our gulp.destination. Which will tell us where we want this file to end up. We need to specify what we want that concatenated file to be called, so we'll go ahead and call it bundle.js.

Let's change our default tasks now. Instead of running the function itself to just run a set of other tasks, for now, just the scripts task. Back to our globbing pattern. What this is going to do for us is look in the .js folder and find any file ending with .js. And grab that to pipe into our next command.

I have two placeholder files here of bar.js and foo.js. Next, we're going to concatenate those files and call that file bundle.js, and then we're specifying a destination of a dist folder which will be created.

When we run the gulp command, that default task is going to run the scripts task. Which will go ahead and concatenate those files, creating a new folder called dist, and putting our bundle file inside that folder.

If we open it here, we'll see that both that foo and bar function are now included in one file. The next step is to set up the uglification or minification of that file. To do that, we'll run npm install gulp uglify.

Once that's been installed, we need to include it in our application. So we'll do var uglify = require('gulp-uglify'). It's really easy to include this in our task that already exists. We're just going to pipe the results of the concat operation into the uglify operation.

We don't need to change the source at all and we don't need to change the destination. Now, if we run gulp again, that default task will run the scripts task. If we open up our bundle.js for a second time, we'll see our uglifyed file.

So not only are the two files concatenated, but now, a whitespace has been removed, and it's been fit onto one line. Now we have one small fast file to load rather than two larger files. This is the basics of bundling your JavaScript files with gulp.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?