-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Dynamically import assets in Sveltekit/Vite #10618
Comments
I think this issue would be better placed in vite, though I don't believe they would accept it. It's quite deliberate having to put a string literal in The best solution is one you already mentioned: +page.ts const imageModules = Object.entries(import.meta.glob("$lib/assets/*.jpg"));
export async function load() {
const images = imageModules
// apply some filter
.filter(([key]) => true)
// import images, if you don't need a filter use { eager: true } to avoid this step
.map(async ([, fn]) => {
const image = await fn();
return image.default;
});
return {
images: await Promise.all(image),
};
} +page.svelte <script>
export let images;
</script>
{#each images as image}
<img src={image} />
{/each} The code might not be perfect as I typed it out in the browser but you hopefully get the idea. I'm gonna close this for now but if you have any more questions let me know 🙏 |
Thanks for looking into that, I was asking if there are other alternative solutions to do it. I mentioned globby, that doesn't seem to work (why?) Maybe there are other ES modules?. Can a preprocessor or other mechanism be used in place? |
What adapter are you using? Might not work on your deployment platform, though I don't know as I've never used globby. I personally would still recommend using |
I've deployed it on node on my server |
I've just realized that |
Describe the problem
I'm asking for alternative ways you may know, in SvelteKit/Vite, to get a list of asset files (in this case images) preferably by hitting an API endpoint like
/api/assets/[param]
By using vite's import.meta.glob works only by hard-coding the directory of interest:
Invalid glob import syntax: Could only use literals
I know this has been asked before and supposedly a fix was merged but without using switch statement, this still doesn't work!
Only this works:
Isn't this ugly and impractical to not being able to dynamically set the destination directory? Are there some other ways?
Describe the proposed solution
const imagePath = import.assets(my_dir)
Alternatives considered
(in server.ts)
leads to
ReferenceError: paths is not defined
(??)Importance
would make my life easier
Additional Information
Note the part:
.name.split('/').pop()
I had to add it to get rid of the full path because otherwise it wasn't working once deployed and built.And this is addition to my svelte.config.js to make the assets available once deployed
To get to this point I had to crawl many scattered information, I hope it could be of some use to others, and hopefully I'm not aware of other simpler way to get a list of files dynamically!
The text was updated successfully, but these errors were encountered: