match-path-sync.js 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.matchFromAbsolutePaths = exports.createMatchPath = void 0;
  4. var path = require("path");
  5. var Filesystem = require("./filesystem");
  6. var MappingEntry = require("./mapping-entry");
  7. var TryPath = require("./try-path");
  8. /**
  9. * Creates a function that can resolve paths according to tsconfig paths property.
  10. *
  11. * @param absoluteBaseUrl Absolute version of baseUrl as specified in tsconfig.
  12. * @param paths The paths as specified in tsconfig.
  13. * @param mainFields A list of package.json field names to try when resolving module files. Select a nested field using an array of field names.
  14. * @param addMatchAll Add a match-all "*" rule if none is present
  15. * @returns a function that can resolve paths.
  16. */
  17. function createMatchPath(absoluteBaseUrl, paths, mainFields, addMatchAll) {
  18. if (mainFields === void 0) { mainFields = ["main"]; }
  19. if (addMatchAll === void 0) { addMatchAll = true; }
  20. var absolutePaths = MappingEntry.getAbsoluteMappingEntries(absoluteBaseUrl, paths, addMatchAll);
  21. return function (requestedModule, readJson, fileExists, extensions) {
  22. return matchFromAbsolutePaths(absolutePaths, requestedModule, readJson, fileExists, extensions, mainFields);
  23. };
  24. }
  25. exports.createMatchPath = createMatchPath;
  26. /**
  27. * Finds a path from tsconfig that matches a module load request.
  28. *
  29. * @param absolutePathMappings The paths to try as specified in tsconfig but resolved to absolute form.
  30. * @param requestedModule The required module name.
  31. * @param readJson Function that can read json from a path (useful for testing).
  32. * @param fileExists Function that checks for existence of a file at a path (useful for testing).
  33. * @param extensions File extensions to probe for (useful for testing).
  34. * @param mainFields A list of package.json field names to try when resolving module files. Select a nested field using an array of field names.
  35. * @returns the found path, or undefined if no path was found.
  36. */
  37. function matchFromAbsolutePaths(absolutePathMappings, requestedModule, readJson, fileExists, extensions, mainFields) {
  38. if (readJson === void 0) { readJson = Filesystem.readJsonFromDiskSync; }
  39. if (fileExists === void 0) { fileExists = Filesystem.fileExistsSync; }
  40. if (extensions === void 0) { extensions = Object.keys(require.extensions); }
  41. if (mainFields === void 0) { mainFields = ["main"]; }
  42. var tryPaths = TryPath.getPathsToTry(extensions, absolutePathMappings, requestedModule);
  43. if (!tryPaths) {
  44. return undefined;
  45. }
  46. return findFirstExistingPath(tryPaths, readJson, fileExists, mainFields);
  47. }
  48. exports.matchFromAbsolutePaths = matchFromAbsolutePaths;
  49. function findFirstExistingMainFieldMappedFile(packageJson, mainFields, packageJsonPath, fileExists) {
  50. for (var index = 0; index < mainFields.length; index++) {
  51. var mainFieldSelector = mainFields[index];
  52. var candidateMapping = typeof mainFieldSelector === "string"
  53. ? packageJson[mainFieldSelector]
  54. : mainFieldSelector.reduce(function (obj, key) { return obj[key]; }, packageJson);
  55. if (candidateMapping && typeof candidateMapping === "string") {
  56. var candidateFilePath = path.join(path.dirname(packageJsonPath), candidateMapping);
  57. if (fileExists(candidateFilePath)) {
  58. return candidateFilePath;
  59. }
  60. }
  61. }
  62. return undefined;
  63. }
  64. function findFirstExistingPath(tryPaths, readJson, fileExists, mainFields) {
  65. if (readJson === void 0) { readJson = Filesystem.readJsonFromDiskSync; }
  66. if (mainFields === void 0) { mainFields = ["main"]; }
  67. for (var _i = 0, tryPaths_1 = tryPaths; _i < tryPaths_1.length; _i++) {
  68. var tryPath = tryPaths_1[_i];
  69. if (tryPath.type === "file" ||
  70. tryPath.type === "extension" ||
  71. tryPath.type === "index") {
  72. if (fileExists(tryPath.path)) {
  73. return TryPath.getStrippedPath(tryPath);
  74. }
  75. }
  76. else if (tryPath.type === "package") {
  77. var packageJson = readJson(tryPath.path);
  78. if (packageJson) {
  79. var mainFieldMappedFile = findFirstExistingMainFieldMappedFile(packageJson, mainFields, tryPath.path, fileExists);
  80. if (mainFieldMappedFile) {
  81. return mainFieldMappedFile;
  82. }
  83. }
  84. }
  85. else {
  86. TryPath.exhaustiveTypeException(tryPath.type);
  87. }
  88. }
  89. return undefined;
  90. }
  91. //# sourceMappingURL=match-path-sync.js.map