forked from sveltejs/language-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module-loader.ts
216 lines (199 loc) · 7.83 KB
/
module-loader.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
import ts from 'typescript';
import { getLastPartOfPath } from '../../utils';
import { DocumentSnapshot } from './DocumentSnapshot';
import { createSvelteSys } from './svelte-sys';
import {
ensureRealSvelteFilePath,
getExtensionFromScriptKind,
isSvelteFilePath,
isVirtualSvelteFilePath,
toVirtualSvelteFilePath
} from './utils';
/**
* Caches resolved modules.
*/
class ModuleResolutionCache {
private cache = new Map<string, ts.ResolvedModule | undefined>();
/**
* Tries to get a cached module.
* Careful: `undefined` can mean either there's no match found, or that the result resolved to `undefined`.
*/
get(moduleName: string, containingFile: string): ts.ResolvedModule | undefined {
return this.cache.get(this.getKey(moduleName, containingFile));
}
/**
* Checks if has cached module.
*/
has(moduleName: string, containingFile: string): boolean {
return this.cache.has(this.getKey(moduleName, containingFile));
}
/**
* Caches resolved module (or undefined).
*/
set(moduleName: string, containingFile: string, resolvedModule: ts.ResolvedModule | undefined) {
this.cache.set(this.getKey(moduleName, containingFile), resolvedModule);
}
/**
* Deletes module from cache. Call this if a file was deleted.
* @param resolvedModuleName full path of the module
*/
delete(resolvedModuleName: string): void {
this.cache.forEach((val, key) => {
if (val?.resolvedFileName === resolvedModuleName) {
this.cache.delete(key);
}
});
}
/**
* Deletes everything from cache that resolved to `undefined`
* and which might match the path.
*/
deleteUnresolvedResolutionsFromCache(path: string): void {
const fileNameWithoutEnding = getLastPartOfPath(path).split('.').shift() || '';
this.cache.forEach((val, key) => {
const moduleName = key.split(':::').pop() || '';
if (!val && moduleName.includes(fileNameWithoutEnding)) {
this.cache.delete(key);
}
});
}
private getKey(moduleName: string, containingFile: string) {
return containingFile + ':::' + ensureRealSvelteFilePath(moduleName);
}
}
class ImpliedNodeFormatResolver {
private alreadyResolved = new Map<string, ReturnType<typeof ts.getModeForResolutionAtIndex>>();
resolve(
importIdxInFile: number,
sourceFile: ts.SourceFile | undefined,
compilerOptions: ts.CompilerOptions
) {
let mode = undefined;
if (sourceFile) {
if (!sourceFile.impliedNodeFormat && isSvelteFilePath(sourceFile.fileName)) {
// impliedNodeFormat is not set for Svelte files, because the TS function which
// calculates this works with a fixed set of file extensions,
// which .svelte is obv not part of. Make it work by faking a TS file.
if (!this.alreadyResolved.has(sourceFile.fileName)) {
sourceFile.impliedNodeFormat = ts.getImpliedNodeFormatForFile(
toVirtualSvelteFilePath(sourceFile.fileName) as any,
undefined,
ts.sys,
compilerOptions
);
this.alreadyResolved.set(sourceFile.fileName, sourceFile.impliedNodeFormat);
} else {
sourceFile.impliedNodeFormat = this.alreadyResolved.get(sourceFile.fileName);
}
}
mode = ts.getModeForResolutionAtIndex(sourceFile, importIdxInFile);
}
return mode;
}
}
/**
* Creates a module loader specifically for `.svelte` files.
*
* The typescript language service tries to look up other files that are referenced in the currently open svelte file.
* For `.ts`/`.js` files this works, for `.svelte` files it does not by default.
* Reason: The typescript language service does not know about the `.svelte` file ending,
* so it assumes it's a normal typescript file and searches for files like `../Component.svelte.ts`, which is wrong.
* In order to fix this, we need to wrap typescript's module resolution and reroute all `.svelte.ts` file lookups to .svelte.
*
* @param getSnapshot A function which returns a (in case of svelte file fully preprocessed) typescript/javascript snapshot
* @param compilerOptions The typescript compiler options
*/
export function createSvelteModuleLoader(
getSnapshot: (fileName: string) => DocumentSnapshot,
compilerOptions: ts.CompilerOptions
) {
const svelteSys = createSvelteSys(getSnapshot);
const moduleCache = new ModuleResolutionCache();
const impliedNodeFormatResolver = new ImpliedNodeFormatResolver();
return {
fileExists: svelteSys.fileExists,
readFile: svelteSys.readFile,
readDirectory: svelteSys.readDirectory,
deleteFromModuleCache: (path: string) => {
svelteSys.deleteFromCache(path);
moduleCache.delete(path);
},
deleteUnresolvedResolutionsFromCache: (path: string) => {
svelteSys.deleteFromCache(path);
moduleCache.deleteUnresolvedResolutionsFromCache(path);
},
resolveModuleNames
};
function resolveModuleNames(
moduleNames: string[],
containingFile: string,
_reusedNames: string[] | undefined,
_redirectedReference: ts.ResolvedProjectReference | undefined,
_options: ts.CompilerOptions,
containingSourceFile?: ts.SourceFile | undefined
): Array<ts.ResolvedModule | undefined> {
return moduleNames.map((moduleName, index) => {
if (moduleCache.has(moduleName, containingFile)) {
return moduleCache.get(moduleName, containingFile);
}
const resolvedModule = resolveModuleName(
moduleName,
containingFile,
containingSourceFile,
index
);
moduleCache.set(moduleName, containingFile, resolvedModule);
return resolvedModule;
});
}
function resolveModuleName(
name: string,
containingFile: string,
containingSourceFile: ts.SourceFile | undefined,
index: number
): ts.ResolvedModule | undefined {
const mode = impliedNodeFormatResolver.resolve(
index,
containingSourceFile,
compilerOptions
);
// Delegate to the TS resolver first.
// If that does not bring up anything, try the Svelte Module loader
// which is able to deal with .svelte files.
const tsResolvedModule = ts.resolveModuleName(
name,
containingFile,
compilerOptions,
ts.sys,
undefined,
undefined,
mode
).resolvedModule;
if (tsResolvedModule && !isVirtualSvelteFilePath(tsResolvedModule.resolvedFileName)) {
return tsResolvedModule;
}
const svelteResolvedModule = ts.resolveModuleName(
name,
containingFile,
compilerOptions,
svelteSys,
undefined,
undefined,
mode
).resolvedModule;
if (
!svelteResolvedModule ||
!isVirtualSvelteFilePath(svelteResolvedModule.resolvedFileName)
) {
return svelteResolvedModule;
}
const resolvedFileName = ensureRealSvelteFilePath(svelteResolvedModule.resolvedFileName);
const snapshot = getSnapshot(resolvedFileName);
const resolvedSvelteModule: ts.ResolvedModuleFull = {
extension: getExtensionFromScriptKind(snapshot && snapshot.scriptKind),
resolvedFileName,
isExternalLibraryImport: svelteResolvedModule.isExternalLibraryImport
};
return resolvedSvelteModule;
}
}