UNPKG

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