Migrate from @astrojs/image to Astro Assets and Unpic

This commit is contained in:
prototypa
2023-08-13 17:34:30 -04:00
parent 9a350af269
commit 77817aa77e
33 changed files with 508 additions and 83 deletions

View File

@@ -1,11 +1,11 @@
import { getImage } from '@astrojs/image';
import type { OpenGraph } from '@astrolib/seo/src/types';
import { getImage } from 'astro:assets';
import type { ImageMetadata } from 'astro';
import type { OpenGraph } from '@astrolib/seo/src/types';
const load = async function () {
let images: Record<string, () => Promise<unknown>> | undefined = undefined;
try {
images = import.meta.glob('~/assets/images/**');
images = import.meta.glob('~/assets/images/**/*.{jpeg,jpg,png,tiff,webp,gif,svg,JPEG,JPG,PNG,TIFF,WEBP,GIF,SVG}');
} catch (e) {
// continue regardless of error
}
@@ -21,24 +21,27 @@ export const fetchLocalImages = async () => {
};
/** */
export const findImage = async (imagePath?: string) => {
export const findImage = async (imagePath?: string | ImageMetadata | null): Promise<string | ImageMetadata | undefined | null> => {
// Not string
if (typeof imagePath !== 'string') {
return null;
return imagePath;
}
// Absolute paths
if (imagePath.startsWith('http://') || imagePath.startsWith('https://') || imagePath.startsWith('/')) {
return imagePath;
}
if (!imagePath.startsWith('~/assets')) {
return null;
} // For now only consume images using ~/assets alias (or absolute)
// Relative paths or not "~/assets/"
if (!imagePath.startsWith('~/assets/images')) {
return imagePath;
}
const images = await fetchLocalImages();
const key = imagePath.replace('~/', '/src/');
return images && typeof images[key] === 'function'
? ((await images[key]()) as { default: unknown })['default']
? ((await images[key]()) as { default: ImageMetadata })['default']
: null;
};
@@ -91,4 +94,4 @@ export const adaptOpenGraphImages = async (
);
return { ...openGraph, ...(adaptedImages ? { images: adaptedImages } : {}) };
};
};