-
Notifications
You must be signed in to change notification settings - Fork 160
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 9 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,14 @@ | ||
// 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 "./". | ||
export function relativeUrl(source, target) { | ||
if (/^\w+:/.test(target)) return target; | ||
const from = `/${source}`.split(/[/]+/g).slice(0, -1); | ||
const to = `/${target}`.split(/[/]+/g); | ||
Comment on lines
+6
to
+7
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. It’s worth mentioning that Serving paths, such as during Source paths, such as during 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. |
||
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,39 @@ | ||
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
|
||
}); | ||
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"); | ||
}); | ||
}); |
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.
Just to avoid writing this twice, how about:
where
In theory you could then move the leading
./
requirement intorelativeImport
instead ofrelativeUrl
, but I think it’s fine to leave it inrelativeUrl
for now too.