feat(adventure): add single-file build pipeline

This commit is contained in:
Alexander Brown
2026-03-26 18:39:44 -07:00
parent 0a7bd170a8
commit 8ad6d19594
3 changed files with 43 additions and 3 deletions

View File

@@ -5,10 +5,11 @@
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc --noEmit && vite build",
"build": "tsc --noEmit && vite build && tsx scripts/inline-build.ts",
"preview": "vite preview"
},
"devDependencies": {
"tsx": "catalog:",
"typescript": "catalog:",
"vite": "catalog:"
},
@@ -30,7 +31,7 @@
"executor": "nx:run-commands",
"cache": true,
"options": {
"command": "vite build --config apps/architecture-adventure/vite.config.ts"
"command": "tsc --noEmit && vite build --config apps/architecture-adventure/vite.config.ts && tsx apps/architecture-adventure/scripts/inline-build.ts"
},
"outputs": [
"{projectRoot}/dist"

View File

@@ -0,0 +1,39 @@
import { readFileSync, writeFileSync, readdirSync, existsSync } from 'node:fs'
import { join } from 'node:path'
const distDir = join(import.meta.dirname, '..', 'dist')
const htmlPath = join(distDir, 'index.html')
let html = readFileSync(htmlPath, 'utf-8')
const assetsDir = join(distDir, 'assets')
if (existsSync(assetsDir)) {
const assets = readdirSync(assetsDir)
// Inline CSS files
for (const file of assets) {
if (file.endsWith('.css')) {
const css = readFileSync(join(assetsDir, file), 'utf-8')
html = html.replace(
new RegExp(`<link[^>]*href="[./]*assets/${file}"[^>]*>`),
`<style>${css}</style>`
)
}
}
// Inline JS files
for (const file of assets) {
if (file.endsWith('.js')) {
const js = readFileSync(join(assetsDir, file), 'utf-8')
html = html.replace(
new RegExp(`<script[^>]*src="[./]*assets/${file}"[^>]*></script>`),
`<script type="module">${js}</script>`
)
}
}
}
writeFileSync(htmlPath, html)
const sizeKB = (Buffer.byteLength(html) / 1024).toFixed(1)
console.warn(`Single-file build complete: ${htmlPath} (${sizeKB} KB)`)

View File

@@ -17,5 +17,5 @@
},
"baseUrl": "."
},
"include": ["src/**/*.ts", "vite.config.ts"]
"include": ["src/**/*.ts", "vite.config.ts", "scripts/**/*.ts"]
}