Sourcemap support in Chrome greatly improves debugging
Monday, April 16, 2012 at 04:02AM The latest version of the Chrome browser has support for 'sourcemaps' which promises to make degugging compiled JavaScript much more pleasant.
Until now, a major drawback to compiled JavaScript (like what is produced by Mascara) is that debugging becomes more tricky. Moderne browsers have quite nice debugging tools, but they only operate on the generated code, not the source we initially wrote.
Browser vendors have recognized this issue and agreed on "source map", a standard for matching compiled javascript with source code. Chrome is the first browser to support this feature.
As an example, here is some code which will generate an error at runtime:
// some filler code just to be sure lines are different
class A {}
class B {} extands A {}
var a : Date = null;
g.getDate(); // should throw runtime error
This is how the runtime error will be displayed normally, if you open the Developer Tools window in Chrome: (Wrench icon -> Tools -> Developer Tools or press F12)

You see the error in the (more or less opaque) generated code. You have to manually match with the source code to find the source of the error.
But with sourcemap support, the browser can show the error directly in the original source:
It is even possible to set breakpoints and debug through the original source:

I havent yet investigated how "deep" the support is for sourcmap in Chrome, but it looks impressive. I would love to hear feedback from Mascara-users about how this feature works for them.
How it works
Under the hood Mascara generating a seperate "sourcemap"-file which associates code positions in the generated code with the source. (The sourcemap file has the same name as the generated file but with an additional ".map" extension.) Debugging requires requires that the map and source files is deployed along with the generated files. You might choose to only allow debugging on the development server and not deploy the map and sources to the production server.
How to enable
When using the command-line compiler, source maps are enabled with the cli argument "--generate-source-mappings-file", eg.
python translate.py somesourcefile.esx --generate-source-mappings-file
Sourcemaps are generated by default when using the Eclipse-plugin though.
It also reqires enabling in Crome developer tools. Click the bolt in the lower right corner of the window to get the settings window, and check "enable source map":

Chrome is the first browser to support source map, but support is also underway in Mozilla/Firefox.
Here is a more detailed article about source maps.
Olav |
2 Comments |