node-source-map-support/README.md

166 lines
6.2 KiB
Markdown
Raw Normal View History

2013-01-18 15:56:20 +08:00
# Source Map Support
This module provides source map support for stack traces in node via the [V8 stack trace API](http://code.google.com/p/v8/wiki/JavaScriptStackTraceApi). It uses the [source-map](https://github.com/mozilla/source-map) module to replace the paths and line numbers of source-mapped files with their original paths and line numbers. The output mimics node's stack trace format with the goal of making every compile-to-JS language more of a first-class citizen. Source maps are completely general (not specific to any one language) so you can use source maps with multiple compile-to-JS languages in the same node process.
2013-01-19 03:04:24 +08:00
2013-07-21 07:41:35 +08:00
## Installation and Usage
2013-01-19 03:04:24 +08:00
#### Node support
2013-01-19 03:04:24 +08:00
npm install source-map-support
2013-07-21 07:41:35 +08:00
Source maps can be generated using libraries such as [source-map-index-generator](https://github.com/twolfson/source-map-index-generator). Once you have a valid source map, insert the following two lines at the top of your compiled code:
2013-01-18 15:56:20 +08:00
2013-07-21 07:41:35 +08:00
//# sourceMappingURL=path/to/source.map
require('source-map-support').install();
2013-07-21 07:41:35 +08:00
The path should either be absolute or relative to the compiled file.
#### Browser support
This library also works in Chrome. While the DevTools console already supports source maps, the V8 engine doesn't and `Error.prototype.stack` will be incorrect without this library. Everything will just work if you deploy your source files using [browserify](http://browserify.org/). Just make sure to pass the `--debug` flag to the browserify command so your source maps are included in the bundled code.
This library also works if you use another build process or just include the source files directly. In this case, include the file `browser-source-map-support.js` in your page and call `sourceMapSupport.install()`. It contains the whole library already bundled for the browser using browserify.
<script src="browser-source-map-support.js"></script>
<script>sourceMapSupport.install();</script>
This library also works if you use AMD (Asynchronous Module Definition), which is used in tools like [RequireJS](http://requirejs.org/). Just list `browser-source-map-support` as a dependency:
<script>
define(['browser-source-map-support'], function(sourceMapSupport) {
sourceMapSupport.install();
});
</script>
2013-07-21 07:41:35 +08:00
## Options
2013-07-21 07:41:35 +08:00
This module installs two things: a change to the `stack` property on `Error` objects and a handler for uncaught exceptions that mimics node's default exception handler (the handler can be seen in the demos below). You may want to disable the handler if you have your own uncaught exception handler. This can be done by passing an argument to the installer:
2013-07-21 07:41:35 +08:00
require('source-map-support').install({
handleUncaughtExceptions: false
});
2013-07-21 07:41:35 +08:00
This module loads source maps from the filesystem by default. You can provide alternate loading behavior through a callback as shown below. For example, [Meteor](https://github.com/meteor) keeps all source maps cached in memory to avoid disk access.
2013-07-21 07:41:35 +08:00
require('source-map-support').install({
retrieveSourceMap: function(source) {
if (source === 'compiled.js') {
return {
url: 'original.js',
map: fs.readFileSync('compiled.js.map', 'utf8')
};
}
return null;
}
});
2013-07-21 07:41:35 +08:00
## Demos
2013-07-28 06:49:46 +08:00
#### Basic Demo
2013-07-21 07:41:35 +08:00
original.js:
2013-07-21 07:41:35 +08:00
throw new Error('test'); // This is the original code
2013-07-21 07:41:35 +08:00
compiled.js:
2013-07-21 07:41:35 +08:00
//# sourceMappingURL=compiled.js.map
require('source-map-support').install();
2013-07-21 07:41:35 +08:00
throw new Error('test'); // This is the compiled code
2013-01-18 15:56:20 +08:00
2013-07-21 07:41:35 +08:00
compiled.js.map:
2013-01-18 15:56:20 +08:00
2013-07-21 07:41:35 +08:00
{
"version": 3,
"file": "compiled.js",
"sources": ["original.js"],
"names": [],
2014-04-27 02:15:38 +08:00
"mappings": ";;;AAAA,MAAM,IAAI"
2013-07-21 07:41:35 +08:00
}
2013-01-18 15:56:20 +08:00
2013-07-21 07:41:35 +08:00
Run compiled.js using node (notice how the stack trace uses original.js instead of compiled.js):
2013-01-18 15:56:20 +08:00
2013-07-21 07:41:35 +08:00
$ node compiled.js
2013-01-18 15:56:20 +08:00
2013-07-21 07:41:35 +08:00
original.js:1
throw new Error('test'); // This is the original code
2013-07-21 07:41:35 +08:00
^
Error: test
at Object.<anonymous> (original.js:1:7)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
2013-01-18 15:56:20 +08:00
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
2013-01-18 15:56:20 +08:00
2013-07-28 06:49:46 +08:00
#### TypeScript Demo
2013-01-18 15:56:20 +08:00
2013-07-21 07:41:35 +08:00
demo.ts:
2013-01-18 15:56:20 +08:00
declare function require(name: string);
require('source-map-support').install();
2013-01-18 15:56:20 +08:00
class Foo {
constructor() { this.bar(); }
bar() { throw new Error('this is a demo'); }
}
new Foo();
2013-07-21 07:41:35 +08:00
Compile and run the file using the TypeScript compiler from the terminal:
2013-01-18 15:56:20 +08:00
$ npm install source-map-support typescript
$ node_modules/typescript/bin/tsc -sourcemap demo.ts
2013-07-21 07:41:35 +08:00
$ node demo.js
2013-01-18 15:56:20 +08:00
demo.ts:5
2013-01-18 15:56:20 +08:00
bar() { throw new Error('this is a demo'); }
^
2013-01-18 15:56:20 +08:00
Error: this is a demo
at Foo.bar (demo.ts:5:17)
at new Foo (demo.ts:4:24)
at Object.<anonymous> (demo.ts:7:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
2013-01-18 15:56:20 +08:00
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
2013-01-18 15:56:20 +08:00
2013-07-28 06:49:46 +08:00
#### CoffeeScript Demo
2013-07-21 07:41:35 +08:00
demo.coffee:
2013-07-21 07:41:35 +08:00
require('source-map-support').install()
foo = ->
bar = -> throw new Error 'this is a demo'
bar()
foo()
Compile and run the file using the CoffeeScript compiler from the terminal:
$ npm install source-map-support coffee-script
$ node_modules/coffee-script/bin/coffee --map --compile demo.coffee
$ node demo.js
demo.coffee:3
2013-07-21 07:41:35 +08:00
bar = -> throw new Error 'this is a demo'
^
2013-07-21 07:41:35 +08:00
Error: this is a demo
at bar (demo.coffee:3:22)
at foo (demo.coffee:4:3)
at Object.<anonymous> (demo.coffee:5:1)
at Object.<anonymous> (demo.coffee:1:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
2013-07-21 07:41:35 +08:00
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
2013-07-21 07:41:35 +08:00
## License
2013-01-18 15:56:20 +08:00
This code is available under the [MIT license](http://opensource.org/licenses/MIT).