import fs from "node:fs/promises"; import path from "node:path"; import sharp from "sharp"; const dir = new URL("../public/images/", import.meta.url); const files = (await fs.readdir(dir)).filter((f) => /\.(png|webp|jpg)$/i.test(f) && !f.endsWith('-600.webp'), ); let before = 0, after = 0; for (const file of files) { const src = new URL(file, dir); const input = await fs.readFile(src); before += input.length; const max = file.startsWith('logo.') ? 350 : 1400; const full = await sharp(input) .resize(max, max, { fit: "inside", withoutEnlargement: true }) .webp({ quality: 83 }) .toBuffer(); const base = path.parse(file).name; await fs.writeFile(new URL(base + ".webp", dir), full); after += full.length; if (!["favicon", "logo"].includes(base)) { const small = await sharp(input) .resize(600, 600, { fit: "inside", withoutEnlargement: true }) .webp({ quality: 80 }) .toBuffer(); await fs.writeFile(new URL(base + "-600.webp", dir), small); } if (file !== base + ".webp") await fs.unlink(src); } console.log( `Full images: ${(before / 1e6).toFixed(1)} MB → ${(after / 1e6).toFixed(1)} MB. Also generated responsive 600px images.`, );