--- import { getImage } from 'astro:assets' import { objectOmit } from '@dzeio/object-util' const formats = ['avif', 'webp'] export interface Props extends Omit { src: ImageMetadata | string srcDark?: ImageMetadata | string width?: number height?: number } type PictureResult = { format: 'new' formats: Array<{ format: string, img: Awaited> }> src: Awaited> } | { format: 'raw' src: string } interface Result { light: PictureResult dark?: PictureResult | undefined } async function resolvePicture(image: ImageMetadata | string): Promise { const ext = typeof image === 'string' ? image.substring(image.lastIndexOf('.')) : image.format if (ext === 'svg') { return { format: 'raw', src: typeof image === 'string' ? image : image.src } } const imageFormats: Array<{ format: string, img: Awaited> }> = await Promise.all( formats.map(async (it) => ({ img: await getImage({ src: Astro.props.src, format: it, width: Astro.props.width, height: Astro.props.height }), format: it })) ) const orig = await getImage({ src: Astro.props.src, format: ext, width: Astro.props.width, height: Astro.props.height }) return { format: 'new', formats: imageFormats, src: orig } } const res = { light: await resolvePicture(Astro.props.src), dark: Astro.props.srcDark ? await resolvePicture(Astro.props.srcDark) : undefined } const props = objectOmit(Astro.props, 'src', 'srcDark', 'class') --- {res.light.format === 'new' && ( {res.light.formats.map((it) => ( ))} ) || ( )} {res.dark && res.dark.format === 'new' && ( {res.dark.formats.map((it) => ( ))} ) || (res.dark && ( ))}