tsconfig-loader.js 4.3 KB

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