tsconfig-loader.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. // tsconfig.loadSync handles if TS_NODE_PROJECT is a file or directory
  20. var loadResult = loadSync(cwd, TS_NODE_PROJECT);
  21. return loadResult;
  22. }
  23. exports.tsConfigLoader = tsConfigLoader;
  24. function loadSyncDefault(cwd, filename) {
  25. // Tsconfig.loadSync uses path.resolve. This is why we can use an absolute path as filename
  26. var configPath = resolveConfigPath(cwd, filename);
  27. if (!configPath) {
  28. return {
  29. tsConfigPath: undefined,
  30. baseUrl: undefined,
  31. paths: undefined,
  32. };
  33. }
  34. var config = loadTsconfig(configPath);
  35. return {
  36. tsConfigPath: configPath,
  37. baseUrl: config && config.compilerOptions && config.compilerOptions.baseUrl,
  38. paths: config && config.compilerOptions && config.compilerOptions.paths,
  39. };
  40. }
  41. function resolveConfigPath(cwd, filename) {
  42. if (filename) {
  43. var absolutePath = fs.lstatSync(filename).isDirectory()
  44. ? path.resolve(filename, "./tsconfig.json")
  45. : path.resolve(cwd, filename);
  46. return absolutePath;
  47. }
  48. if (fs.statSync(cwd).isFile()) {
  49. return path.resolve(cwd);
  50. }
  51. var configAbsolutePath = walkForTsConfig(cwd);
  52. return configAbsolutePath ? path.resolve(configAbsolutePath) : undefined;
  53. }
  54. function walkForTsConfig(directory, existsSync) {
  55. if (existsSync === void 0) { existsSync = fs.existsSync; }
  56. var configPath = path.join(directory, "./tsconfig.json");
  57. if (existsSync(configPath)) {
  58. return configPath;
  59. }
  60. var parentDirectory = path.join(directory, "../");
  61. // If we reached the top
  62. if (directory === parentDirectory) {
  63. return undefined;
  64. }
  65. return walkForTsConfig(parentDirectory, existsSync);
  66. }
  67. exports.walkForTsConfig = walkForTsConfig;
  68. function loadTsconfig(configFilePath, existsSync, readFileSync) {
  69. if (existsSync === void 0) { existsSync = fs.existsSync; }
  70. if (readFileSync === void 0) { readFileSync = function (filename) {
  71. return fs.readFileSync(filename, "utf8");
  72. }; }
  73. if (!existsSync(configFilePath)) {
  74. return undefined;
  75. }
  76. var configString = readFileSync(configFilePath);
  77. var cleanedJson = StripBom(configString);
  78. var config = JSON5.parse(cleanedJson);
  79. var extendedConfig = config.extends;
  80. if (extendedConfig) {
  81. if (typeof extendedConfig === "string" &&
  82. extendedConfig.indexOf(".json") === -1) {
  83. extendedConfig += ".json";
  84. }
  85. var currentDir = path.dirname(configFilePath);
  86. var extendedConfigPath = path.join(currentDir, extendedConfig);
  87. if (extendedConfig.indexOf("/") !== -1 &&
  88. extendedConfig.indexOf(".") !== -1 &&
  89. !existsSync(extendedConfigPath)) {
  90. extendedConfigPath = path.join(currentDir, "node_modules", extendedConfig);
  91. }
  92. var base = loadTsconfig(extendedConfigPath, existsSync, readFileSync) || {};
  93. // baseUrl should be interpreted as relative to the base tsconfig,
  94. // but we need to update it so it is relative to the original tsconfig being loaded
  95. if (base.compilerOptions && base.compilerOptions.baseUrl) {
  96. var extendsDir = path.dirname(extendedConfig);
  97. base.compilerOptions.baseUrl = path.join(extendsDir, base.compilerOptions.baseUrl);
  98. }
  99. return __assign({}, base, config, { compilerOptions: __assign({}, base.compilerOptions, config.compilerOptions) });
  100. }
  101. return config;
  102. }
  103. exports.loadTsconfig = loadTsconfig;