import { execFileSync } from 'node:child_process'
import { readFileSync, writeFileSync } from 'node:fs'
import { defineConfig, type Plugin } from 'vite'
// Builds the HyperFrames composition from deck/ and copies its slideshow
// island into the wrapper page, so slide order and notes live in one place.
function deck(): Plugin {
let island = ''
// A fresh Node process per build, so edits to any deck/ module are picked up
// (an in-process import would keep serving cached copies of lib.mjs etc).
const rebuild = async () => {
execFileSync(process.execPath, ['scripts/build-deck.mjs'], { stdio: 'inherit' })
const out = readFileSync('public/deck/index.html', 'utf8')
island = out.match(/application\/hyperframes-slideshow\+json">([\s\S]*?)<\/script>/)?.[1]?.trim() ?? ''
}
return {
name: 'kody-deck',
async buildStart() {
await rebuild()
},
transformIndexHtml(html) {
// read the island fresh: the deck may have been rebuilt outside this process
const built = readFileSync('public/deck/index.html', 'utf8')
island = built.match(/application\/hyperframes-slideshow\+json">([\s\S]*?)<\/script>/)?.[1]?.trim() ?? island
return html.replace(
'',
``,
)
},
configureServer(server) {
server.watcher.add(['deck/**'])
const onChange = async (file: string) => {
if (!file.includes('/deck/') || file.includes('/public/')) return
try {
await rebuild()
server.ws.send({ type: 'full-reload' })
} catch (error) {
server.config.logger.error(String(error))
}
}
// editors and `sed -i` often replace the file, which arrives as `add`
server.watcher.on('change', onChange)
server.watcher.on('add', onChange)
},
}
}
// Published as a Kody package app: the router serves the page, and every file
// is a static asset under `/_assets/`. The router swaps this
// placeholder base for the real mount (see app/router.ts).
export const KODY_BASE = '/__KODY_BASE__/'
// After a build, write dist/index.html into a module the package router imports.
function pageModule(): Plugin {
return {
name: 'kody-page-module',
apply: 'build',
closeBundle() {
const page = readFileSync('dist/index.html', 'utf8')
writeFileSync(
'app/page.generated.ts',
`// Generated by \`npm run build\` from dist/index.html. Do not edit.\nexport const page = ${JSON.stringify(page)}\n`,
)
},
}
}
export default defineConfig(({ command }) => ({
base: command === 'build' ? KODY_BASE : '/',
plugins: [deck(), pageModule()],
server: { port: 5199 },
}))