Skip to content

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

Merged
merged 10 commits into from
Nov 15, 2023
Merged

build with relative paths #173

merged 10 commits into from
Nov 15, 2023

Conversation

Fil
Copy link
Contributor

@Fil Fil commented Nov 14, 2023

closes #42

Note: the "to" implementation is pretty lame, I'm sure there's a better way to compute the relative path from x to y. I ended up creating one without relying on node's path.relative (which is meant for files not URLs).

@Fil Fil requested a review from cinxmo November 14, 2023 18:06
Comment on lines +14 to +15
assert.strictEqual(relativeUrl("/foo/", "/foo/"), "./");
assert.strictEqual(relativeUrl("/foo", "/foo/"), "./foo/");
Copy link
Contributor

@cinxmo cinxmo Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you explain the difference between source being /foo/ vs /foo?
/foo/ means you're in a directory under /foo vs in /foo

Copy link
Contributor

Choose a reason for hiding this comment

The 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 /foo/ without the nested directory (like /foo/bar). Would the target ever have a trailing slash?

Copy link
Contributor Author

@Fil Fil Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it's when you have docs/features/index.md, vs docs/features.md; see #171 #172 and #175 for discussion

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You get /foo/ if you have a foo/index.md. You get /foo if you have a foo.md.

Co-authored-by: Mike Bostock <[email protected]>
Comment on lines +5 to +6
const from = `/${source}`.split(/[/]+/g).slice(0, -1);
const to = `/${target}`.split(/[/]+/g);
Copy link
Member

@mbostock mbostock Nov 15, 2023

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.

Co-authored-by: Mike Bostock <[email protected]>
@@ -105,7 +106,7 @@ export function rewriteImports(output: any, rootNode: JavaScriptNode, sourcePath
: node.specifiers.find(isNamespaceSpecifier)?.local.name ?? "{}"
} = await import(${JSON.stringify(
isLocalImport(value, sourcePath)
? join("/_import/", join(dirname(sourcePath), value))
? relativeUrl(sourcePath, join("/_import/", dirname(sourcePath), value))
Copy link
Member

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:

Suggested change
? relativeUrl(sourcePath, join("/_import/", dirname(sourcePath), value))
? relativeImport(sourcePath, value)

where

function relativeImport(sourcePath, value) {
  return relativeUrl(sourcePath, join("/_import/", dirname(sourcePath), value));
}

In theory you could then move the leading ./ requirement into relativeImport instead of relativeUrl, but I think it’s fine to leave it in relativeUrl for now too.

Copy link
Member

@mbostock mbostock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice tests. 👍

@Fil Fil enabled auto-merge (squash) November 15, 2023 15:24
@Fil Fil merged commit a2b5c87 into main Nov 15, 2023
@Fil Fil deleted the fil/relative-paths branch November 15, 2023 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Specify a root path for serving
3 participants