From 09813f1610dff4cf4806b7b048b5fa090780e542 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Fri, 10 Mar 2023 07:35:40 -0600 Subject: [PATCH] fix: prefer dynamic import for `image-size` (#6495) --- packages/astro/src/assets/utils/metadata.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/astro/src/assets/utils/metadata.ts b/packages/astro/src/assets/utils/metadata.ts index 8cf0cc470..710e758d9 100644 --- a/packages/astro/src/assets/utils/metadata.ts +++ b/packages/astro/src/assets/utils/metadata.ts @@ -1,4 +1,3 @@ -import sizeOf from 'image-size'; import fs from 'node:fs/promises'; import { fileURLToPath } from 'node:url'; import { ImageMetadata, InputFormat } from '../types.js'; @@ -7,10 +6,14 @@ export interface Metadata extends ImageMetadata { orientation?: number; } +let sizeOf: typeof import('image-size').default | undefined; export async function imageMetadata( src: URL | string, data?: Buffer ): Promise { + if (!sizeOf) { + sizeOf = await import('image-size').then(mod => mod.default); + } let file = data; if (!file) { try { @@ -20,7 +23,7 @@ export async function imageMetadata( } } - const { width, height, type, orientation } = await sizeOf(file); + const { width, height, type, orientation } = await sizeOf!(file); const isPortrait = (orientation || 0) >= 5; if (!width || !height || !type) {