UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

65 lines (63 loc) 3.19 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf, __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: !0 }); }, __copyProps = (to, from, except, desc) => { if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: !0 }) : target, mod )), __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod); var getImageData_exports = {}; __export(getImageData_exports, { getImageData: () => getImageData, getSharp: () => getSharp, processImageMeta: () => processImageMeta }); module.exports = __toCommonJS(getImageData_exports); var import_node_fs = require("node:fs"), import_node_path = require("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 = (0, import_node_path.resolve)("./public", filePath.slice(1)), src = filePath) : (resolvedPath = (0, import_node_path.resolve)(filePath), src = filePath.replace(/^\.\.?\//, "").replace(/^public\//, ""), src.startsWith("/") || (src = "/" + src)); const defaultResult = { src, width: 0, height: 0, blurDataURL: "" }; if (!(0, import_node_fs.existsSync)(resolvedPath)) return console.warn(`[one] getImageData: File not found: ${resolvedPath}`), defaultResult; const meta = await processImageMeta(resolvedPath); return meta ? { src, ...meta } : defaultResult; } //# sourceMappingURL=getImageData.js.map