UNPKG

5.43 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.matchFromAbsolutePathsAsync = exports.createMatchPathAsync = void 0;
4var path = require("path");
5var TryPath = require("./try-path");
6var MappingEntry = require("./mapping-entry");
7var Filesystem = require("./filesystem");
8/**
9 * See the sync version for docs.
10 */
11function createMatchPathAsync(absoluteBaseUrl, paths, mainFields, addMatchAll) {
12 if (mainFields === void 0) { mainFields = ["main"]; }
13 if (addMatchAll === void 0) { addMatchAll = true; }
14 var absolutePaths = MappingEntry.getAbsoluteMappingEntries(absoluteBaseUrl, paths, addMatchAll);
15 return function (requestedModule, readJson, fileExists, extensions, callback) {
16 return matchFromAbsolutePathsAsync(absolutePaths, requestedModule, readJson, fileExists, extensions, callback, mainFields);
17 };
18}
19exports.createMatchPathAsync = createMatchPathAsync;
20/**
21 * See the sync version for docs.
22 */
23function matchFromAbsolutePathsAsync(absolutePathMappings, requestedModule, readJson, fileExists, extensions, callback, mainFields) {
24 if (readJson === void 0) { readJson = Filesystem.readJsonFromDiskAsync; }
25 if (fileExists === void 0) { fileExists = Filesystem.fileExistsAsync; }
26 if (extensions === void 0) { extensions = Object.keys(require.extensions); }
27 if (mainFields === void 0) { mainFields = ["main"]; }
28 var tryPaths = TryPath.getPathsToTry(extensions, absolutePathMappings, requestedModule);
29 if (!tryPaths) {
30 return callback();
31 }
32 findFirstExistingPath(tryPaths, readJson, fileExists, callback, 0, mainFields);
33}
34exports.matchFromAbsolutePathsAsync = matchFromAbsolutePathsAsync;
35function findFirstExistingMainFieldMappedFile(packageJson, mainFields, packageJsonPath, fileExistsAsync, doneCallback, index) {
36 if (index === void 0) { index = 0; }
37 if (index >= mainFields.length) {
38 return doneCallback(undefined, undefined);
39 }
40 var tryNext = function () {
41 return findFirstExistingMainFieldMappedFile(packageJson, mainFields, packageJsonPath, fileExistsAsync, doneCallback, index + 1);
42 };
43 var mainFieldSelector = mainFields[index];
44 var mainFieldMapping = typeof mainFieldSelector === "string"
45 ? packageJson[mainFieldSelector]
46 : mainFieldSelector.reduce(function (obj, key) { return obj[key]; }, packageJson);
47 if (typeof mainFieldMapping !== "string") {
48 // Skip mappings that are not pointers to replacement files
49 return tryNext();
50 }
51 var mappedFilePath = path.join(path.dirname(packageJsonPath), mainFieldMapping);
52 fileExistsAsync(mappedFilePath, function (err, exists) {
53 if (err) {
54 return doneCallback(err);
55 }
56 if (exists) {
57 return doneCallback(undefined, mappedFilePath);
58 }
59 return tryNext();
60 });
61}
62// Recursive loop to probe for physical files
63function findFirstExistingPath(tryPaths, readJson, fileExists, doneCallback, index, mainFields) {
64 if (index === void 0) { index = 0; }
65 if (mainFields === void 0) { mainFields = ["main"]; }
66 var tryPath = tryPaths[index];
67 if (tryPath.type === "file" ||
68 tryPath.type === "extension" ||
69 tryPath.type === "index") {
70 fileExists(tryPath.path, function (err, exists) {
71 if (err) {
72 return doneCallback(err);
73 }
74 if (exists) {
75 return doneCallback(undefined, TryPath.getStrippedPath(tryPath));
76 }
77 if (index === tryPaths.length - 1) {
78 return doneCallback();
79 }
80 // Continue with the next path
81 return findFirstExistingPath(tryPaths, readJson, fileExists, doneCallback, index + 1, mainFields);
82 });
83 }
84 else if (tryPath.type === "package") {
85 readJson(tryPath.path, function (err, packageJson) {
86 if (err) {
87 return doneCallback(err);
88 }
89 if (packageJson) {
90 return findFirstExistingMainFieldMappedFile(packageJson, mainFields, tryPath.path, fileExists, function (mainFieldErr, mainFieldMappedFile) {
91 if (mainFieldErr) {
92 return doneCallback(mainFieldErr);
93 }
94 if (mainFieldMappedFile) {
95 return doneCallback(undefined, mainFieldMappedFile);
96 }
97 // No field in package json was a valid option. Continue with the next path.
98 return findFirstExistingPath(tryPaths, readJson, fileExists, doneCallback, index + 1, mainFields);
99 });
100 }
101 // This is async code, we need to return unconditionally, otherwise the code still falls
102 // through and keeps recursing. While this might work in general, libraries that use neo-async
103 // like Webpack will actually not allow you to call the same callback twice.
104 //
105 // An example of where this caused issues:
106 // https://github.com/dividab/tsconfig-paths-webpack-plugin/issues/11
107 //
108 // Continue with the next path
109 return findFirstExistingPath(tryPaths, readJson, fileExists, doneCallback, index + 1, mainFields);
110 });
111 }
112 else {
113 TryPath.exhaustiveTypeException(tryPath.type);
114 }
115}
116//# sourceMappingURL=match-path-async.js.map
\No newline at end of file