index.js
2.92 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
function resolveModuleName(request, issuer, compilerOptions, moduleResolutionHost, parentResolver) {
const pnp = require(`pnpapi`);
const [, prefix = ``, packageName = ``, rest] = request.match(/^(!(?:.*!)+)?((?!\.{0,2}\/)(?:@[^\/]+\/)?[^\/]+)?(.*)/);
let failedLookupLocations = [];
// First we try the resolution on "@types/package-name" starting from the project root
if (packageName) {
const typesPackagePath = `@types/${packageName.replace(/\//g, `__`)}${rest}`;
let unqualified;
try {
unqualified = pnp.resolveToUnqualified(typesPackagePath, issuer, {considerBuiltins: false});
} catch (error) {}
if (unqualified) {
// TypeScript checks whether the directory of the candidate is a directory
// which may cause issues w/ zip loading (since the zip archive is still
// reported as a file). To workaround this we add a trailing slash, which
// causes TypeScript to assume the parent is a directory.
if (moduleResolutionHost.directoryExists && moduleResolutionHost.directoryExists(unqualified))
unqualified += `/`;
const finalResolution = parentResolver(unqualified, issuer, compilerOptions, moduleResolutionHost);
if (finalResolution.resolvedModule || finalResolution.resolvedTypeReferenceDirective) {
return finalResolution;
} else {
failedLookupLocations = failedLookupLocations.concat(finalResolution.failedLookupLocations);
}
}
}
// Then we try on "package-name", this time starting from the package that makes the request
if (true) {
const regularPackagePath = `${packageName || ``}${rest}`;
let unqualified;
try {
unqualified = pnp.resolveToUnqualified(regularPackagePath, issuer, {considerBuiltins: false});
} catch (error) {}
if (unqualified) {
// TypeScript checks whether the directory of the candidate is a directory
// which may cause issues w/ zip loading (since the zip archive is still
// reported as a file). To workaround this we add a trailing slash, which
// causes TypeScript to assume the parent is a directory.
if (moduleResolutionHost.directoryExists && moduleResolutionHost.directoryExists(unqualified))
unqualified += `/`;
const finalResolution = parentResolver(unqualified, issuer, compilerOptions, moduleResolutionHost);
if (finalResolution.resolvedModule || finalResolution.resolvedTypeReferenceDirective) {
return finalResolution;
} else {
failedLookupLocations = failedLookupLocations.concat(finalResolution.failedLookupLocations);
}
}
}
return {
resolvedModule: undefined,
resolvedTypeReferenceDirective: undefined,
failedLookupLocations,
};
}
module.exports.resolveModuleName = process.versions.pnp
? resolveModuleName
: (moduleName, containingFile, compilerOptions, compilerHost, resolveModuleName) =>
resolveModuleName(moduleName, containingFile, compilerOptions, compilerHost);