| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- // Build-time generator: embeds the TypeScript stdlib .d.ts closure into
- // src/compiler/libs.generated.ts so the workflow compiler runs with zero disk
- // access. The bundled/SEA CLI has no node_modules on disk, so the compiler host
- // cannot read lib.es2022.d.ts (and its /// <reference lib> chain) from the
- // typescript package the way ts.createCompilerHost does. We snapshot that exact
- // closure at build time instead.
- //
- // Idempotent: if libs.generated.ts already matches the installed typescript
- // version, exit without rewriting so watch/test loops stay fast.
- import { createRequire } from "node:module";
- import { existsSync, readFileSync, writeFileSync } from "node:fs";
- import { dirname, join, resolve } from "node:path";
- import { fileURLToPath } from "node:url";
- const scriptDir = dirname(fileURLToPath(import.meta.url));
- const packageRoot = resolve(scriptDir, "..");
- const outputPath = join(packageRoot, "src", "compiler", "libs.generated.ts");
- // Root of the lib closure: mirror COMPILER_OPTIONS.lib in compile.ts.
- const ROOT_LIB_FILE = "lib.es2022.d.ts";
- // `/// <reference lib="es2021" />` → lib.es2021.d.ts
- const REFERENCE_LIB = /\/\/\/\s*<reference\s+lib\s*=\s*["']([^"']+)["']\s*\/>/g;
- const require = createRequire(join(packageRoot, "package.json"));
- const typescriptPackageJsonPath = require.resolve("typescript/package.json");
- const typescriptVersion = JSON.parse(
- readFileSync(typescriptPackageJsonPath, "utf8"),
- ).version;
- const libDirectory = join(dirname(typescriptPackageJsonPath), "lib");
- if (existsSync(outputPath)) {
- const existing = readFileSync(outputPath, "utf8");
- const versionMatch = /TS_LIB_VERSION = "([^"]+)"/.exec(existing);
- if (versionMatch?.[1] === typescriptVersion) {
- process.exit(0);
- }
- }
- // Walk the reference chain transitively from the root lib file.
- const libs = {};
- const queue = [ROOT_LIB_FILE];
- const seen = new Set();
- while (queue.length > 0) {
- const fileName = queue.shift();
- if (seen.has(fileName)) continue;
- seen.add(fileName);
- const text = readFileSync(join(libDirectory, fileName), "utf8");
- libs[fileName] = text;
- REFERENCE_LIB.lastIndex = 0;
- let match;
- while ((match = REFERENCE_LIB.exec(text)) !== null) {
- queue.push(`lib.${match[1]}.d.ts`);
- }
- }
- const entries = Object.keys(libs)
- .sort()
- .map((name) => ` ${JSON.stringify(name)}: ${JSON.stringify(libs[name])},`)
- .join("\n");
- const output = `// GENERATED FILE — DO NOT EDIT.
- // Regenerated by scripts/generate-libs.mjs whenever the installed typescript
- // version changes. Embeds the TypeScript stdlib .d.ts closure so the workflow
- // compiler host is fully virtual (no disk access in the bundled/SEA CLI).
- export const TS_LIB_VERSION = ${JSON.stringify(typescriptVersion)};
- export const TS_LIBS: Record<string, string> = {
- ${entries}
- };
- `;
- writeFileSync(outputPath, output);
- process.stdout.write(
- `generate-libs: wrote ${Object.keys(libs).length} lib files for typescript@${typescriptVersion}\n`,
- );
|