match-path-async.js 5.2 KB

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