mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 16:10:07 +00:00
94163f76e0
tsc needed about 3.4 GB of heap for the server build, all of it for the type graph, while the build never failed on type errors anyway. An install that builds from source on a 4 GB container (the Proxmox script gives Node 75% of its memory) ran out of heap with 5.2.2. SWC compiles the files tsconfig.build.json names, one at a time and with the options read from it, in about a second and 130 MB; the type check stays with npm run typecheck in CI. Built with both on the same commit, dist holds the same 2390 files, and the server boots with the same 859 routes and 226 modules and answers the same requests the same way.
46 lines
2.2 KiB
JavaScript
46 lines
2.2 KiB
JavaScript
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).`);
|