generate-libs.mjs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Build-time generator: embeds the TypeScript stdlib .d.ts closure into
  2. // src/compiler/libs.generated.ts so the workflow compiler runs with zero disk
  3. // access. The bundled/SEA CLI has no node_modules on disk, so the compiler host
  4. // cannot read lib.es2022.d.ts (and its /// <reference lib> chain) from the
  5. // typescript package the way ts.createCompilerHost does. We snapshot that exact
  6. // closure at build time instead.
  7. //
  8. // Idempotent: if libs.generated.ts already matches the installed typescript
  9. // version, exit without rewriting so watch/test loops stay fast.
  10. import { createRequire } from "node:module";
  11. import { existsSync, readFileSync, writeFileSync } from "node:fs";
  12. import { dirname, join, resolve } from "node:path";
  13. import { fileURLToPath } from "node:url";
  14. const scriptDir = dirname(fileURLToPath(import.meta.url));
  15. const packageRoot = resolve(scriptDir, "..");
  16. const outputPath = join(packageRoot, "src", "compiler", "libs.generated.ts");
  17. // Root of the lib closure: mirror COMPILER_OPTIONS.lib in compile.ts.
  18. const ROOT_LIB_FILE = "lib.es2022.d.ts";
  19. // `/// <reference lib="es2021" />` → lib.es2021.d.ts
  20. const REFERENCE_LIB = /\/\/\/\s*<reference\s+lib\s*=\s*["']([^"']+)["']\s*\/>/g;
  21. const require = createRequire(join(packageRoot, "package.json"));
  22. const typescriptPackageJsonPath = require.resolve("typescript/package.json");
  23. const typescriptVersion = JSON.parse(
  24. readFileSync(typescriptPackageJsonPath, "utf8"),
  25. ).version;
  26. const libDirectory = join(dirname(typescriptPackageJsonPath), "lib");
  27. if (existsSync(outputPath)) {
  28. const existing = readFileSync(outputPath, "utf8");
  29. const versionMatch = /TS_LIB_VERSION = "([^"]+)"/.exec(existing);
  30. if (versionMatch?.[1] === typescriptVersion) {
  31. process.exit(0);
  32. }
  33. }
  34. // Walk the reference chain transitively from the root lib file.
  35. const libs = {};
  36. const queue = [ROOT_LIB_FILE];
  37. const seen = new Set();
  38. while (queue.length > 0) {
  39. const fileName = queue.shift();
  40. if (seen.has(fileName)) continue;
  41. seen.add(fileName);
  42. const text = readFileSync(join(libDirectory, fileName), "utf8");
  43. libs[fileName] = text;
  44. REFERENCE_LIB.lastIndex = 0;
  45. let match;
  46. while ((match = REFERENCE_LIB.exec(text)) !== null) {
  47. queue.push(`lib.${match[1]}.d.ts`);
  48. }
  49. }
  50. const entries = Object.keys(libs)
  51. .sort()
  52. .map((name) => ` ${JSON.stringify(name)}: ${JSON.stringify(libs[name])},`)
  53. .join("\n");
  54. const output = `// GENERATED FILE — DO NOT EDIT.
  55. // Regenerated by scripts/generate-libs.mjs whenever the installed typescript
  56. // version changes. Embeds the TypeScript stdlib .d.ts closure so the workflow
  57. // compiler host is fully virtual (no disk access in the bundled/SEA CLI).
  58. export const TS_LIB_VERSION = ${JSON.stringify(typescriptVersion)};
  59. export const TS_LIBS: Record<string, string> = {
  60. ${entries}
  61. };
  62. `;
  63. writeFileSync(outputPath, output);
  64. process.stdout.write(
  65. `generate-libs: wrote ${Object.keys(libs).length} lib files for typescript@${typescriptVersion}\n`,
  66. );