import fs from 'node:fs/promises' import path from 'node:path' import { fileURLToPath } from 'node:url' import sharp from 'sharp' const __dirname = path.dirname(fileURLToPath(import.meta.url)) const root = path.resolve(__dirname, '..') const outThumb = path.join(root, 'public/assets/stage/thumb') const outFull = path.join(root, 'public/assets/stage/full') /** @type {{ src: string; name: string; thumbW: number; fullW: number }[]} */ const STAGE_SOURCES = [ { src: 'public/assets/logo.png', name: 'logo', thumbW: 48, fullW: 512 }, { src: 'public/assets/qr-contact.png', name: 'qr-contact', thumbW: 48, fullW: 512 }, { src: 'public/assets/t30-bottle.png', name: 't30-bottle', thumbW: 64, fullW: 800 }, { src: 'public/assets/brand/emb_p6_5.png', name: 'emb_p6_5', thumbW: 64, fullW: 960 }, { src: 'public/assets/brand/emb_p33_1.png', name: 'emb_p33_1', thumbW: 64, fullW: 960 }, ] async function ensureDirs() { await fs.mkdir(outThumb, { recursive: true }) await fs.mkdir(outFull, { recursive: true }) } async function writeThumb(srcPath, name, width) { const dest = path.join(outThumb, `${name}.webp`) await sharp(srcPath) .resize(width, null, { withoutEnlargement: true }) .blur(2) .webp({ quality: 50 }) .toFile(dest) const stat = await fs.stat(dest) return { dest, kb: Math.round(stat.size / 1024 * 10) / 10 } } async function writeFull(srcPath, name, width) { const dest = path.join(outFull, `${name}.webp`) await sharp(srcPath) .resize(width, null, { withoutEnlargement: true }) .webp({ quality: 82 }) .toFile(dest) const stat = await fs.stat(dest) return { dest, kb: Math.round(stat.size / 1024 * 10) / 10 } } async function main() { await ensureDirs() let thumbTotal = 0 let fullTotal = 0 for (const item of STAGE_SOURCES) { const srcPath = path.join(root, item.src) try { await fs.access(srcPath) } catch { console.warn(`skip (missing): ${item.src}`) continue } const thumb = await writeThumb(srcPath, item.name, item.thumbW) const full = await writeFull(srcPath, item.name, item.fullW) thumbTotal += thumb.kb fullTotal += full.kb console.log(`${item.name}: thumb ${thumb.kb} KB, full ${full.kb} KB`) } console.log(`\nDone. thumb total ~${Math.round(thumbTotal)} KB, full total ~${Math.round(fullTotal)} KB`) } main().catch((err) => { console.error(err) process.exit(1) })