| 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 | * Creates a function that can resolve paths according to tsconfig paths property.
|
|---|
| 10 | * @param absoluteBaseUrl Absolute version of baseUrl as specified in tsconfig.
|
|---|
| 11 | * @param paths The paths as specified in tsconfig.
|
|---|
| 12 | * @param mainFields A list of package.json field names to try when resolving module files.
|
|---|
| 13 | * @param addMatchAll Add a match-all "*" rule if none is present
|
|---|
| 14 | * @returns a function that can resolve paths.
|
|---|
| 15 | */
|
|---|
| 16 | function createMatchPath(absoluteBaseUrl, paths, mainFields, addMatchAll) {
|
|---|
| 17 | if (mainFields === void 0) { mainFields = ["main"]; }
|
|---|
| 18 | if (addMatchAll === void 0) { addMatchAll = true; }
|
|---|
| 19 | var absolutePaths = MappingEntry.getAbsoluteMappingEntries(absoluteBaseUrl, paths, addMatchAll);
|
|---|
| 20 | return function (requestedModule, readJson, fileExists, extensions) {
|
|---|
| 21 | return matchFromAbsolutePaths(absolutePaths, requestedModule, readJson, fileExists, extensions, mainFields);
|
|---|
| 22 | };
|
|---|
| 23 | }
|
|---|
| 24 | exports.createMatchPath = createMatchPath;
|
|---|
| 25 | /**
|
|---|
| 26 | * Finds a path from tsconfig that matches a module load request.
|
|---|
| 27 | * @param absolutePathMappings The paths to try as specified in tsconfig but resolved to absolute form.
|
|---|
| 28 | * @param requestedModule The required module name.
|
|---|
| 29 | * @param readJson Function that can read json from a path (useful for testing).
|
|---|
| 30 | * @param fileExists Function that checks for existence of a file at a path (useful for testing).
|
|---|
| 31 | * @param extensions File extensions to probe for (useful for testing).
|
|---|
| 32 | * @param mainFields A list of package.json field names to try when resolving module files.
|
|---|
| 33 | * @returns the found path, or undefined if no path was found.
|
|---|
| 34 | */
|
|---|
| 35 | function matchFromAbsolutePaths(absolutePathMappings, requestedModule, readJson, fileExists, extensions, mainFields) {
|
|---|
| 36 | if (readJson === void 0) { readJson = Filesystem.readJsonFromDiskSync; }
|
|---|
| 37 | if (fileExists === void 0) { fileExists = Filesystem.fileExistsSync; }
|
|---|
| 38 | if (extensions === void 0) { extensions = Object.keys(require.extensions); }
|
|---|
| 39 | if (mainFields === void 0) { mainFields = ["main"]; }
|
|---|
| 40 | var tryPaths = TryPath.getPathsToTry(extensions, absolutePathMappings, requestedModule);
|
|---|
| 41 | if (!tryPaths) {
|
|---|
| 42 | return undefined;
|
|---|
| 43 | }
|
|---|
| 44 | return findFirstExistingPath(tryPaths, readJson, fileExists, mainFields);
|
|---|
| 45 | }
|
|---|
| 46 | exports.matchFromAbsolutePaths = matchFromAbsolutePaths;
|
|---|
| 47 | function findFirstExistingMainFieldMappedFile(packageJson, mainFields, packageJsonPath, fileExists) {
|
|---|
| 48 | for (var index = 0; index < mainFields.length; index++) {
|
|---|
| 49 | var mainFieldName = mainFields[index];
|
|---|
| 50 | var candidateMapping = packageJson[mainFieldName];
|
|---|
| 51 | if (candidateMapping && typeof candidateMapping === "string") {
|
|---|
| 52 | var candidateFilePath = path.join(path.dirname(packageJsonPath), candidateMapping);
|
|---|
| 53 | if (fileExists(candidateFilePath)) {
|
|---|
| 54 | return candidateFilePath;
|
|---|
| 55 | }
|
|---|
| 56 | }
|
|---|
| 57 | }
|
|---|
| 58 | return undefined;
|
|---|
| 59 | }
|
|---|
| 60 | function findFirstExistingPath(tryPaths, readJson, fileExists, mainFields) {
|
|---|
| 61 | if (readJson === void 0) { readJson = Filesystem.readJsonFromDiskSync; }
|
|---|
| 62 | if (mainFields === void 0) { mainFields = ["main"]; }
|
|---|
| 63 | for (var _i = 0, tryPaths_1 = tryPaths; _i < tryPaths_1.length; _i++) {
|
|---|
| 64 | var tryPath = tryPaths_1[_i];
|
|---|
| 65 | if (tryPath.type === "file" ||
|
|---|
| 66 | tryPath.type === "extension" ||
|
|---|
| 67 | tryPath.type === "index") {
|
|---|
| 68 | if (fileExists(tryPath.path)) {
|
|---|
| 69 | return TryPath.getStrippedPath(tryPath);
|
|---|
| 70 | }
|
|---|
| 71 | }
|
|---|
| 72 | else if (tryPath.type === "package") {
|
|---|
| 73 | var packageJson = readJson(tryPath.path);
|
|---|
| 74 | if (packageJson) {
|
|---|
| 75 | var mainFieldMappedFile = findFirstExistingMainFieldMappedFile(packageJson, mainFields, tryPath.path, fileExists);
|
|---|
| 76 | if (mainFieldMappedFile) {
|
|---|
| 77 | return mainFieldMappedFile;
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|
| 81 | else {
|
|---|
| 82 | TryPath.exhaustiveTypeException(tryPath.type);
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 | return undefined;
|
|---|
| 86 | }
|
|---|
| 87 | //# sourceMappingURL=match-path-sync.js.map |
|---|