/** * Renders every photograph in src/assets/images at fixed widths as AVIF and * WebP, for the bucket (src/lib/media/variants.ts), and writes the manifest * next/image's loader reads: * * pnpm run media:render render what is missing into .media/, write the manifest * pnpm run media:render --check exit 1 if the manifest does not match the originals * * A variant already rendered is skipped, so a rerun only does what changed. * Runs on a developer's machine or in CI, never on the app box; the manifest * is committed, the variants are not (`pnpm run media:upload` sends them). */ import { createHash } from "node:crypto"; import { existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs"; import { dirname, extname, join } from "node:path"; import sharp, { type Sharp } from "sharp"; import { MEDIA_ENCODE, MEDIA_FORMATS, variantKey, widthsFor, type MediaEntry, type MediaFormat, type MediaManifest, } from "../src/lib/media/variants"; const ROOT = process.cwd(); const SOURCE = join(ROOT, "src/assets/images"); const OUT = join(ROOT, ".media"); const MANIFEST = join(ROOT, "src/lib/media/manifest.json"); const IMAGE = /\.(webp|jpe?g|png|avif)$/i; const ENCODE: Record Sharp> = { avif: (s) => s.avif(MEDIA_ENCODE.avif), webp: (s) => s.webp(MEDIA_ENCODE.webp), }; export async function describe(file: string): Promise { const bytes = readFileSync(join(SOURCE, file)); const meta = await sharp(bytes).metadata(); if (!meta.width || !meta.height) throw new Error(`${file}: no dimensions`); return { hash: createHash("sha256").update(bytes).digest("hex").slice(0, 12), width: meta.width, height: meta.height, widths: widthsFor(meta.width), }; } async function main() { const check = process.argv.includes("--check"); const files = readdirSync(SOURCE) .filter((f) => IMAGE.test(f)) .sort(); const manifest: MediaManifest = { photos: {} }; for (const file of files) { const name = file.slice(0, -extname(file).length); if (manifest.photos[name]) throw new Error(`two originals named ${name}`); manifest.photos[name] = await describe(file); } const text = JSON.stringify(manifest, null, 2) + "\n"; if (check) { const held = existsSync(MANIFEST) ? readFileSync(MANIFEST, "utf8") : ""; if (held !== text) { console.error("src/lib/media/manifest.json is stale: run pnpm run media:render"); process.exit(1); } console.log(`manifest matches ${files.length} originals`); return; } let rendered = 0; let kept = 0; for (const file of files) { const name = file.slice(0, -extname(file).length); const entry = manifest.photos[name]; const original = readFileSync(join(SOURCE, file)); for (const width of entry.widths) { for (const format of MEDIA_FORMATS) { const out = join(OUT, variantKey(name, entry, width, format)); if (existsSync(out)) { kept++; continue; } mkdirSync(dirname(out), { recursive: true }); await ENCODE[format](sharp(original).resize({ width, withoutEnlargement: true })).toFile( out, ); rendered++; } } } writeFileSync(MANIFEST, text); console.log(`${files.length} photographs: rendered ${rendered}, already there ${kept} (.media/)`); } main().catch((error) => { console.error(error); process.exit(1); });