fix: temporary solution for images dynamic import

This commit is contained in:
saicaca 2023-10-23 19:12:51 +08:00
parent 1ae4a8eee9
commit e30f20e86a
1 changed files with 5 additions and 2 deletions

View File

@ -1,4 +1,5 @@
--- ---
import path from "path";
interface Props { interface Props {
id?: string id?: string
src: string; src: string;
@ -12,9 +13,12 @@ const className = Astro.props.class;
const isLocal = !(src.startsWith('/') || src.startsWith('http') || src.startsWith('https') || src.startsWith('data:')); const isLocal = !(src.startsWith('/') || src.startsWith('http') || src.startsWith('https') || src.startsWith('data:'));
// TODO temporary workaround for images dynamic import
let img; let img;
if (isLocal) { if (isLocal) {
img = (await import("../../" + src)).default; const files = import.meta.glob<ImageMetadata>("../../**", { import: 'default' });
let normalizedPath = "../../" + path.normalize(src).replace(/\\/g, "/");
img = await (files[normalizedPath])();
} }
--- ---
@ -22,6 +26,5 @@ if (isLocal) {
<div class="transition absolute inset-0 dark:bg-black/10 bg-opacity-50 pointer-events-none"></div> <div class="transition absolute inset-0 dark:bg-black/10 bg-opacity-50 pointer-events-none"></div>
{isLocal && <Image src={img} alt={alt || ""} class="w-full h-full object-center object-cover" />} {isLocal && <Image src={img} alt={alt || ""} class="w-full h-full object-center object-cover" />}
{!isLocal && <img src={src} alt={alt || ""} class="w-full h-full object-center object-cover" />} {!isLocal && <img src={src} alt={alt || ""} class="w-full h-full object-center object-cover" />}
</div> </div>