-
-
Notifications
You must be signed in to change notification settings - Fork 421
/
Copy pathtypescriptRuntime.ts
349 lines (314 loc) · 10.3 KB
/
typescriptRuntime.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import type { TextRange } from '@volar/vue-code-gen';
import type * as ts from 'typescript/lib/tsserverlibrary';
import * as path from 'path';
import useHtmlPlugin from './plugins/html';
import usePugPlugin from './plugins/pug';
import { LanguageServiceHost } from './types';
import * as localTypes from './utils/localTypes';
import { injectCacheLogicToLanguageServiceHost } from './utils/ts';
import { createVueFile, EmbeddedFile } from './vueFile';
import { createVueFiles } from './vueFiles';
export interface VueLanguagePlugin {
compileTemplate?(tmplate: string, lang: string): {
html: string,
mapping(htmlStart: number, htmlEnd: number): { start: number, end: number; } | undefined,
} | undefined;
}
export type TypeScriptRuntime = ReturnType<typeof createTypeScriptRuntime>;
export function createTypeScriptRuntime(options: {
typescript: typeof import('typescript/lib/tsserverlibrary'),
vueLsHost: LanguageServiceHost,
baseCssModuleType: string,
getCssClasses: (cssEmbeddeFile: EmbeddedFile) => Record<string, TextRange[]>,
isTsPlugin?: boolean,
isVueTsc?: boolean,
}) {
const { typescript: ts } = options;
const vueCompilerOptions = options.vueLsHost.getVueCompilationSettings();
const tsFileVersions = new Map<string, string>();
const vueFiles = createVueFiles();
const plugins = [
useHtmlPlugin(),
usePugPlugin(),
];
const tsLsHost = createTsLsHost();
const tsLsRaw = ts.createLanguageService(tsLsHost);
const localTypesScript = ts.ScriptSnapshot.fromString(localTypes.getTypesCode(vueCompilerOptions.experimentalCompatMode ?? 3));
let lastProjectVersion: string | undefined;
let tsProjectVersion = 0;
if (!options.isVueTsc) {
injectCacheLogicToLanguageServiceHost(ts, tsLsHost, tsLsRaw);
}
return {
vueLsHost: options.vueLsHost,
vueFiles,
getTsLs: () => tsLsRaw,
getTsLsHost: () => tsLsHost,
update,
dispose: () => {
tsLsRaw.dispose();
},
getLocalTypesFiles: () => {
const fileNames = getLocalTypesFiles();
const code = localTypes.getTypesCode(vueCompilerOptions.experimentalCompatMode ?? 3);
return {
fileNames,
code,
};
},
};
function getLocalTypesFiles() {
return vueFiles.getDirs().map(dir => path.join(dir, localTypes.typesFileName));
}
function update() {
const newProjectVersion = options.vueLsHost.getProjectVersion?.();
if (newProjectVersion === undefined || newProjectVersion !== lastProjectVersion) {
lastProjectVersion = newProjectVersion;
const fileNames = options.vueLsHost.getScriptFileNames();
const vueFileNames = new Set(fileNames.filter(file => file.endsWith('.vue')));
const tsFileNames = new Set(fileNames.filter(file => !file.endsWith('.vue')));
const fileNamesToRemove: string[] = [];
const fileNamesToCreate: string[] = [];
const fileNamesToUpdate: string[] = [];
let tsFileUpdated = false;
// .vue
for (const vueFile of vueFiles.getAll()) {
if (!vueFileNames.has(vueFile.fileName) && !options.vueLsHost.fileExists?.(vueFile.fileName)) {
// delete
fileNamesToRemove.push(vueFile.fileName);
}
else {
// update
const newVersion = options.vueLsHost.getScriptVersion(vueFile.fileName);
if (vueFile.getVersion() !== newVersion) {
fileNamesToUpdate.push(vueFile.fileName);
}
}
}
for (const nowFileName of vueFileNames) {
if (!vueFiles.get(nowFileName)) {
// add
fileNamesToCreate.push(nowFileName);
}
}
// .ts / .js / .d.ts / .json ...
for (const tsFileVersion of tsFileVersions) {
if (!vueFileNames.has(tsFileVersion[0]) && !options.vueLsHost.fileExists?.(tsFileVersion[0])) {
// delete
tsFileVersions.delete(tsFileVersion[0]);
tsFileUpdated = true;
}
else {
// update
const newVersion = options.vueLsHost.getScriptVersion(tsFileVersion[0]);
if (tsFileVersion[1] !== newVersion) {
tsFileVersions.set(tsFileVersion[0], newVersion);
tsFileUpdated = true;
}
}
}
for (const nowFileName of tsFileNames) {
if (!tsFileVersions.has(nowFileName)) {
// add
const newVersion = options.vueLsHost.getScriptVersion(nowFileName);
tsFileVersions.set(nowFileName, newVersion);
tsFileUpdated = true;
}
}
if (tsFileUpdated) {
tsProjectVersion++;
}
const finalUpdateFileNames = fileNamesToCreate.concat(fileNamesToUpdate);
if (fileNamesToRemove.length) {
unsetSourceFiles(fileNamesToRemove);
}
if (finalUpdateFileNames.length) {
updateSourceFiles(finalUpdateFileNames);
}
}
}
function createTsLsHost() {
const scriptSnapshots = new Map<string, [string, ts.IScriptSnapshot]>();
const fileVersions = new WeakMap<EmbeddedFile, string>();
const _tsHost: Partial<ts.LanguageServiceHost> = {
fileExists: options.vueLsHost.fileExists
? fileName => {
// .vue.js -> .vue
// .vue.ts -> .vue
// .vue.d.ts (never)
const fileNameTrim = fileName.substring(0, fileName.lastIndexOf('.'));
if (fileNameTrim.endsWith('.vue')) {
const vueFile = vueFiles.get(fileNameTrim);
if (!vueFile) {
const fileExists = !!options.vueLsHost.fileExists?.(fileNameTrim);
if (fileExists) {
updateSourceFiles([fileNameTrim]); // create virtual files
}
}
}
if (!!vueFiles.fromEmbeddedFileName(fileName)) {
return true;
}
return !!options.vueLsHost.fileExists?.(fileName);
}
: undefined,
getProjectVersion: () => {
return tsProjectVersion.toString();
},
getScriptFileNames,
getScriptVersion,
getScriptSnapshot,
readDirectory: (_path, extensions, exclude, include, depth) => {
const result = options.vueLsHost.readDirectory?.(_path, extensions, exclude, include, depth) ?? [];
for (const vuePath of vueFiles.getFileNames()) {
const vuePath2 = path.join(_path, path.basename(vuePath));
if (path.relative(_path.toLowerCase(), vuePath.toLowerCase()).startsWith('..')) {
continue;
}
if (!depth && vuePath.toLowerCase() === vuePath2.toLowerCase()) {
result.push(vuePath2);
}
else if (depth) {
result.push(vuePath2); // TODO: depth num
}
}
return result;
},
getScriptKind(fileName) {
switch (path.extname(fileName)) {
case '.vue': return ts.ScriptKind.TSX; // can't use External, Unknown
case '.js': return ts.ScriptKind.JS;
case '.jsx': return ts.ScriptKind.JSX;
case '.ts': return ts.ScriptKind.TS;
case '.tsx': return ts.ScriptKind.TSX;
case '.json': return ts.ScriptKind.JSON;
default: return ts.ScriptKind.Unknown;
}
},
};
const tsHost = new Proxy<ts.LanguageServiceHost>(_tsHost as ts.LanguageServiceHost, {
get: (target, property: keyof ts.LanguageServiceHost) => {
return target[property] || options.vueLsHost[property];
},
});
return tsHost;
function getScriptFileNames() {
const tsFileNames = getLocalTypesFiles();
for (const mapped of vueFiles.getEmbeddeds()) {
if (mapped.embedded.file.isTsHostFile) {
tsFileNames.push(mapped.embedded.file.fileName); // virtual .ts
}
}
for (const fileName of options.vueLsHost.getScriptFileNames()) {
if (options.isTsPlugin) {
tsFileNames.push(fileName); // .vue + .ts
}
else if (!fileName.endsWith('.vue')) {
tsFileNames.push(fileName); // .ts
}
}
return tsFileNames;
}
function getScriptVersion(fileName: string) {
const basename = path.basename(fileName);
if (basename === localTypes.typesFileName) {
return '';
}
let mapped = vueFiles.fromEmbeddedFileName(fileName);
if (mapped) {
if (fileVersions.has(mapped.embedded.file)) {
return fileVersions.get(mapped.embedded.file)!;
}
else {
let version = ts.sys.createHash?.(mapped.embedded.file.content) ?? mapped.embedded.file.content;
if (options.isVueTsc) {
// fix https://github.com/johnsoncodehk/volar/issues/1082
version = mapped.vueFile.getVersion() + ':' + version;
}
fileVersions.set(mapped.embedded.file, version);
return version;
}
}
return options.vueLsHost.getScriptVersion(fileName);
}
function getScriptSnapshot(fileName: string) {
const version = getScriptVersion(fileName);
const cache = scriptSnapshots.get(fileName.toLowerCase());
if (cache && cache[0] === version) {
return cache[1];
}
const basename = path.basename(fileName);
if (basename === localTypes.typesFileName) {
return localTypesScript;
}
const mapped = vueFiles.fromEmbeddedFileName(fileName);
if (mapped) {
const text = mapped.embedded.file.content;
const snapshot = ts.ScriptSnapshot.fromString(text);
scriptSnapshots.set(fileName.toLowerCase(), [version, snapshot]);
return snapshot;
}
let tsScript = options.vueLsHost.getScriptSnapshot(fileName);
if (tsScript) {
if ((vueCompilerOptions.experimentalSuppressUnknownJsxPropertyErrors ?? true) && basename === 'runtime-dom.d.ts') {
// allow arbitrary attributes
let tsScriptText = tsScript.getText(0, tsScript.getLength());
tsScriptText = tsScriptText.replace(
'type ReservedProps = {',
'type ReservedProps = { [name: string]: any',
);
tsScript = ts.ScriptSnapshot.fromString(tsScriptText);
}
scriptSnapshots.set(fileName.toLowerCase(), [version, tsScript]);
return tsScript;
}
}
}
function updateSourceFiles(fileNames: string[]) {
let vueScriptsUpdated = false;
for (const fileName of fileNames) {
const sourceFile = vueFiles.get(fileName);
const scriptSnapshot = options.vueLsHost.getScriptSnapshot(fileName);
if (!scriptSnapshot) {
continue;
}
const scriptText = scriptSnapshot.getText(0, scriptSnapshot.getLength());
const scriptVersion = options.vueLsHost.getScriptVersion(fileName);
if (!sourceFile) {
vueFiles.set(fileName, createVueFile(
fileName,
scriptText,
scriptVersion,
plugins,
options.vueLsHost.getVueCompilationSettings(),
options.typescript,
options.baseCssModuleType,
options.getCssClasses,
tsLsRaw,
tsLsHost,
));
vueScriptsUpdated = true;
}
else {
const updates = sourceFile.update(scriptText, scriptVersion);
if (updates.scriptUpdated) {
vueScriptsUpdated = true;
}
}
}
if (vueScriptsUpdated) {
tsProjectVersion++;
}
}
function unsetSourceFiles(uris: string[]) {
let updated = false;
for (const uri of uris) {
if (vueFiles.delete(uri)) {
updated = true;
}
}
if (updated) {
tsProjectVersion++;
}
}
}