// 기술 역량 원본 이미지를 웹 전송용 WebP로 변환합니다. // 실행: node scripts/generate-capability-webp.mjs [--force] import fs from "node:fs"; import path from "node:path"; import vm from "node:vm"; import { execFileSync } from "node:child_process"; import { fileURLToPath } from "node:url"; const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); const force = process.argv.includes("--force"); const context = { window: {} }; vm.runInNewContext(fs.readFileSync(path.join(root, "js", "capabilities-data.js"), "utf8"), context); const sources = new Map(); for (const item of context.window.CAPABILITY_PROJECTS) { if (item.image) sources.set(item.image, "background"); if (item.objectImage) sources.set(item.objectImage, "object"); if (item.objectImageSecondary) sources.set(item.objectImageSecondary, "object"); } function dimensions(source) { const output = execFileSync("sips", ["-g", "pixelWidth", "-g", "pixelHeight", source], { encoding: "utf8", }); return { width: Number(output.match(/pixelWidth: (\d+)/)?.[1]), height: Number(output.match(/pixelHeight: (\d+)/)?.[1]), }; } function optimizedPath(source) { return source .replace("assets/images/", "assets/images-webp/") .replace(/\.(?:png|jpe?g)$/i, ".webp"); } let sourceBytes = 0; let outputBytes = 0; let generated = 0; for (const [relativeSource, type] of sources) { const source = path.join(root, relativeSource); const target = path.join(root, optimizedPath(relativeSource)); const sourceStat = fs.statSync(source); sourceBytes += sourceStat.size; fs.mkdirSync(path.dirname(target), { recursive: true }); if (!force && fs.existsSync(target) && fs.statSync(target).mtimeMs >= sourceStat.mtimeMs) { outputBytes += fs.statSync(target).size; continue; } const { width, height } = dimensions(source); const maxWidth = type === "background" ? 2560 : 2400; const maxHeight = type === "background" ? 1800 : 2400; const scale = Math.min(1, maxWidth / width, maxHeight / height); const args = ["-quiet", "-mt", "-m", "6", "-q", type === "background" ? "80" : "86"]; if (type === "background") args.push("-sharp_yuv"); else args.push("-alpha_q", "100", "-exact"); if (scale < 1) args.push("-resize", String(Math.round(width * scale)), String(Math.round(height * scale))); args.push(source, "-o", target); execFileSync("cwebp", args, { stdio: "inherit" }); outputBytes += fs.statSync(target).size; generated += 1; } const savedPercent = Math.round((1 - outputBytes / sourceBytes) * 100); console.log( `${generated ? `Generated ${generated}` : "Checked"} WebP assets: ` + `${(sourceBytes / 1048576).toFixed(1)}MB -> ${(outputBytes / 1048576).toFixed(1)}MB (${savedPercent}% reduction)`, );