One command, zero config, and only the client ever bundles: v8 server code is plain ESM that Node runs as-authored, so there is no server build to rot (the lesson of v9). - start: Vite middleware mode + HMR with per-request streaming SSR via ssrLoadModule; a virtual client entry generates the browser glue so the user writes exactly one isomorphic module (plan decision D5). - build: client bundle via Vite 8/Rolldown, manifest-derived route assets in dist/bankai.json, service worker built with the precache list injected (choo-service-worker convention, manifest edition), brotli+gzip precompression of every text asset. - serve: immutable caching + precompressed negotiation for hashed assets, 103 Early Hints (res.writeEarlyHints) with the route's assets before every page, streaming SSR, and a window.initialState tail with script-breakout-safe serialization and choo internals filtered out. - inspect: raw/gzip/brotli size report. Integration tests drive the real counter example through build and serve, asserting the 103 interim response at the HTTP level. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014NgfSjHE11oFpoSnLVKLXd
60 lines
1.9 KiB
JavaScript
60 lines
1.9 KiB
JavaScript
// bankai start — the dev server: Vite middleware mode for the module
|
|
// graph and HMR, per-request streaming SSR of the same entry through
|
|
// vite.ssrLoadModule so server code shares Vite's transforms and cache.
|
|
|
|
import { createServer } from 'node:http'
|
|
|
|
import bankaiPlugin, { CLIENT_URL } from './plugin.js'
|
|
import { resolveApp } from './app.js'
|
|
import { documentHead, documentTail } from './document.js'
|
|
|
|
export default async function dev ({ entry, port = 8080, title = 'choo' }) {
|
|
const { createServer: createViteServer } = await import('vite')
|
|
|
|
const vite = await createViteServer({
|
|
appType: 'custom',
|
|
server: { middlewareMode: true },
|
|
plugins: [bankaiPlugin(entry)],
|
|
logLevel: 'warn'
|
|
})
|
|
|
|
const server = createServer((req, res) => {
|
|
vite.middlewares(req, res, async () => {
|
|
// not an asset Vite knows: this is a page navigation — SSR it
|
|
try {
|
|
const mod = await vite.ssrLoadModule(entry)
|
|
const app = resolveApp(mod, entry)
|
|
const state = {}
|
|
|
|
res.writeHead(200, { 'content-type': 'text/html; charset=utf-8' })
|
|
res.write(documentHead({
|
|
title,
|
|
scripts: ['/@vite/client', CLIENT_URL]
|
|
}))
|
|
|
|
for await (const chunk of app.toStream(req.url, state)) {
|
|
res.write(chunk)
|
|
}
|
|
res.end(documentTail(state))
|
|
} catch (err) {
|
|
vite.ssrFixStacktrace(err)
|
|
console.error(err)
|
|
if (!res.headersSent) res.writeHead(500, { 'content-type': 'text/plain' })
|
|
res.end('bankai dev error:\n\n' + (err.stack || err.message))
|
|
}
|
|
})
|
|
})
|
|
|
|
await new Promise((resolve) => server.listen(port, resolve))
|
|
return {
|
|
server,
|
|
vite,
|
|
port: server.address().port,
|
|
origin: `http://localhost:${server.address().port}`,
|
|
close: async () => {
|
|
await vite.close()
|
|
await new Promise((resolve) => server.close(resolve))
|
|
}
|
|
}
|
|
}
|