-
-
Notifications
You must be signed in to change notification settings - Fork 365
/
ts.ts
218 lines (199 loc) · 6.72 KB
/
ts.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
import type * as ts from 'typescript/lib/tsserverlibrary';
import * as path from 'path';
import type { RawVueCompilerOptions, VueCompilerOptions, VueLanguagePlugin } from '../types';
export type ParsedCommandLine = ts.ParsedCommandLine & {
vueOptions: Partial<VueCompilerOptions>;
};
export function createParsedCommandLineByJson(
ts: typeof import('typescript/lib/tsserverlibrary'),
parseConfigHost: ts.ParseConfigHost,
rootDir: string,
json: any,
extraFileExtensions: ts.FileExtensionInfo[],
): ParsedCommandLine {
const tsConfigPath = path.join(rootDir, 'jsconfig.json');
const proxyHost = proxyParseConfigHostForExtendConfigPaths(parseConfigHost);
const content = ts.parseJsonConfigFileContent(json, proxyHost.host, rootDir, {}, tsConfigPath, undefined, extraFileExtensions);
let vueOptions: Partial<VueCompilerOptions> = {};
for (const extendPath of proxyHost.extendConfigPaths.reverse()) {
try {
vueOptions = {
...vueOptions,
...getVueCompilerOptions(ts, ts.readJsonConfigFile(extendPath, proxyHost.host.readFile)),
};
} catch (err) { }
}
return {
...content,
vueOptions,
};
}
export function createParsedCommandLine(
ts: typeof import('typescript/lib/tsserverlibrary'),
parseConfigHost: ts.ParseConfigHost,
tsConfigPath: string,
extraFileExtensions: ts.FileExtensionInfo[],
): ParsedCommandLine {
try {
const proxyHost = proxyParseConfigHostForExtendConfigPaths(parseConfigHost);
const config = ts.readJsonConfigFile(tsConfigPath, proxyHost.host.readFile);
const content = ts.parseJsonSourceFileConfigFileContent(config, proxyHost.host, path.dirname(tsConfigPath), {}, tsConfigPath, undefined, extraFileExtensions);
// fix https://github.com/johnsoncodehk/volar/issues/1786
// https://github.com/microsoft/TypeScript/issues/30457
// patching ts server broke with outDir + rootDir + composite/incremental
content.options.outDir = undefined;
let vueOptions: Partial<VueCompilerOptions> = {};
for (const extendPath of proxyHost.extendConfigPaths.reverse()) {
try {
vueOptions = {
...vueOptions,
...getVueCompilerOptions(ts, ts.readJsonConfigFile(extendPath, proxyHost.host.readFile)),
};
} catch (err) { }
}
return {
...content,
vueOptions,
};
}
catch (err) {
console.warn('Failed to resolve tsconfig path:', tsConfigPath, err);
return {
fileNames: [],
options: {},
vueOptions: {},
errors: [],
};
}
}
function proxyParseConfigHostForExtendConfigPaths(parseConfigHost: ts.ParseConfigHost) {
const extendConfigPaths: string[] = [];
const host = new Proxy(parseConfigHost, {
get(target, key) {
if (key === 'readFile') {
return (fileName: string) => {
if (!fileName.endsWith('/package.json') && !extendConfigPaths.includes(fileName)) {
extendConfigPaths.push(fileName);
}
return target.readFile(fileName);
};
}
return target[key as keyof typeof target];
}
});
return {
host,
extendConfigPaths,
};
}
function getVueCompilerOptions(
ts: typeof import('typescript/lib/tsserverlibrary'),
tsConfigSourceFile: ts.TsConfigSourceFile,
): Partial<VueCompilerOptions> {
const folder = path.dirname(tsConfigSourceFile.fileName);
const obj = ts.convertToObject(tsConfigSourceFile, []);
const rawOptions: RawVueCompilerOptions = obj?.vueCompilerOptions ?? {};
const result: Partial<VueCompilerOptions> = {
...rawOptions as any,
};
const target = rawOptions.target ?? 'auto';
if (target === 'auto') {
const resolvedPath = resolvePath('vue/package.json');
if (resolvedPath) {
const vuePackageJson = require(resolvedPath);
const versionNumbers = vuePackageJson.version.split('.');
result.target = Number(versionNumbers[0] + '.' + versionNumbers[1]);
}
else {
// console.warn('Load vue/package.json failed from', folder);
}
}
else {
result.target = target;
}
if (rawOptions.plugins) {
const plugins = rawOptions.plugins
.map<VueLanguagePlugin | undefined>((pluginPath: string) => {
try {
const resolvedPath = resolvePath(pluginPath);
if (resolvedPath) {
return require(resolvedPath);
}
}
catch (error) {
console.warn('Load plugin failed', pluginPath, error);
}
})
.filter((plugin): plugin is NonNullable<typeof plugin> => !!plugin);
result.plugins = plugins;
}
if (rawOptions.hooks) {
result.hooks = rawOptions.hooks
.map(resolvePath)
.filter((hook): hook is NonNullable<typeof hook> => !!hook);
}
if (rawOptions.experimentalAdditionalLanguageModules) {
result.experimentalAdditionalLanguageModules = rawOptions.experimentalAdditionalLanguageModules
.map(resolvePath)
.filter((module): module is NonNullable<typeof module> => !!module);
}
return result;
function resolvePath(scriptPath: string): string | undefined {
try {
if (require?.resolve) {
return require.resolve(scriptPath, { paths: [folder] });
}
else {
// console.warn('failed to resolve path:', scriptPath, 'require.resolve is not supported in web');
}
}
catch (error) {
// console.warn(error);
}
}
}
export function resolveVueCompilerOptions(vueOptions: Partial<VueCompilerOptions>): VueCompilerOptions {
const target = vueOptions.target ?? 3.3;
return {
...vueOptions,
target,
extensions: vueOptions.extensions ?? ['.vue'],
lib: vueOptions.lib || (target < 2.7 ? '@vue/runtime-dom' : 'vue'),
jsxSlots: vueOptions.jsxSlots ?? false,
strictTemplates: vueOptions.strictTemplates ?? false,
skipTemplateCodegen: vueOptions.skipTemplateCodegen ?? false,
dataAttributes: vueOptions.dataAttributes ?? [],
htmlAttributes: vueOptions.htmlAttributes ?? ['aria-*'],
optionsWrapper: vueOptions.optionsWrapper ?? (
target >= 2.7
? [`(await import('vue')).defineComponent(`, `)`]
: [`(await import('vue')).default.extend(`, `)`]
),
macros: vueOptions.macros ?? {
defineProps: ['defineProps'],
defineSlots: ['defineSlots'],
defineEmits: ['defineEmits'],
defineExpose: ['defineExpose'],
withDefaults: ['withDefaults'],
},
plugins: vueOptions.plugins ?? [],
hooks: vueOptions.hooks ?? [],
// experimental
experimentalDefinePropProposal: vueOptions.experimentalDefinePropProposal ?? false,
experimentalAdditionalLanguageModules: vueOptions.experimentalAdditionalLanguageModules ?? [],
experimentalResolveStyleCssClasses: vueOptions.experimentalResolveStyleCssClasses ?? 'scoped',
// https://github.com/vuejs/vue-next/blob/master/packages/compiler-dom/src/transforms/vModel.ts#L49-L51
// https://vuejs.org/guide/essentials/forms.html#form-input-bindings
experimentalModelPropName: vueOptions.experimentalModelPropName ?? {
'': {
input: true
},
value: {
input: { type: 'text' },
textarea: true,
select: true
}
},
experimentalUseElementAccessInTemplate: vueOptions.experimentalUseElementAccessInTemplate ?? false,
};
}