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

Allow loading images from /public #54

Merged
merged 8 commits into from
May 18, 2022

Conversation

IanVS
Copy link
Contributor

@IanVS IanVS commented May 7, 2022

Fixes #1
Might help with some of the problems in #41

I am in the process of adding this tool, and found it difficult to do because normally astro loads images from /public, and so all my existing images live there, including images that I reference from markdown files. I hit errors during build like Input file is missing, without knowing why (because I didn't realize it was my images in markdown that were being automatically processed, and the error message does not say the filepath that failed).

So, this change allows markdown and components to specify images in /public (or other configured publicDir), without the need for explicitly adding /public to the start of the url. I've tested it in my own app in dev and build, with a <Picture /> element loading from /src and from public, as well as markdown files pointing to public images as well as http images.

I'd like to add some better tests around the method, though, to be sure that it is handling the edge cases properly (I'm not sure it will deal with relative paths correctly, for instance). What would you think about adding some vitest tests? They are easy to set up, support esm natively, and run nice and quick. Would you be open to a few basic tests, to see how it works? Tests were added in #58, but are currently failing until #64 is merged. I've added some test cases here as well. Ideally, we'll add some higher-level tests soon too, to be sure that it's all working end-to-end.

I also wonder if you might consider a breaking change to treat all absolute paths as being inside /public, and relative paths like ./src/foo.jpg as relative to the project root. Or ideally relative to the file itself, but that might be a little trickier. Talked in discord, and @RafidMuhymin prefers that we try to find files relative to both locations, which is what I've done in this PR

lastly, it might be better to add a try/catch around the call to sharp with a warning there when files can't be found, rather than throwing a warning in the function I made. I'll experiment with that approach as well. I've taken out the warning, and opened #66 to track as a separate feature request.

@vercel
Copy link

vercel bot commented May 7, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

2 Ignored Deployments
Name Status Preview Updated
astro-imagetools-demo ⬜️ Ignored (Inspect) May 18, 2022 at 4:27PM (UTC)
astro-imagetools-docs ⬜️ Ignored (Inspect) May 18, 2022 at 4:27PM (UTC)

@IanVS IanVS marked this pull request as draft May 7, 2022 05:09
@IanVS
Copy link
Contributor Author

IanVS commented May 18, 2022

Tests will fail until #64 is merged and this is rebased on it. Otherwise, I think it's ready to go.

I guess we need to write the astro config to a file because of SSR? Am I correct in thinking that sometimes this code runs in node, and sometimes in the browser, which is why we can't just create a variable in memory and rely on closures to pass it around?

@IanVS IanVS marked this pull request as ready for review May 18, 2022 02:20
@IanVS
Copy link
Contributor Author

IanVS commented May 18, 2022

I also added an .npmignore file, to avoid publishing tests and test fixtures to npm, which would just bloat the install size.

@IanVS
Copy link
Contributor Author

IanVS commented May 18, 2022

@RafidMuhymin okay, this is ready for a review, thanks!

@RafidMuhymin
Copy link
Owner

LGTM 👍 ! Thanks for contributing @IanVS !

@RafidMuhymin RafidMuhymin merged commit 86da2f3 into RafidMuhymin:main May 18, 2022
@IanVS IanVS deleted the allow-public branch May 18, 2022 17:44
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.

Usage with images from public folder
2 participants