/**
* Rebuild backend/tab-assets/ from source. Run after changing the browser
* client, the in-process demo router, or htmx.
*
* node build-assets.mjs (or: npm run build-assets)
*
* Produces, in ../../backend/tab-assets/:
* - pear-request-client.bundle.js (IIFE: window.createPearRequest + Buffer)
* - router.cjs (CJS bundle: PearRequestRouter + demo routes)
* - htmx.min.js (copied)
* - assets.js (wrapper.html + htmx + client as require-able
* strings — Pear virtualizes the FS, so backend
* modules can't fs.readFileSync app files)
* wrapper.html in tab-assets is hand-maintained (the Stage-2 wrapper) and is NOT
* overwritten here — only re-inlined into assets.js.
*/
import * as esbuild from 'esbuild'
import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
const dir = path.dirname(fileURLToPath(import.meta.url))
const OUT = path.resolve(dir, '../../backend/tab-assets')
await esbuild.build({
entryPoints: [path.join(dir, 'client-entry.mjs')],
bundle: true, format: 'iife', platform: 'browser',
define: { 'process.env.NODE_ENV': '"production"', global: 'globalThis' },
outfile: path.join(OUT, 'pear-request-client.bundle.js')
})
await esbuild.build({
entryPoints: [path.join(dir, 'router-entry.mjs')],
bundle: true, format: 'cjs', platform: 'node',
outfile: path.join(OUT, 'router.cjs')
})
fs.copyFileSync(path.join(dir, 'htmx.min.js'), path.join(OUT, 'htmx.min.js'))
const read = (f) => fs.readFileSync(path.join(OUT, f), 'utf8')
const mod = '// Generated by examples/headless-tab/build-assets.mjs — inline assets (Pear apps\n' +
'// virtualize the filesystem, so backend modules ship assets as require-able strings).\n' +
'module.exports = {\n' +
' wrapper: ' + JSON.stringify(read('wrapper.html')) + ',\n' +
' htmx: ' + JSON.stringify(read('htmx.min.js')) + ',\n' +
' client: ' + JSON.stringify(read('pear-request-client.bundle.js')) + '\n}\n'
fs.writeFileSync(path.join(OUT, 'assets.js'), mod)
console.log('tab-assets rebuilt:')
for (const f of ['pear-request-client.bundle.js', 'router.cjs', 'htmx.min.js', 'assets.js']) {
console.log(' ' + f + ' (' + fs.statSync(path.join(OUT, f)).size + ' bytes)')
}