Files
booksorter/scripts/build-web.ts
T
2026-11-18 09:46:46 -07:00

32 lines
1.2 KiB
TypeScript

import { build } from 'esbuild';
import { copyFile, mkdir, readFile, writeFile } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
const root = fileURLToPath(new URL('..', import.meta.url));
const out = `${root}dist/web`;
await mkdir(out, { recursive: true });
await build({
entryPoints: [`${root}src/web/main.tsx`],
outfile: `${out}/app.js`,
bundle: true,
format: 'esm',
sourcemap: true,
jsx: 'automatic',
jsxImportSource: 'preact',
target: 'es2022',
minify: false,
logLevel: 'info',
});
// zxing-wasm ships a default locateFile pointing at jsDelivr. scanner.ts always overrides it, so it is
// dead code, but neutralise the URL anyway so the bundle contains no CDN address at all.
const appJs = await readFile(`${out}/app.js`, 'utf8');
await writeFile(`${out}/app.js`, appJs.replaceAll('https://fastly.jsdelivr.net/npm/', '/local-only/'));
await copyFile(`${root}src/web/index.html`, `${out}/index.html`);
await copyFile(`${root}src/web/styles.css`, `${out}/styles.css`);
// The zxing reader wasm is served from our own server at /zxing_reader.wasm (see scanner.ts).
await copyFile(`${root}node_modules/zxing-wasm/dist/reader/zxing_reader.wasm`, `${out}/zxing_reader.wasm`);
console.log('Built dist/web');