import sharp from "sharp"; import { readdir, unlink, stat } from "node:fs/promises"; import { join } from "node:path"; /** * Verkleinert die aufgenommenen Screenshots für das Repository. * Die Aufnahme läuft mit doppelter Pixeldichte (scharfe Schrift), abgelegt * wird die halbe Kantenlänge als WebP — das spart rund 90 Prozent. */ const DIR = "docs/screenshots"; const files = (await readdir(DIR)).filter((name) => name.endsWith(".png")); let before = 0; let after = 0; for (const name of files) { const source = join(DIR, name); before += (await stat(source)).size; const image = sharp(source); const meta = await image.metadata(); const target = join(DIR, name.replace(/\.png$/, ".webp")); await image .resize({ width: Math.round((meta.width ?? 1400) / 2) }) .webp({ quality: 82, effort: 5 }) .toFile(target); after += (await stat(target)).size; await unlink(source); } const mb = (bytes) => (bytes / 1024 / 1024).toFixed(1); console.log(`${files.length} Bilder: ${mb(before)} MB -> ${mb(after)} MB`);