fix: prefer dynamic import for image-size (#6495)

This commit is contained in:
Nate Moore 2023-03-10 07:35:40 -06:00 committed by GitHub
parent abaf860af7
commit 09813f1610
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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<Metadata | undefined> {
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) {