From 161be66adcb3d3dc31f30da44563e51b5a7a6f55 Mon Sep 17 00:00:00 2001 From: Florian Reiterer Date: Mon, 18 Mar 2013 23:43:02 +0100 Subject: [PATCH] Resolve sources relative to the source map MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Source Map Revision 3: Resolving Sources If the sources are not absolute URLs after prepending of the “sourceRoot”, the sources are resolved relative to the SourceMap (like resolving script src in a html document). --- source-map-support.js | 10 ++++++++-- test.js | 45 ++++++++++++++++++++++--------------------- 2 files changed, 31 insertions(+), 24 deletions(-) diff --git a/source-map-support.js b/source-map-support.js index d997f32..5549b5d 100644 --- a/source-map-support.js +++ b/source-map-support.js @@ -19,13 +19,19 @@ function mapSourcePosition(cache, position) { if (fs.existsSync(sourceMappingURL)) { var sourceMapData = fs.readFileSync(sourceMappingURL, 'utf8'); try { - sourceMap = new SourceMapConsumer(sourceMapData); + sourceMap = [sourceMappingURL, new SourceMapConsumer(sourceMapData)]; cache[position.source] = sourceMap; } catch (e) { } } } - return sourceMap ? sourceMap.originalPositionFor(position) : position; + if (sourceMap) { + var originalPosition = sourceMap[1].originalPositionFor(position); + originalPosition.source = path.resolve(path.dirname(sourceMap[0]), originalPosition.source) + return originalPosition + } else { + return position + } } // Parses code generated by FormatEvalOrigin(), a function inside V8: diff --git a/test.js b/test.js index 53e0b8d..6e20615 100644 --- a/test.js +++ b/test.js @@ -4,6 +4,7 @@ var SourceMapGenerator = require('source-map').SourceMapGenerator; var child_process = require('child_process'); var assert = require('assert'); var fs = require('fs'); +var path = require('path'); function compareStackTrace(source, expected) { var sourceMap = new SourceMapGenerator({ @@ -64,7 +65,7 @@ it('normal throw', function() { 'throw new Error("test");' ], [ 'Error: test', - ' at Object.exports.test (./line1.js:1001:101)' + ' at Object.exports.test ('+path.join(process.cwd(),'line1.js')+':1001:101)' ]); }); @@ -76,8 +77,8 @@ it('throw inside function', function() { 'foo();' ], [ 'Error: test', - ' at foo (./line2.js:1002:102)', - ' at Object.exports.test (./line4.js:1004:104)' + ' at foo ('+path.join(process.cwd(),'line2.js')+':1002:102)', + ' at Object.exports.test ('+path.join(process.cwd(),'line4.js')+':1004:104)' ]); }); @@ -92,9 +93,9 @@ it('throw inside function inside function', function() { 'foo();' ], [ 'Error: test', - ' at bar (./line3.js:1003:103)', - ' at foo (./line5.js:1005:105)', - ' at Object.exports.test (./line7.js:1007:107)' + ' at bar ('+path.join(process.cwd(),'line3.js')+':1003:103)', + ' at foo ('+path.join(process.cwd(),'line5.js')+':1005:105)', + ' at Object.exports.test ('+path.join(process.cwd(),'line7.js')+':1007:107)' ]); }); @@ -103,8 +104,8 @@ it('eval', function() { 'eval("throw new Error(\'test\')");' ], [ 'Error: test', - ' at Object.eval (eval at (./line1.js:1001:101))', - ' at Object.exports.test (./line1.js:1001:101)' + ' at Object.eval (eval at ('+path.join(process.cwd(),'line1.js')+':1001:101))', + ' at Object.exports.test ('+path.join(process.cwd(),'line1.js')+':1001:101)' ]); }); @@ -113,9 +114,9 @@ it('eval inside eval', function() { 'eval("eval(\'throw new Error(\\"test\\")\')");' ], [ 'Error: test', - ' at Object.eval (eval at (eval at (./line1.js:1001:101)))', - ' at Object.eval (eval at (./line1.js:1001:101))', - ' at Object.exports.test (./line1.js:1001:101)' + ' at Object.eval (eval at (eval at ('+path.join(process.cwd(),'line1.js')+':1001:101)))', + ' at Object.eval (eval at ('+path.join(process.cwd(),'line1.js')+':1001:101))', + ' at Object.exports.test ('+path.join(process.cwd(),'line1.js')+':1001:101)' ]); }); @@ -127,9 +128,9 @@ it('eval inside function', function() { 'foo();' ], [ 'Error: test', - ' at eval (eval at foo (./line2.js:1002:102))', - ' at foo (./line2.js:1002:102)', - ' at Object.exports.test (./line4.js:1004:104)' + ' at eval (eval at foo ('+path.join(process.cwd(),'line2.js')+':1002:102))', + ' at foo ('+path.join(process.cwd(),'line2.js')+':1002:102)', + ' at Object.exports.test ('+path.join(process.cwd(),'line4.js')+':1004:104)' ]); }); @@ -139,7 +140,7 @@ it('eval with sourceURL', function() { ], [ 'Error: test', ' at Object.eval (sourceURL.js:1:7)', - ' at Object.exports.test (./line1.js:1001:101)' + ' at Object.exports.test ('+path.join(process.cwd(),'line1.js')+':1001:101)' ]); }); @@ -149,8 +150,8 @@ it('eval with sourceURL inside eval', function() { ], [ 'Error: test', ' at Object.eval (sourceURL.js:1:7)', - ' at Object.eval (eval at (./line1.js:1001:101))', - ' at Object.exports.test (./line1.js:1001:101)' + ' at Object.eval (eval at ('+path.join(process.cwd(),'line1.js')+':1001:101))', + ' at Object.exports.test ('+path.join(process.cwd(),'line1.js')+':1001:101)' ]); }); @@ -163,11 +164,11 @@ it('default options', function(done) { 'process.nextTick(function() { process.exit(1); });' ], [ '', - './.original.js:1', + path.join(process.cwd(),'.original.js')+':1', 'this is the original code', '^', 'Error: this is the error', - ' at foo (./.original.js:1:1)' + ' at foo ('+path.join(process.cwd(),'.original.js')+':1:1)' ]); }); @@ -179,11 +180,11 @@ it('handleUncaughtExceptions is true', function(done) { 'process.nextTick(foo);' ], [ '', - './.original.js:1', + path.join(process.cwd(),'.original.js')+':1', 'this is the original code', '^', 'Error: this is the error', - ' at foo (./.original.js:1:1)' + ' at foo ('+path.join(process.cwd(),'.original.js')+':1:1)' ]); }); @@ -199,6 +200,6 @@ it('handleUncaughtExceptions is false', function(done) { 'function foo() { throw new Error("this is the error"); }', ' ^', 'Error: this is the error', - ' at foo (./.original.js:1:1)' + ' at foo ('+path.join(process.cwd(),'.original.js')+':1:1)' ]); });