You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

config.js 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const path = require("path");
  4. const utils_1 = require("./utils");
  5. function getConfigFile(compiler, colors, loader, loaderOptions, compilerCompatible, log, compilerDetailsLogMessage) {
  6. const configFilePath = findConfigFile(compiler, path.dirname(loader.resourcePath), loaderOptions.configFile);
  7. let configFileError;
  8. let configFile;
  9. if (configFilePath !== undefined) {
  10. if (compilerCompatible) {
  11. log.logInfo(`${compilerDetailsLogMessage} and ${configFilePath}`);
  12. }
  13. else {
  14. log.logInfo(`ts-loader: Using config file at ${configFilePath}`);
  15. }
  16. configFile = compiler.readConfigFile(configFilePath, compiler.sys.readFile);
  17. if (configFile.error !== undefined) {
  18. configFileError = utils_1.formatErrors([configFile.error], loaderOptions, colors, compiler, { file: configFilePath }, loader.context)[0];
  19. }
  20. }
  21. else {
  22. if (compilerCompatible) {
  23. log.logInfo(compilerDetailsLogMessage);
  24. }
  25. configFile = {
  26. config: {
  27. compilerOptions: {},
  28. files: []
  29. }
  30. };
  31. }
  32. if (configFileError === undefined) {
  33. configFile.config.compilerOptions = Object.assign({}, configFile.config.compilerOptions, loaderOptions.compilerOptions);
  34. }
  35. return {
  36. configFilePath,
  37. configFile,
  38. configFileError
  39. };
  40. }
  41. exports.getConfigFile = getConfigFile;
  42. /**
  43. * Find a tsconfig file by name or by path.
  44. * By name, the tsconfig.json is found using the same method as `tsc`, starting in the current
  45. * directory and continuing up the parent directory chain.
  46. * By path, the file will be found by resolving the given path relative to the requesting entry file.
  47. *
  48. * @param compiler The TypeScript compiler instance
  49. * @param requestDirPath The directory in which the entry point requesting the tsconfig.json lies
  50. * @param configFile The tsconfig file name to look for or a path to that file
  51. * @return The absolute path to the tsconfig file, undefined if none was found.
  52. */
  53. function findConfigFile(compiler, requestDirPath, configFile) {
  54. // If `configFile` is an absolute path, return it right away
  55. if (path.isAbsolute(configFile)) {
  56. return compiler.sys.fileExists(configFile) ? configFile : undefined;
  57. }
  58. // If `configFile` is a relative path, resolve it.
  59. // We define a relative path as: starts with
  60. // one or two dots + a common directory delimiter
  61. if (configFile.match(/^\.\.?(\/|\\)/) !== null) {
  62. const resolvedPath = path.resolve(requestDirPath, configFile);
  63. return compiler.sys.fileExists(resolvedPath) ? resolvedPath : undefined;
  64. // If `configFile` is a file name, find it in the directory tree
  65. }
  66. else {
  67. while (true) {
  68. const fileName = path.join(requestDirPath, configFile);
  69. if (compiler.sys.fileExists(fileName)) {
  70. return fileName;
  71. }
  72. const parentPath = path.dirname(requestDirPath);
  73. if (parentPath === requestDirPath) {
  74. break;
  75. }
  76. requestDirPath = parentPath;
  77. }
  78. return undefined;
  79. }
  80. }
  81. function getConfigParseResult(compiler, configFile, basePath) {
  82. const configParseResult = compiler.parseJsonConfigFileContent(configFile.config, compiler.sys, basePath);
  83. return configParseResult;
  84. }
  85. exports.getConfigParseResult = getConfigParseResult;