I have the following Svelte 4 component, with some images loaded by batch from a specific folder (and one image loaded normally just for reference):
<script> import ImageThatWorks from "$lib/images/singles/image.jpg" const imagesBlob = import.meta.glob(`$lib/images/batch/*.{png,jpg}`) const imagesUrl = Object.keys(imagesBlob)</script><div> <img src={ImageThatWorks} alt='Image that works' width='300' /><div class="photogrid"> {#each imagesUrl as imgUrl}<img src={imgUrl} /> {/each}</div></div>
When running in dev mode with npm run dev
(a script that runs vite dev
), all images displays properly, since, as far as I understand, vite loads assets from their original locations in dev mode.
When building (with npm run build
), images are successfully copied in the assets folder (dist/assets
). Then, when running the build with npm run preview
, only the single reference ImageThatWorks
is displayed and called from dist/assets
. The other images loaded in batch are not displayed and still called from their original location (src/lib/images
).
Why? How should I manage images when loaded by batch?
Side question: what's the best way to load also their thumbnail version within the same batch?
Here is the vite config file for information:
import { defineConfig } from 'vite'import { svelte } from '@sveltejs/vite-plugin-svelte'import path from 'path'export default defineConfig({ base: './', build: { outDir: 'dist', assetsDir: 'assets' }, resolve: { alias: { $lib: path.resolve('./src/lib') } }, plugins: [ svelte(), ]})