source-map-support
Advanced tools
Comparing version 0.1.8 to 0.1.9
{ | ||
"name": "source-map-support", | ||
"description": "Fixes stack traces for files with source maps", | ||
"version": "0.1.8", | ||
"version": "0.1.9", | ||
"main": "./source-map-support.js", | ||
@@ -10,3 +10,3 @@ "scripts": { | ||
"dependencies": { | ||
"source-map": "0.1.24" | ||
"source-map": "0.1.26" | ||
}, | ||
@@ -13,0 +13,0 @@ "devDependencies": { |
122
README.md
@@ -5,32 +5,69 @@ # Source Map Support | ||
### Installation | ||
## Installation and Usage | ||
npm install source-map-support | ||
This module takes effect globally and should be initialized by inserting `require('source-map-support').install()` at the top of your code. | ||
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: | ||
### CoffeeScript Demo | ||
//# sourceMappingURL=path/to/source.map | ||
require('source-map-support').install(); | ||
The following terminal commands show a stack trace in node with CoffeeScript filenames: | ||
The path should either be absolute or relative to the compiled file. | ||
$ cat > demo.coffee | ||
## Options | ||
require('source-map-support').install() | ||
foo = -> | ||
bar = -> throw new Error 'this is a demo' | ||
bar() | ||
foo() | ||
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: | ||
$ npm install source-map-support coffee-script | ||
$ node_modules/coffee-script/bin/coffee --map --compile demo.coffee | ||
$ node demo | ||
require('source-map-support').install({ | ||
handleUncaughtExceptions: false | ||
}); | ||
demo.coffee:4 | ||
bar = -> throw new Error 'this is a demo' | ||
^ | ||
Error: this is a demo | ||
at bar (demo.coffee:4:21) | ||
at foo (demo.coffee:5:3) | ||
at Object.<anonymous> (demo.coffee:6) | ||
at Object.<anonymous> (demo.coffee:2) | ||
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. | ||
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; | ||
} | ||
}); | ||
## Demos | ||
### Basic Demo | ||
original.js: | ||
throw new Error('test'); // This is the original code | ||
compiled.js: | ||
//# sourceMappingURL=compiled.js.map | ||
require('source-map-support').install(); | ||
throw new Error('test'); // This is the compiled code | ||
compiled.js.map: | ||
{ | ||
"version": 3, | ||
"file": "compiled.js", | ||
"sources": ["original.js"], | ||
"names": [], | ||
"mappings": ";;;AAAA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC" | ||
} | ||
Run compiled.js using node (notice how the stack trace uses original.js instead of compiled.js): | ||
$ node compiled.js | ||
original.js:1 | ||
throw new Error("test"); // This is the original code | ||
^ | ||
Error: test | ||
at Object.<anonymous> (original.js:1:7) | ||
at Module._compile (module.js:449:26) | ||
@@ -45,6 +82,4 @@ at Object.Module._extensions..js (module.js:467:10) | ||
The following terminal commands show a stack trace in node with TypeScript filenames: | ||
demo.ts: | ||
$ cat > demo.ts | ||
declare function require(name: string); | ||
@@ -58,5 +93,7 @@ require('source-map-support').install(); | ||
Compile and run the file using the TypeScript compiler from the terminal: | ||
$ npm install source-map-support typescript | ||
$ node_modules/typescript/bin/tsc -sourcemap demo.ts | ||
$ node demo | ||
$ node demo.js | ||
@@ -77,12 +114,35 @@ demo.ts:6 | ||
### Options | ||
### CoffeeScript Demo | ||
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 above). 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: | ||
demo.coffee: | ||
require('source-map-support').install({ | ||
handleUncaughtExceptions: false | ||
}); | ||
require('source-map-support').install() | ||
foo = -> | ||
bar = -> throw new Error 'this is a demo' | ||
bar() | ||
foo() | ||
### License | ||
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:4 | ||
bar = -> throw new Error 'this is a demo' | ||
^ | ||
Error: this is a demo | ||
at bar (demo.coffee:4:21) | ||
at foo (demo.coffee:5:3) | ||
at Object.<anonymous> (demo.coffee:6) | ||
at Object.<anonymous> (demo.coffee:2) | ||
at Module._compile (module.js:449:26) | ||
at Object.Module._extensions..js (module.js:467:10) | ||
at Module.load (module.js:356:32) | ||
at Function.Module._load (module.js:312:12) | ||
at Module.runMain (module.js:492:10) | ||
at process.startup.processNextTick.process._tickCallback (node.js:244:9) | ||
## License | ||
This code is available under the [MIT license](http://opensource.org/licenses/MIT). |
@@ -5,34 +5,54 @@ var SourceMapConsumer = require('source-map').SourceMapConsumer; | ||
exports.mapSourcePosition = mapSourcePosition = function(cache, position) { | ||
var sourceMap = cache[position.source]; | ||
if (!sourceMap && fs.existsSync(position.source)) { | ||
// Get the URL of the source map | ||
var fileData = fs.readFileSync(position.source, 'utf8'); | ||
var match = /\/\/[#@]\s*sourceMappingURL=(.*)\s*$/m.exec(fileData); | ||
if (!match) return position; | ||
var sourceMappingURL = match[1]; | ||
// Can be overridden by the retrieveSourceMap option to install. Takes a | ||
// generated source filename; returns a {map, optional url} object, or null if | ||
// there is no source map. The map field may be either a string or the parsed | ||
// JSON object (ie, it must be a valid argument to the SourceMapConsumer | ||
// constructor). | ||
var retrieveSourceMap = function (source) { | ||
if (!fs.existsSync(source)) | ||
return null; | ||
// Read the contents of the source map | ||
var sourceMapData; | ||
var dataUrlPrefix = "data:application/json;base64,"; | ||
if (sourceMappingURL.slice(0, dataUrlPrefix.length).toLowerCase() == dataUrlPrefix) { | ||
// Support source map URL as a data url | ||
sourceMapData = new Buffer(sourceMappingURL.slice(dataUrlPrefix.length), "base64").toString(); | ||
} | ||
else { | ||
// Support source map URLs relative to the source URL | ||
var dir = path.dirname(position.source); | ||
sourceMappingURL = path.resolve(dir, sourceMappingURL); | ||
// Get the URL of the source map | ||
var fileData = fs.readFileSync(source, 'utf8'); | ||
var match = /\/\/[#@]\s*sourceMappingURL=(.*)\s*$/m.exec(fileData); | ||
if (!match) return null; | ||
var sourceMappingURL = match[1]; | ||
if (fs.existsSync(sourceMappingURL)) { | ||
sourceMapData = fs.readFileSync(sourceMappingURL, 'utf8'); | ||
} | ||
// Read the contents of the source map | ||
var sourceMapData; | ||
var dataUrlPrefix = "data:application/json;base64,"; | ||
if (sourceMappingURL.slice(0, dataUrlPrefix.length).toLowerCase() == dataUrlPrefix) { | ||
// Support source map URL as a data url | ||
sourceMapData = new Buffer(sourceMappingURL.slice(dataUrlPrefix.length), "base64").toString(); | ||
} | ||
else { | ||
// Support source map URLs relative to the source URL | ||
var dir = path.dirname(source); | ||
sourceMappingURL = path.resolve(dir, sourceMappingURL); | ||
if (fs.existsSync(sourceMappingURL)) { | ||
sourceMapData = fs.readFileSync(sourceMappingURL, 'utf8'); | ||
} | ||
} | ||
if (sourceMapData) { | ||
sourceMap = { | ||
url: sourceMappingURL, | ||
map: new SourceMapConsumer(sourceMapData) | ||
if (!sourceMapData) { | ||
return null; | ||
} | ||
return { | ||
url: sourceMappingURL, | ||
map: sourceMapData | ||
}; | ||
}; | ||
var mapSourcePosition = exports.mapSourcePosition = function(cache, position) { | ||
var sourceMap = cache[position.source]; | ||
if (!sourceMap) { | ||
// Call the (overrideable) retrieveSourceMap function to get the source map. | ||
var urlAndMap = retrieveSourceMap(position.source); | ||
if (urlAndMap) { | ||
sourceMap = cache[position.source] = { | ||
url: urlAndMap.url, | ||
map: new SourceMapConsumer(urlAndMap.map) | ||
}; | ||
cache[position.source] = sourceMap; | ||
} | ||
@@ -51,3 +71,5 @@ } | ||
if (originalPosition.source !== null) { | ||
originalPosition.source = path.resolve(path.dirname(sourceMap.url), originalPosition.source); | ||
if (sourceMap.url) { | ||
originalPosition.source = path.resolve(path.dirname(sourceMap.url), originalPosition.source); | ||
} | ||
return originalPosition; | ||
@@ -58,3 +80,3 @@ } | ||
return position; | ||
} | ||
}; | ||
@@ -168,2 +190,7 @@ // Parses code generated by FormatEvalOrigin(), a function inside V8: | ||
// Allow source maps to be found by methods other than reading the files | ||
// directly from disk. | ||
if (options.retrieveSourceMap) | ||
retrieveSourceMap = options.retrieveSourceMap; | ||
// Provide the option to not install the uncaught exception handler. This is | ||
@@ -170,0 +197,0 @@ // to support other uncaught exception handlers (in test frameworks, for |
21916
456
145
+ Addedsource-map@0.1.26(transitive)
- Removedsource-map@0.1.24(transitive)
Updatedsource-map@0.1.26