diff --git a/src/helpers/image.ts b/src/helpers/image.ts index 8a39c79..ab4695f 100644 --- a/src/helpers/image.ts +++ b/src/helpers/image.ts @@ -1,19 +1,29 @@ let loadingSharp = false; import { rgbaToThumbHash } from "thumbhash"; -export async function generateThumbHash(image: { width: number, height: number }) { +let s: typeof import("sharp") | undefined; + +async function getSharp(): Promise { + + if (s) return s; + + if (import.meta.env.MODE !== "development") { + s = (await import("sharp")).default; + return s; + } + if (!loadingSharp) { loadingSharp = true; setTimeout(async () => { - // @ts-ignore - globalThis["sharp"] = (await import("sharp")).default; + s = (await import("sharp")).default; }, 1000); return; } +} - // @ts-ignore - const sharp = globalThis["sharp"] as typeof import("sharp"); +export async function generateThumbHash(image: { width: number, height: number }) { + const sharp = await getSharp(); if (!sharp) return; const scaleFactor = 100 / Math.max(image.width, image.height);