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
115 lines
3.7 KiB
JavaScript
115 lines
3.7 KiB
JavaScript
// bankai build — the production build. Only the CLIENT is bundled: the
|
|
// server side of a choo v8 app is plain ESM that Node runs as-authored,
|
|
// so there is no server build to rot. Outputs:
|
|
// dist/assets/* hashed client chunks + css
|
|
// dist/.vite/manifest.json Vite's build manifest
|
|
// dist/bankai.json bankai's route/asset metadata for serving
|
|
// dist/sw.js service worker (if <entry dir>/sw.js exists),
|
|
// with the precached asset list defined in
|
|
// *.br / *.gz precompressed siblings for every text asset
|
|
|
|
import { readFile, writeFile, readdir, stat, access } from 'node:fs/promises'
|
|
import { join, dirname, resolve } from 'node:path'
|
|
import { brotliCompress, gzip, constants } from 'node:zlib'
|
|
import { promisify } from 'node:util'
|
|
|
|
import bankaiPlugin, { CLIENT_ID } from './plugin.js'
|
|
|
|
const brotli = promisify(brotliCompress)
|
|
const gzipP = promisify(gzip)
|
|
|
|
export default async function buildApp ({ entry, outDir = 'dist', title = 'choo' }) {
|
|
const { build } = await import('vite')
|
|
entry = resolve(entry)
|
|
outDir = resolve(outDir)
|
|
|
|
await build({
|
|
appType: 'custom',
|
|
plugins: [bankaiPlugin(entry)],
|
|
logLevel: 'warn',
|
|
build: {
|
|
outDir,
|
|
emptyOutDir: true,
|
|
manifest: true,
|
|
rollupOptions: { input: { main: CLIENT_ID } }
|
|
}
|
|
})
|
|
|
|
// derive the route-independent asset set from Vite's manifest
|
|
const manifest = JSON.parse(await readFile(join(outDir, '.vite', 'manifest.json'), 'utf8'))
|
|
const entryChunk = Object.values(manifest).find((m) => m.isEntry)
|
|
const modulepreload = (entryChunk.imports || []).map((key) => '/' + manifest[key].file)
|
|
const assets = {
|
|
scripts: ['/' + entryChunk.file],
|
|
modulepreload,
|
|
css: collectCss(manifest, entryChunk)
|
|
}
|
|
|
|
const meta = { title, entry, assets, builtAt: new Date().toISOString() }
|
|
await writeFile(join(outDir, 'bankai.json'), JSON.stringify(meta, null, 2))
|
|
|
|
// service worker: <entry dir>/sw.js, built standalone with the asset
|
|
// list injected — the choo-service-worker convention, manifest edition
|
|
const swSource = join(dirname(entry), 'sw.js')
|
|
if (await exists(swSource)) {
|
|
const precache = ['/', ...assets.scripts, ...assets.modulepreload, ...assets.css]
|
|
await build({
|
|
appType: 'custom',
|
|
logLevel: 'warn',
|
|
define: { __BANKAI_ASSETS__: JSON.stringify(precache) },
|
|
build: {
|
|
outDir,
|
|
emptyOutDir: false,
|
|
rollupOptions: {
|
|
input: { sw: swSource },
|
|
output: { entryFileNames: 'sw.js' }
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
// precompress text assets so the server never compresses at request time
|
|
const compressed = await precompress(outDir)
|
|
|
|
return { outDir, meta, compressed }
|
|
}
|
|
|
|
function collectCss (manifest, entryChunk) {
|
|
const css = new Set(entryChunk.css || [])
|
|
for (const key of entryChunk.imports || []) {
|
|
for (const file of manifest[key].css || []) css.add(file)
|
|
}
|
|
return [...css].map((file) => '/' + file)
|
|
}
|
|
|
|
const COMPRESSIBLE = /\.(js|mjs|css|html|json|svg|txt|map)$/
|
|
|
|
async function precompress (dir) {
|
|
let count = 0
|
|
for (const name of await readdir(dir)) {
|
|
const path = join(dir, name)
|
|
const info = await stat(path)
|
|
if (info.isDirectory()) {
|
|
count += await precompress(path)
|
|
continue
|
|
}
|
|
if (!COMPRESSIBLE.test(name) || info.size < 1024) continue
|
|
const data = await readFile(path)
|
|
await writeFile(path + '.br', await brotli(data, {
|
|
params: { [constants.BROTLI_PARAM_QUALITY]: constants.BROTLI_MAX_QUALITY }
|
|
}))
|
|
await writeFile(path + '.gz', await gzipP(data, { level: constants.Z_BEST_COMPRESSION }))
|
|
count++
|
|
}
|
|
return count
|
|
}
|
|
|
|
async function exists (path) {
|
|
try {
|
|
await access(path)
|
|
return true
|
|
} catch (e) {
|
|
return false
|
|
}
|
|
}
|