-
-
Notifications
You must be signed in to change notification settings - Fork 371
/
languageModule.ts
205 lines (197 loc) · 6.52 KB
/
languageModule.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
import { forEachEmbeddedCode, type LanguagePlugin } from '@volar/language-core';
import type * as ts from 'typescript';
import { getBasePlugins } from './plugins';
import type { VueCompilerOptions, VueLanguagePlugin } from './types';
import { VueVirtualCode } from './virtualFile/vueFile';
import * as CompilerDOM from '@vue/compiler-dom';
import * as CompilerVue2 from './utils/vue2TemplateCompiler';
import useHtmlFilePlugin from './plugins/file-html';
import useMdFilePlugin from './plugins/file-md';
import useVueFilePlugin from './plugins/file-vue';
const normalFileRegistries: {
key: string;
plugins: VueLanguagePlugin[];
files: Map<string, VueVirtualCode>;
}[] = [];
const holderFileRegistries: typeof normalFileRegistries = [];
function getVueFileRegistry(isGlobalTypesHolder: boolean, key: string, plugins: VueLanguagePlugin[]) {
const fileRegistries = isGlobalTypesHolder ? holderFileRegistries : normalFileRegistries;
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: ReturnType<VueLanguagePlugin>[],
) {
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 interface _Plugin extends LanguagePlugin<VueVirtualCode> {
getCanonicalFileName: (fileName: string) => string;
pluginContext: Parameters<VueLanguagePlugin>[0];
}
export function createVueLanguagePlugin(
ts: typeof import('typescript'),
getFileName: (fileId: string) => string,
useCaseSensitiveFileNames: boolean,
getProjectVersion: () => string,
getScriptFileNames: () => string[] | Set<string>,
compilerOptions: ts.CompilerOptions,
vueCompilerOptions: VueCompilerOptions,
): _Plugin {
const pluginContext: Parameters<VueLanguagePlugin>[0] = {
modules: {
'@vue/compiler-dom': vueCompilerOptions.target < 3
? {
...CompilerDOM,
compile: CompilerVue2.compile,
}
: CompilerDOM,
typescript: ts,
},
compilerOptions,
vueCompilerOptions,
globalTypesHolder: undefined,
};
const basePlugins = getBasePlugins(pluginContext);
const vueSfcPlugin = useVueFilePlugin(pluginContext);
const vitePressSfcPlugin = useMdFilePlugin(pluginContext);
const petiteVueSfcPlugin = useHtmlFilePlugin(pluginContext);
const getCanonicalFileName = useCaseSensitiveFileNames
? (fileName: string) => fileName
: (fileName: string) => fileName.toLowerCase();
let canonicalRootFileNames = new Set<string>();
let canonicalRootFileNamesVersion: string | undefined;
return {
getCanonicalFileName,
pluginContext,
getLanguageId(scriptId) {
if (vueCompilerOptions.extensions.some(ext => scriptId.endsWith(ext))) {
return 'vue';
}
if (vueCompilerOptions.vitePressExtensions.some(ext => scriptId.endsWith(ext))) {
return 'markdown';
}
if (vueCompilerOptions.petiteVueExtensions.some(ext => scriptId.endsWith(ext))) {
return 'html';
}
},
createVirtualCode(fileId, languageId, snapshot) {
if (languageId === 'vue' || languageId === 'markdown' || languageId === 'html') {
const fileName = getFileName(fileId);
const projectVersion = getProjectVersion();
if (projectVersion !== canonicalRootFileNamesVersion) {
canonicalRootFileNames = new Set([...getScriptFileNames()].map(getCanonicalFileName));
canonicalRootFileNamesVersion = projectVersion;
}
if (!pluginContext.globalTypesHolder && canonicalRootFileNames.has(getCanonicalFileName(fileName))) {
pluginContext.globalTypesHolder = fileName;
}
const fileRegistry = getFileRegistry(pluginContext.globalTypesHolder === fileName);
const code = fileRegistry.get(fileId);
if (code) {
code.update(snapshot);
return code;
}
else {
const code = new VueVirtualCode(
fileName,
languageId,
snapshot,
vueCompilerOptions,
languageId === 'html'
? [petiteVueSfcPlugin, ...basePlugins]
: languageId === 'markdown'
? [vitePressSfcPlugin, ...basePlugins]
: [vueSfcPlugin, ...basePlugins],
ts,
);
fileRegistry.set(fileId, code);
return code;
}
}
},
updateVirtualCode(_fileId, code, snapshot) {
code.update(snapshot);
return code;
},
// TODO: when global types holder deleted, move global types to another file
// disposeVirtualCode(fileId, code) {
// const isGlobalTypesHolder = code.fileName === pluginContext.globalTypesHolder;
// const fileRegistry = getFileRegistry(isGlobalTypesHolder);
// fileRegistry.delete(fileId);
// if (isGlobalTypesHolder) {
// pluginContext.globalTypesHolder = undefined;
// const fileRegistry2 = getFileRegistry(false);
// for (const [fileId, code] of fileRegistry2) {
// if (isValidGlobalTypesHolder(code.fileName)) {
// pluginContext.globalTypesHolder = code.fileName;
// fileRegistry2.delete(fileId);
// // force dirty
// files?.delete(fileId);
// files?.set(
// fileId,
// code.languageId,
// code.snapshot,
// );
// break;
// }
// }
// }
// },
typescript: {
extraFileExtensions: [
...vueCompilerOptions.extensions,
...vueCompilerOptions.vitePressExtensions,
...vueCompilerOptions.petiteVueExtensions,
].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 (code.id.startsWith('script_')) {
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,
};
}
}
},
},
};
function getFileRegistry(isGlobalTypesHolder: boolean) {
return getVueFileRegistry(
isGlobalTypesHolder,
getFileRegistryKey(compilerOptions, vueCompilerOptions, basePlugins),
vueCompilerOptions.plugins,
);
}
}