123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- var path = require("path");
- var Filesystem = require("./filesystem");
- var MappingEntry = require("./mapping-entry");
- var TryPath = require("./try-path");
- function createMatchPath(absoluteBaseUrl, paths, mainFields, addMatchAll) {
- if (mainFields === void 0) { mainFields = ["main"]; }
- if (addMatchAll === void 0) { addMatchAll = true; }
- var absolutePaths = MappingEntry.getAbsoluteMappingEntries(absoluteBaseUrl, paths, addMatchAll);
- return function (requestedModule, readJson, fileExists, extensions) {
- return matchFromAbsolutePaths(absolutePaths, requestedModule, readJson, fileExists, extensions, mainFields);
- };
- }
- exports.createMatchPath = createMatchPath;
- function matchFromAbsolutePaths(absolutePathMappings, requestedModule, readJson, fileExists, extensions, mainFields) {
- if (readJson === void 0) { readJson = Filesystem.readJsonFromDiskSync; }
- if (fileExists === void 0) { fileExists = Filesystem.fileExistsSync; }
- if (extensions === void 0) { extensions = Object.keys(require.extensions); }
- if (mainFields === void 0) { mainFields = ["main"]; }
- var tryPaths = TryPath.getPathsToTry(extensions, absolutePathMappings, requestedModule);
- if (!tryPaths) {
- return undefined;
- }
- return findFirstExistingPath(tryPaths, readJson, fileExists, mainFields);
- }
- exports.matchFromAbsolutePaths = matchFromAbsolutePaths;
- function findFirstExistingMainFieldMappedFile(packageJson, mainFields, packageJsonPath, fileExists) {
- for (var index = 0; index < mainFields.length; index++) {
- var mainFieldName = mainFields[index];
- var candidateMapping = packageJson[mainFieldName];
- if (candidateMapping && typeof candidateMapping === "string") {
- var candidateFilePath = path.join(path.dirname(packageJsonPath), candidateMapping);
- if (fileExists(candidateFilePath)) {
- return candidateFilePath;
- }
- }
- }
- return undefined;
- }
- function findFirstExistingPath(tryPaths, readJson, fileExists, mainFields) {
- if (readJson === void 0) { readJson = Filesystem.readJsonFromDiskSync; }
- if (mainFields === void 0) { mainFields = ["main"]; }
- for (var _i = 0, tryPaths_1 = tryPaths; _i < tryPaths_1.length; _i++) {
- var tryPath = tryPaths_1[_i];
- if (tryPath.type === "file" ||
- tryPath.type === "extension" ||
- tryPath.type === "index") {
- if (fileExists(tryPath.path)) {
-
- return TryPath.getStrippedPath(tryPath);
- }
- }
- else if (tryPath.type === "package") {
- var packageJson = readJson(tryPath.path);
- if (packageJson) {
- var mainFieldMappedFile = findFirstExistingMainFieldMappedFile(packageJson, mainFields, tryPath.path, fileExists);
- if (mainFieldMappedFile) {
-
- return Filesystem.removeExtension(mainFieldMappedFile);
- }
- }
- }
- else {
- TryPath.exhaustiveTypeException(tryPath.type);
- }
- }
- return undefined;
- }
|