Skip to content

scripts/embed-assets.mjs

25 lines · 1.0 KB · JavaScript
Raw
// Embeds public/ images into src/assets.ts so the app can serve them (no static hosting).
// Run after changing anything in public/: node scripts/embed-assets.mjs
import fs from 'node:fs'
import path from 'node:path'
const root = path.resolve(import.meta.dirname, '..')
const types = { '.jpg': 'image/jpeg', '.png': 'image/png', '.svg': 'image/svg+xml' }
const out = {}
const walk = (dir) => {
	for (const f of fs.readdirSync(dir).sort()) {
		const p = path.join(dir, f)
		if (fs.statSync(p).isDirectory()) walk(p)
		else if (types[path.extname(f)]) {
			out['/' + path.relative(path.join(root, 'public'), p).split(path.sep).join('/')] = {
				type: types[path.extname(f)],
				b64: fs.readFileSync(p).toString('base64'),
			}
		}
	}
}
walk(path.join(root, 'public'))
fs.writeFileSync(
	path.join(root, 'src/assets.ts'),
	`// Generated by scripts/embed-assets.mjs from public/. Do not edit by hand.\nexport const ASSETS: Record<string, { type: string; b64: string }> = ${JSON.stringify(out, null, '\t')}\n`,
)
console.log('embedded', Object.keys(out).join(', '))