browserify support, fixes #16
This commit is contained in:
parent
be0579d0e1
commit
70c343e4c4
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "source-map-support",
|
"name": "source-map-support",
|
||||||
"description": "Fixes stack traces for files with source maps",
|
"description": "Fixes stack traces for files with source maps",
|
||||||
"version": "0.1.9",
|
"version": "0.2.0",
|
||||||
"main": "./source-map-support.js",
|
"main": "./source-map-support.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "node_modules/mocha/bin/mocha"
|
"test": "node_modules/mocha/bin/mocha"
|
||||||
|
@ -2,17 +2,43 @@ var SourceMapConsumer = require('source-map').SourceMapConsumer;
|
|||||||
var path = require('path');
|
var path = require('path');
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
|
|
||||||
|
function isInBrowser() {
|
||||||
|
return typeof window !== 'undefined';
|
||||||
|
}
|
||||||
|
|
||||||
|
function retrieveFile(path) {
|
||||||
|
// Use SJAX if we are in the browser
|
||||||
|
if (isInBrowser()) {
|
||||||
|
var xhr = new XMLHttpRequest();
|
||||||
|
xhr.open('GET', path, false);
|
||||||
|
xhr.send(null);
|
||||||
|
return xhr.readyState === 4 ? xhr.responseText : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise, use the filesystem
|
||||||
|
try {
|
||||||
|
return fs.readFileSync(path, 'utf8');
|
||||||
|
} catch (e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Support URLs relative to a directory, but be careful about a protocol prefix
|
||||||
|
// in case we are in the browser (i.e. directories may start with "http://")
|
||||||
|
function supportRelativeURL(dir, url) {
|
||||||
|
var match = /^\w+:\/\/[^\/]*/.exec(dir);
|
||||||
|
var protocol = match ? match[0] : '';
|
||||||
|
return protocol + path.resolve(dir.slice(protocol.length), url);
|
||||||
|
}
|
||||||
|
|
||||||
// Can be overridden by the retrieveSourceMap option to install. Takes a
|
// Can be overridden by the retrieveSourceMap option to install. Takes a
|
||||||
// generated source filename; returns a {map, optional url} object, or null if
|
// 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
|
// 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
|
// JSON object (ie, it must be a valid argument to the SourceMapConsumer
|
||||||
// constructor).
|
// constructor).
|
||||||
var retrieveSourceMap = function (source) {
|
var retrieveSourceMap = function (source) {
|
||||||
if (!fs.existsSync(source))
|
|
||||||
return null;
|
|
||||||
|
|
||||||
// Get the URL of the source map
|
// Get the URL of the source map
|
||||||
var fileData = fs.readFileSync(source, 'utf8');
|
var fileData = retrieveFile(source);
|
||||||
var match = /\/\/[#@]\s*sourceMappingURL=(.*)\s*$/m.exec(fileData);
|
var match = /\/\/[#@]\s*sourceMappingURL=(.*)\s*$/m.exec(fileData);
|
||||||
if (!match) return null;
|
if (!match) return null;
|
||||||
var sourceMappingURL = match[1];
|
var sourceMappingURL = match[1];
|
||||||
@ -23,15 +49,11 @@ var retrieveSourceMap = function (source) {
|
|||||||
if (sourceMappingURL.slice(0, dataUrlPrefix.length).toLowerCase() == dataUrlPrefix) {
|
if (sourceMappingURL.slice(0, dataUrlPrefix.length).toLowerCase() == dataUrlPrefix) {
|
||||||
// Support source map URL as a data url
|
// Support source map URL as a data url
|
||||||
sourceMapData = new Buffer(sourceMappingURL.slice(dataUrlPrefix.length), "base64").toString();
|
sourceMapData = new Buffer(sourceMappingURL.slice(dataUrlPrefix.length), "base64").toString();
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
// Support source map URLs relative to the source URL
|
// Support source map URLs relative to the source URL
|
||||||
var dir = path.dirname(source);
|
var dir = path.dirname(source);
|
||||||
sourceMappingURL = path.resolve(dir, sourceMappingURL);
|
sourceMappingURL = supportRelativeURL(dir, sourceMappingURL);
|
||||||
|
sourceMapData = retrieveFile(sourceMappingURL, 'utf8');
|
||||||
if (fs.existsSync(sourceMappingURL)) {
|
|
||||||
sourceMapData = fs.readFileSync(sourceMappingURL, 'utf8');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!sourceMapData) {
|
if (!sourceMapData) {
|
||||||
@ -68,7 +90,8 @@ var mapSourcePosition = exports.mapSourcePosition = function(cache, position) {
|
|||||||
// location in the original file.
|
// location in the original file.
|
||||||
if (originalPosition.source !== null) {
|
if (originalPosition.source !== null) {
|
||||||
if (sourceMap.url) {
|
if (sourceMap.url) {
|
||||||
originalPosition.source = path.resolve(path.dirname(sourceMap.url), originalPosition.source);
|
originalPosition.source = supportRelativeURL(
|
||||||
|
path.dirname(sourceMap.url), originalPosition.source);
|
||||||
}
|
}
|
||||||
return originalPosition;
|
return originalPosition;
|
||||||
}
|
}
|
||||||
@ -196,7 +219,7 @@ exports.install = function(options) {
|
|||||||
// exception handler and the process will still be terminated. However, the
|
// exception handler and the process will still be terminated. However, the
|
||||||
// generated JavaScript code will be shown above the stack trace instead of
|
// generated JavaScript code will be shown above the stack trace instead of
|
||||||
// the original source code.
|
// the original source code.
|
||||||
if (installHandler) {
|
if (installHandler && !isInBrowser()) {
|
||||||
process.on('uncaughtException', handleUncaughtExceptions);
|
process.on('uncaughtException', handleUncaughtExceptions);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user