Skip to content

Commit

Permalink
fix: prefer dynamic import for image-size (withastro#6495)
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re committed Mar 10, 2023
1 parent abaf860 commit 09813f1
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/astro/src/assets/utils/metadata.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<Metadata | undefined> {
if (!sizeOf) {
sizeOf = await import('image-size').then(mod => mod.default);
}
let file = data;
if (!file) {
try {
Expand All @@ -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) {
Expand Down

0 comments on commit 09813f1

Please sign in to comment.