/
config.ts
166 lines (150 loc) · 4.58 KB
/
config.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
import { Chalk } from 'chalk';
import * as path from 'path';
import * as typescript from 'typescript';
import * as webpack from 'webpack';
import { LoaderOptions, WebpackError } from './interfaces';
import * as logger from './logger';
import { formatErrors } from './utils';
import { getCompilerOptions } from './compilerSetup';
interface ConfigFile {
config?: any;
error?: typescript.Diagnostic;
}
export function getConfigFile(
compiler: typeof typescript,
colors: Chalk,
loader: webpack.loader.LoaderContext,
loaderOptions: LoaderOptions,
compilerCompatible: boolean,
log: logger.Logger,
compilerDetailsLogMessage: string
) {
const configFilePath = findConfigFile(
compiler,
path.dirname(loader.resourcePath),
loaderOptions.configFile
);
let configFileError: WebpackError | undefined;
let configFile: ConfigFile;
if (configFilePath !== undefined) {
if (compilerCompatible) {
log.logInfo(`${compilerDetailsLogMessage} and ${configFilePath}`);
} else {
log.logInfo(`ts-loader: Using config file at ${configFilePath}`);
}
configFile = compiler.readConfigFile(configFilePath, compiler.sys.readFile);
if (configFile.error !== undefined) {
configFileError = formatErrors(
[configFile.error],
loaderOptions,
colors,
compiler,
{ file: configFilePath },
loader.context
)[0];
}
} else {
if (compilerCompatible) {
log.logInfo(compilerDetailsLogMessage);
}
configFile = {
config: {
compilerOptions: {},
files: []
}
};
}
if (configFileError === undefined) {
configFile.config.compilerOptions = Object.assign(
{},
configFile.config.compilerOptions,
loaderOptions.compilerOptions
);
}
return {
configFilePath,
configFile,
configFileError
};
}
/**
* Find a tsconfig file by name or by path.
* By name, the tsconfig.json is found using the same method as `tsc`, starting in the current
* directory and continuing up the parent directory chain.
* By path, the file will be found by resolving the given path relative to the requesting entry file.
*
* @param compiler The TypeScript compiler instance
* @param requestDirPath The directory in which the entry point requesting the tsconfig.json lies
* @param configFile The tsconfig file name to look for or a path to that file
* @return The absolute path to the tsconfig file, undefined if none was found.
*/
function findConfigFile(
compiler: typeof typescript,
requestDirPath: string,
configFile: string
): string | undefined {
// If `configFile` is an absolute path, return it right away
if (path.isAbsolute(configFile)) {
return compiler.sys.fileExists(configFile) ? configFile : undefined;
}
// If `configFile` is a relative path, resolve it.
// We define a relative path as: starts with
// one or two dots + a common directory delimiter
if (configFile.match(/^\.\.?(\/|\\)/) !== null) {
const resolvedPath = path.resolve(requestDirPath, configFile);
return compiler.sys.fileExists(resolvedPath) ? resolvedPath : undefined;
// If `configFile` is a file name, find it in the directory tree
} else {
while (true) {
const fileName = path.join(requestDirPath, configFile);
if (compiler.sys.fileExists(fileName)) {
return fileName;
}
const parentPath = path.dirname(requestDirPath);
if (parentPath === requestDirPath) {
break;
}
requestDirPath = parentPath;
}
return undefined;
}
}
export function getConfigParseResult(
compiler: typeof typescript,
configFile: ConfigFile,
basePath: string
) {
const configParseResult = compiler.parseJsonConfigFileContent(
configFile.config,
compiler.sys,
basePath
);
return configParseResult;
}
const extendedConfigCache = new Map() as typescript.Map<
typescript.ExtendedConfigCacheEntry
>;
export function getParsedCommandLine(
compiler: typeof typescript,
loaderOptions: LoaderOptions,
configFilePath: string
): typescript.ParsedCommandLine | undefined {
const host: typescript.ParseConfigFileHost = compiler.sys as any;
host.onUnRecoverableConfigFileDiagnostic = () => {};
const result = compiler.getParsedCommandLineOfConfigFile(
configFilePath,
loaderOptions.compilerOptions,
host,
extendedConfigCache
);
host.onUnRecoverableConfigFileDiagnostic = undefined!;
if (result) {
const options = getCompilerOptions(result);
(compiler as any).setConfigFileInOptions(
options,
(result.options as any).configFile
);
result.options = options;
}
return result;
}