// Bundles and minifies client TypeScript and modular CSS into dist/bundle/. import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import * as esbuild from 'esbuild'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const APP_ROOT = path.resolve(__dirname, '..'); const BUNDLE_DIR = path.join(APP_ROOT, 'dist', 'bundle'); const COMMON_OPTIONS = { bundle: true, minify: true, sourcemap: true, legalComments: 'eof', logLevel: 'info', }; const CLIENT_TARGETS = [ { ...COMMON_OPTIONS, entryPoints: [path.join(APP_ROOT, 'src', 'client', 'app.ts')], outfile: path.join(BUNDLE_DIR, 'app.js'), format: 'esm', platform: 'browser', target: ['es2022'], }, { ...COMMON_OPTIONS, entryPoints: [path.join(APP_ROOT, 'public', 'css', 'style.css')], outfile: path.join(BUNDLE_DIR, 'style.css'), }, ]; async function main() { const watchMode = process.argv.includes('--watch'); fs.rmSync(BUNDLE_DIR, { recursive: true, force: true }); fs.mkdirSync(BUNDLE_DIR, { recursive: true }); if (!watchMode) { await Promise.all(CLIENT_TARGETS.map((opts) => esbuild.build(opts))); return; } const contexts = await Promise.all(CLIENT_TARGETS.map((opts) => esbuild.context(opts))); await Promise.all(contexts.map((ctx) => ctx.watch())); console.log('[esbuild] Watching client JS & CSS...'); const shutdown = async () => { await Promise.all(contexts.map((ctx) => ctx.dispose())); process.exit(0); }; process.on('SIGINT', shutdown); process.on('SIGTERM', shutdown); } main().catch((err) => { console.error(err); process.exit(1); });