|
| 1 | +/** |
| 2 | + * Compiles all API handlers into self-contained ESM bundles so the |
| 3 | + * local-api-server.mjs sidecar can discover and load them without node_modules. |
| 4 | + * |
| 5 | + * Two passes: |
| 6 | + * 1. TypeScript handlers (api/**\/*.ts) → bundled .js at same path |
| 7 | + * 2. Plain JS handlers (api/*.js root level) → bundled in-place to inline npm deps |
| 8 | + * |
| 9 | + * Run: node docker/build-handlers.mjs |
| 10 | + */ |
| 11 | + |
| 12 | +import { build } from 'esbuild'; |
| 13 | +import { readdir, stat } from 'node:fs/promises'; |
| 14 | +import { fileURLToPath } from 'node:url'; |
| 15 | +import path from 'node:path'; |
| 16 | + |
| 17 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 18 | +const projectRoot = path.resolve(__dirname, '..'); |
| 19 | +const apiRoot = path.join(projectRoot, 'api'); |
| 20 | + |
| 21 | +// ── Pass 1: TypeScript handlers in subdirectories ───────────────────────── |
| 22 | +async function findTsHandlers(dir) { |
| 23 | + const entries = await readdir(dir, { withFileTypes: true }); |
| 24 | + const handlers = []; |
| 25 | + for (const entry of entries) { |
| 26 | + const fullPath = path.join(dir, entry.name); |
| 27 | + if (entry.isDirectory()) { |
| 28 | + handlers.push(...await findTsHandlers(fullPath)); |
| 29 | + } else if ( |
| 30 | + entry.name.endsWith('.ts') && |
| 31 | + !entry.name.startsWith('_') && |
| 32 | + !entry.name.endsWith('.test.ts') && |
| 33 | + !entry.name.endsWith('.d.ts') |
| 34 | + ) { |
| 35 | + handlers.push(fullPath); |
| 36 | + } |
| 37 | + } |
| 38 | + return handlers; |
| 39 | +} |
| 40 | + |
| 41 | +// ── Pass 2: Plain JS handlers at api/ root level ────────────────────────── |
| 42 | +async function findJsHandlers(dir) { |
| 43 | + const entries = await readdir(dir, { withFileTypes: true }); |
| 44 | + return entries |
| 45 | + .filter(e => |
| 46 | + e.isFile() && |
| 47 | + e.name.endsWith('.js') && |
| 48 | + !e.name.startsWith('_') && |
| 49 | + !e.name.endsWith('.test.js') && |
| 50 | + !e.name.endsWith('.test.mjs') |
| 51 | + ) |
| 52 | + .map(e => path.join(dir, e.name)); |
| 53 | +} |
| 54 | + |
| 55 | +async function compileHandlers(handlers, label) { |
| 56 | + if (handlers.length === 0) { |
| 57 | + console.log(`${label}: nothing to compile`); |
| 58 | + return 0; |
| 59 | + } |
| 60 | + console.log(`${label}: compiling ${handlers.length} handlers...`); |
| 61 | + |
| 62 | + const results = await Promise.allSettled( |
| 63 | + handlers.map(async (entryPoint) => { |
| 64 | + const outfile = entryPoint.replace(/\.ts$/, '.js'); |
| 65 | + await build({ |
| 66 | + entryPoints: [entryPoint], |
| 67 | + outfile, |
| 68 | + bundle: true, |
| 69 | + format: 'esm', |
| 70 | + platform: 'node', |
| 71 | + target: 'node20', |
| 72 | + treeShaking: true, |
| 73 | + allowOverwrite: true, |
| 74 | + loader: { '.ts': 'ts' }, |
| 75 | + }); |
| 76 | + const { size } = await stat(outfile); |
| 77 | + return { file: path.relative(projectRoot, outfile), size }; |
| 78 | + }) |
| 79 | + ); |
| 80 | + |
| 81 | + let ok = 0, failed = 0; |
| 82 | + for (const result of results) { |
| 83 | + if (result.status === 'fulfilled') { |
| 84 | + const { file, size } = result.value; |
| 85 | + console.log(` ✓ ${file} (${(size / 1024).toFixed(1)} KB)`); |
| 86 | + ok++; |
| 87 | + } else { |
| 88 | + console.error(` ✗ ${result.reason?.message || result.reason}`); |
| 89 | + failed++; |
| 90 | + } |
| 91 | + } |
| 92 | + return failed; |
| 93 | +} |
| 94 | + |
| 95 | +const tsHandlers = await findTsHandlers(apiRoot); |
| 96 | +const jsHandlers = await findJsHandlers(apiRoot); |
| 97 | + |
| 98 | +const tsFailed = await compileHandlers(tsHandlers, 'build-handlers [TS]'); |
| 99 | +// JS handlers bundled AFTER TS so compiled .js outputs don't get re-processed |
| 100 | +const jsFailed = await compileHandlers(jsHandlers, 'build-handlers [JS]'); |
| 101 | + |
| 102 | +const totalFailed = tsFailed + jsFailed; |
| 103 | +console.log(`\nbuild-handlers: complete (${totalFailed} failures)`); |
| 104 | +if (totalFailed > 0) process.exit(1); |
0 commit comments