import { readTsconfig, swcOptionsFrom } from './build-options.mjs'; import { transformFileSync } from '@swc/core'; import { existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'; import { dirname, join, relative } from 'node:path'; import { fileURLToPath } from 'node:url'; // Resolved from this file rather than cwd, so the directory that gets removed is // always this package's own dist no matter where the script is invoked from. const packageRoot = dirname(dirname(fileURLToPath(import.meta.url))); // SWC compiles each file on its own, the way the test suite runs the code, and // never builds the type graph: tsc needed more than 3 GB of heap for that alone, // more than an install building on a small container has. Type errors are the // job of `npm run typecheck`, which CI runs; the build never failed on them. The // files, the directories and the compiler options are the ones tsc would take // from tsconfig.build.json. const tsconfig = readTsconfig(join(packageRoot, 'tsconfig.build.json')); const { rootDir, outDir } = tsconfig.options; const swcOptions = swcOptionsFrom(tsconfig.options); // A module deleted or renamed in src would otherwise leave its compiled twin // behind. For most code that is merely stale, but the migrator globs // dist/db/migrations at boot: an orphan there is a live migration with no // source, and it runs. Same for dist/db/seeders. Nothing else writes into dist, // since the client bundle goes to server/public and the runtime assets the // Dockerfile needs are copied in separately, so it is safe to drop wholesale. rmSync(outDir, { recursive: true, force: true }); let count = 0; for (const file of tsconfig.fileNames) { if (file.endsWith('.d.ts')) continue; if (!file.endsWith('.ts')) throw new Error(`[build] ${file}: only .ts sources are compiled`); const out = join(outDir, relative(rootDir, file)).replace(/\.ts$/, '.js'); mkdirSync(dirname(out), { recursive: true }); writeFileSync(out, transformFileSync(file, swcOptions).code); count++; } if (!existsSync(join(outDir, 'index.js'))) { console.error(`[build] ${join(outDir, 'index.js')} is missing after compiling ${count} files.`); process.exit(1); } console.log(`[build] dist ready (${count} files).`);