From 46ec6030fffbaf94e51c3b41954246549b027599 Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Thu, 6 Mar 2014 19:48:27 +0100 Subject: [PATCH] Make sourceRoot resolving more sensible MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A source root such as `/scripts/subdir` is not treated as `/scripts/subdir/` — that is, as a directory called “subdir”, not a file called “subdir”. Pointing to a file as source root does not makes sense. --- lib/source-map-resolve-node.js | 7 ++++++- source-map-resolve.js | 7 ++++++- test/source-map-resolve.js | 34 ++++++++++++++++++++++++++++++++-- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/lib/source-map-resolve-node.js b/lib/source-map-resolve-node.js index f3eab39..393e1fc 100644 --- a/lib/source-map-resolve-node.js +++ b/lib/source-map-resolve-node.js @@ -161,13 +161,18 @@ function resolveSourcesSync(map, mapUrl, read) { return sources } +var endingSlash = /\/?$/ + function resolveSourcesHelper(map, mapUrl, fn) { mapUrl = urix(mapUrl) var fullUrl var sourceContent for (var index = 0, len = map.sources.length; index < len; index++) { if (map.sourceRoot) { - fullUrl = resolveUrl(mapUrl, map.sourceRoot, map.sources[index]) + // Make sure that the sourceRoot ends with a slash, so that `/scripts/subdir` becomes + // `/scripts/subdir/`, not `/scripts/`. Pointing to a file as source root + // does not make sense. + fullUrl = resolveUrl(mapUrl, map.sourceRoot.replace(endingSlash, "/"), map.sources[index]) } else { fullUrl = resolveUrl(mapUrl, map.sources[index]) } diff --git a/source-map-resolve.js b/source-map-resolve.js index de378e9..df19e7d 100644 --- a/source-map-resolve.js +++ b/source-map-resolve.js @@ -168,12 +168,17 @@ void (function(root, factory) { return sources } + var endingSlash = /\/?$/ + function resolveSourcesHelper(map, mapUrl, fn) { var fullUrl var sourceContent for (var index = 0, len = map.sources.length; index < len; index++) { if (map.sourceRoot) { - fullUrl = resolveUrl(mapUrl, map.sourceRoot, map.sources[index]) + // Make sure that the sourceRoot ends with a slash, so that `/scripts/subdir` becomes + // `/scripts/subdir/`, not `/script/`. Pointing to a file as source root + // does not make sense. + fullUrl = resolveUrl(mapUrl, map.sourceRoot.replace(endingSlash, "/"), map.sources[index]) } else { fullUrl = resolveUrl(mapUrl, map.sources[index]) } diff --git a/test/source-map-resolve.js b/test/source-map-resolve.js index e0cf84c..5f22273 100644 --- a/test/source-map-resolve.js +++ b/test/source-map-resolve.js @@ -29,6 +29,12 @@ var map = { sources: ["foo.js", "lib/bar.js", "../vendor/dom.js", "/version.js", "//foo.org/baz.js"], names: [] }, + sourceRootNoSlash: { + mappings: "AAAA", + sourceRoot: "/static/js/app", + sources: ["foo.js", "lib/bar.js", "../vendor/dom.js", "/version.js", "//foo.org/baz.js"], + names: [] + }, sourceContents: { mappings: "AAAA", sourceRoot: "/static/js/app/", @@ -270,7 +276,7 @@ function testResolveSources(method, sync) { var mapUrl = "http://example.com/a/b/c/foo.js.map" - t.plan(1 + (sync ? 8 : 11) + 5*3 + 4) + t.plan(1 + (sync ? 8 : 11) + 6*3 + 4) t.equal(typeof method, "function", "is a function") @@ -313,6 +319,18 @@ function testResolveSources(method, sync) { isAsync() }) + method(map.sourceRootNoSlash, mapUrl, wrap(identity), function(error, result) { + t.error(error) + t.deepEqual(result, [ + "http://example.com/static/js/app/foo.js", + "http://example.com/static/js/app/lib/bar.js", + "http://example.com/static/js/vendor/dom.js", + "http://example.com/version.js", + "http://foo.org/baz.js" + ], "sourceRootNoSlash") + isAsync() + }) + method(map.sourceContents, mapUrl, wrap(Throws), function(error, result) { t.error(error) t.deepEqual(result, [ @@ -375,7 +393,7 @@ function testResolve(method, sync) { var codeUrl = "http://example.com/a/b/c/foo.js" - t.plan(1 + (sync ? 7 : 10) + 16*3 + 5*3 + 4) + t.plan(1 + (sync ? 7 : 10) + 16*3 + 6*3 + 4) t.equal(typeof method, "function", "is a function") @@ -581,6 +599,18 @@ function testResolve(method, sync) { isAsync() }) + method(code.fileRelative, codeUrl, readMap(map.sourceRootNoSlash), function(error, result) { + t.error(error) + t.deepEqual(result.sources, [ + "http://example.com/static/js/app/foo.js", + "http://example.com/static/js/app/lib/bar.js", + "http://example.com/static/js/vendor/dom.js", + "http://example.com/version.js", + "http://foo.org/baz.js" + ], "sourceRootNoSlash") + isAsync() + }) + method(code.fileRelative, codeUrl, readMap(map.sourceContents), function(error, result) { t.error(error) t.deepEqual(result.sources, [