import { readFileSync, writeFileSync, rmSync, readdirSync } from 'fs'
import { resolve, dirname } from 'path'
import { fileURLToPath } from 'url'
const __dirname = dirname(fileURLToPath(import.meta.url))
const root = resolve(__dirname, '..')
const buildDir = process.env.BUILD_DIR || 'dist'
// Import SSR bundle (always built to dist/server inside project)
const { render } = await import('../dist/server/entry-server.js')
let html = readFileSync(resolve(root, buildDir, 'index.html'), 'utf-8')
// Inject prerendered HTML
const appHtml = render()
html = html.replace('
', `${appHtml}
`)
// Fix preload font: find actual cyrillic-700 woff2 hash in dist/assets
const assetsDir = resolve(root, buildDir, 'assets')
const fontFile = readdirSync(assetsDir).find(f => f.match(/manrope-cyrillic-700-normal-.+\.woff2/))
if (fontFile) {
html = html.replace(
/href="\/assets\/manrope-cyrillic-700-normal-[^"]+\.woff2"/,
`href="/assets/${fontFile}"`
)
console.log(`✓ Preload font updated: ${fontFile}`)
}
writeFileSync(resolve(root, buildDir, 'index.html'), html)
// Cleanup SSR bundle
rmSync(resolve(root, 'dist/server'), { recursive: true, force: true })
console.log('✓ Prerendered index.html')