// Pre-compile every public/*.jsx to public/dist/*.js using esbuild.
// Issue #139 — production was shipping React dev builds + in-browser @babel/standalone.
// Now JSX is transformed at Docker build time and the runtime loads minified UMD React.
const { build } = require('esbuild');
const path = require('path');
const fs = require('fs');

const root = path.resolve(__dirname, '..');
const publicDir = path.join(root, 'public');
const outDir = path.join(publicDir, 'dist');
fs.mkdirSync(outDir, { recursive: true });

const entries = fs.readdirSync(publicDir).filter(f => f.endsWith('.jsx'));

(async () => {
  for (const f of entries) {
    const src = path.join(publicDir, f);
    const out = path.join(outDir, f.replace(/\.jsx$/, '.js'));
    await build({
      entryPoints: [src],
      outfile: out,
      bundle: false,
      loader: { '.jsx': 'jsx' },
      jsx: 'transform',
      jsxFactory: 'React.createElement',
      jsxFragment: 'React.Fragment',
      minify: true,
      sourcemap: true,
      target: ['es2019'],
      logLevel: 'info',
    });
  }
  console.log(`[build-jsx] compiled ${entries.length} jsx files to ${outDir}`);
})().catch(err => { console.error(err); process.exit(1); });
