// Build a single, self-contained dist/index.html (plus PWA sidecar files). // // Everything — app code, the @scure/@noble crypto libs, the QR generator, and // all CSS — is bundled and inlined so the result is one file you can save and // open offline straight from the filesystem (file://). No server, no network. // // For the hosted site we also emit a web manifest, icons, and a small service // worker so the page can be installed as a PWA and still work offline once // installed. index.html stays self-contained; these are optional extras that // simply 404 (harmlessly) when the file is opened on its own from file://. import { mkdir, readdir } from 'node:fs/promises'; const FAVICON = 'data:image/svg+xml,' + encodeURIComponent( `` ); // Static assets copied verbatim into dist/ (icons referenced by the manifest). const STATIC = ['icon-192.png', 'icon-512.png', 'icon-maskable-512.png', 'icon.svg']; const MANIFEST = { id: '/', name: 'Hal', short_name: 'Hal', description: 'Self-custody Bitcoin wallet that runs entirely in your browser.', start_url: './', scope: './', display: 'standalone', background_color: '#eef0f3', theme_color: '#f7931a', icons: [ { src: 'icon-192.png', sizes: '192x192', type: 'image/png', purpose: 'any' }, { src: 'icon-512.png', sizes: '512x512', type: 'image/png', purpose: 'any' }, { src: 'icon-maskable-512.png', sizes: '512x512', type: 'image/png', purpose: 'maskable' }, ], }; // Service worker: precache the app shell, network-first for the page (so a new // deploy shows up), cache-first for the icons/manifest, and never touch the // cross-origin explorer/Nostr requests. CACHE carries a build token so each // deploy supersedes the previous cache. {{VERSION}} is filled in at build time. const SW = `const CACHE = 'cold-{{VERSION}}'; // jsqr.js is precached so the lazy QR decoder is available offline too (for // installed PWAs on browsers without a native BarcodeDetector). const SHELL = ['./', 'manifest.webmanifest', 'icon-192.png', 'icon-512.png', 'jsqr.js']; self.addEventListener('install', (e) => { self.skipWaiting(); e.waitUntil(caches.open(CACHE).then((c) => c.addAll(SHELL))); }); self.addEventListener('activate', (e) => { e.waitUntil( caches .keys() .then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))) .then(() => self.clients.claim()) ); }); self.addEventListener('fetch', (e) => { const req = e.request; if (req.method !== 'GET') return; if (new URL(req.url).origin !== self.location.origin) return; // explorer/ws → network if (req.mode === 'navigate') { e.respondWith( fetch(req) .then((res) => { const copy = res.clone(); caches.open(CACHE).then((c) => c.put('./', copy)); return res; }) .catch(() => caches.match('./')) ); return; } // Cache-first, but cache same-origin assets (e.g. the chosen locale, icons) // on first fetch so they're available offline without precaching them all. e.respondWith( caches.match(req).then( (hit) => hit || fetch(req).then((res) => { if (res.ok) { const copy = res.clone(); caches.open(CACHE).then((c) => c.put(req, copy)); } return res; }) ) ); }); `; const PWA_HEAD = ` `; const SW_REGISTER = ``; // Bundle the standalone jsQR decoder (sets window.jsQR) for lazy loading. export async function buildJsQr({ minify = true } = {}) { const result = await Bun.build({ entrypoints: ['./src/jsqr-global.js'], target: 'browser', minify, }); if (!result.success) { for (const log of result.logs) console.error(log); throw new Error('jsqr bundle failed'); } return result.outputs[0].text(); } // The silent-payment scanning worker, bundled separately (its own global scope) // and embedded as base64 so the single-file app can spawn it from a Blob URL. export async function buildSpWorker({ minify = true } = {}) { const result = await Bun.build({ entrypoints: ['./src/sp-worker.js'], target: 'browser', minify, }); if (!result.success) { for (const log of result.logs) console.error(log); throw new Error('sp-worker bundle failed'); } return result.outputs[0].text(); } export async function buildHtml({ minify = true, pwa = minify } = {}) { const result = await Bun.build({ entrypoints: ['./src/app.js'], target: 'browser', minify, }); if (!result.success) { for (const log of result.logs) console.error(log); throw new Error('bundle failed'); } let js = await result.outputs[0].text(); // Guard against a literal inside the bundle closing our tag early. js = js.replaceAll('