/
ts.ts
155 lines (137 loc) · 4.95 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
import type * as ts from 'typescript/lib/tsserverlibrary';
import { createModuleSpecifierCache } from './moduleSpecifierCache';
import { createPackageJsonCache, PackageJsonInfo, Ternary } from './packageJsonCache';
import * as path from 'path';
export function injectCacheLogicToLanguageServiceHost(
ts: typeof import('typescript/lib/tsserverlibrary'),
host: ts.LanguageServiceHost,
service: ts.LanguageService,
) {
const versionNums = ts.version.split('.').map(s => Number(s));
const greaterThan47 = versionNums[0] > 4 || (versionNums[0] === 4 && versionNums[1] >= 7);
if (!greaterThan47) {
console.log('Please downgrade to v0.34.17 or lower for TypeScript version:', ts.version);
return;
}
const _createCacheableExportInfoMap = (ts as any).createCacheableExportInfoMap;
const _combinePaths = (ts as any).combinePaths;
const _forEachAncestorDirectory = (ts as any).forEachAncestorDirectory;
const _getDirectoryPath = (ts as any).getDirectoryPath;
const _toPath = (ts as any).toPath;
const _createGetCanonicalFileName = (ts as any).createGetCanonicalFileName;
if (
!_createCacheableExportInfoMap
|| !_combinePaths
|| !_forEachAncestorDirectory
|| !_getDirectoryPath
|| !_toPath
|| !_createGetCanonicalFileName
) return;
const moduleSpecifierCache = createModuleSpecifierCache();
const exportMapCache = _createCacheableExportInfoMap({
getCurrentProgram() {
return service.getProgram();
},
getPackageJsonAutoImportProvider() {
return service.getProgram();
},
getGlobalTypingsCacheLocation() {
return undefined;
},
});
const packageJsonCache = createPackageJsonCache(ts, {
...host,
// @ts-expect-error
host: { ...host },
toPath,
});
// @ts-expect-error
host.getCachedExportInfoMap = () => exportMapCache;
// @ts-expect-error
host.getModuleSpecifierCache = () => moduleSpecifierCache;
// @ts-expect-error
host.getPackageJsonsVisibleToFile = (fileName: string, rootDir?: string) => {
const rootPath = rootDir && toPath(rootDir);
const filePath = toPath(fileName);
const result: PackageJsonInfo[] = [];
const processDirectory = (directory: ts.Path): boolean | undefined => {
switch (packageJsonCache.directoryHasPackageJson(directory)) {
// Sync and check same directory again
case Ternary.Maybe:
packageJsonCache.searchDirectoryAndAncestors(directory);
return processDirectory(directory);
// Check package.json
case Ternary.True:
const packageJsonFileName = _combinePaths(directory, "package.json");
// this.watchPackageJsonFile(packageJsonFileName as ts.Path); // TODO
const info = packageJsonCache.getInDirectory(directory);
if (info) result.push(info);
}
if (rootPath && rootPath === directory) {
return true;
}
};
_forEachAncestorDirectory(_getDirectoryPath(filePath), processDirectory);
return result;
};
function toPath(fileName: string) {
return _toPath(fileName, host.getCurrentDirectory(), _createGetCanonicalFileName(host.useCaseSensitiveFileNames?.()));
}
}
export function createParsedCommandLine(
ts: typeof import('typescript/lib/tsserverlibrary'),
parseConfigHost: ts.ParseConfigHost,
tsConfig: string,
extendsSet = new Set<string>(),
): ts.ParsedCommandLine & {
vueOptions: {
experimentalCompatMode?: 2 | 3;
experimentalTemplateCompilerOptions?: any;
experimentalTemplateCompilerOptionsRequirePath?: string;
};
} {
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(tsConfig);
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;
}