Skip to content

Instantly share code, notes, and snippets.

@Pigamo
Last active May 6, 2024 22:40
Show Gist options
  • Save Pigamo/6ad9d769a23bbdc07a9ef6f7226cbb55 to your computer and use it in GitHub Desktop.
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

I have used this code to auto import image
thank you

@mkxto
Copy link

mkxto commented Feb 24, 2023

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

@mkxto
Copy link

mkxto commented Feb 24, 2023

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
Copy link
Author

Pigamo commented Feb 24, 2023 via email

@mkxto
Copy link

mkxto commented Feb 24, 2023

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
Copy link
Author

Pigamo commented Feb 24, 2023 via email

@Pigamo
Copy link
Author

Pigamo commented Feb 24, 2023

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

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