meteor/node-source-map-support

Name: node-source-map-support

Owner: Meteor

Description: Adds source map support to node.js (for stack traces)

Forked from: evanw/node-source-map-support

Created: 2015-06-25 21:51:07.0

Updated: 2017-09-28 16:06:02.0

Pushed: 2017-09-28 17:37:32.0

Homepage: null

Size: 457

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Source Map Support

Build Status

This module provides source map support for stack traces in node via the V8 stack trace API. It uses the 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.

Installation and Usage
Node support
m install source-map-support

Source maps can be generated using libraries such as source-map-index-generator. Once you have a valid source map, insert the following line at the top of your compiled code:

ire('source-map-support').install();

And place a source mapping comment somewhere in the file (usually done automatically or with an option by your transpiler):

sourceMappingURL=path/to/source.map

If multiple sourceMappingURL comments exist in one file, the last sourceMappingURL comment will be respected (e.g. if a file mentions the comment in code, or went through multiple transpilers). The path should either be absolute or relative to the compiled file.

It is also possible to install the source map support directly by requiring the register module which can be handy with ES6:

rt 'source-map-support/register'

nstead of:
rt sourceMapSupport from 'source-map-support'
ceMapSupport.install()

Note: if you're using babel-register, it includes source-map-support already.

It is also very useful with Mocha:

cha --require source-map-support/register tests/
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. 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.

ipt src="browser-source-map-support.js"></script>
ipt>sourceMapSupport.install();</script>

This library also works if you use AMD (Asynchronous Module Definition), which is used in tools like RequireJS. Just list browser-source-map-support as a dependency:

ipt>
fine(['browser-source-map-support'], function(sourceMapSupport) {
sourceMapSupport.install();
;
ript>
Options

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:

ire('source-map-support').install({
ndleUncaughtExceptions: false

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 keeps all source maps cached in memory to avoid disk access.

ire('source-map-support').install({
trieveSourceMap: function(source) {
if (source === 'compiled.js') {
  return {
    url: 'original.js',
    map: fs.readFileSync('compiled.js.map', 'utf8')
  };
}
return null;


The module will by default assume a browser environment if XMLHttpRequest and window are defined. If either of these do not exist it will instead assume a node environment. In some rare cases, e.g. when running a browser emulation and where both variables are also set, you can explictly specify the environment to be either 'browser' or 'node'.

ire('source-map-support').install({
vironment: 'node'

To support files with inline source maps, the hookRequire options can be specified, which will monitor all source files for inline source maps.

ire('source-map-support').install({
okRequire: true

This monkey patches the require module loading chain, so is not enabled by default and is not recommended for any sort of production usage.

Demos
Basic Demo

original.js:

w new Error('test'); // This is the original code

compiled.js:

ire('source-map-support').install();

w new Error('test'); // This is the compiled code
he next line defines the sourceMapping.
sourceMappingURL=compiled.js.map

compiled.js.map:


ersion": 3,
ile": "compiled.js",
ources": ["original.js"],
ames": [],
appings": ";;AAAA,MAAM,IAAI"

Run compiled.js using node (notice how the stack trace uses original.js instead of compiled.js):

de compiled.js

inal.js:1
w new Error('test'); // This is the original code
  ^
r: test
at Object.<anonymous> (original.js:1:7)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
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
TypeScript Demo

demo.ts:

are function require(name: string);
ire('source-map-support').install();
s Foo {
nstructor() { this.bar(); }
r() { throw new Error('this is a demo'); }

Foo();

Compile and run the file using the TypeScript compiler from the terminal:

m install source-map-support typescript
de_modules/typescript/bin/tsc -sourcemap demo.ts
de demo.js

.ts:5
r() { throw new Error('this is a demo'); }
            ^
r: 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)
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
CoffeeScript Demo

demo.coffee:

ire('source-map-support').install()
= ->
r = -> throw new Error 'this is a demo'
r()
)

Compile and run the file using the CoffeeScript compiler from the terminal:

m install source-map-support coffee-script
de_modules/coffee-script/bin/coffee --map --compile demo.coffee
de demo.js

.coffee:3
r = -> throw new Error 'this is a demo'
                 ^
r: 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)
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)
Tests

This repo contains both automated tests for node and manual tests for the browser. The automated tests can be run using mocha (type mocha in the root directory). To run the manual tests:

License

This code is available under the MIT license.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.