-
-
Notifications
You must be signed in to change notification settings - Fork 371
/
index.ts
80 lines (71 loc) · 2.18 KB
/
index.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
import { runTsc } from '@volar/typescript/lib/quickstart/runTsc';
import * as vue from '@vue/language-core';
import type * as ts from 'typescript/lib/tsserverlibrary';
export function run() {
let runExtensions = ['.vue'];
const windowsPathReg = /\\/g;
const extensionsChangedException = new Error('extensions changed');
const main = () => runTsc(
require.resolve('typescript/lib/tsc'),
runExtensions,
(ts, options) => {
const { configFilePath } = options.options;
const vueOptions = typeof configFilePath === 'string'
? vue.createParsedCommandLine(ts, ts.sys, configFilePath.replace(windowsPathReg, '/')).vueOptions
: {};
const extensions = vueOptions.extensions ?? ['.vue'];
if (
runExtensions.length === extensions.length
&& runExtensions.every(ext => extensions.includes(ext))
) {
return vue.createLanguages(
ts,
options.options,
vueOptions,
false,
createFakeGlobalTypesHolder(options)?.replace(windowsPathReg, '/'),
);
}
else {
runExtensions = extensions;
throw extensionsChangedException;
}
},
);
try {
main();
} catch (err) {
if (err === extensionsChangedException) {
main();
}
}
}
export function createFakeGlobalTypesHolder(options: ts.CreateProgramOptions) {
const firstVueFile = options.rootNames.find(fileName => fileName.endsWith('.vue'));
if (firstVueFile) {
const fakeFileName = firstVueFile + '__VLS_globalTypes.vue';
(options.rootNames as string[]).push(fakeFileName);
const fileExists = options.host!.fileExists.bind(options.host);
const readFile = options.host!.readFile.bind(options.host);
const writeFile = options.host!.writeFile.bind(options.host);
options.host!.fileExists = fileName => {
if (fileName.endsWith('__VLS_globalTypes.vue')) {
return true;
}
return fileExists(fileName);
};
options.host!.readFile = fileName => {
if (fileName.endsWith('__VLS_globalTypes.vue')) {
return '<script setup lang="ts"></script>';
}
return readFile(fileName);
};
options.host!.writeFile = (fileName, ...args) => {
if (fileName.endsWith('__VLS_globalTypes.vue.d.ts')) {
return;
}
return writeFile(fileName, ...args);
};
return fakeFileName;
}
}