one
Version:
One is a new React Framework that makes Vite serve both native and web.
121 lines (118 loc) • 3.47 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all) __defProp(target, name, {
get: all[name],
enumerable: true
});
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from)) if (!__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: true
}) : target, mod));
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
value: true
}), mod);
var getImageData_exports = {};
__export(getImageData_exports, {
getImageData: () => getImageData,
getSharp: () => getSharp,
processImageMeta: () => processImageMeta
});
module.exports = __toCommonJS(getImageData_exports);
var import_fs = require("fs");
var import_path = require("path");
var sharpWarned = false;
async function getSharp() {
try {
var 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) {
var sharp = await getSharp();
if (!sharp) {
return null;
}
try {
var image = sharp(filePath);
var metadata = await image.metadata();
var {
width = 0,
height = 0
} = metadata;
var blurBuffer = await image.resize(10).blur(1).jpeg({
quality: 40
}).toBuffer();
var 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) {
var resolvedPath;
var src;
if (filePath.startsWith("/")) {
resolvedPath = (0, import_path.resolve)("./public", filePath.slice(1));
src = filePath;
} else {
resolvedPath = (0, import_path.resolve)(filePath);
src = filePath.replace(/^\.\.?\//, "").replace(/^public\//, "");
if (!src.startsWith("/")) {
src = "/" + src;
}
}
var defaultResult = {
src,
width: 0,
height: 0,
blurDataURL: ""
};
if (!(0, import_fs.existsSync)(resolvedPath)) {
console.warn(`[one] getImageData: File not found: ${resolvedPath}`);
return defaultResult;
}
var meta = await processImageMeta(resolvedPath);
if (!meta) {
return defaultResult;
}
return {
src,
...meta
};
}
//# sourceMappingURL=getImageData.native.js.map