-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
63 lines (56 loc) · 1.52 KB
/
index.js
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
const path = require("path");
const {
create
} = require("enhanced-resolve");
const isBuiltin = require("is-core-module");
const resolverDefaults = {
extensions: [".ts", ".tsx", ".d.ts", ".js"],
};
let resolver;
function createResolver(configs) {
return create.sync({
...resolverDefaults,
...configs,
})
}
function cleanRequest(request) {
const finalBang = request.lastIndexOf("!");
if (finalBang >= 0) {
request = request.slice(finalBang + 1);
}
const finalQuestionMark = request.lastIndexOf("?");
if (finalQuestionMark >= 0) {
request = request.slice(0, finalQuestionMark);
}
return request;
}
module.exports.interfaceVersion = 2;
module.exports.resolve = function resolve(request, sourceFile, config) {
resolver = resolver || createResolver(config);
// Other resolvers incorrectly check for core modules before resolving. We are maintaining
// this functionality for ease of transition.
if (config.legacyCoreModuleResolution && isBuiltin(request)) {
return {
found: true,
path: null
};
}
try {
request = cleanRequest(request);
const resolved = resolver({}, path.dirname(sourceFile), request);
return {
found: true,
path: resolved
};
} catch (e) {
if (isBuiltin(request)) {
return {
found: true,
path: null
};
}
return {
found: false
};
}
};