Enhance Your JavaScript Debugging with Cross-Browser Source Maps

Originally published at: http://www.sitepoint.com/enhance-your-javascript-debugging-with-cross-browser-source-maps/

As a JavaScript developer, I’m sure you’ve already been falling into this scenario: something goes wrong with the production version of your code, and debugging it directly from the production server is a nightmare simply because it has been minified or has been compiled from another language such as TypeScript or CoffeeScript.

The good news? The latest versions of browsers can help you solve this problem by using source map. In this tutorial, I’ll show you how to find Source Maps in all of the browsers and get the most out of those few minutes you have to debug.

Wait, what are Source Maps?

According to the great Introduction to JavaScript Source Maps article, source map is “a way to map a combined/minified file back to an unbuilt state. When you build for production, along with minifying and combining your JavaScript files, you generate a source map which holds information about your original files”.

Please don’t hesitate to read Ryan Seddon’s article first as it goes in great details on how source map works. You’ll then learn that source map uses an intermediate file that does the matching between the production version of your code and its original development state. The format of this file is being described here: Source Map Revision 3 Proposal

Now to illustrate, I’m going to share the way we’re currently working while developing our WebGL Babylon.js open-source framework: http://www.babylonjs.com. It’s written in TypeScript. But the principles will remain the same if you’re using plain JavaScript compressed/minified or other languages such as CoffeeScript.

Let’s now play with the source map magic directly in the browsers.

The demo page we’re going to use

Recently, I’ve been implementing the support of the Gamepad API in our gaming engine. Let’s use its code for this tutorial.

In this article, I’m using the following browsers:

  • Internet Explorer 11 , August update (version 11.0.9600.17239) or even better, the developer channel version: devchannel.modern.ie supporting the Gamepad API. A sidenote on IE: Microsoft is working on a new browser Microsoft Edge so be sure to check latest web standards support for it: status.modern.IE.
  • Chrome 38 developer channel (version 38.0.2125.8 dev-m) / Opera 23
  • Firefox 31 or Firefox 34 Nightly

Navigate to this URL: http://david.blob.core.windows.net/babylonjs/gamepad/index.html and you’ll see this page:

GamePad Test Page

Plug an Xbox 360 or Xbox One controller in the USB port of your machine. Press the A button to activate the gamepad and play with it:

Test Page Properties

But don’t worry, you won’t need a gamepad controller to follow this tutorial.

Note: The TypeScript compiler is automatically generating the source map for you. If you’d like to generate a source map while generating your minified version of your code, I would recommend using Uglify JS 2: https://github.com/mishoo/UglifyJS2

For this article, I even mixed both. I’ve minified the JS generated by TypeScript and kept the source mapping intact using this command line:

uglifyjs testgamepad.js -o testgamepad.min.js --source-map testgamepad.min.js.map --in-source-map testgamepad.js.map

How to debug with the original source code

Using Internet Explorer 11

Once the gamepad test page has loaded, press F12 in IE11.

You’ll see that the HTML source is referencing 2 JavaScript files: babylon.gamepads.js at the beginning of the page & testgamepad.min.js at the very end. The first file is coming from our framework on Github and the second one a simple sample showing how to consume it.

Continue reading this article on SitePoint

Namastey

I read your blog it’s really nice and i like it very much.Thanks for sharing this useful information.

Thanks
sheena

Btw, Chrome also lets you add a source map. (Right-click on the .min.js file, then “Add source map…”.)

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.