-
-
Notifications
You must be signed in to change notification settings - Fork 536
/
Copy pathresolver-functions.ts
179 lines (170 loc) · 6.09 KB
/
resolver-functions.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
import { resolve } from 'path';
import type * as _ts from 'typescript';
/**
* @internal
* In a factory because these are shared across both CompilerHost and LanguageService codepaths
*/
export function createResolverFunctions(kwargs: {
ts: typeof _ts;
serviceHost: _ts.ModuleResolutionHost;
cwd: string;
getCanonicalFileName: (filename: string) => string;
config: _ts.ParsedCommandLine;
configFilePath: string | undefined;
}) {
const {
serviceHost,
ts,
config,
cwd,
getCanonicalFileName,
configFilePath,
} = kwargs;
const moduleResolutionCache = ts.createModuleResolutionCache(
cwd,
getCanonicalFileName,
config.options
);
const knownInternalFilenames = new Set<string>();
/** "Buckets" (module directories) whose contents should be marked "internal" */
const internalBuckets = new Set<string>();
// Get bucket for a source filename. Bucket is the containing `./node_modules/*/` directory
// For '/project/node_modules/foo/node_modules/bar/lib/index.js' bucket is '/project/node_modules/foo/node_modules/bar/'
// For '/project/node_modules/foo/node_modules/@scope/bar/lib/index.js' bucket is '/project/node_modules/foo/node_modules/@scope/bar/'
const moduleBucketRe = /.*\/node_modules\/(?:@[^\/]+\/)?[^\/]+\//;
function getModuleBucket(filename: string) {
const find = moduleBucketRe.exec(filename);
if (find) return find[0];
return '';
}
// Mark that this file and all siblings in its bucket should be "internal"
function markBucketOfFilenameInternal(filename: string) {
internalBuckets.add(getModuleBucket(filename));
}
function isFileInInternalBucket(filename: string) {
return internalBuckets.has(getModuleBucket(filename));
}
function isFileKnownToBeInternal(filename: string) {
return knownInternalFilenames.has(filename);
}
/**
* If we need to emit JS for a file, force TS to consider it non-external
*/
const fixupResolvedModule = (
resolvedModule: _ts.ResolvedModule | _ts.ResolvedTypeReferenceDirective
) => {
const { resolvedFileName } = resolvedModule;
if (resolvedFileName === undefined) return;
// .ts is always switched to internal
// .js is switched on-demand
if (
resolvedModule.isExternalLibraryImport &&
((resolvedFileName.endsWith('.ts') &&
!resolvedFileName.endsWith('.d.ts')) ||
isFileKnownToBeInternal(resolvedFileName) ||
isFileInInternalBucket(resolvedFileName))
) {
resolvedModule.isExternalLibraryImport = false;
}
if (!resolvedModule.isExternalLibraryImport) {
knownInternalFilenames.add(resolvedFileName);
}
};
/*
* NOTE:
* Older ts versions do not pass `redirectedReference` nor `options`.
* We must pass `redirectedReference` to newer ts versions, but cannot rely on `options`, hence the weird argument name
*/
const resolveModuleNames: _ts.LanguageServiceHost['resolveModuleNames'] = (
moduleNames: string[],
containingFile: string,
reusedNames: string[] | undefined,
redirectedReference: _ts.ResolvedProjectReference | undefined,
optionsOnlyWithNewerTsVersions: _ts.CompilerOptions
): (_ts.ResolvedModule | undefined)[] => {
return moduleNames.map((moduleName) => {
const { resolvedModule } = ts.resolveModuleName(
moduleName,
containingFile,
config.options,
serviceHost,
moduleResolutionCache,
redirectedReference
);
if (resolvedModule) {
fixupResolvedModule(resolvedModule);
}
return resolvedModule;
});
};
// language service never calls this, but TS docs recommend that we implement it
const getResolvedModuleWithFailedLookupLocationsFromCache: _ts.LanguageServiceHost['getResolvedModuleWithFailedLookupLocationsFromCache'] = (
moduleName,
containingFile
): _ts.ResolvedModuleWithFailedLookupLocations | undefined => {
const ret = ts.resolveModuleNameFromCache(
moduleName,
containingFile,
moduleResolutionCache
);
if (ret && ret.resolvedModule) {
fixupResolvedModule(ret.resolvedModule);
}
return ret;
};
const resolveTypeReferenceDirectives: _ts.LanguageServiceHost['resolveTypeReferenceDirectives'] = (
typeDirectiveNames: string[],
containingFile: string,
redirectedReference: _ts.ResolvedProjectReference | undefined,
options: _ts.CompilerOptions
): (_ts.ResolvedTypeReferenceDirective | undefined)[] => {
// Note: seems to be called with empty typeDirectiveNames array for all files.
return typeDirectiveNames.map((typeDirectiveName) => {
let { resolvedTypeReferenceDirective } = ts.resolveTypeReferenceDirective(
typeDirectiveName,
containingFile,
config.options,
serviceHost,
redirectedReference
);
if (typeDirectiveName === 'node' && !resolvedTypeReferenceDirective) {
// Resolve @types/node relative to project first, then __dirname (copy logic from elsewhere / refactor into reusable function)
let typesNodePackageJsonPath: string | undefined;
try {
typesNodePackageJsonPath = require.resolve(
'@types/node/package.json',
{
paths: [configFilePath ?? cwd, __dirname],
}
);
} catch {} // gracefully do nothing when @types/node is not installed for any reason
if (typesNodePackageJsonPath) {
const typeRoots = [resolve(typesNodePackageJsonPath, '../..')];
({
resolvedTypeReferenceDirective,
} = ts.resolveTypeReferenceDirective(
typeDirectiveName,
containingFile,
{
...config.options,
typeRoots,
},
serviceHost,
redirectedReference
));
}
}
if (resolvedTypeReferenceDirective) {
fixupResolvedModule(resolvedTypeReferenceDirective);
}
return resolvedTypeReferenceDirective;
});
};
return {
resolveModuleNames,
getResolvedModuleWithFailedLookupLocationsFromCache,
resolveTypeReferenceDirectives,
isFileKnownToBeInternal,
markBucketOfFilenameInternal,
};
}