registerTransformHook
uses node:module
Compile marked cache modules (enums, namespaces, parameter properties) as this process loads them: once a process, whichever copy of tcm asks (an app of tcm's, opening a codebase, is one), so the hook never comes after another that must see a module first (Flight's, which loads browser code as references).
import { TRANSFORM_MARK } from "#codebase/tcmts/cli/codebase"; import { transpileForNode } from "#codebase/tcmts/core/program"; import fs from "node:fs"; import { registerHooks } from "node:module"; export function registerTransformHook() { const g = globalThis as { __tcmTransformHook?: boolean }; if (g.__tcmTransformHook) return; g.__tcmTransformHook = true; registerHooks({ load(url, context, nextLoad) { if (url.startsWith("file:") && (url.endsWith(".ts") || url.endsWith(".tsx"))) { const source = fs.readFileSync(new URL(url), "utf8"); if (source.startsWith(TRANSFORM_MARK)) return { format: "module", source: transpileForNode(source, url), shortCircuit: true }; } return nextLoad(url, context); }, }); }