-
Notifications
You must be signed in to change notification settings - Fork 156
build with relative paths #173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2f0c5ee
ec1ebfe
7a44d85
1b6b12b
e28a2a3
e351ea3
5ce0a3c
1f867f1
06e50e1
c663dbc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Returns the relative path from "/file/path/to/a" to "/file/path/of/b". To | ||
// make relative imports work, paths to the same directory are prefixed with | ||
// "./", and paths that start without a slash are considered from the root. | ||
export function relativeUrl(source, target) { | ||
if (/^\w+:/.test(target)) return target; | ||
const from = `/${source}`.split(/[/]+/g).slice(0, -1); | ||
const to = `/${target}`.split(/[/]+/g); | ||
const f = to.pop()!; | ||
const m = from.length; | ||
const n = Math.min(m, to.length); | ||
let i = 0; | ||
while (i < n && from[i] === to[i]) ++i; | ||
const k = m - i; | ||
return (k ? "../".repeat(k) : "./") + to.slice(i).concat(f).join("/"); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
define({id: "0", outputs: ["foo"], body: async () => { | ||
const foo = await import("/_import/noent.js"); | ||
const foo = await import("./_import/noent.js"); | ||
return {foo}; | ||
}}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
define({id: "0", outputs: ["foo"], body: async () => { | ||
const foo = await import("/_import/bar.js"); | ||
const foo = await import("./_import/bar.js"); | ||
return {foo}; | ||
}}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
define({id: "0", outputs: ["foo"], body: async () => { | ||
const foo = await import("/_import/other/foo.js"); | ||
const foo = await import("./_import/other/foo.js"); | ||
return {foo}; | ||
}}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
define({id: "0", outputs: ["foo"], body: async () => { | ||
const {foo} = await import("/_import/other/foo.js"); | ||
const {foo} = await import("./_import/other/foo.js"); | ||
return {foo}; | ||
}}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import assert from "node:assert"; | ||
import {relativeUrl} from "../src/url.js"; | ||
|
||
describe("relativeUrls", () => { | ||
it("respects absolute links", () => { | ||
assert.strictEqual(relativeUrl("/", "https://whatever"), "https://whatever"); | ||
assert.strictEqual(relativeUrl("/", "http://example.org"), "http://example.org"); | ||
assert.strictEqual(relativeUrl("/", "https://example.org/"), "https://example.org/"); | ||
mbostock marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert.strictEqual(relativeUrl("/", "mailto:hello@example.org"), "mailto:hello@example.org"); | ||
}); | ||
it("return the expected result", () => { | ||
Fil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert.strictEqual(relativeUrl("/", "/"), "./"); | ||
assert.strictEqual(relativeUrl("/foo", "/"), "./"); | ||
assert.strictEqual(relativeUrl("/foo.html", "/"), "./"); | ||
assert.strictEqual(relativeUrl("/", "/foo"), "./foo"); | ||
assert.strictEqual(relativeUrl("/", "/foo.html"), "./foo.html"); | ||
assert.strictEqual(relativeUrl("/", "/foo/bar/baz"), "./foo/bar/baz"); | ||
assert.strictEqual(relativeUrl("/foo", "/foo"), "./foo"); | ||
assert.strictEqual(relativeUrl("/foo/", "/foo/"), "./"); | ||
assert.strictEqual(relativeUrl("/foo", "/foo/"), "./foo/"); | ||
Comment on lines
+19
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know if we should make the differentiation between "/foo" vs "/foo/". What is the use case for
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You get |
||
assert.strictEqual(relativeUrl("/foo/", "/foo"), "../foo"); | ||
assert.strictEqual(relativeUrl("/foo/bar", "/foo/bar"), "./bar"); | ||
assert.strictEqual(relativeUrl("/foo/bar/", "/foo/bar/"), "./"); | ||
assert.strictEqual(relativeUrl("/foo/bar", "/foo/bar/"), "./bar/"); | ||
assert.strictEqual(relativeUrl("/foo/bar/", "/foo/bar"), "../bar"); | ||
assert.strictEqual(relativeUrl("/foo", "/bar"), "./bar"); | ||
assert.strictEqual(relativeUrl("/foo/bar", "/baz"), "../baz"); | ||
assert.strictEqual(relativeUrl("/foo/bar", "/foo"), "../foo"); | ||
assert.strictEqual(relativeUrl("/foo/bar", "/foo.csv"), "../foo.csv"); | ||
assert.strictEqual(relativeUrl("/foo/bar", "/foo/"), "./"); | ||
assert.strictEqual(relativeUrl("/foo/bar", "/baz/bar"), "../baz/bar"); | ||
assert.strictEqual(relativeUrl("foo", "bar"), "./bar"); | ||
assert.strictEqual(relativeUrl("foo/bar", "baz"), "../baz"); | ||
assert.strictEqual(relativeUrl("foo/bar", "foo"), "../foo"); | ||
assert.strictEqual(relativeUrl("foo/bar", "foo.csv"), "../foo.csv"); | ||
assert.strictEqual(relativeUrl("foo/bar", "foo/"), "./"); | ||
assert.strictEqual(relativeUrl("foo/bar", "baz/bar"), "../baz/bar"); | ||
Fil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert.strictEqual(relativeUrl("foo////baz", "baz//bar"), "../baz/bar"); | ||
}); | ||
}); |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It’s worth mentioning that
relativeUrl
can be called with two different types of paths with different characteristics: serving paths and source paths.Serving paths, such as during
render
, start with a slash and don’t end with an extension. For example/javascript/displays
.Source paths, such as during
rewriteImports
, don’t start with a slash and do end with an extension. For example,javascript/displays.md
. Source paths are typically relative to the source root, or to a subdirectory in source root (such as when computing transitive imports).It looks like the code already handles both of these correctly, but I think it’s worth calling it out in the tests, and having explicit tests for each type of path. I think we also want more tests for the source path case.