UNPKG

5.22 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.getParsedCommandLine = exports.getConfigParseResult = exports.getConfigFile = void 0;
4const path = require("path");
5const compilerSetup_1 = require("./compilerSetup");
6const utils_1 = require("./utils");
7function getConfigFile(compiler, colors, loader, loaderOptions, compilerCompatible, log, compilerDetailsLogMessage) {
8 const configFilePath = findConfigFile(compiler, path.dirname(loader.resourcePath), loaderOptions.configFile);
9 let configFileError;
10 let configFile;
11 if (configFilePath !== undefined) {
12 if (compilerCompatible) {
13 log.logInfo(`${compilerDetailsLogMessage} and ${configFilePath}`);
14 }
15 else {
16 log.logInfo(`ts-loader: Using config file at ${configFilePath}`);
17 }
18 configFile = compiler.readConfigFile(configFilePath, compiler.sys.readFile);
19 if (configFile.error !== undefined) {
20 configFileError = utils_1.formatErrors([configFile.error], loaderOptions, colors, compiler, { file: configFilePath }, loader.context)[0];
21 }
22 }
23 else {
24 if (compilerCompatible) {
25 log.logInfo(compilerDetailsLogMessage);
26 }
27 configFile = {
28 config: {
29 compilerOptions: {},
30 files: [],
31 },
32 };
33 }
34 if (configFileError === undefined) {
35 configFile.config.compilerOptions = Object.assign({}, configFile.config.compilerOptions);
36 }
37 return {
38 configFilePath,
39 configFile,
40 configFileError,
41 };
42}
43exports.getConfigFile = getConfigFile;
44/**
45 * Find a tsconfig file by name or by path.
46 * By name, the tsconfig.json is found using the same method as `tsc`, starting in the current
47 * directory and continuing up the parent directory chain.
48 * By path, the file will be found by resolving the given path relative to the requesting entry file.
49 *
50 * @param compiler The TypeScript compiler instance
51 * @param requestDirPath The directory in which the entry point requesting the tsconfig.json lies
52 * @param configFile The tsconfig file name to look for or a path to that file
53 * @return The absolute path to the tsconfig file, undefined if none was found.
54 */
55function findConfigFile(compiler, requestDirPath, configFile) {
56 // If `configFile` is an absolute path, return it right away
57 if (path.isAbsolute(configFile)) {
58 return compiler.sys.fileExists(configFile) ? configFile : undefined;
59 }
60 // If `configFile` is a relative path, resolve it.
61 // We define a relative path as: starts with
62 // one or two dots + a common directory delimiter
63 if (configFile.match(/^\.\.?(\/|\\)/) !== null) {
64 const resolvedPath = path.resolve(requestDirPath, configFile);
65 return compiler.sys.fileExists(resolvedPath) ? resolvedPath : undefined;
66 // If `configFile` is a file name, find it in the directory tree
67 }
68 else {
69 while (true) {
70 const fileName = path.join(requestDirPath, configFile);
71 if (compiler.sys.fileExists(fileName)) {
72 return fileName;
73 }
74 const parentPath = path.dirname(requestDirPath);
75 if (parentPath === requestDirPath) {
76 break;
77 }
78 requestDirPath = parentPath;
79 }
80 return undefined;
81 }
82}
83function getConfigParseResult(compiler, configFile, basePath, configFilePath, loaderOptions) {
84 const configParseResult = compiler.parseJsonConfigFileContent(configFile.config, Object.assign(Object.assign({}, compiler.sys), { useCaseSensitiveFileNames: utils_1.useCaseSensitiveFileNames(compiler, loaderOptions) }), basePath, getCompilerOptionsToExtend(compiler, loaderOptions, basePath, configFilePath || 'tsconfig.json'));
85 if (!loaderOptions.projectReferences) {
86 configParseResult.projectReferences = undefined;
87 }
88 // set internal options.configFilePath flag on options to denote that we read this from a file
89 configParseResult.options = Object.assign({}, configParseResult.options, {
90 configFilePath,
91 });
92 return configParseResult;
93}
94exports.getConfigParseResult = getConfigParseResult;
95const extendedConfigCache = new Map();
96function getParsedCommandLine(compiler, loaderOptions, configFilePath) {
97 const result = compiler.getParsedCommandLineOfConfigFile(configFilePath, getCompilerOptionsToExtend(compiler, loaderOptions, path.dirname(configFilePath), configFilePath), Object.assign(Object.assign({}, compiler.sys), { useCaseSensitiveFileNames: utils_1.useCaseSensitiveFileNames(compiler, loaderOptions),
98 // eslint-disable-next-line @typescript-eslint/no-empty-function
99 onUnRecoverableConfigFileDiagnostic: () => { } }), extendedConfigCache);
100 if (result) {
101 result.options = compilerSetup_1.getCompilerOptions(result);
102 }
103 return result;
104}
105exports.getParsedCommandLine = getParsedCommandLine;
106function getCompilerOptionsToExtend(compiler, loaderOptions, basePath, configFileName) {
107 return compiler.convertCompilerOptionsFromJson(loaderOptions.compilerOptions, basePath, configFileName).options;
108}
109//# sourceMappingURL=config.js.map
\No newline at end of file