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.
62 lines
2.4 KiB
JavaScript
62 lines
2.4 KiB
JavaScript
import ts from 'typescript';
|
|
|
|
/**
|
|
* The SWC options for `npm run build`, read from the tsconfig the build used to
|
|
* hand to tsc, so the compiler options stay written in one place. Only the
|
|
* settings the server is built with are translated; anything else stops the
|
|
* build instead of compiling differently from what the tsconfig says.
|
|
*
|
|
* @param {import('typescript').CompilerOptions} options
|
|
* @returns {import('@swc/core').Options}
|
|
*/
|
|
export function swcOptionsFrom(options) {
|
|
if (options.module !== ts.ModuleKind.CommonJS) {
|
|
throw new Error(
|
|
`[build] only CommonJS output is translated, the tsconfig asks for ${ts.ModuleKind[options.module]}`,
|
|
);
|
|
}
|
|
if (!options.esModuleInterop) {
|
|
throw new Error('[build] only esModuleInterop output is translated');
|
|
}
|
|
if (options.emitDecoratorMetadata && !options.experimentalDecorators) {
|
|
throw new Error('[build] emitDecoratorMetadata without experimentalDecorators is not translated');
|
|
}
|
|
const target = options.target ?? ts.ScriptTarget.ES5;
|
|
return {
|
|
swcrc: false,
|
|
configFile: false,
|
|
sourceMaps: Boolean(options.sourceMap),
|
|
jsc: {
|
|
parser: { syntax: 'typescript', decorators: Boolean(options.experimentalDecorators) },
|
|
target: ts.ScriptTarget[target].toLowerCase(),
|
|
transform: {
|
|
legacyDecorator: Boolean(options.experimentalDecorators),
|
|
decoratorMetadata: Boolean(options.emitDecoratorMetadata),
|
|
// tsc's own default: class fields are defined, not assigned, from ES2022 on.
|
|
useDefineForClassFields: options.useDefineForClassFields ?? target >= ts.ScriptTarget.ES2022,
|
|
},
|
|
},
|
|
module: { type: 'commonjs' },
|
|
};
|
|
}
|
|
|
|
/**
|
|
* A tsconfig file as tsc reads it: `extends` resolved, its options and the files it compiles.
|
|
*
|
|
* @param {string} tsconfigPath
|
|
* @returns {import('typescript').ParsedCommandLine}
|
|
*/
|
|
export function readTsconfig(tsconfigPath) {
|
|
const parsed = ts.getParsedCommandLineOfConfigFile(tsconfigPath, undefined, {
|
|
...ts.sys,
|
|
onUnRecoverableConfigFileDiagnostic: (diagnostic) => {
|
|
throw new Error(ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'));
|
|
},
|
|
});
|
|
if (!parsed) throw new Error(`[build] cannot read ${tsconfigPath}`);
|
|
if (parsed.errors.length > 0) {
|
|
throw new Error(parsed.errors.map((d) => ts.flattenDiagnosticMessageText(d.messageText, '\n')).join('\n'));
|
|
}
|
|
return parsed;
|
|
}
|