tsconfig-loader.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. Object.defineProperty(exports, "__esModule", { value: true });
  14. exports.loadTsconfig = exports.walkForTsConfig = exports.tsConfigLoader = void 0;
  15. var path = require("path");
  16. var fs = require("fs");
  17. // tslint:disable:no-require-imports
  18. var JSON5 = require("json5");
  19. var StripBom = require("strip-bom");
  20. function tsConfigLoader(_a) {
  21. var getEnv = _a.getEnv, cwd = _a.cwd, _b = _a.loadSync, loadSync = _b === void 0 ? loadSyncDefault : _b;
  22. var TS_NODE_PROJECT = getEnv("TS_NODE_PROJECT");
  23. var TS_NODE_BASEURL = getEnv("TS_NODE_BASEURL");
  24. // tsconfig.loadSync handles if TS_NODE_PROJECT is a file or directory
  25. // and also overrides baseURL if TS_NODE_BASEURL is available.
  26. var loadResult = loadSync(cwd, TS_NODE_PROJECT, TS_NODE_BASEURL);
  27. return loadResult;
  28. }
  29. exports.tsConfigLoader = tsConfigLoader;
  30. function loadSyncDefault(cwd, filename, baseUrl) {
  31. // Tsconfig.loadSync uses path.resolve. This is why we can use an absolute path as filename
  32. var configPath = resolveConfigPath(cwd, filename);
  33. if (!configPath) {
  34. return {
  35. tsConfigPath: undefined,
  36. baseUrl: undefined,
  37. paths: undefined,
  38. };
  39. }
  40. var config = loadTsconfig(configPath);
  41. return {
  42. tsConfigPath: configPath,
  43. baseUrl: baseUrl ||
  44. (config && config.compilerOptions && config.compilerOptions.baseUrl),
  45. paths: config && config.compilerOptions && config.compilerOptions.paths,
  46. };
  47. }
  48. function resolveConfigPath(cwd, filename) {
  49. if (filename) {
  50. var absolutePath = fs.lstatSync(filename).isDirectory()
  51. ? path.resolve(filename, "./tsconfig.json")
  52. : path.resolve(cwd, filename);
  53. return absolutePath;
  54. }
  55. if (fs.statSync(cwd).isFile()) {
  56. return path.resolve(cwd);
  57. }
  58. var configAbsolutePath = walkForTsConfig(cwd);
  59. return configAbsolutePath ? path.resolve(configAbsolutePath) : undefined;
  60. }
  61. function walkForTsConfig(directory, existsSync) {
  62. if (existsSync === void 0) { existsSync = fs.existsSync; }
  63. var configPath = path.join(directory, "./tsconfig.json");
  64. if (existsSync(configPath)) {
  65. return configPath;
  66. }
  67. var parentDirectory = path.join(directory, "../");
  68. // If we reached the top
  69. if (directory === parentDirectory) {
  70. return undefined;
  71. }
  72. return walkForTsConfig(parentDirectory, existsSync);
  73. }
  74. exports.walkForTsConfig = walkForTsConfig;
  75. function loadTsconfig(configFilePath, existsSync, readFileSync) {
  76. if (existsSync === void 0) { existsSync = fs.existsSync; }
  77. if (readFileSync === void 0) { readFileSync = function (filename) {
  78. return fs.readFileSync(filename, "utf8");
  79. }; }
  80. if (!existsSync(configFilePath)) {
  81. return undefined;
  82. }
  83. var configString = readFileSync(configFilePath);
  84. var cleanedJson = StripBom(configString);
  85. var config = JSON5.parse(cleanedJson);
  86. var extendedConfig = config.extends;
  87. if (extendedConfig) {
  88. if (typeof extendedConfig === "string" &&
  89. extendedConfig.indexOf(".json") === -1) {
  90. extendedConfig += ".json";
  91. }
  92. var currentDir = path.dirname(configFilePath);
  93. var extendedConfigPath = path.join(currentDir, extendedConfig);
  94. if (extendedConfig.indexOf("/") !== -1 &&
  95. extendedConfig.indexOf(".") !== -1 &&
  96. !existsSync(extendedConfigPath)) {
  97. extendedConfigPath = path.join(currentDir, "node_modules", extendedConfig);
  98. }
  99. var base = loadTsconfig(extendedConfigPath, existsSync, readFileSync) || {};
  100. // baseUrl should be interpreted as relative to the base tsconfig,
  101. // but we need to update it so it is relative to the original tsconfig being loaded
  102. if (base.compilerOptions && base.compilerOptions.baseUrl) {
  103. var extendsDir = path.dirname(extendedConfig);
  104. base.compilerOptions.baseUrl = path.join(extendsDir, base.compilerOptions.baseUrl);
  105. }
  106. return __assign(__assign(__assign({}, base), config), { compilerOptions: __assign(__assign({}, base.compilerOptions), config.compilerOptions) });
  107. }
  108. return config;
  109. }
  110. exports.loadTsconfig = loadTsconfig;
  111. //# sourceMappingURL=tsconfig-loader.js.map