match-path-async.ts
5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import * as path from "path";
import * as TryPath from "./try-path";
import * as MappingEntry from "./mapping-entry";
import * as Filesystem from "./filesystem";
/**
* Function that can match a path async
*/
export interface MatchPathAsync {
(
requestedModule: string,
readJson: Filesystem.ReadJsonAsync | undefined,
fileExists: Filesystem.FileExistsAsync | undefined,
extensions: ReadonlyArray<string> | undefined,
callback: MatchPathAsyncCallback
): void;
}
export interface MatchPathAsyncCallback {
(err?: Error, path?: string): void;
}
/**
* See the sync version for docs.
*/
export function createMatchPathAsync(
absoluteBaseUrl: string,
paths: { [key: string]: Array<string> },
mainFields: string[] = ["main"],
addMatchAll: boolean = true
): MatchPathAsync {
const absolutePaths = MappingEntry.getAbsoluteMappingEntries(
absoluteBaseUrl,
paths,
addMatchAll
);
return (
requestedModule: string,
readJson: Filesystem.ReadJsonAsync | undefined,
fileExists: Filesystem.FileExistsAsync | undefined,
extensions: ReadonlyArray<string> | undefined,
callback: MatchPathAsyncCallback
) =>
matchFromAbsolutePathsAsync(
absolutePaths,
requestedModule,
readJson,
fileExists,
extensions,
callback,
mainFields
);
}
/**
* See the sync version for docs.
*/
export function matchFromAbsolutePathsAsync(
absolutePathMappings: ReadonlyArray<MappingEntry.MappingEntry>,
requestedModule: string,
readJson: Filesystem.ReadJsonAsync = Filesystem.readJsonFromDiskAsync,
fileExists: Filesystem.FileExistsAsync = Filesystem.fileExistsAsync,
extensions: ReadonlyArray<string> = Object.keys(require.extensions),
callback: MatchPathAsyncCallback,
mainFields: string[] = ["main"]
): void {
const tryPaths = TryPath.getPathsToTry(
extensions,
absolutePathMappings,
requestedModule
);
if (!tryPaths) {
return callback();
}
findFirstExistingPath(
tryPaths,
readJson,
fileExists,
callback,
0,
mainFields
);
}
function findFirstExistingMainFieldMappedFile(
packageJson: Filesystem.PackageJson,
mainFields: string[],
packageJsonPath: string,
fileExistsAsync: Filesystem.FileExistsAsync,
doneCallback: (err?: Error, filepath?: string) => void,
index: number = 0
): void {
if (index >= mainFields.length) {
return doneCallback(undefined, undefined);
}
const tryNext = () =>
findFirstExistingMainFieldMappedFile(
packageJson,
mainFields,
packageJsonPath,
fileExistsAsync,
doneCallback,
index + 1
);
const mainFieldMapping = packageJson[mainFields[index]];
if (typeof mainFieldMapping !== "string") {
// Skip mappings that are not pointers to replacement files
return tryNext();
}
const mappedFilePath = path.join(
path.dirname(packageJsonPath),
mainFieldMapping
);
fileExistsAsync(mappedFilePath, (err?: Error, exists?: boolean) => {
if (err) {
return doneCallback(err);
}
if (exists) {
return doneCallback(undefined, mappedFilePath);
}
return tryNext();
});
}
// Recursive loop to probe for physical files
function findFirstExistingPath(
tryPaths: ReadonlyArray<TryPath.TryPath>,
readJson: Filesystem.ReadJsonAsync,
fileExists: Filesystem.FileExistsAsync,
doneCallback: MatchPathAsyncCallback,
index: number = 0,
mainFields: string[] = ["main"]
): void {
const tryPath = tryPaths[index];
if (
tryPath.type === "file" ||
tryPath.type === "extension" ||
tryPath.type === "index"
) {
fileExists(tryPath.path, (err: Error, exists: boolean) => {
if (err) {
return doneCallback(err);
}
if (exists) {
return doneCallback(undefined, TryPath.getStrippedPath(tryPath));
}
if (index === tryPaths.length - 1) {
return doneCallback();
}
// Continue with the next path
return findFirstExistingPath(
tryPaths,
readJson,
fileExists,
doneCallback,
index + 1,
mainFields
);
});
} else if (tryPath.type === "package") {
readJson(tryPath.path, (err, packageJson) => {
if (err) {
return doneCallback(err);
}
if (packageJson) {
return findFirstExistingMainFieldMappedFile(
packageJson,
mainFields,
tryPath.path,
fileExists,
(mainFieldErr?: Error, mainFieldMappedFile?: string) => {
if (mainFieldErr) {
return doneCallback(mainFieldErr);
}
if (mainFieldMappedFile) {
return doneCallback(undefined, mainFieldMappedFile);
}
// No field in package json was a valid option. Continue with the next path.
return findFirstExistingPath(
tryPaths,
readJson,
fileExists,
doneCallback,
index + 1,
mainFields
);
}
);
}
// This is async code, we need to return unconditionally, otherwise the code still falls
// through and keeps recursing. While this might work in general, libraries that use neo-async
// like Webpack will actually not allow you to call the same callback twice.
//
// An example of where this caused issues:
// https://github.com/dividab/tsconfig-paths-webpack-plugin/issues/11
//
// Continue with the next path
return findFirstExistingPath(
tryPaths,
readJson,
fileExists,
doneCallback,
index + 1,
mainFields
);
});
} else {
TryPath.exhaustiveTypeException(tryPath.type);
}
}