Files
scriptc/packages/compiler/src/frontend/comptime-node.ts
T
Chris Tate 9d30b56c6b Speed up compiler startup and trim release runtime packs
- Cache host libc detection and load TypeScript 5 only when needed.
- Exclude debug payloads from release runtime packs and vendor archives.
- Preserve native self-hosting, portable diagnostics, and reliable Sandbox execution.
2026-09-29 21:41:39 -05:00

18 lines
834 B
TypeScript

import vm from "node:vm";
import { createRequire } from "node:module";
const require = createRequire(import.meta.url);
/** The Node client's compile-time evaluator. Only source text and the
* resulting value cross this boundary; TS5 ASTs never enter the frontend.
* Every invocation gets fresh globals and a bounded execution window. */
export function evaluateNodeComptime(source: string, timeoutMs: number): unknown {
const ts5 = require("typescript5") as typeof import("typescript5");
const js = ts5.transpileModule(`(${source})()`, {
compilerOptions: { target: ts5.ScriptTarget.ESNext },
}).outputText;
// Shadow V8's otherwise silent per-context console. The capture walk
// rejects direct console references before evaluation.
return vm.runInNewContext(js, { console: undefined }, { timeout: timeoutMs });
}