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