While developing a web app, it’s a good idea to bundle all your various JavaScript files and libraries into a single, minified file for production. Bundling makes your app faster by reducing download size and saving the browser from having to perform multitudes of HTTP requests just to render the page.
However, bundling comes at a cost. Debugging bundled JavaScript is often a nightmare. If your app has an error, the error in the console points at the point in the bundle file where the code failed, which may or may not bear any resemblance to your actual project files.
Which would you rather debug?
Wouldn’t it be great if the debugger was smart enough to point you at your original, pre-bundled code instead? Enter source maps, a way to preserve reference to your original file and code structure, even through bundling.
Before We Begin
This article uses the following tools, so make sure you have them first (or at least the basic concepts about them):
- Gulp for build automation
- The following Gulp plugins:
- A browser with dev tools that support source maps:
- Google Chrome
- Firefox 23+ / Firebug
- Internet Explorer 11+
(PS: Grunt and Browserify can do source maps, too.)
How Source Maps Work
A source map is a composed of data that links pieces of your minified bundle back to the original, unbuilt files. Modern dev tools will automatically follow the map for any console.log or line queries you perform.
Showing The Problem
Let’s say I have a simple console.log in an angularjs controller someplace:
angular.module('myApp') .controller('CtrlMain', function ($scope) { console.log('hello'); });
When we run the app and look in Chrome’s JavaScript console, it provides a very unhelpful link to where the log was executed:
Oh, it’s in the one and only line in my bundled script? I never would have guessed.
To generate bundle.js, I’m using the following task in Gulp:
gulp.task('build-js', function () { gulp.src([jsDir + 'module.js',jsDir + '*.js']) .pipe(concat('bundle.js')) .pipe(ngAnnotate()) .pipe(uglify()) .pipe(gulp.dest('build')); });
This concatenates all JavaScript files into a single file called bundle.js, creates dependency injection annotations so Angular doesn’t choke on the minified code, minifies the code using gulp-uglify, and finally writes it to a build directory.
Adding Source Maps To The Build Process
Here’s how to add source map creation to this build process, using gulp-sourcemaps:
gulp.task('build-js', function () { gulp.src([jsDir + 'module.js',jsDir + '*.js']) .pipe(sourcemaps.init()) .pipe(concat('bundle.js')) .pipe(ngAnnotate()) .pipe(uglify()) .pipe(sourcemaps.write()) .pipe(gulp.dest('build')); });
Before we do anything to the JavaScript files, we call sourcemaps.init(). This tells gulp-sourcemaps to pay attention and generate our source maps while the build process occurs. sourcemaps.write() signals that we’re done processing the .js files and ready to write out the bundled file.
IMPORTANT NOTE: All plugins used between sourcemaps.init() and sourcemaps.write() must be supported specifically by gulp-sourcemaps. You can find a list of supported plugins here.
The Final Product
Now let’s build and run the app again and see what we get:
Much better
Now the console.log behaves as if we’re working with the clear, unbuilt versions of our files. We can click that link on the left to see exactly which line contains the log. Ahhhh.
tҺe website іѕ really good, I really like it!