// 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(); } 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(' Hal ${pwa ? PWA_HEAD : ''}
${pwa ? SW_REGISTER + '\n' : ''} `; } if (import.meta.main) { await mkdir('dist', { recursive: true }); const html = await buildHtml({ minify: true }); await Bun.write('dist/index.html', html); // Lazy-loaded QR decoder — kept out of index.html, fetched only when a // browser without BarcodeDetector opens the scanner. await Bun.write('dist/jsqr.js', await buildJsQr()); // Per-language strings — fetched on demand so visitors only download theirs. await mkdir('dist/locales', { recursive: true }); const locales = await readdir('src/locales'); for (const f of locales) await Bun.write('dist/locales/' + f, Bun.file('src/locales/' + f)); // PWA sidecars. await Bun.write('dist/manifest.webmanifest', JSON.stringify(MANIFEST, null, 2)); const version = Bun.hash(html).toString(36); await Bun.write('dist/sw.js', SW.replace('{{VERSION}}', version)); for (const f of STATIC) await Bun.write('dist/' + f, Bun.file('static/' + f)); const kb = (Buffer.byteLength(html) / 1024).toFixed(0); console.log(`✓ dist/index.html written (${kb} KB) — open it offline, no server needed`); console.log(`✓ PWA: manifest.webmanifest, sw.js (cold-${version}), ${STATIC.length} icons, jsqr.js, ${locales.length} locales`); } ref='#n51'>51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249