48 lines
1.6 KiB
JavaScript
48 lines
1.6 KiB
JavaScript
/**
|
|
* 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(
|
|
/<link rel="icon"[^>]*href="[^"]*favicon\.svg"[^>]*\/?>/i,
|
|
`<link rel="icon" type="image/svg+xml" href="${dataUri}" />`
|
|
)
|
|
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)
|
|
const iconsPath = join(distDir, 'icons.svg')
|
|
if (existsSync(iconsPath)) {
|
|
rmSync(iconsPath)
|
|
console.log('[inline-assets] Removed icons.svg')
|
|
}
|
|
|
|
// 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')
|
|
}
|
|
|
|
console.log('[inline-assets] Done — dist/ contains only index.html')
|