Skip to content
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

feat: support images outside the project #7139

Merged
merged 8 commits into from
May 26, 2023
Merged

Conversation

Princesseuh
Copy link
Member

@Princesseuh Princesseuh commented May 19, 2023

Changes

Changes the returned src for imported images to be absolute path with a Vite /@fs/ thing so we can support imports from outside the project. This also allows us to add an error message when someone make the common mistake of passing an imported image's src to Image and getImage instead of passing the full import

This PR also includes some minor fixes to error messages that I noticed while adding a new error.

Fix #6635

Testing

Added tests, modified tests that tested the URL precisely, and tests should still pass!

Docs

All the usages of src that are documented should still work, so no change necessary.

@changeset-bot
Copy link

changeset-bot bot commented May 19, 2023

🦋 Changeset detected

Latest commit: 994507a

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label May 19, 2023
@Princesseuh Princesseuh marked this pull request as ready for review May 25, 2023 17:39
@Princesseuh Princesseuh requested a review from a team as a code owner May 25, 2023 17:39
@Princesseuh Princesseuh marked this pull request as draft May 25, 2023 17:39
@Princesseuh Princesseuh marked this pull request as ready for review May 26, 2023 13:22
Copy link
Member

@bluwy bluwy left a comment

Choose a reason for hiding this comment

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

The /@fs handling looks good to me 👍

Comment on lines -43 to +42
meta.src = rootRelativePath(settings.config, url);
meta.src = `/@fs` + prependForwardSlash(fileURLToNormalizedPath(url));
Copy link
Member

Choose a reason for hiding this comment

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

A note on this: Vite alternates between a root relative path and /@fs depending on whether the path is inside the root or not. Here it's always using /@fs which is and should fine, but there are small bugs now and then because Vite's rule is not applied by frameworks. Something to keep an eye on.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, I'm willing to take the risk. I think the consistency of it is great, even though I don't necessarily want users to rely on it. Will definitely reconsider if it becomes an issue!

Comment on lines +114 to +115
const filePath = url.searchParams.get('href')?.slice('/@fs'.length);
const filePathURL = new URL('.' + filePath, 'file:');
Copy link
Member

Choose a reason for hiding this comment

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

This actually kinda neat and it seems to handle Windows too. Vite handles it in a slightly complex way.

@Princesseuh Princesseuh merged commit f2f18b4 into main May 26, 2023
@Princesseuh Princesseuh deleted the fix/images-outside-project branch May 26, 2023 20:03
@astrobot-houston astrobot-houston mentioned this pull request May 26, 2023
@bluwy bluwy mentioned this pull request May 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

image asset: ENOENT error for remote markdown files with relative paths
4 participants