Skip to content
This repository has been archived by the owner on Nov 4, 2023. It is now read-only.

Commit

Permalink
Make sourceRoot resolving more sensible
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lydell committed Mar 6, 2014
1 parent b07d032 commit 46ec603
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
7 changes: 6 additions & 1 deletion lib/source-map-resolve-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/<source>`, not `/scripts/<source>`. 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])
}
Expand Down
7 changes: 6 additions & 1 deletion source-map-resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/<source>`, not `/script/<source>`. 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])
}
Expand Down
34 changes: 32 additions & 2 deletions test/source-map-resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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, [
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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, [
Expand Down

0 comments on commit 46ec603

Please sign in to comment.