one
Version:
One is a new React Framework that makes Vite serve both native and web.
56 lines (54 loc) • 1.7 kB
JavaScript
import { existsSync } from "node:fs";
import { resolve } from "node:path";
let sharpWarned = !1;
async function getSharp() {
try {
const sharpModule = await import("sharp");
return sharpModule.default || sharpModule;
} catch {
return sharpWarned || (sharpWarned = !0, console.warn(`
[one] To use getImageData, install sharp:
npm install sharp
`)), null;
}
}
async function processImageMeta(filePath) {
const sharp = await getSharp();
if (!sharp) return null;
try {
const image = sharp(filePath),
metadata = await image.metadata(),
{
width = 0,
height = 0
} = metadata,
blurDataURL = `data:image/jpeg;base64,${(await image.resize(10).blur(1).jpeg({
quality: 40
}).toBuffer()).toString("base64")}`;
return {
width,
height,
blurDataURL
};
} catch (e) {
return console.warn(`[one] processImageMeta: Failed to process ${filePath}:`, e), null;
}
}
async function getImageData(filePath) {
let resolvedPath, src;
filePath.startsWith("/") ? (resolvedPath = resolve("./public", filePath.slice(1)), src = filePath) : (resolvedPath = resolve(filePath), src = filePath.replace(/^\.\.?\//, "").replace(/^public\//, ""), src.startsWith("/") || (src = "/" + src));
const defaultResult = {
src,
width: 0,
height: 0,
blurDataURL: ""
};
if (!existsSync(resolvedPath)) return console.warn(`[one] getImageData: File not found: ${resolvedPath}`), defaultResult;
const meta = await processImageMeta(resolvedPath);
return meta ? {
src,
...meta
} : defaultResult;
}
export { getImageData, getSharp, processImageMeta };
//# sourceMappingURL=getImageData.mjs.map