/** * Generates the PWA icon set from the brand isotype SVG. * * Usage: bun --filter @gruperly/web icons * * The outputs are versioned in `public/`; the Vite build does not regenerate * them (sharp is a devDependency and must not end up in the Docker image). * Re-run only when `src/assets/brand/isotipo-y-organica.svg` changes. */ import { mkdir, readFile, writeFile } from 'node:fs/promises' import { dirname, join } from 'node:path' import { fileURLToPath } from 'node:url' import sharp from 'sharp' const webDir = dirname(dirname(fileURLToPath(import.meta.url))) const source = join(webDir, 'src/assets/brand/isotipo-y-organica.svg') const publicDir = join(webDir, 'public') /** Light theme background; it also serves as the canvas for opaque icons. */ const BACKGROUND = '#ffffff' const TRANSPARENT = { r: 0, g: 0, b: 0, alpha: 0 } as const type IconTarget = { file: string size: number /** Fills the canvas with `BACKGROUND` instead of leaving it transparent. */ opaque: boolean /** Fraction of the side the logo takes up. 1 = no padding. */ contentRatio: number } const targets: IconTarget[] = [ { file: 'pwa-192x192.png', size: 192, opaque: false, contentRatio: 0.9 }, { file: 'pwa-512x512.png', size: 512, opaque: false, contentRatio: 0.9 }, // Android crops adaptive icons in a circle: the useful content lives in the // central 80% of the canvas, so the logo goes on white and scaled. { file: 'pwa-maskable-512x512.png', size: 512, opaque: true, contentRatio: 0.8 }, { file: 'apple-touch-icon-180.png', size: 180, opaque: true, contentRatio: 0.82 }, ] // The isotype viewBox does not start at 0 and leaves air around the glyph: // `trim` crops that margin so the framing uses the drawing's real area. const logo = await sharp(await readFile(source), { density: 512 }) .trim({ threshold: 1 }) .png() .toBuffer() await mkdir(publicDir, { recursive: true }) for (const { file, size, opaque, contentRatio } of targets) { const box = Math.round(size * contentRatio) const layer = await sharp(logo) .resize(box, box, { fit: 'inside' }) .png() .toBuffer() const output = await sharp({ create: { width: size, height: size, channels: 4, background: opaque ? BACKGROUND : TRANSPARENT }, }) .composite([{ input: layer, gravity: 'center' }]) .png({ compressionLevel: 9 }) .toBuffer() await writeFile(join(publicDir, file), output) console.log(`${file.padEnd(28)} ${size}x${size} ${(output.byteLength / 1024).toFixed(1)} KB`) }