/**
* Post-build script: inline remaining external assets (favicon) into index.html
* and remove leftover files so only a single HTML file remains.
*/
import { readFileSync, writeFileSync, rmSync, existsSync } from 'fs'
import { join, dirname } from 'path'
import { fileURLToPath } from 'url'
const __dirname = dirname(fileURLToPath(import.meta.url))
const distDir = join(__dirname, '..', 'dist')
// Read favicon SVG and encode as data URI
const faviconPath = join(distDir, 'favicon.svg')
if (existsSync(faviconPath)) {
const svgContent = readFileSync(faviconPath, 'utf8')
const encoded = Buffer.from(svgContent).toString('base64')
const dataUri = `data:image/svg+xml;base64,${encoded}`
// Replace the favicon link in index.html
const indexPath = join(distDir, 'index.html')
let html = readFileSync(indexPath, 'utf8')
html = html.replace(
/]*href="[^"]*favicon\.svg"[^>]*\/?>/i,
``
)
writeFileSync(indexPath, html)
// Remove the standalone SVG
rmSync(faviconPath)
console.log('[inline-assets] Inlined favicon.svg into index.html')
}
// Remove any other leftover asset files (e.g. icons.svg from Svelte compiler,
// and the source pwa-icon.svg which is only needed to regenerate the PNGs).
for (const leftover of ['icons.svg', 'pwa-icon.svg']) {
const p = join(distDir, leftover)
if (existsSync(p)) {
rmSync(p)
console.log(`[inline-assets] Removed ${leftover}`)
}
}
// Remove assets directory if it exists
const assetsDir = join(distDir, 'assets')
if (existsSync(assetsDir)) {
rmSync(assetsDir, { recursive: true })
console.log('[inline-assets] Removed assets/ directory')
}
// Keep the PWA companion files that MUST ship alongside index.html so a user
// can install the app: the web manifest, the service worker, and the icon set.
// (These are public/ files Vite copied into dist/; do not delete them.)
for (const keep of ['manifest.webmanifest', 'sw.js', 'icons']) {
const p = join(distDir, keep)
if (existsSync(p)) console.log(`[inline-assets] Preserved PWA ${keep}: ${p}`)
}
console.log('[inline-assets] Done — dist/ contains index.html + PWA files')