tsconfig-loader.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. "use strict";
  2. var __create = Object.create;
  3. var __defProp = Object.defineProperty;
  4. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  5. var __getOwnPropNames = Object.getOwnPropertyNames;
  6. var __getProtoOf = Object.getPrototypeOf;
  7. var __hasOwnProp = Object.prototype.hasOwnProperty;
  8. var __export = (target, all) => {
  9. for (var name in all)
  10. __defProp(target, name, { get: all[name], enumerable: true });
  11. };
  12. var __copyProps = (to, from, except, desc) => {
  13. if (from && typeof from === "object" || typeof from === "function") {
  14. for (let key of __getOwnPropNames(from))
  15. if (!__hasOwnProp.call(to, key) && key !== except)
  16. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  17. }
  18. return to;
  19. };
  20. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  21. // If the importer is in node compatibility mode or this is not an ESM
  22. // file that has been converted to a CommonJS file using a Babel-
  23. // compatible transform (i.e. "__esModule" has not been set), then set
  24. // "default" to the CommonJS "module.exports" for node compatibility.
  25. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  26. mod
  27. ));
  28. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  29. var tsconfig_loader_exports = {};
  30. __export(tsconfig_loader_exports, {
  31. loadTsConfig: () => loadTsConfig
  32. });
  33. module.exports = __toCommonJS(tsconfig_loader_exports);
  34. var import_path = __toESM(require("path"));
  35. var import_fs = __toESM(require("fs"));
  36. var import_utilsBundle = require("../utilsBundle");
  37. function loadTsConfig(configPath) {
  38. try {
  39. const references = [];
  40. const config = innerLoadTsConfig(configPath, references);
  41. return [config, ...references];
  42. } catch (e) {
  43. throw new Error(`Failed to load tsconfig file at ${configPath}:
  44. ${e.message}`);
  45. }
  46. }
  47. function resolveConfigFile(baseConfigFile, referencedConfigFile) {
  48. if (!referencedConfigFile.endsWith(".json"))
  49. referencedConfigFile += ".json";
  50. const currentDir = import_path.default.dirname(baseConfigFile);
  51. let resolvedConfigFile = import_path.default.resolve(currentDir, referencedConfigFile);
  52. if (referencedConfigFile.includes("/") && referencedConfigFile.includes(".") && !import_fs.default.existsSync(resolvedConfigFile))
  53. resolvedConfigFile = import_path.default.join(currentDir, "node_modules", referencedConfigFile);
  54. return resolvedConfigFile;
  55. }
  56. function innerLoadTsConfig(configFilePath, references, visited = /* @__PURE__ */ new Map()) {
  57. if (visited.has(configFilePath))
  58. return visited.get(configFilePath);
  59. let result = {
  60. tsConfigPath: configFilePath
  61. };
  62. visited.set(configFilePath, result);
  63. if (!import_fs.default.existsSync(configFilePath))
  64. return result;
  65. const configString = import_fs.default.readFileSync(configFilePath, "utf-8");
  66. const cleanedJson = StripBom(configString);
  67. const parsedConfig = import_utilsBundle.json5.parse(cleanedJson);
  68. const extendsArray = Array.isArray(parsedConfig.extends) ? parsedConfig.extends : parsedConfig.extends ? [parsedConfig.extends] : [];
  69. for (const extendedConfig of extendsArray) {
  70. const extendedConfigPath = resolveConfigFile(configFilePath, extendedConfig);
  71. const base = innerLoadTsConfig(extendedConfigPath, references, visited);
  72. Object.assign(result, base, { tsConfigPath: configFilePath });
  73. }
  74. if (parsedConfig.compilerOptions?.allowJs !== void 0)
  75. result.allowJs = parsedConfig.compilerOptions.allowJs;
  76. if (parsedConfig.compilerOptions?.paths !== void 0) {
  77. result.paths = {
  78. mapping: parsedConfig.compilerOptions.paths,
  79. pathsBasePath: import_path.default.dirname(configFilePath)
  80. };
  81. }
  82. if (parsedConfig.compilerOptions?.baseUrl !== void 0) {
  83. result.absoluteBaseUrl = import_path.default.resolve(import_path.default.dirname(configFilePath), parsedConfig.compilerOptions.baseUrl);
  84. }
  85. for (const ref of parsedConfig.references || [])
  86. references.push(innerLoadTsConfig(resolveConfigFile(configFilePath, ref.path), references, visited));
  87. if (import_path.default.basename(configFilePath) === "jsconfig.json" && result.allowJs === void 0)
  88. result.allowJs = true;
  89. return result;
  90. }
  91. function StripBom(string) {
  92. if (typeof string !== "string") {
  93. throw new TypeError(`Expected a string, got ${typeof string}`);
  94. }
  95. if (string.charCodeAt(0) === 65279) {
  96. return string.slice(1);
  97. }
  98. return string;
  99. }
  100. // Annotate the CommonJS export names for ESM import in node:
  101. 0 && (module.exports = {
  102. loadTsConfig
  103. });