medieval-kit/scripts/build-artifact.ts
CRT f62975f948 Hash the site's bundles, and take the ops notes out of the README
Two corrections, and the first was a bug I had just written.

`vercel.json` served `/viewer.js` and `/viewer.css` with a year of immutable
caching, and those names carry no content hash — they are pinned in the vite
config. Immutable caching on a fixed name means a returning visitor is handed
the bundle they already have, for a year, no matter how many times the site is
rebuilt. The rule and the filenames were written a few minutes apart and
contradicted each other.

So the bundles are hashed now, which makes the caching correct rather than
wrong, and the cost is exactly the thing the rule creates: nothing downstream
may name them literally. `build-artifact.ts` finds them by pattern. So does
`build-site.ts` — whose own first version listed `viewer.js` by name in its
size report, and fell over on the very build that introduced the hash. The
mistake and its consequence landed in the same commit.

The cache rules now live in a `_headers` file written into the output, where
the host looks for them: a year on the hashed bundles, and revalidation on the
HTML, which must NOT be cached that way — it is what points at the current
hash, and a stale copy of it pins a visitor to a build that no longer exists.

Second: the README had grown a hosting comparison — which providers are free
with a private repository, what GitHub Pages costs. None of that belongs in a
README. It is a public-facing document about a model registry, read by people
deciding whether to install it, and my deployment arithmetic is neither their
business nor true for their situation. What stays is the part that is about
this project: the command, what the output is, and that nothing is fetched at
run time. `vercel.json` goes with it, since it was config for a host this
project does not use.
2026-08-27 06:27:29 +00:00

63 lines
2.4 KiB
TypeScript

/**
* Reduces the dist-viewer/ output to a single self-contained HTML file.
*
* Artifact pages run under a strict CSP: no requests to external hosts. The
* one exception is Google Fonts. So the JS and CSS have to be inlined.
*
* Run:
* bunx vite build --config vite.viewer.config.ts
* bun scripts/build-artifact.ts
*/
import { readdir, readFile, writeFile } from 'node:fs/promises'
import { dirname, join, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
const projectRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..')
const distRoot = join(projectRoot, 'dist-viewer')
// Only the weights actually used. Google Fonts loads every weight lazily;
// asking for weights you do not want just inflates the CSS for nothing.
const FONTS =
'https://fonts.googleapis.com/css2?family=Archivo:wght@400;700' +
'&family=IBM+Plex+Mono:wght@400;500;600&display=swap'
/**
* By pattern, because the built names carry a content hash.
*
* They are hashed so the published site can serve them with a year of
* immutable caching, which is only correct if the name changes when the bytes
* do. The cost is exactly this: nothing downstream may name them literally.
*/
const built = await readdir(distRoot)
const find = (extension: string): string => {
const hit = built.find((name) => name.startsWith('viewer-') && name.endsWith(extension))
if (!hit) throw new Error(`no viewer-*${extension} in dist-viewer — did the vite build run?`)
return join(distRoot, hit)
}
const css = await readFile(find('.css'), 'utf8')
const js = await readFile(find('.js'), 'utf8')
// A `</script` sequence inside the inlined script would close the HTML parser
// early. This escape does not change the JavaScript meaning.
const safeJs = js.replaceAll('</script', String.raw`<\/script`)
const html = `<title>vibe3d Model Workbench</title>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link rel="stylesheet" href="${FONTS}" />
<style>
${css}
</style>
<div id="app"></div>
<script type="module">
${safeJs}
</script>
`
const output = join(distRoot, 'artifact.html')
await writeFile(output, html, 'utf8')
const kb = (value: number) => `${(value / 1024).toFixed(0)} KB`
console.log(`dist-viewer/artifact.html written`)
console.log(` CSS ${kb(css.length)} · JS ${kb(js.length)} · total ${kb(html.length)}`)