load-jsconfig.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = loadJsConfig;
  6. var _path = _interopRequireDefault(require("path"));
  7. var _fileExists = require("../lib/file-exists");
  8. var Log = _interopRequireWildcard(require("./output/log"));
  9. var _getTypeScriptConfiguration = require("../lib/typescript/getTypeScriptConfiguration");
  10. var _fs = require("fs");
  11. var _isError = _interopRequireDefault(require("../lib/is-error"));
  12. var _hasNecessaryDependencies = require("../lib/has-necessary-dependencies");
  13. async function loadJsConfig(dir, config) {
  14. let typeScriptPath;
  15. try {
  16. const deps = await (0, _hasNecessaryDependencies).hasNecessaryDependencies(dir, [
  17. {
  18. pkg: "typescript",
  19. file: "typescript/lib/typescript.js",
  20. exportsRestrict: true
  21. },
  22. ]);
  23. typeScriptPath = deps.resolved.get("typescript");
  24. } catch (_) {}
  25. const tsConfigPath = _path.default.join(dir, config.typescript.tsconfigPath);
  26. const useTypeScript = Boolean(typeScriptPath && await (0, _fileExists).fileExists(tsConfigPath));
  27. let implicitBaseurl;
  28. let jsConfig;
  29. // jsconfig is a subset of tsconfig
  30. if (useTypeScript) {
  31. if (config.typescript.tsconfigPath !== "tsconfig.json" && TSCONFIG_WARNED === false) {
  32. TSCONFIG_WARNED = true;
  33. Log.info(`Using tsconfig file: ${config.typescript.tsconfigPath}`);
  34. }
  35. const ts = await Promise.resolve(require(typeScriptPath));
  36. const tsConfig = await (0, _getTypeScriptConfiguration).getTypeScriptConfiguration(ts, tsConfigPath, true);
  37. jsConfig = {
  38. compilerOptions: tsConfig.options
  39. };
  40. implicitBaseurl = _path.default.dirname(tsConfigPath);
  41. }
  42. const jsConfigPath = _path.default.join(dir, "jsconfig.json");
  43. if (!useTypeScript && await (0, _fileExists).fileExists(jsConfigPath)) {
  44. jsConfig = parseJsonFile(jsConfigPath);
  45. implicitBaseurl = _path.default.dirname(jsConfigPath);
  46. }
  47. let resolvedBaseUrl;
  48. if (jsConfig) {
  49. var ref;
  50. if ((ref = jsConfig.compilerOptions) == null ? void 0 : ref.baseUrl) {
  51. resolvedBaseUrl = _path.default.resolve(dir, jsConfig.compilerOptions.baseUrl);
  52. } else {
  53. resolvedBaseUrl = implicitBaseurl;
  54. }
  55. }
  56. return {
  57. useTypeScript,
  58. jsConfig,
  59. resolvedBaseUrl
  60. };
  61. }
  62. function _interopRequireDefault(obj) {
  63. return obj && obj.__esModule ? obj : {
  64. default: obj
  65. };
  66. }
  67. function _getRequireWildcardCache() {
  68. if (typeof WeakMap !== "function") return null;
  69. var cache = new WeakMap();
  70. _getRequireWildcardCache = function() {
  71. return cache;
  72. };
  73. return cache;
  74. }
  75. function _interopRequireWildcard(obj) {
  76. if (obj && obj.__esModule) {
  77. return obj;
  78. }
  79. if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
  80. return {
  81. default: obj
  82. };
  83. }
  84. var cache = _getRequireWildcardCache();
  85. if (cache && cache.has(obj)) {
  86. return cache.get(obj);
  87. }
  88. var newObj = {};
  89. var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
  90. for(var key in obj){
  91. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  92. var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
  93. if (desc && (desc.get || desc.set)) {
  94. Object.defineProperty(newObj, key, desc);
  95. } else {
  96. newObj[key] = obj[key];
  97. }
  98. }
  99. }
  100. newObj.default = obj;
  101. if (cache) {
  102. cache.set(obj, newObj);
  103. }
  104. return newObj;
  105. }
  106. let TSCONFIG_WARNED = false;
  107. function parseJsonFile(filePath) {
  108. const JSON5 = require("next/dist/compiled/json5");
  109. const contents = (0, _fs).readFileSync(filePath, "utf8");
  110. // Special case an empty file
  111. if (contents.trim() === "") {
  112. return {};
  113. }
  114. try {
  115. return JSON5.parse(contents);
  116. } catch (err) {
  117. if (!(0, _isError).default(err)) throw err;
  118. const { codeFrameColumns } = require("next/dist/compiled/babel/code-frame");
  119. const codeFrame = codeFrameColumns(String(contents), {
  120. start: {
  121. line: err.lineNumber || 0,
  122. column: err.columnNumber || 0
  123. }
  124. }, {
  125. message: err.message,
  126. highlightCode: true
  127. });
  128. throw new Error(`Failed to parse "${filePath}":\n${codeFrame}`);
  129. }
  130. }
  131. //# sourceMappingURL=load-jsconfig.js.map