match-path-sync.js 4.3 KB

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