-
-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix options.sourceFileName gennerate bug (#260)
* improve sourceFileName gennerate logic with new relative method * add test for relative method
- Loading branch information
1 parent
a8b9189
commit 501d60d
Showing
3 changed files
with
59 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* Make a path relative to a URL or another path. | ||
* Borrowed from https://github.com/mozilla/source-map/blob/master/lib/util.js | ||
* | ||
* @param aRoot The root path or URL. | ||
* @param aPath The path or URL to be made relative to aRoot. | ||
*/ | ||
module.exports = function relative(aRoot, aPath) { | ||
if (aRoot === "") { | ||
aRoot = "."; | ||
} | ||
|
||
aRoot = aRoot.replace(/\/$/, ""); | ||
|
||
// It is possible for the path to be above the root. In this case, simply | ||
// checking whether the root is a prefix of the path won't work. Instead, we | ||
// need to remove components from the root one by one, until either we find | ||
// a prefix that fits, or we run out of components to remove. | ||
let level = 0; | ||
while (aPath.indexOf(aRoot + "/") !== 0) { | ||
const index = aRoot.lastIndexOf("/"); | ||
if (index < 0) { | ||
return aPath; | ||
} | ||
|
||
// If the only part of the root that is left is the scheme (i.e. http://, | ||
// file:///, etc.), one or more slashes (/), or simply nothing at all, we | ||
// have exhausted all components, so the path is not relative to the root. | ||
aRoot = aRoot.slice(0, index); | ||
if (aRoot.match(/^([^\/]+:\/)?\/*$/)) { | ||
return aPath; | ||
} | ||
|
||
++level; | ||
} | ||
|
||
// Make sure we add a '../' for each component we removed from the root. | ||
return Array(level + 1).join("../") + aPath.substr(aRoot.length + 1); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import test from "ava"; | ||
import relative from "../../lib/utils/relative.js"; | ||
|
||
test("should get correct relative path", (t) => { | ||
t.is(relative("/the/root", "/the/root/one.js"), "one.js"); | ||
t.is(relative("/the/root", "/the/rootone.js"), "../rootone.js"); | ||
t.is(relative("/the/root", "/therootone.js"), "/therootone.js"); | ||
|
||
t.is(relative("", "/the/root/one.js"), "/the/root/one.js"); | ||
t.is(relative(".", "/the/root/one.js"), "/the/root/one.js"); | ||
t.is(relative("", "the/root/one.js"), "the/root/one.js"); | ||
t.is(relative(".", "the/root/one.js"), "the/root/one.js"); | ||
|
||
t.is(relative("/", "/the/root/one.js"), "the/root/one.js"); | ||
t.is(relative("/", "the/root/one.js"), "the/root/one.js"); | ||
}); |