ttsc
Version:
General-purpose TypeScript-Go compiler, runtime, plugin host, and LSP host.
59 lines • 2.81 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const node_path_1 = __importDefault(require("node:path"));
const node_url_1 = require("node:url");
const runtimeHooks_1 = require("./runtimeHooks");
/**
* Bootstrap `ttsx` spawns as the child's main module: it installs the runtime
* module hooks, then loads the TypeScript entry _from source_.
*
* Why load the entry here instead of passing it as Node's main entry: a
* CommonJS `require("./x")` chain only reaches `module.registerHooks` when the
* entry itself is loaded through a CommonJS `require`. Running the `.ts` as
* Node's ESM-first main entry leaves inner `require`s on the native loader,
* where they cannot resolve a sibling `.ts`. So the parent invokes `node
* registerRuntimeHooks.js <entry> [argv...]` and we `require()` (or dynamically
* `import()`) the entry after the hooks are live.
*
* `process.argv` is rewritten to `[node, <entry>, ...argv]` so the program sees
* the same argv it would under a direct `node <entry>` invocation.
*/
const entry = process.argv[2];
if (entry === undefined) {
process.stderr.write("ttsx: internal error — no entry passed to bootstrap\n");
process.exit(2);
}
const forwarded = process.argv.slice(3);
process.argv = [process.argv[0], entry, ...forwarded];
(0, runtimeHooks_1.installRuntimeHooks)();
// The physical path, because the format question is a runtime question: the
// hooks serve the entry under its real path, and `moduleFormat` walks from the
// path it is handed to find the `package.json` that decides. Asking from a
// symlink's own directory reads the wrong package scope and hands an ESM emit
// to `require`.
const kind = (0, runtimeHooks_1.entryModuleFormat)((0, runtimeHooks_1.realPath)(node_path_1.default.resolve(entry)));
function fail(error) {
// Match Node's own uncaught-exception surfacing for a thrown entry error.
process.stderr.write(`${error instanceof Error ? (error.stack ?? error.message) : String(error)}\n`);
process.exit(1);
}
// A genuine dynamic `import()`. Authored directly it would be downlevelled to a
// `require()` shim under this package's CommonJS emit, which cannot load a
// `file:` URL or an ESM module — so build it through `Function` to keep it an
// `import` at runtime.
const dynamicImport = new Function("specifier", "return import(specifier);");
if (kind === "module") {
dynamicImport((0, node_url_1.pathToFileURL)(node_path_1.default.resolve(entry)).href).catch(fail);
}
else {
try {
require(node_path_1.default.resolve(entry));
}
catch (error) {
fail(error);
}
}
//# sourceMappingURL=registerRuntimeHooks.js.map