/** * After Vite and sync-extracted-from-dist: * 1) Writes dist/qwb-builtin-embed.js (EM fallback). * 2) Injects built site + manifest into dist/index.html as JSON (base64) so * WEBSITE publish can read them from the live document — no fetch() of * site.html / manifest.json (often 404/503 in the Hub for non-entry files). * * Must run *after* sync, which copies dist/dev.html → dist/index.html. */ import { readFileSync, writeFileSync, existsSync, copyFileSync } from 'node:fs'; import { fileURLToPath } from 'node:url'; import { dirname, join } from 'node:path'; const __dir = dirname(fileURLToPath(import.meta.url)); const root = join(__dir, '..'); const dist = join(root, 'dist'); const sitePath = join(dist, 'site.html'); const manPath = join(dist, 'manifest.json'); const indexPath = join(dist, 'index.html'); const outPath = join(dist, 'qwb-builtin-embed.js'); if (!existsSync(sitePath) || !existsSync(manPath)) { console.warn( 'emit-builtin-embed: dist/site.html or dist/manifest.json missing — run vite build first. Skipping.', ); process.exit(0); } const site = readFileSync(sitePath, 'utf8'); const man = readFileSync(manPath, 'utf8'); const siteB64 = Buffer.from(site, 'utf8').toString('base64'); const manB64 = Buffer.from(man, 'utf8').toString('base64'); const payload = JSON.stringify({ siteB64, manB64 }); // Optional ESM fallback (some hosts block arbitrary sibling files; index inject is primary) const esm = '// auto-generated from dist/site.html + dist/manifest.json; do not edit\n' + 'export const BUILTIN_SITE_HTML = ' + JSON.stringify(site) + ';\n' + 'export const BUILTIN_MANIFEST = ' + JSON.stringify(man) + ';\n'; writeFileSync(outPath, esm, 'utf8'); console.log('Wrote', outPath); if (!existsSync(indexPath)) { console.warn('emit-builtin-embed: dist/index.html missing — cannot inject. Run sync-extracted first.'); process.exit(0); } let html = readFileSync(indexPath, 'utf8'); html = html.replace(/\n`; if (/<\/body>/i.test(html)) { html = html.replace(/<\/body>/i, tag + ' '); } else { html = html + tag; } writeFileSync(indexPath, html, 'utf8'); console.log('Injected qwb-builtin-data into', indexPath); copyFileSync(indexPath, join(root, 'index.html')); copyFileSync(indexPath, join(root, 'index.hub.html')); console.log('Updated project index.html and index.hub.html'); const extractedIndex = join(root, 'extracted', 'index.html'); if (existsSync(extractedIndex)) { copyFileSync(indexPath, extractedIndex); console.log('Updated extracted/index.html'); }