1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.matchFromAbsolutePaths = exports.createMatchPath = void 0;
|
4 | var path = require("path");
|
5 | var Filesystem = require("./filesystem");
|
6 | var MappingEntry = require("./mapping-entry");
|
7 | var TryPath = require("./try-path");
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | function createMatchPath(absoluteBaseUrl, paths, mainFields, addMatchAll) {
|
18 | if (mainFields === void 0) { mainFields = ["main"]; }
|
19 | if (addMatchAll === void 0) { addMatchAll = true; }
|
20 | var absolutePaths = MappingEntry.getAbsoluteMappingEntries(absoluteBaseUrl, paths, addMatchAll);
|
21 | return function (requestedModule, readJson, fileExists, extensions) {
|
22 | return matchFromAbsolutePaths(absolutePaths, requestedModule, readJson, fileExists, extensions, mainFields);
|
23 | };
|
24 | }
|
25 | exports.createMatchPath = createMatchPath;
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | function matchFromAbsolutePaths(absolutePathMappings, requestedModule, readJson, fileExists, extensions, mainFields) {
|
38 | if (readJson === void 0) { readJson = Filesystem.readJsonFromDiskSync; }
|
39 | if (fileExists === void 0) { fileExists = Filesystem.fileExistsSync; }
|
40 | if (extensions === void 0) { extensions = Object.keys(require.extensions); }
|
41 | if (mainFields === void 0) { mainFields = ["main"]; }
|
42 | var tryPaths = TryPath.getPathsToTry(extensions, absolutePathMappings, requestedModule);
|
43 | if (!tryPaths) {
|
44 | return undefined;
|
45 | }
|
46 | return findFirstExistingPath(tryPaths, readJson, fileExists, mainFields);
|
47 | }
|
48 | exports.matchFromAbsolutePaths = matchFromAbsolutePaths;
|
49 | function findFirstExistingMainFieldMappedFile(packageJson, mainFields, packageJsonPath, fileExists) {
|
50 | for (var index = 0; index < mainFields.length; index++) {
|
51 | var mainFieldSelector = mainFields[index];
|
52 | var candidateMapping = typeof mainFieldSelector === "string"
|
53 | ? packageJson[mainFieldSelector]
|
54 | : mainFieldSelector.reduce(function (obj, key) { return obj[key]; }, packageJson);
|
55 | if (candidateMapping && typeof candidateMapping === "string") {
|
56 | var candidateFilePath = path.join(path.dirname(packageJsonPath), candidateMapping);
|
57 | if (fileExists(candidateFilePath)) {
|
58 | return candidateFilePath;
|
59 | }
|
60 | }
|
61 | }
|
62 | return undefined;
|
63 | }
|
64 | function findFirstExistingPath(tryPaths, readJson, fileExists, mainFields) {
|
65 | if (readJson === void 0) { readJson = Filesystem.readJsonFromDiskSync; }
|
66 | if (mainFields === void 0) { mainFields = ["main"]; }
|
67 | for (var _i = 0, tryPaths_1 = tryPaths; _i < tryPaths_1.length; _i++) {
|
68 | var tryPath = tryPaths_1[_i];
|
69 | if (tryPath.type === "file" ||
|
70 | tryPath.type === "extension" ||
|
71 | tryPath.type === "index") {
|
72 | if (fileExists(tryPath.path)) {
|
73 | return TryPath.getStrippedPath(tryPath);
|
74 | }
|
75 | }
|
76 | else if (tryPath.type === "package") {
|
77 | var packageJson = readJson(tryPath.path);
|
78 | if (packageJson) {
|
79 | var mainFieldMappedFile = findFirstExistingMainFieldMappedFile(packageJson, mainFields, tryPath.path, fileExists);
|
80 | if (mainFieldMappedFile) {
|
81 | return mainFieldMappedFile;
|
82 | }
|
83 | }
|
84 | }
|
85 | else {
|
86 | TryPath.exhaustiveTypeException(tryPath.type);
|
87 | }
|
88 | }
|
89 | return undefined;
|
90 | }
|
91 |
|
\ | No newline at end of file |