mapping-entry.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getAbsoluteMappingEntries = void 0;
  4. var path = require("path");
  5. /**
  6. * Converts an absolute baseUrl and paths to an array of absolute mapping entries.
  7. * The array is sorted by longest prefix.
  8. * Having an array with entries allows us to keep a sorting order rather than
  9. * sort by keys each time we use the mappings.
  10. * @param absoluteBaseUrl
  11. * @param paths
  12. * @param addMatchAll
  13. */
  14. function getAbsoluteMappingEntries(absoluteBaseUrl, paths, addMatchAll) {
  15. // Resolve all paths to absolute form once here, and sort them by
  16. // longest prefix once here, this saves time on each request later.
  17. // We need to put them in an array to preserve the sorting order.
  18. var sortedKeys = sortByLongestPrefix(Object.keys(paths));
  19. var absolutePaths = [];
  20. for (var _i = 0, sortedKeys_1 = sortedKeys; _i < sortedKeys_1.length; _i++) {
  21. var key = sortedKeys_1[_i];
  22. absolutePaths.push({
  23. pattern: key,
  24. paths: paths[key].map(function (pathToResolve) {
  25. return path.join(absoluteBaseUrl, pathToResolve);
  26. }),
  27. });
  28. }
  29. // If there is no match-all path specified in the paths section of tsconfig, then try to match
  30. // all paths relative to baseUrl, this is how typescript works.
  31. if (!paths["*"] && addMatchAll) {
  32. absolutePaths.push({
  33. pattern: "*",
  34. paths: ["".concat(absoluteBaseUrl.replace(/\/$/, ""), "/*")],
  35. });
  36. }
  37. return absolutePaths;
  38. }
  39. exports.getAbsoluteMappingEntries = getAbsoluteMappingEntries;
  40. /**
  41. * Sort path patterns.
  42. * If a module name can be matched with multiple patterns then pattern with the longest prefix will be picked.
  43. */
  44. function sortByLongestPrefix(arr) {
  45. return arr
  46. .concat()
  47. .sort(function (a, b) { return getPrefixLength(b) - getPrefixLength(a); });
  48. }
  49. function getPrefixLength(pattern) {
  50. var prefixLength = pattern.indexOf("*");
  51. return pattern.substr(0, prefixLength).length;
  52. }
  53. //# sourceMappingURL=mapping-entry.js.map