This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

Gulp and Browserify - Adding Babel & Source Maps

4:30 JavaScript lesson by

What is already a nice workflow using Gulp, Browserify, Watchify & Browsersync to enable auto rebuilds & live browser reloading can be further improved by adding Babelify. A simple wrapper around the popular transpiler Babel, Babelify can ‘transform’ source files as they pass through our stream to enable certain Javascript features not yet available in the Browser or Node. We end the lesson by looking at how to generate & extract sourcemaps using exorcist.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

What is already a nice workflow using Gulp, Browserify, Watchify & Browsersync to enable auto rebuilds & live browser reloading can be further improved by adding Babelify. A simple wrapper around the popular transpiler Babel, Babelify can ‘transform’ source files as they pass through our stream to enable certain Javascript features not yet available in the Browser or Node. We end the lesson by looking at how to generate & extract sourcemaps using exorcist.

Avatar
Rolando

Hello, thanks for this tutorial, first time I use broswerify but even though my gulp watch is running fine I'm getting 'Uncaught ReferenceError: require is not defined' in the console, broswerify is install but doesn't work, gulp doesn't throw you any error message, I clone your repo from github and that one does actually works in my machine, why is that??

In reply to egghead.io
Avatar
Ge

It looks like the exorcist library is not actively maintained anymore, and the example does not work.

The gulp-sourcemap library is a more up-to-date choice for now but there are bugs with the source map.

We've got a really nice workflow set up here with Gulp, Browserify and BrowserSync. Next, we want to add the ability to write in the next version of JavaScript, whatever that may be.

For example, should we want to use default function parameters, along with some string interpolation. Start our server to see this in action.

If we open up the console, we're going to see that we've got an error here. One expected token. This is because at the time of recording, default function parameters are not yet supported in Chrome.

To fix this, we can use a popular project called Babel. Close that, npm install babelify. Back in our Gulp file, we'll bring that in.

Babelify is a wrapper around the Babel project, that enables it to be used as a transformer for Browserify. Before we call bundle, we simply call transform and pass that along Babelify.

This will act on every file that comes through our bundle.

We can check that's working by running our server again. Reload the browser and you can see default function parameters have now compiled correctly. Just to prove it is working, you can change that, hit "Save," and there you go.

Now we have ES6 compilation, along with a live reloading development server that should enable a really efficient workflow.

There is one more piece to this puzzle, however. If I put a debug statement here, you can see that the debugger has RAM, but it's reporting that we're in bundle.js line 14. Which is correct, according to Chrome, because it's loading this file. You can see line 14 here.

But when debugging our code, we want it to report greeting.js line 2. For that, we need source maps. We'll close down the server, go back to our Gulp file, and install Exorcist. Bringing it into our Gulp file.

Exorcist, as the name suggests, can extract a source map from a Browserify stream. The way you hook this up is, you put it before, we've transformed the Browserify stream into the vinyl source stream.

We simply pipe into it, and we pass it along wherever we want our source map to be called. We'll pull it alongside the bundle file. We'll say app.js.map.

There's one more thing we need to do to enable this. Browserify needs to know that we're in debug mode, so that it generates source maps. We'll take this out of here, and we'll first add to it debug = true. That should be enough.

Let's run our task again. This time, when the debugger runs, we are in greeting.js line 2, which is exactly where we placed the debugger statement.


Featured course:

Create an SVG Icon System

Create an SVG Icon System

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