Bluewaterblog/blog/src/lib/findImage.ts
2026-06-27 14:48:09 +02:00

13 lines
No EOL
483 B
TypeScript

const images = import.meta.glob("/src/assets/**/*.{jpg,png}", { eager: true }) as Record<string, { default: string }>;
export function findImageByPath(path: string, { fallback }: { fallback?: string } = {}): any {
if (!path) return fallback ?? null;
const key = Object.keys(images).find(
p => p.endsWith("/" + path) || p.endsWith(path)
);
if (!key) {
if (fallback) return fallback;
throw new Error(`Image not found: ${path}`);
}
return images[key].default;
}