/** * KoalaSync Static Site Generator (i18n compiler) * Build pipeline: esbuild + AVIF + hashing + SVG min. */ const fs = require('fs'); const path = require('path'); const crypto = require('crypto'); const sharp = require('sharp'); const esbuild = require('esbuild'); const htmlnano = require('htmlnano'); const { optimize: svgoOptimize } = require('svgo'); // CSS minifier: simple regex-based (proven, 27% reduction, no deps) function minifyCSS(code) { return code .replace(/\/\*[\s\S]*?\*\//g, '') // Never strip whitespace before `:`: `.parent :is(...)` is a // descendant selector, while `.parent:is(...)` targets the parent. .replace(/\s*([{},;])\s*/g, '$1') .replace(/:\s+/g, ':') .replace(/\s+/g, ' ') .replace(/;\}/g, '}') .trim(); } if (minifyCSS('.parent :is(.a, .b) { color: red; }') !== '.parent :is(.a,.b){color:red}') { throw new Error('CSS minifier must preserve descendant combinators before functional pseudo-classes'); } // HTML only: parser-based comment removal and conservative whitespace collapse. // CSS, JS, JSON, and SVG keep their dedicated build steps below. const HTML_MINIFY_OPTIONS = Object.freeze({ skipConfigLoading: true, removeComments: 'safe', collapseWhitespace: 'conservative' }); const HTML_MINIFY_PRESET = Object.freeze({}); async function minifyHTML(html) { return (await htmlnano.process(html, HTML_MINIFY_OPTIONS, HTML_MINIFY_PRESET)).html; } async function verifyHTMLMinifier() { const pre = '
  keep\n    exactly  
'; const jsonLd = ''; const conditional = ''; const result = await minifyHTML(`--unsafe-->${pre}${jsonLd}${conditional}`); const ordinaryComments = result.replace(conditional, ''); if (ordinaryComments.includes('