-
-
Notifications
You must be signed in to change notification settings - Fork 415
/
languagePlugin.ts
151 lines (144 loc) · 4.09 KB
/
languagePlugin.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
/// <reference types="@volar/typescript" />
import { forEachEmbeddedCode, LanguagePlugin } from '@volar/language-core';
import * as CompilerDOM from '@vue/compiler-dom';
import type * as ts from 'typescript';
import { createPlugins } from './plugins';
import type { VueCompilerOptions, VueLanguagePlugin, VueLanguagePluginReturn } from './types';
import * as CompilerVue2 from './utils/vue2TemplateCompiler';
import { VueVirtualCode } from './virtualFile/vueFile';
const fileRegistries: {
key: string;
plugins: VueLanguagePlugin[];
files: Map<string, VueVirtualCode>;
}[] = [];
function getVueFileRegistry(key: string, plugins: VueLanguagePlugin[]) {
let fileRegistry = fileRegistries.find(r =>
r.key === key
&& r.plugins.length === plugins.length
&& r.plugins.every(plugin => plugins.includes(plugin))
)?.files;
if (!fileRegistry) {
fileRegistry = new Map();
fileRegistries.push({
key: key,
plugins: plugins,
files: fileRegistry,
});
}
return fileRegistry;
}
function getFileRegistryKey(
compilerOptions: ts.CompilerOptions,
vueCompilerOptions: VueCompilerOptions,
plugins: VueLanguagePluginReturn[]
) {
const values = [
...Object.keys(vueCompilerOptions)
.sort()
.filter(key => key !== 'plugins')
.map(key => [key, vueCompilerOptions[key as keyof VueCompilerOptions]]),
[...new Set(plugins.map(plugin => plugin.requiredCompilerOptions ?? []).flat())]
.sort()
.map(key => [key, compilerOptions[key as keyof ts.CompilerOptions]]),
];
return JSON.stringify(values);
}
export function createVueLanguagePlugin<T>(
ts: typeof import('typescript'),
compilerOptions: ts.CompilerOptions,
vueCompilerOptions: VueCompilerOptions,
asFileName: (scriptId: T) => string
): LanguagePlugin<T, VueVirtualCode> {
const pluginContext: Parameters<VueLanguagePlugin>[0] = {
modules: {
'@vue/compiler-dom': vueCompilerOptions.target < 3
? {
...CompilerDOM,
compile: CompilerVue2.compile,
}
: CompilerDOM,
typescript: ts,
},
compilerOptions,
vueCompilerOptions,
};
const plugins = createPlugins(pluginContext);
const fileRegistry = getVueFileRegistry(
getFileRegistryKey(compilerOptions, vueCompilerOptions, plugins),
vueCompilerOptions.plugins
);
return {
getLanguageId(scriptId) {
const fileName = asFileName(scriptId);
for (const plugin of plugins) {
const languageId = plugin.getLanguageId?.(fileName);
if (languageId) {
return languageId;
}
}
},
createVirtualCode(scriptId, languageId, snapshot) {
const fileName = asFileName(scriptId);
if (plugins.some(plugin => plugin.isValidFile?.(fileName, languageId))) {
const code = fileRegistry.get(fileName);
if (code) {
code.update(snapshot);
return code;
}
else {
const code = new VueVirtualCode(
fileName,
languageId,
snapshot,
vueCompilerOptions,
plugins,
ts,
);
fileRegistry.set(fileName, code);
return code;
}
}
},
updateVirtualCode(_fileId, code, snapshot) {
code.update(snapshot);
return code;
},
typescript: {
extraFileExtensions: getAllExtensions(vueCompilerOptions)
.map<ts.FileExtensionInfo>(ext => ({
extension: ext.slice(1),
isMixedContent: true,
scriptKind: 7 satisfies ts.ScriptKind.Deferred,
})),
getServiceScript(root) {
for (const code of forEachEmbeddedCode(root)) {
if (/script_(js|jsx|ts|tsx)/.test(code.id)) {
const lang = code.id.substring('script_'.length);
return {
code,
extension: '.' + lang,
scriptKind: lang === 'js' ? ts.ScriptKind.JS
: lang === 'jsx' ? ts.ScriptKind.JSX
: lang === 'tsx' ? ts.ScriptKind.TSX
: ts.ScriptKind.TS,
};
}
}
},
},
};
}
export function getAllExtensions(options: VueCompilerOptions) {
const result = new Set<string>();
for (const key in options) {
if (key === 'extensions' || key.endsWith('Extensions')) {
const value = options[key as keyof VueCompilerOptions];
if (Array.isArray(value) && value.every(v => typeof v === 'string')) {
for (const ext of value) {
result.add(ext);
}
}
}
}
return [...result];
}