/
ts.ts
98 lines (83 loc) · 3.53 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
import type * as ts from 'typescript/lib/tsserverlibrary';
import * as path from 'path';
import type { VueCompilerOptions, ResolvedVueCompilerOptions } from '../types';
export type ParsedCommandLine = ts.ParsedCommandLine & {
vueOptions: 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 content = ts.parseJsonConfigFileContent(json, parseConfigHost, rootDir, {}, tsConfigPath, undefined, extraFileExtensions);
return createParsedCommandLineBase(ts, parseConfigHost, content, tsConfigPath, extraFileExtensions, new Set());
}
export function createParsedCommandLine(
ts: typeof import('typescript/lib/tsserverlibrary'),
parseConfigHost: ts.ParseConfigHost,
tsConfigPath: string,
extraFileExtensions: ts.FileExtensionInfo[],
extendsSet = new Set<string>(),
): ParsedCommandLine {
const config = ts.readJsonConfigFile(tsConfigPath, parseConfigHost.readFile);
const content = ts.parseJsonSourceFileConfigFileContent(config, parseConfigHost, 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;
return createParsedCommandLineBase(ts, parseConfigHost, content, tsConfigPath, extraFileExtensions, extendsSet);
}
function createParsedCommandLineBase(
ts: typeof import('typescript/lib/tsserverlibrary'),
parseConfigHost: ts.ParseConfigHost,
content: ts.ParsedCommandLine,
tsConfigPath: string,
extraFileExtensions: ts.FileExtensionInfo[],
extendsSet: Set<string>,
): ParsedCommandLine {
let vueOptions = {};
const folder = path.dirname(tsConfigPath);
extendsSet.add(tsConfigPath);
if (content.raw.extends) {
try {
const extendsPath = require.resolve(content.raw.extends, { paths: [folder] });
if (!extendsSet.has(extendsPath)) {
vueOptions = createParsedCommandLine(ts, parseConfigHost, extendsPath, extraFileExtensions, extendsSet).vueOptions;
}
}
catch (error) {
console.error(error);
}
}
return {
...content,
vueOptions,
};
}
export function resolveVueCompilerOptions(vueOptions: VueCompilerOptions): ResolvedVueCompilerOptions {
const target = vueOptions.target ?? 3;
return {
...vueOptions,
target,
jsxTemplates: vueOptions.jsxTemplates ?? false,
strictTemplates: vueOptions.strictTemplates ?? false,
skipTemplateCodegen: vueOptions.skipTemplateCodegen ?? false,
dataAttributes: vueOptions.dataAttributes ?? ['data-*'],
htmlAttributes: vueOptions.htmlAttributes ?? ['aria-*'],
optionsWrapper: vueOptions.optionsWrapper ?? (
target >= 2.7
? [`(await import('vue')).defineComponent(`, `)`]
: [`(await import('vue')).default.extend(`, `)`]
),
narrowingTypesInInlineHandlers: vueOptions.narrowingTypesInInlineHandlers ?? false,
plugins: vueOptions.plugins ?? [],
bypassDefineComponentToExposePropsAndEmitsForJsScriptSetupComponents: vueOptions.bypassDefineComponentToExposePropsAndEmitsForJsScriptSetupComponents ?? true,
// experimental
experimentalRuntimeMode: vueOptions.experimentalRuntimeMode ?? 'runtime-dom',
experimentalResolveStyleCssClasses: vueOptions.experimentalResolveStyleCssClasses ?? 'scoped',
experimentalRfc436: vueOptions.experimentalRfc436 ?? false,
};
}