-
-
Notifications
You must be signed in to change notification settings - Fork 366
/
ts.ts
66 lines (56 loc) · 2.01 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
import type * as ts from 'typescript/lib/tsserverlibrary';
import * as path from 'path';
import type { VueCompilerOptions } from '../types';
export function createParsedCommandLine(
ts: typeof import('typescript/lib/tsserverlibrary'),
parseConfigHost: ts.ParseConfigHost,
tsConfig: string,
extendsSet = new Set<string>(),
): ts.ParsedCommandLine & {
vueOptions: VueCompilerOptions;
} {
const tsConfigPath = ts.sys.resolvePath(tsConfig);
const config = ts.readJsonConfigFile(tsConfigPath, ts.sys.readFile);
const content = ts.parseJsonSourceFileConfigFileContent(config, parseConfigHost, path.dirname(tsConfigPath), {}, path.basename(tsConfigPath));
content.options.outDir = undefined; // TODO: patching ts server broke with outDir + rootDir + composite/incremental
let baseVueOptions = {};
const folder = path.dirname(tsConfigPath);
extendsSet.add(tsConfig);
if (content.raw.extends) {
try {
const extendsPath = require.resolve(content.raw.extends, { paths: [folder] });
if (!extendsSet.has(extendsPath)) {
baseVueOptions = createParsedCommandLine(ts, parseConfigHost, extendsPath, extendsSet).vueOptions;
}
}
catch (error) {
console.error(error);
}
}
return {
...content,
vueOptions: {
...baseVueOptions,
...resolveVueCompilerOptions(content.raw.vueCompilerOptions ?? {}, folder),
},
};
}
function resolveVueCompilerOptions(rawOptions: {
[key: string]: any,
experimentalTemplateCompilerOptionsRequirePath?: string,
}, rootPath: string) {
const result = { ...rawOptions };
let templateOptionsPath = rawOptions.experimentalTemplateCompilerOptionsRequirePath;
if (templateOptionsPath) {
if (!path.isAbsolute(templateOptionsPath)) {
templateOptionsPath = require.resolve(templateOptionsPath, { paths: [rootPath] });
}
try {
result.experimentalTemplateCompilerOptions = require(templateOptionsPath).default;
} catch (error) {
console.warn('Failed to require "experimentalTemplateCompilerOptionsRequirePath":', templateOptionsPath);
console.error(error);
}
}
return result;
}