/
vue-typescript-scripts.ts
113 lines (102 loc) · 3.61 KB
/
vue-typescript-scripts.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
import { generate as genScript } from '@volar/vue-code-gen/out/generators/script';
import type * as templateGen from '@volar/vue-code-gen/out/generators/template';
import { parseScriptRanges } from '@volar/vue-code-gen/out/parsers/scriptRanges';
import { parseScriptSetupRanges } from '@volar/vue-code-gen/out/parsers/scriptSetupRanges';
import { Ref } from '@vue/reactivity';
import { VueCompilerOptions } from '../types';
import { EmbeddedFile, VueLanguagePlugin } from '../sourceFile';
export default function (
lang: Ref<string>,
scriptRanges: Ref<ReturnType<typeof parseScriptRanges> | undefined>,
scriptSetupRanges: Ref<ReturnType<typeof parseScriptSetupRanges> | undefined>,
htmlGen: Ref<ReturnType<typeof templateGen.generate> | undefined>,
compilerOptions: VueCompilerOptions,
cssVars: Ref<string[]>,
): VueLanguagePlugin {
return {
getEmbeddedFilesCount(fileName, sfc) {
return fileName.endsWith('.html') ? 0 : 2;
},
getEmbeddedFile(fileName, sfc, i) {
let shimComponentOptionsMode: 'defineComponent' | 'Vue.extend' | false = false;
if (
(compilerOptions.experimentalImplicitWrapComponentOptionsWithVue2Extend ?? 'onlyJs') === 'onlyJs'
? lang.value === 'js' || lang.value === 'jsx'
: !!compilerOptions.experimentalImplicitWrapComponentOptionsWithVue2Extend
) {
shimComponentOptionsMode = 'Vue.extend';
}
if (
(compilerOptions.experimentalImplicitWrapComponentOptionsWithDefineComponent ?? 'onlyJs') === 'onlyJs'
? lang.value === 'js' || lang.value === 'jsx'
: !!compilerOptions.experimentalImplicitWrapComponentOptionsWithDefineComponent
) {
shimComponentOptionsMode = 'defineComponent';
}
// true override 'onlyJs'
if (compilerOptions.experimentalImplicitWrapComponentOptionsWithVue2Extend === true) {
shimComponentOptionsMode = 'Vue.extend';
}
if (compilerOptions.experimentalImplicitWrapComponentOptionsWithDefineComponent === true) {
shimComponentOptionsMode = 'defineComponent';
}
const codeGen = genScript(
i === 0 ? 'script' : 'template',
fileName,
sfc.script ?? undefined,
sfc.scriptSetup ?? undefined,
scriptRanges.value,
scriptSetupRanges.value,
() => htmlGen.value,
() => {
const bindTexts: string[] = [];
for (const cssVar of cssVars.value) {
bindTexts.push(cssVar);
}
return bindTexts;
},
shimComponentOptionsMode,
(compilerOptions.experimentalDowngradePropsAndEmitsToSetupReturnOnScriptSetup ?? 'onlyJs') === 'onlyJs'
? lang.value === 'js' || lang.value === 'jsx'
: !!compilerOptions.experimentalDowngradePropsAndEmitsToSetupReturnOnScriptSetup,
compilerOptions.target ?? 3,
);
let file: EmbeddedFile | undefined;
if (i === 0) {
file = {
fileName: fileName + '.' + lang.value,
content: codeGen.codeGen.getText(),
capabilities: {
diagnostics: !sfc.script?.src,
foldingRanges: false,
formatting: false,
documentSymbol: false,
codeActions: !sfc.script?.src,
inlayHints: !sfc.script?.src,
},
isTsHostFile: true,
mappings: codeGen.codeGen.getMappings(),
teleportMappings: codeGen.teleports,
};
}
else if (sfc.script || sfc.scriptSetup) {
file = {
fileName: fileName + '.__VLS_script.' + lang.value,
content: codeGen.codeGen.getText(),
capabilities: {
diagnostics: false,
foldingRanges: false,
formatting: false,
documentSymbol: false,
codeActions: false,
inlayHints: false,
},
isTsHostFile: true,
mappings: codeGen.codeGen.getMappings(),
teleportMappings: codeGen.teleports,
};
}
return file;
},
};
}