match-path-async.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.matchFromAbsolutePathsAsync = exports.createMatchPathAsync = void 0;
  4. var path = require("path");
  5. var TryPath = require("./try-path");
  6. var MappingEntry = require("./mapping-entry");
  7. var Filesystem = require("./filesystem");
  8. /**
  9. * See the sync version for docs.
  10. */
  11. function createMatchPathAsync(absoluteBaseUrl, paths, mainFields, addMatchAll) {
  12. if (mainFields === void 0) { mainFields = ["main"]; }
  13. if (addMatchAll === void 0) { addMatchAll = true; }
  14. var absolutePaths = MappingEntry.getAbsoluteMappingEntries(absoluteBaseUrl, paths, addMatchAll);
  15. return function (requestedModule, readJson, fileExists, extensions, callback) {
  16. return matchFromAbsolutePathsAsync(absolutePaths, requestedModule, readJson, fileExists, extensions, callback, mainFields);
  17. };
  18. }
  19. exports.createMatchPathAsync = createMatchPathAsync;
  20. /**
  21. * See the sync version for docs.
  22. */
  23. function matchFromAbsolutePathsAsync(absolutePathMappings, requestedModule, readJson, fileExists, extensions, callback, mainFields) {
  24. if (readJson === void 0) { readJson = Filesystem.readJsonFromDiskAsync; }
  25. if (fileExists === void 0) { fileExists = Filesystem.fileExistsAsync; }
  26. if (extensions === void 0) { extensions = Object.keys(require.extensions); }
  27. if (mainFields === void 0) { mainFields = ["main"]; }
  28. var tryPaths = TryPath.getPathsToTry(extensions, absolutePathMappings, requestedModule);
  29. if (!tryPaths) {
  30. return callback();
  31. }
  32. findFirstExistingPath(tryPaths, readJson, fileExists, callback, 0, mainFields);
  33. }
  34. exports.matchFromAbsolutePathsAsync = matchFromAbsolutePathsAsync;
  35. function findFirstExistingMainFieldMappedFile(packageJson, mainFields, packageJsonPath, fileExistsAsync, doneCallback, index) {
  36. if (index === void 0) { index = 0; }
  37. if (index >= mainFields.length) {
  38. return doneCallback(undefined, undefined);
  39. }
  40. var tryNext = function () {
  41. return findFirstExistingMainFieldMappedFile(packageJson, mainFields, packageJsonPath, fileExistsAsync, doneCallback, index + 1);
  42. };
  43. var mainFieldMapping = packageJson[mainFields[index]];
  44. if (typeof mainFieldMapping !== "string") {
  45. // Skip mappings that are not pointers to replacement files
  46. return tryNext();
  47. }
  48. var mappedFilePath = path.join(path.dirname(packageJsonPath), mainFieldMapping);
  49. fileExistsAsync(mappedFilePath, function (err, exists) {
  50. if (err) {
  51. return doneCallback(err);
  52. }
  53. if (exists) {
  54. return doneCallback(undefined, mappedFilePath);
  55. }
  56. return tryNext();
  57. });
  58. }
  59. // Recursive loop to probe for physical files
  60. function findFirstExistingPath(tryPaths, readJson, fileExists, doneCallback, index, mainFields) {
  61. if (index === void 0) { index = 0; }
  62. if (mainFields === void 0) { mainFields = ["main"]; }
  63. var tryPath = tryPaths[index];
  64. if (tryPath.type === "file" ||
  65. tryPath.type === "extension" ||
  66. tryPath.type === "index") {
  67. fileExists(tryPath.path, function (err, exists) {
  68. if (err) {
  69. return doneCallback(err);
  70. }
  71. if (exists) {
  72. return doneCallback(undefined, TryPath.getStrippedPath(tryPath));
  73. }
  74. if (index === tryPaths.length - 1) {
  75. return doneCallback();
  76. }
  77. // Continue with the next path
  78. return findFirstExistingPath(tryPaths, readJson, fileExists, doneCallback, index + 1, mainFields);
  79. });
  80. }
  81. else if (tryPath.type === "package") {
  82. readJson(tryPath.path, function (err, packageJson) {
  83. if (err) {
  84. return doneCallback(err);
  85. }
  86. if (packageJson) {
  87. return findFirstExistingMainFieldMappedFile(packageJson, mainFields, tryPath.path, fileExists, function (mainFieldErr, mainFieldMappedFile) {
  88. if (mainFieldErr) {
  89. return doneCallback(mainFieldErr);
  90. }
  91. if (mainFieldMappedFile) {
  92. return doneCallback(undefined, mainFieldMappedFile);
  93. }
  94. // No field in package json was a valid option. Continue with the next path.
  95. return findFirstExistingPath(tryPaths, readJson, fileExists, doneCallback, index + 1, mainFields);
  96. });
  97. }
  98. // This is async code, we need to return unconditionally, otherwise the code still falls
  99. // through and keeps recursing. While this might work in general, libraries that use neo-async
  100. // like Webpack will actually not allow you to call the same callback twice.
  101. //
  102. // An example of where this caused issues:
  103. // https://github.com/dividab/tsconfig-paths-webpack-plugin/issues/11
  104. //
  105. // Continue with the next path
  106. return findFirstExistingPath(tryPaths, readJson, fileExists, doneCallback, index + 1, mainFields);
  107. });
  108. }
  109. else {
  110. TryPath.exhaustiveTypeException(tryPath.type);
  111. }
  112. }
  113. //# sourceMappingURL=match-path-async.js.map