-
-
Notifications
You must be signed in to change notification settings - Fork 529
/
configuration.ts
320 lines (296 loc) · 8.96 KB
/
configuration.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import { resolve, dirname } from 'path';
import type * as _ts from 'typescript';
import {
CreateOptions,
DEFAULTS,
OptionBasePaths,
TSCommon,
TsConfigOptions,
} from './index';
import type { TSInternal } from './ts-compiler-types';
import { createTsInternals } from './ts-internals';
import { getDefaultTsconfigJsonForNodeVersion } from './tsconfigs';
import { assign, createProjectLocalResolveHelper } from './util';
/**
* TypeScript compiler option values required by `ts-node` which cannot be overridden.
*/
const TS_NODE_COMPILER_OPTIONS = {
sourceMap: true,
inlineSourceMap: false,
inlineSources: true,
declaration: false,
noEmit: false,
outDir: '.ts-node',
};
/*
* Do post-processing on config options to support `ts-node`.
*/
function fixConfig(ts: TSCommon, config: _ts.ParsedCommandLine) {
// Delete options that *should not* be passed through.
delete config.options.out;
delete config.options.outFile;
delete config.options.composite;
delete config.options.declarationDir;
delete config.options.declarationMap;
delete config.options.emitDeclarationOnly;
// Target ES5 output by default (instead of ES3).
if (config.options.target === undefined) {
config.options.target = ts.ScriptTarget.ES5;
}
// Target CommonJS modules by default (instead of magically switching to ES6 when the target is ES6).
if (config.options.module === undefined) {
config.options.module = ts.ModuleKind.CommonJS;
}
return config;
}
/**
* Load TypeScript configuration. Returns the parsed TypeScript config and
* any `ts-node` options specified in the config file.
*
* Even when a tsconfig.json is not loaded, this function still handles merging
* compilerOptions from various sources: API, environment variables, etc.
*
* @internal
*/
export function readConfig(
cwd: string,
ts: TSCommon,
rawApiOptions: CreateOptions
): {
/**
* Path of tsconfig file if one was loaded
*/
configFilePath: string | undefined;
/**
* Parsed TypeScript configuration with compilerOptions merged from all other sources (env vars, etc)
*/
config: _ts.ParsedCommandLine;
/**
* ts-node options pulled from `tsconfig.json`, NOT merged with any other sources. Merging must happen outside
* this function.
*/
tsNodeOptionsFromTsconfig: TsConfigOptions;
optionBasePaths: OptionBasePaths;
} {
// Ordered [a, b, c] where config a extends b extends c
const configChain: Array<{
config: any;
basePath: string;
configPath: string;
}> = [];
let config: any = { compilerOptions: {} };
let basePath = cwd;
let configFilePath: string | undefined = undefined;
const projectSearchDir = resolve(cwd, rawApiOptions.projectSearchDir ?? cwd);
const {
fileExists = ts.sys.fileExists,
readFile = ts.sys.readFile,
skipProject = DEFAULTS.skipProject,
project = DEFAULTS.project,
tsTrace = DEFAULTS.tsTrace,
} = rawApiOptions;
// Read project configuration when available.
if (!skipProject) {
configFilePath = project
? resolve(cwd, project)
: ts.findConfigFile(projectSearchDir, fileExists);
if (configFilePath) {
let pathToNextConfigInChain = configFilePath;
const tsInternals = createTsInternals(ts);
const errors: Array<_ts.Diagnostic> = [];
// Follow chain of "extends"
while (true) {
const result = ts.readConfigFile(pathToNextConfigInChain, readFile);
// Return diagnostics.
if (result.error) {
return {
configFilePath,
config: { errors: [result.error], fileNames: [], options: {} },
tsNodeOptionsFromTsconfig: {},
optionBasePaths: {},
};
}
const c = result.config;
const bp = dirname(pathToNextConfigInChain);
configChain.push({
config: c,
basePath: bp,
configPath: pathToNextConfigInChain,
});
if (c.extends == null) break;
const resolvedExtendedConfigPath = tsInternals.getExtendsConfigPath(
c.extends,
{
fileExists,
readDirectory: ts.sys.readDirectory,
readFile,
useCaseSensitiveFileNames: ts.sys.useCaseSensitiveFileNames,
trace: tsTrace,
},
bp,
errors,
(ts as unknown as TSInternal).createCompilerDiagnostic
);
if (errors.length) {
return {
configFilePath,
config: { errors, fileNames: [], options: {} },
tsNodeOptionsFromTsconfig: {},
optionBasePaths: {},
};
}
if (resolvedExtendedConfigPath == null) break;
pathToNextConfigInChain = resolvedExtendedConfigPath;
}
({ config, basePath } = configChain[0]);
}
}
// Merge and fix ts-node options that come from tsconfig.json(s)
const tsNodeOptionsFromTsconfig: TsConfigOptions = {};
const optionBasePaths: OptionBasePaths = {};
for (let i = configChain.length - 1; i >= 0; i--) {
const { config, basePath, configPath } = configChain[i];
const options = filterRecognizedTsConfigTsNodeOptions(
config['ts-node']
).recognized;
// Some options are relative to the config file, so must be converted to absolute paths here
if (options.require) {
// Modules are found relative to the tsconfig file, not the `dir` option
const tsconfigRelativeResolver = createProjectLocalResolveHelper(
dirname(configPath)
);
options.require = options.require.map((path: string) =>
tsconfigRelativeResolver(path, false)
);
}
if (options.scopeDir) {
options.scopeDir = resolve(basePath, options.scopeDir!);
}
// Downstream code uses the basePath; we do not do that here.
if (options.moduleTypes) {
optionBasePaths.moduleTypes = basePath;
}
if (options.transpiler != null) {
optionBasePaths.transpiler = basePath;
}
if (options.compiler != null) {
optionBasePaths.compiler = basePath;
}
assign(tsNodeOptionsFromTsconfig, options);
}
// Remove resolution of "files".
const files =
rawApiOptions.files ?? tsNodeOptionsFromTsconfig.files ?? DEFAULTS.files;
if (!files) {
config.files = [];
config.include = [];
}
// Only if a config file is *not* loaded, load an implicit configuration from @tsconfig/bases
const skipDefaultCompilerOptions = configFilePath != null;
const defaultCompilerOptionsForNodeVersion = skipDefaultCompilerOptions
? undefined
: {
...getDefaultTsconfigJsonForNodeVersion(ts).compilerOptions,
types: ['node'],
};
// Merge compilerOptions from all sources
config.compilerOptions = Object.assign(
{},
// automatically-applied options from @tsconfig/bases
defaultCompilerOptionsForNodeVersion,
// tsconfig.json "compilerOptions"
config.compilerOptions,
// from env var
DEFAULTS.compilerOptions,
// tsconfig.json "ts-node": "compilerOptions"
tsNodeOptionsFromTsconfig.compilerOptions,
// passed programmatically
rawApiOptions.compilerOptions,
// overrides required by ts-node, cannot be changed
TS_NODE_COMPILER_OPTIONS
);
const fixedConfig = fixConfig(
ts,
ts.parseJsonConfigFileContent(
config,
{
fileExists,
readFile,
readDirectory: ts.sys.readDirectory,
useCaseSensitiveFileNames: ts.sys.useCaseSensitiveFileNames,
},
basePath,
undefined,
configFilePath
)
);
return {
configFilePath,
config: fixedConfig,
tsNodeOptionsFromTsconfig,
optionBasePaths,
};
}
/**
* Given the raw "ts-node" sub-object from a tsconfig, return an object with only the properties
* recognized by "ts-node"
*/
function filterRecognizedTsConfigTsNodeOptions(jsonObject: any): {
recognized: TsConfigOptions;
unrecognized: any;
} {
if (jsonObject == null) return { recognized: {}, unrecognized: {} };
const {
compiler,
compilerHost,
compilerOptions,
emit,
files,
ignore,
ignoreDiagnostics,
logError,
preferTsExts,
pretty,
require,
skipIgnore,
transpileOnly,
typeCheck,
transpiler,
scope,
scopeDir,
moduleTypes,
experimentalReplAwait,
swc,
experimentalResolverFeatures,
...unrecognized
} = jsonObject as TsConfigOptions;
const filteredTsConfigOptions = {
compiler,
compilerHost,
compilerOptions,
emit,
experimentalReplAwait,
files,
ignore,
ignoreDiagnostics,
logError,
preferTsExts,
pretty,
require,
skipIgnore,
transpileOnly,
typeCheck,
transpiler,
scope,
scopeDir,
moduleTypes,
swc,
experimentalResolverFeatures,
};
// Use the typechecker to make sure this implementation has the correct set of properties
const catchExtraneousProps: keyof TsConfigOptions =
null as any as keyof typeof filteredTsConfigOptions;
const catchMissingProps: keyof typeof filteredTsConfigOptions =
null as any as keyof TsConfigOptions;
return { recognized: filteredTsConfigOptions, unrecognized };
}