tsconfig-loader.js 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 base = loadTsconfig(path.join(currentDir, extendedConfig), existsSync, readFileSync) || {};
  87. // baseUrl should be interpreted as relative to the base tsconfig,
  88. // but we need to update it so it is relative to the original tsconfig being loaded
  89. if (base && base.compilerOptions && base.compilerOptions.baseUrl) {
  90. var extendsDir = path.dirname(extendedConfig);
  91. base.compilerOptions.baseUrl = path.join(extendsDir, base.compilerOptions.baseUrl);
  92. }
  93. return __assign({}, base, config, { compilerOptions: __assign({}, base.compilerOptions, config.compilerOptions) });
  94. }
  95. return config;
  96. }
  97. exports.loadTsconfig = loadTsconfig;