|
5 | 5 | * Use of this source code is governed by an MIT-style license that can be
|
6 | 6 | * found in the LICENSE file at https://angular.io/license
|
7 | 7 | */
|
8 |
| -// TODO: cleanup this file, it's copied as is from Angular CLI. |
| 8 | + |
| 9 | +import { ParsedConfiguration } from '@angular/compiler-cli'; |
9 | 10 | import * as path from 'path';
|
10 | 11 |
|
11 |
| -export function readTsconfig(tsconfigPath: string) { |
12 |
| - // build-angular has a peer dependency on typescript |
13 |
| - const projectTs = require('typescript') as typeof import('typescript'); |
14 |
| - const configResult = projectTs.readConfigFile(tsconfigPath, projectTs.sys.readFile); |
15 |
| - const tsConfig = projectTs.parseJsonConfigFileContent(configResult.config, projectTs.sys, |
16 |
| - path.dirname(tsconfigPath), undefined, tsconfigPath); |
| 12 | +/** |
| 13 | + * Reads and parses a given TsConfig file. |
| 14 | + * |
| 15 | + * @param tsconfigPath - An absolute or relative path from 'workspaceRoot' of the tsconfig file. |
| 16 | + * @param workspaceRoot - workspaceRoot root location when provided |
| 17 | + * it will resolve 'tsconfigPath' from this path. |
| 18 | + */ |
| 19 | +export function readTsconfig(tsconfigPath: string, workspaceRoot?: string): ParsedConfiguration { |
| 20 | + const tsConfigFullPath = workspaceRoot |
| 21 | + ? path.resolve(workspaceRoot, tsconfigPath) |
| 22 | + : tsconfigPath; |
| 23 | + |
| 24 | + // We use 'ng' instead of 'ts' here because 'ts' is not aware of 'angularCompilerOptions' |
| 25 | + // and will not merged them if they are at un upper level tsconfig file when using `extends`. |
| 26 | + const ng: typeof import('@angular/compiler-cli') = require('@angular/compiler-cli'); |
17 | 27 |
|
18 |
| - if (tsConfig.errors.length > 0) { |
19 |
| - throw new Error( |
20 |
| - `Errors found while reading ${tsconfigPath}:\n ${ |
21 |
| - tsConfig.errors.map(e => e.messageText).join('\n ') |
22 |
| - }`, |
23 |
| - ); |
| 28 | + const configResult = ng.readConfiguration(tsConfigFullPath); |
| 29 | + if (configResult.errors && configResult.errors.length) { |
| 30 | + throw new Error(ng.formatDiagnostics(configResult.errors)); |
24 | 31 | }
|
25 | 32 |
|
26 |
| - return tsConfig; |
| 33 | + return configResult; |
27 | 34 | }
|
0 commit comments