tsconfig-loader.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.tsConfigLoader = tsConfigLoader;
  6. exports.walkForTsConfig = walkForTsConfig;
  7. var path = _interopRequireWildcard(require("path"));
  8. var fs = _interopRequireWildcard(require("fs"));
  9. var _utilsBundle = require("../utilsBundle");
  10. function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  11. function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  12. /**
  13. * The MIT License (MIT)
  14. *
  15. * Copyright (c) 2016 Jonas Kello
  16. *
  17. * Permission is hereby granted, free of charge, to any person obtaining a copy
  18. * of this software and associated documentation files (the "Software"), to deal
  19. * in the Software without restriction, including without limitation the rights
  20. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  21. * copies of the Software, and to permit persons to whom the Software is
  22. * furnished to do so, subject to the following conditions:
  23. *
  24. * The above copyright notice and this permission notice shall be included in all
  25. * copies or substantial portions of the Software.
  26. *
  27. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  28. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  29. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  30. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  31. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  32. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  33. * SOFTWARE.
  34. */
  35. /* eslint-disable */
  36. /**
  37. * Typing for the parts of tsconfig that we care about
  38. */
  39. function tsConfigLoader({
  40. cwd
  41. }) {
  42. const loadResult = loadSyncDefault(cwd);
  43. loadResult.serialized = JSON.stringify(loadResult);
  44. return loadResult;
  45. }
  46. function loadSyncDefault(cwd) {
  47. var _config$compilerOptio;
  48. // Tsconfig.loadSync uses path.resolve. This is why we can use an absolute path as filename
  49. const configPath = resolveConfigPath(cwd);
  50. if (!configPath) {
  51. return {
  52. tsConfigPath: undefined,
  53. baseUrl: undefined,
  54. paths: undefined,
  55. serialized: undefined,
  56. allowJs: false
  57. };
  58. }
  59. const config = loadTsconfig(configPath);
  60. return {
  61. tsConfigPath: configPath,
  62. baseUrl: config && config.compilerOptions && config.compilerOptions.baseUrl,
  63. paths: config && config.compilerOptions && config.compilerOptions.paths,
  64. serialized: undefined,
  65. allowJs: !!(config !== null && config !== void 0 && (_config$compilerOptio = config.compilerOptions) !== null && _config$compilerOptio !== void 0 && _config$compilerOptio.allowJs)
  66. };
  67. }
  68. function resolveConfigPath(cwd) {
  69. if (fs.statSync(cwd).isFile()) {
  70. return path.resolve(cwd);
  71. }
  72. const configAbsolutePath = walkForTsConfig(cwd);
  73. return configAbsolutePath ? path.resolve(configAbsolutePath) : undefined;
  74. }
  75. function walkForTsConfig(directory, existsSync = fs.existsSync) {
  76. const tsconfigPath = path.join(directory, "./tsconfig.json");
  77. if (existsSync(tsconfigPath)) {
  78. return tsconfigPath;
  79. }
  80. const jsconfigPath = path.join(directory, "./jsconfig.json");
  81. if (existsSync(jsconfigPath)) {
  82. return jsconfigPath;
  83. }
  84. const parentDirectory = path.join(directory, "../");
  85. // If we reached the top
  86. if (directory === parentDirectory) {
  87. return undefined;
  88. }
  89. return walkForTsConfig(parentDirectory, existsSync);
  90. }
  91. function loadTsconfig(configFilePath) {
  92. var _config$compilerOptio2;
  93. if (!fs.existsSync(configFilePath)) {
  94. return undefined;
  95. }
  96. const configString = fs.readFileSync(configFilePath, 'utf-8');
  97. const cleanedJson = StripBom(configString);
  98. const parsedConfig = _utilsBundle.json5.parse(cleanedJson);
  99. let config = {};
  100. const extendsArray = Array.isArray(parsedConfig.extends) ? parsedConfig.extends : parsedConfig.extends ? [parsedConfig.extends] : [];
  101. for (let extendedConfig of extendsArray) {
  102. if (typeof extendedConfig === "string" && extendedConfig.indexOf(".json") === -1) {
  103. extendedConfig += ".json";
  104. }
  105. const currentDir = path.dirname(configFilePath);
  106. let extendedConfigPath = path.join(currentDir, extendedConfig);
  107. if (extendedConfig.indexOf("/") !== -1 && extendedConfig.indexOf(".") !== -1 && !fs.existsSync(extendedConfigPath)) {
  108. extendedConfigPath = path.join(currentDir, "node_modules", extendedConfig);
  109. }
  110. const base = loadTsconfig(extendedConfigPath) || {};
  111. // baseUrl should be interpreted as relative to the base tsconfig,
  112. // but we need to update it so it is relative to the original tsconfig being loaded
  113. if (base.compilerOptions && base.compilerOptions.baseUrl) {
  114. const extendsDir = path.dirname(extendedConfig);
  115. base.compilerOptions.baseUrl = path.join(extendsDir, base.compilerOptions.baseUrl);
  116. }
  117. config = mergeConfigs(config, base);
  118. }
  119. config = mergeConfigs(config, parsedConfig);
  120. // The only top-level property that is excluded from inheritance is "references".
  121. // https://www.typescriptlang.org/tsconfig#extends
  122. config.references = parsedConfig.references;
  123. if (path.basename(configFilePath) === 'jsconfig.json' && ((_config$compilerOptio2 = config.compilerOptions) === null || _config$compilerOptio2 === void 0 ? void 0 : _config$compilerOptio2.allowJs) === undefined) {
  124. config.compilerOptions = config.compilerOptions || {};
  125. config.compilerOptions.allowJs = true;
  126. }
  127. return config;
  128. }
  129. function mergeConfigs(base, override) {
  130. return {
  131. ...base,
  132. ...override,
  133. compilerOptions: {
  134. ...base.compilerOptions,
  135. ...override.compilerOptions
  136. }
  137. };
  138. }
  139. function StripBom(string) {
  140. if (typeof string !== 'string') {
  141. throw new TypeError(`Expected a string, got ${typeof string}`);
  142. }
  143. // Catches EFBBBF (UTF-8 BOM) because the buffer-to-string
  144. // conversion translates it to FEFF (UTF-16 BOM).
  145. if (string.charCodeAt(0) === 0xFEFF) {
  146. return string.slice(1);
  147. }
  148. return string;
  149. }