-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.ts
214 lines (185 loc) · 5.19 KB
/
index.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
import path from 'path';
import slash from 'slash';
import type { TsConfigJson, TsConfigJsonResolved, Cache } from '../types.js';
import { normalizePath } from '../utils/normalize-path.js';
import { readJsonc } from '../utils/read-jsonc.js';
import { implicitBaseUrlSymbol } from '../utils/symbols.js';
import { resolveExtendsPath } from './resolve-extends-path.js';
const resolveExtends = (
extendsPath: string,
fromDirectoryPath: string,
circularExtendsTracker: Set<string>,
cache?: Cache<string>,
) => {
const resolvedExtendsPath = resolveExtendsPath(
extendsPath,
fromDirectoryPath,
cache,
);
if (!resolvedExtendsPath) {
throw new Error(`File '${extendsPath}' not found.`);
}
if (circularExtendsTracker.has(resolvedExtendsPath)) {
throw new Error(`Circularity detected while resolving configuration: ${resolvedExtendsPath}`);
}
circularExtendsTracker.add(resolvedExtendsPath);
const extendsDirectoryPath = path.dirname(resolvedExtendsPath);
const extendsConfig = _parseTsconfig(resolvedExtendsPath, cache, circularExtendsTracker);
delete extendsConfig.references;
const { compilerOptions } = extendsConfig;
if (compilerOptions) {
const resolvePaths = ['baseUrl', 'outDir'] as const;
for (const property of resolvePaths) {
const unresolvedPath = compilerOptions[property];
if (unresolvedPath) {
compilerOptions[property] = slash(path.relative(
fromDirectoryPath,
path.join(extendsDirectoryPath, unresolvedPath),
)) || './';
}
}
}
if (extendsConfig.files) {
extendsConfig.files = extendsConfig.files.map(
file => slash(path.relative(
fromDirectoryPath,
path.join(extendsDirectoryPath, file),
)),
);
}
if (extendsConfig.include) {
extendsConfig.include = extendsConfig.include.map(
file => slash(path.relative(
fromDirectoryPath,
path.join(extendsDirectoryPath, file),
)),
);
}
if (extendsConfig.exclude) {
extendsConfig.exclude = extendsConfig.exclude.map(
file => slash(path.relative(
fromDirectoryPath,
path.join(extendsDirectoryPath, file),
)),
);
}
return extendsConfig;
};
const _parseTsconfig = (
tsconfigPath: string,
cache?: Cache<string>,
circularExtendsTracker = new Set<string>(),
): TsConfigJsonResolved => {
/**
* Decided not to cache the TsConfigJsonResolved object because it's
* mutable.
*
* Note how `resolveExtends` can call `parseTsconfig` rescursively
* and actually mutates the object. It can also be mutated in
* user-land.
*
* By only caching fs results, we can avoid serving mutated objects
*/
let config: TsConfigJson;
try {
config = readJsonc(tsconfigPath, cache) || {};
} catch {
throw new Error(`Cannot resolve tsconfig at path: ${tsconfigPath}`);
}
if (typeof config !== 'object') {
throw new SyntaxError(`Failed to parse tsconfig at: ${tsconfigPath}`);
}
const directoryPath = path.dirname(tsconfigPath);
if (config.compilerOptions) {
const { compilerOptions } = config;
if (
compilerOptions.paths
&& !compilerOptions.baseUrl
) {
type WithImplicitBaseUrl = TsConfigJson.CompilerOptions & {
[implicitBaseUrlSymbol]: string;
};
(compilerOptions as WithImplicitBaseUrl)[implicitBaseUrlSymbol] = directoryPath;
}
}
if (config.extends) {
const extendsPathList = (
Array.isArray(config.extends)
? config.extends
: [config.extends]
);
delete config.extends;
for (const extendsPath of extendsPathList.reverse()) {
const extendsConfig = resolveExtends(
extendsPath,
directoryPath,
new Set(circularExtendsTracker),
cache,
);
const merged = {
...extendsConfig,
...config,
compilerOptions: {
...extendsConfig.compilerOptions,
...config.compilerOptions,
},
};
if (extendsConfig.watchOptions) {
merged.watchOptions = {
...extendsConfig.watchOptions,
...config.watchOptions,
};
}
config = merged;
}
}
if (config.compilerOptions) {
const { compilerOptions } = config;
const normalizedPaths = [
'baseUrl',
'rootDir',
] as const;
for (const property of normalizedPaths) {
const unresolvedPath = compilerOptions[property];
if (unresolvedPath) {
const resolvedBaseUrl = path.resolve(directoryPath, unresolvedPath);
const relativeBaseUrl = normalizePath(path.relative(
directoryPath,
resolvedBaseUrl,
));
compilerOptions[property] = relativeBaseUrl;
}
}
const { outDir } = compilerOptions;
if (outDir) {
if (!Array.isArray(config.exclude)) {
config.exclude = [];
}
if (!config.exclude.includes(outDir)) {
config.exclude.push(outDir);
}
compilerOptions.outDir = normalizePath(outDir);
}
} else {
config.compilerOptions = {};
}
if (config.files) {
config.files = config.files.map(normalizePath);
}
if (config.include) {
config.include = config.include.map(slash);
}
if (config.watchOptions) {
const { watchOptions } = config;
if (watchOptions.excludeDirectories) {
watchOptions.excludeDirectories = watchOptions.excludeDirectories.map(
excludePath => slash(path.resolve(directoryPath, excludePath)),
);
}
}
return config;
};
export const parseTsconfig = (
tsconfigPath: string,
cache: Cache<string> = new Map(),
): TsConfigJsonResolved => _parseTsconfig(tsconfigPath, cache);