#!/usr/bin/env node /** * Post-build: inline critical CSS in dist/*.html (SvelteKit adapter-static output). * Runs after vite build; Beasties reads/writes relative to dist/. * * Beasties with preload:'default' adds preload tags; same options as legacy Critters. */ import Beasties from 'beasties'; import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const ROOT = path.join(__dirname, '..'); const DIST = path.join(ROOT, 'dist'); async function main() { if (!fs.existsSync(DIST)) { console.error('dist/ not found. Run vite build first.'); process.exit(1); } const beasties = new Beasties({ path: DIST, preload: 'default', noscriptFallback: true, pruneSource: false, logLevel: 'warn', }); const files = fs.readdirSync(DIST).filter((f) => f.endsWith('.html')); for (const file of files) { const filePath = path.join(DIST, file); let html = fs.readFileSync(filePath, 'utf8'); html = await beasties.process(html); fs.writeFileSync(filePath, html, 'utf8'); console.log('✓ Critical CSS inlined → dist/' + file); } console.log('Critical CSS step complete.'); } main().catch((err) => { console.error(err); process.exit(1); });