Skip to content

Commit

Permalink
fix options.sourceFileName gennerate bug (#260)
Browse files Browse the repository at this point in the history
* improve sourceFileName gennerate logic with new relative method

* add test for relative method
  • Loading branch information
creeperyang authored and danez committed Nov 8, 2016
1 parent a8b9189 commit 501d60d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const loaderUtils = require("loader-utils");
const path = require("path");
const cache = require("./fs-cache.js");
const exists = require("./utils/exists")();
const relative = require("./utils/relative");
const read = require("./utils/read")();
const resolveRc = require("./resolve-rc.js");
const pkg = require("./../package.json");
Expand Down Expand Up @@ -98,9 +99,9 @@ module.exports = function(source, inputSourceMap) {
}

if (options.sourceFileName === undefined) {
options.sourceFileName = path.relative(
options.sourceRoot,
options.filename
options.sourceFileName = relative(
options.sourceRoot,
options.filename
);
}

Expand Down
39 changes: 39 additions & 0 deletions src/utils/relative.js
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);
};
16 changes: 16 additions & 0 deletions test/utils/relative.test.js
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");
});

0 comments on commit 501d60d

Please sign in to comment.