/
tsconfig.ts
123 lines (106 loc) · 3.92 KB
/
tsconfig.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
import type { CompilerOptions, ParsedConfiguration } from '@angular/compiler-cli';
import * as path from 'path';
import ts from 'typescript';
import { EntryPointNode } from '../ng-package/nodes';
import { ngCompilerCli } from '../utils/load-esm';
import * as log from '../utils/log';
export const defaultTsConfigPath = path.join(__dirname, 'conf', 'tsconfig.ngc.json');
/**
* Reads the default TypeScript configuration.
*/
async function readDefaultTsConfig(fileName = defaultTsConfigPath): Promise<ParsedConfiguration> {
// these options are mandatory
const extraOptions: CompilerOptions = {
target: ts.ScriptTarget.ES2022,
// sourcemaps
sourceMap: false,
inlineSources: true,
inlineSourceMap: true,
outDir: '',
declaration: true,
// ng compiler
enableResourceInlining: true,
// these are required to set the appropriate EmitFlags
flatModuleId: 'AUTOGENERATED',
flatModuleOutFile: 'AUTOGENERATED',
};
const { readConfiguration } = await ngCompilerCli();
return readConfiguration(fileName, extraOptions);
}
/**
* Creates a parsed TypeScript configuration object.
*
* @param values File path or parsed configuration.
*/
async function createDefaultTsConfig(values?: ParsedConfiguration | string): Promise<ParsedConfiguration> {
if (!values) {
return readDefaultTsConfig();
} else if (typeof values === 'string') {
return readDefaultTsConfig(values);
} else {
return values;
}
}
/**
* Initializes TypeScript Compiler options and Angular Compiler options by overriding the
* default config with entry point-specific values.
*/
export async function initializeTsConfig(
defaultTsConfig: ParsedConfiguration | string | undefined,
entryPoints: EntryPointNode[],
): Promise<void> {
const defaultTsConfigParsed = await createDefaultTsConfig(defaultTsConfig);
if (defaultTsConfigParsed.errors.length > 0) {
const { formatDiagnostics } = await ngCompilerCli();
throw new Error(formatDiagnostics(defaultTsConfigParsed.errors));
}
entryPoints.forEach(currentEntryPoint => {
const { entryPoint } = currentEntryPoint.data;
log.debug(`Initializing tsconfig for ${entryPoint.moduleId}`);
const basePath = path.dirname(entryPoint.entryFilePath);
// Resolve defaults from DI token and create a deep copy of the defaults
const tsConfig: ParsedConfiguration = JSON.parse(JSON.stringify(defaultTsConfigParsed));
const overrideOptions: CompilerOptions = {
flatModuleId: entryPoint.moduleId,
flatModuleOutFile: `${entryPoint.flatModuleFile}.js`,
basePath,
rootDir: basePath,
sourceRoot: '',
};
tsConfig.rootNames = [entryPoint.entryFilePath];
tsConfig.options = { ...tsConfig.options, ...overrideOptions };
currentEntryPoint.data.tsConfig = tsConfig;
});
}
/**
* Set the paths for entrypoint dependencies.
*
* This doesn't mutate the object.
*
* @param parsedTsConfig - A parsed tsconfig
* @param entryPoints - A list of entryPoints
* @param pointToSource Point the path mapping to either the source code or emitted declarations.
* Typically for analysis one should point to the source files while for a compilation once should use the emitted declarations
*/
export function setDependenciesTsConfigPaths(
parsedTsConfig: ParsedConfiguration,
entryPoints: EntryPointNode[],
pointToSource = false,
): ParsedConfiguration {
const tsConfig = JSON.parse(JSON.stringify(parsedTsConfig));
// Add paths mappings for dependencies
if (!tsConfig.options.paths) {
tsConfig.options.paths = {};
}
for (const dep of entryPoints) {
const { entryPoint } = dep.data;
const { moduleId, destinationFiles, entryFilePath } = entryPoint;
const mappedPath = [pointToSource ? entryFilePath : destinationFiles.declarations];
if (!tsConfig.options.paths[moduleId]) {
tsConfig.options.paths[moduleId] = mappedPath;
} else {
tsConfig.options.paths[moduleId].unshift(...mappedPath);
}
}
return tsConfig;
}