fix: prefer dynamic import for image-size
(#6495)
This commit is contained in:
parent
abaf860af7
commit
09813f1610
1 changed files with 5 additions and 2 deletions
|
@ -1,4 +1,3 @@
|
||||||
import sizeOf from 'image-size';
|
|
||||||
import fs from 'node:fs/promises';
|
import fs from 'node:fs/promises';
|
||||||
import { fileURLToPath } from 'node:url';
|
import { fileURLToPath } from 'node:url';
|
||||||
import { ImageMetadata, InputFormat } from '../types.js';
|
import { ImageMetadata, InputFormat } from '../types.js';
|
||||||
|
@ -7,10 +6,14 @@ export interface Metadata extends ImageMetadata {
|
||||||
orientation?: number;
|
orientation?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let sizeOf: typeof import('image-size').default | undefined;
|
||||||
export async function imageMetadata(
|
export async function imageMetadata(
|
||||||
src: URL | string,
|
src: URL | string,
|
||||||
data?: Buffer
|
data?: Buffer
|
||||||
): Promise<Metadata | undefined> {
|
): Promise<Metadata | undefined> {
|
||||||
|
if (!sizeOf) {
|
||||||
|
sizeOf = await import('image-size').then(mod => mod.default);
|
||||||
|
}
|
||||||
let file = data;
|
let file = data;
|
||||||
if (!file) {
|
if (!file) {
|
||||||
try {
|
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;
|
const isPortrait = (orientation || 0) >= 5;
|
||||||
|
|
||||||
if (!width || !height || !type) {
|
if (!width || !height || !type) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue