Skip to content

Instantly share code, notes, and snippets.

@Pigamo
Last active March 20, 2025 07:39
Show Gist options
  • Select an option

  • Save Pigamo/6ad9d769a23bbdc07a9ef6f7226cbb55 to your computer and use it in GitHub Desktop.

Select an option

Save Pigamo/6ad9d769a23bbdc07a9ef6f7226cbb55 to your computer and use it in GitHub Desktop.
Imports multiple images from a folder using Vite globs
const gallery = Object.values(import.meta.glob('@assets/*.{png,jpg,jpeg,PNG,JPEG}', { eager: true, as: 'url' }))
@mohamad68

Copy link
Copy Markdown

I have used this code to auto import image
thank you

@mkxto

mkxto commented Feb 24, 2023

Copy link
Copy Markdown

I cannot manage to make this work, any idea ?
The bundler don't fix URL but bundles all the necessary image :/

@mkxto

mkxto commented Feb 24, 2023

Copy link
Copy Markdown

I mean, it does work on dev, but as soon as I build, image are bundled but the href to the image is not correct

@Pigamo

Pigamo commented Feb 24, 2023 via email

Copy link
Copy Markdown
Author

@mkxto

mkxto commented Feb 24, 2023

Copy link
Copy Markdown

The issue is the path to the image, I've posted a question on Vite's discussion thing on GitHub .
If you find or remember anything, tell me!
Thank you dude

@Pigamo

Pigamo commented Feb 24, 2023 via email

Copy link
Copy Markdown
Author

@Pigamo

Pigamo commented Feb 24, 2023

Copy link
Copy Markdown
Author

Thanks for asking the right questions. I updated the gist to reflect the answer!

The issue is the path to the image, I've posted a question on Vite's discussion thing on GitHub . If you find or remember anything, tell me! Thank you dude

Thanks @mkxto

@PullRequestTimeout

Copy link
Copy Markdown

Does anyone know how to use this in SvelteKit?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment