config.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getParsedCommandLine = exports.getConfigParseResult = exports.getConfigFile = void 0;
  4. const path = require("path");
  5. const compilerSetup_1 = require("./compilerSetup");
  6. const utils_1 = require("./utils");
  7. function getConfigFile(compiler, colors, loader, loaderOptions, compilerCompatible, log, compilerDetailsLogMessage) {
  8. const configFilePath = findConfigFile(compiler, path.dirname(loader.resourcePath), loaderOptions.configFile);
  9. let configFileError;
  10. let configFile;
  11. if (configFilePath !== undefined) {
  12. if (compilerCompatible) {
  13. log.logInfo(`${compilerDetailsLogMessage} and ${configFilePath}`);
  14. }
  15. else {
  16. log.logInfo(`ts-loader: Using config file at ${configFilePath}`);
  17. }
  18. configFile = compiler.readConfigFile(configFilePath, compiler.sys.readFile);
  19. if (configFile.error !== undefined) {
  20. configFileError = (0, utils_1.formatErrors)([configFile.error], loaderOptions, colors, compiler, { file: configFilePath }, loader.context)[0];
  21. }
  22. }
  23. else {
  24. if (compilerCompatible) {
  25. log.logInfo(compilerDetailsLogMessage);
  26. }
  27. configFile = {
  28. config: {
  29. compilerOptions: {},
  30. files: [],
  31. },
  32. };
  33. }
  34. if (configFileError === undefined) {
  35. configFile.config.compilerOptions = Object.assign({}, configFile.config.compilerOptions);
  36. }
  37. return {
  38. configFilePath,
  39. configFile,
  40. configFileError,
  41. };
  42. }
  43. exports.getConfigFile = getConfigFile;
  44. /**
  45. * Find a tsconfig file by name or by path.
  46. * By name, the tsconfig.json is found using the same method as `tsc`, starting in the current
  47. * directory and continuing up the parent directory chain.
  48. * By path, the file will be found by resolving the given path relative to the requesting entry file.
  49. *
  50. * @param compiler The TypeScript compiler instance
  51. * @param requestDirPath The directory in which the entry point requesting the tsconfig.json lies
  52. * @param configFile The tsconfig file name to look for or a path to that file
  53. * @return The absolute path to the tsconfig file, undefined if none was found.
  54. */
  55. function findConfigFile(compiler, requestDirPath, configFile) {
  56. // If `configFile` is an absolute path, return it right away
  57. if (path.isAbsolute(configFile)) {
  58. return compiler.sys.fileExists(configFile) ? configFile : undefined;
  59. }
  60. // If `configFile` is a relative path, resolve it.
  61. // We define a relative path as: starts with
  62. // one or two dots + a common directory delimiter
  63. if (configFile.match(/^\.\.?(\/|\\)/) !== null) {
  64. const resolvedPath = path.resolve(requestDirPath, configFile);
  65. return compiler.sys.fileExists(resolvedPath) ? resolvedPath : undefined;
  66. // If `configFile` is a file name, find it in the directory tree
  67. }
  68. else {
  69. while (true) {
  70. const fileName = path.join(requestDirPath, configFile);
  71. if (compiler.sys.fileExists(fileName)) {
  72. return fileName;
  73. }
  74. const parentPath = path.dirname(requestDirPath);
  75. if (parentPath === requestDirPath) {
  76. break;
  77. }
  78. requestDirPath = parentPath;
  79. }
  80. return undefined;
  81. }
  82. }
  83. function getConfigParseResult(compiler, configFile, basePath, configFilePath, loaderOptions) {
  84. const configParseResult = compiler.parseJsonConfigFileContent(configFile.config, {
  85. ...compiler.sys,
  86. useCaseSensitiveFileNames: (0, utils_1.useCaseSensitiveFileNames)(compiler, loaderOptions),
  87. }, basePath, getCompilerOptionsToExtend(compiler, loaderOptions, basePath, configFilePath || 'tsconfig.json'));
  88. if (!loaderOptions.projectReferences) {
  89. configParseResult.projectReferences = undefined;
  90. }
  91. // set internal options.configFilePath flag on options to denote that we read this from a file
  92. configParseResult.options = Object.assign({}, configParseResult.options, {
  93. configFilePath,
  94. });
  95. return configParseResult;
  96. }
  97. exports.getConfigParseResult = getConfigParseResult;
  98. const extendedConfigCache = new Map();
  99. function getParsedCommandLine(compiler, loaderOptions, configFilePath) {
  100. const result = compiler.getParsedCommandLineOfConfigFile(configFilePath, getCompilerOptionsToExtend(compiler, loaderOptions, path.dirname(configFilePath), configFilePath), {
  101. ...compiler.sys,
  102. useCaseSensitiveFileNames: (0, utils_1.useCaseSensitiveFileNames)(compiler, loaderOptions),
  103. // eslint-disable-next-line @typescript-eslint/no-empty-function
  104. onUnRecoverableConfigFileDiagnostic: () => { },
  105. }, extendedConfigCache);
  106. if (result) {
  107. result.options = (0, compilerSetup_1.getCompilerOptions)(result, compiler);
  108. }
  109. return result;
  110. }
  111. exports.getParsedCommandLine = getParsedCommandLine;
  112. function getCompilerOptionsToExtend(compiler, loaderOptions, basePath, configFileName) {
  113. return compiler.convertCompilerOptionsFromJson(loaderOptions.compilerOptions, basePath, configFileName).options;
  114. }
  115. //# sourceMappingURL=config.js.map