-
Notifications
You must be signed in to change notification settings - Fork 104
/
match-path-data.ts
241 lines (238 loc) · 8.15 KB
/
match-path-data.ts
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import { join, dirname } from "path";
import { removeExtension } from "../../filesystem";
export interface OneTest {
readonly name: string;
readonly only?: boolean;
readonly skip?: boolean;
readonly absoluteBaseUrl: string;
readonly paths: { [key: string]: Array<string> };
readonly mainFields?: (string | string[])[];
readonly addMatchAll?: boolean;
readonly existingFiles: ReadonlyArray<string>;
readonly requestedModule: string;
readonly extensions: ReadonlyArray<string>;
readonly packageJson?: {};
readonly expectedPath: string | undefined;
}
const defaultExtensionsWhenRunningInTsNode = [
".js",
".json",
".node",
".ts",
".tsx",
];
export const tests: ReadonlyArray<OneTest> = [
{
name: "should locate path that matches with star and exists",
absoluteBaseUrl: "/root/",
paths: {
"lib/*": ["location/*"],
},
existingFiles: [join("/root", "location", "mylib", "index.ts")],
requestedModule: "lib/mylib",
expectedPath: dirname(join("/root", "location", "mylib", "index.ts")),
extensions: defaultExtensionsWhenRunningInTsNode,
},
{
name: "should resolve to correct path when many are specified",
absoluteBaseUrl: "/root/",
paths: {
"lib/*": ["foo1/*", "foo2/*", "location/*", "foo3/*"],
},
existingFiles: [join("/root", "location", "mylib", "index.ts")],
requestedModule: "lib/mylib",
extensions: [".ts"],
expectedPath: dirname(join("/root", "location", "mylib", "index.ts")),
},
{
name:
"should locate path that matches with star and prioritize pattern with longest prefix",
absoluteBaseUrl: "/root/",
paths: {
"*": ["location/*"],
"lib/*": ["location/*"],
},
existingFiles: [
join("/root", "location", "lib", "mylib", "index.ts"),
join("/root", "location", "mylib", "index.ts"),
],
requestedModule: "lib/mylib",
expectedPath: dirname(join("/root", "location", "mylib", "index.ts")),
extensions: defaultExtensionsWhenRunningInTsNode,
},
{
name: "should locate path that matches with star and exists with extension",
absoluteBaseUrl: "/root/",
paths: { "lib/*": ["location/*"] },
existingFiles: [join("/root", "location", "mylib.myext")],
requestedModule: "lib/mylib",
extensions: [".js", ".myext"],
expectedPath: removeExtension(join("/root", "location", "mylib.myext")),
},
{
name: "should resolve request with extension specified",
absoluteBaseUrl: "/root/",
paths: { "lib/*": ["location/*"] },
existingFiles: [join("/root", "location", "test.jpg")],
requestedModule: "lib/test.jpg",
expectedPath: join("/root", "location", "test.jpg"),
extensions: defaultExtensionsWhenRunningInTsNode,
},
{
name: "should locate path that matches without star and exists",
absoluteBaseUrl: "/root/",
paths: {
"lib/foo": ["location/foo"],
},
existingFiles: [join("/root", "location", "foo.ts")],
requestedModule: "lib/foo",
expectedPath: removeExtension(join("/root", "location", "foo.ts")),
extensions: defaultExtensionsWhenRunningInTsNode,
},
{
name: "should resolve to parent folder when filename is in subfolder",
absoluteBaseUrl: "/root/",
paths: { "lib/*": ["location/*"] },
existingFiles: [join("/root", "location", "mylib", "index.ts")],
requestedModule: "lib/mylib",
expectedPath: dirname(join("/root", "location", "mylib", "index.ts")),
extensions: defaultExtensionsWhenRunningInTsNode,
},
{
name: "should resolve from main field in package.json",
absoluteBaseUrl: "/root/",
paths: { "lib/*": ["location/*"] },
existingFiles: [join("/root", "location", "mylib", "kalle.ts")],
packageJson: { main: "./kalle.ts" },
requestedModule: "lib/mylib",
expectedPath: join("/root", "location", "mylib", "kalle.ts"),
extensions: defaultExtensionsWhenRunningInTsNode,
},
{
name: "should resolve from main field in package.json (js)",
absoluteBaseUrl: "/root",
paths: { "lib/*": ["location/*"] },
existingFiles: [join("/root", "location", "mylib.js", "kalle.js")],
packageJson: { main: "./kalle.js" },
requestedModule: "lib/mylib.js",
extensions: [".ts", ".js"],
expectedPath: join("/root", "location", "mylib.js", "kalle.js"),
},
{
name: "should resolve from list of fields by priority in package.json",
absoluteBaseUrl: "/root/",
paths: { "lib/*": ["location/*"] },
mainFields: ["missing", "browser", "main"],
packageJson: { main: "./main.js", browser: "./browser.js" },
existingFiles: [
join("/root", "location", "mylibjs", "main.js"), // mainFilePath
join("/root", "location", "mylibjs", "browser.js"), // browserFilePath
],
extensions: [".ts", ".js"],
requestedModule: "lib/mylibjs",
expectedPath: join("/root", "location", "mylibjs", "browser.js"),
},
{
name: "should ignore field mappings to missing files in package.json",
absoluteBaseUrl: "/root/",
paths: { "lib/*": ["location/*"] },
mainFields: ["browser", "main"],
existingFiles: [join("/root", "location", "mylibjs", "kalle.js")],
requestedModule: "lib/mylibjs",
packageJson: {
main: "./kalle.js",
browser: "./nope.js",
},
extensions: [".ts", ".js"],
expectedPath: join("/root", "location", "mylibjs", "kalle.js"),
},
{
name: "should resolve nested main fields",
absoluteBaseUrl: "/root/",
paths: { "lib/*": ["location/*"] },
mainFields: [["esnext", "main"]],
packageJson: { esnext: { main: "./main.js" } },
existingFiles: [join("/root", "location", "mylibjs", "main.js")],
extensions: [".ts", ".js"],
requestedModule: "lib/mylibjs",
expectedPath: join("/root", "location", "mylibjs", "main.js"),
},
{
name: "should ignore advanced field mappings in package.json",
absoluteBaseUrl: "/root/",
paths: { "lib/*": ["location/*"] },
existingFiles: [
join("/root", "location", "mylibjs", "kalle.js"),
join("/root", "location", "mylibjs", "browser.js"),
],
requestedModule: "lib/mylibjs",
packageJson: {
main: "./kalle.js",
browser: { mylibjs: "./browser.js", "./kalle.js": "./browser.js" },
},
extensions: [".ts", ".js"],
expectedPath: join("/root", "location", "mylibjs", "kalle.js"),
},
{
name: "should resolve to with the help of baseUrl when not explicitly set",
absoluteBaseUrl: "/root/",
paths: {},
existingFiles: [join("/root", "mylib", "index.ts")],
requestedModule: "mylib",
expectedPath: dirname(join("/root", "mylib", "index.ts")),
extensions: defaultExtensionsWhenRunningInTsNode,
},
{
name: "should not resolve with the help of baseUrl when asked not to",
absoluteBaseUrl: "/root/",
paths: {},
addMatchAll: false,
existingFiles: [join("/root", "mylib", "index.ts")],
requestedModule: "mylib",
expectedPath: undefined,
extensions: defaultExtensionsWhenRunningInTsNode,
},
{
name: "should not locate path that does not match",
absoluteBaseUrl: "/root/",
paths: { "lib/*": ["location/*"] },
existingFiles: [join("root", "location", "mylib")],
requestedModule: "mylib",
expectedPath: undefined,
extensions: defaultExtensionsWhenRunningInTsNode,
},
{
name: "should not resolve typings file (index.d.ts)",
absoluteBaseUrl: "/root/",
paths: {
"lib/*": ["location/*"],
},
existingFiles: [join("/root", "location", "mylib", "index.d.ts")],
requestedModule: "lib/mylib",
expectedPath: undefined,
extensions: defaultExtensionsWhenRunningInTsNode,
},
{
name: "should resolve main file with cjs file extension",
absoluteBaseUrl: "/root/",
paths: {},
existingFiles: [join("/root", "mylib", "index.cjs")],
packageJson: {
main: "./index.cjs",
},
requestedModule: "mylib",
expectedPath: join("/root", "mylib", "index.cjs"),
extensions: defaultExtensionsWhenRunningInTsNode,
},
{
name: "should resolve .ts from .js alias",
absoluteBaseUrl: "/root/",
paths: {
"@/*": ["src/*"],
},
existingFiles: [join("/root", "src", "foo.ts")],
requestedModule: "@/foo.js",
expectedPath: join("/root", "src", "foo"),
extensions: defaultExtensionsWhenRunningInTsNode,
},
];