refactor(ngcc): move PathMappings to separate file to avoid circular dependency (#36626)

Now that `ngcc/src/ngcc_options` imports `FileWriter` type, there is a
circular dependency detected by the `ts-circular-deps:check` lint check:

```
ngcc/src/ngcc_options.ts
  → ngcc/src/writing/file_writer.ts
  → ngcc/src/packages/entry_point_bundle.ts
  → ngcc/src/ngcc_options.ts
```

This commit moves the `PathMappings` type (and related helpers) to a
separate file to avoid the circular dependency.

NOTE:
The circular dependency was only with taking types into account. There
was no circular dependency for the actual (JS) code.

PR Close #36626
This commit is contained in:
George Kalpakas
2020-04-29 21:28:29 +03:00
committed by Andrew Kushnir
parent 4779c4b94a
commit 45c09416ed
13 changed files with 46 additions and 35 deletions

View File

@ -5,12 +5,13 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {absoluteFrom, AbsoluteFsPath, FileSystem, getFileSystem, resolve} from '../../src/ngtsc/file_system';
import {absoluteFrom, AbsoluteFsPath, FileSystem, getFileSystem} from '../../src/ngtsc/file_system';
import {ParsedConfiguration, readConfiguration} from '../../src/perform_compile';
import {ConsoleLogger} from './logging/console_logger';
import {Logger, LogLevel} from './logging/logger';
import {SUPPORTED_FORMAT_PROPERTIES} from './packages/entry_point';
import {getPathMappingsFromTsConfig, PathMappings} from './path_mappings';
import {FileWriter} from './writing/file_writer';
import {InPlaceFileWriter} from './writing/in_place_file_writer';
import {NewEntryPointFileWriter} from './writing/new_entry_point_file_writer';
@ -135,25 +136,6 @@ export type AsyncNgccOptions = Omit<SyncNgccOptions, 'async'>&{async: true};
*/
export type NgccOptions = AsyncNgccOptions|SyncNgccOptions;
export type PathMappings = {
baseUrl: string,
paths: {[key: string]: string[]}
};
/**
* If `pathMappings` is not provided directly, then try getting it from `tsConfig`, if available.
*/
export function getPathMappingsFromTsConfig(
tsConfig: ParsedConfiguration|null, projectPath: AbsoluteFsPath): PathMappings|undefined {
if (tsConfig !== null && tsConfig.options.baseUrl !== undefined &&
tsConfig.options.paths !== undefined) {
return {
baseUrl: resolve(projectPath, tsConfig.options.baseUrl),
paths: tsConfig.options.paths,
};
}
}
export type OptionalNgccOptionKeys = 'targetEntryPointPath'|'tsConfigPath'|'pathMappings';
export type RequiredNgccOptions = Required<Omit<NgccOptions, OptionalNgccOptionKeys>>;
export type OptionalNgccOptions = Pick<NgccOptions, OptionalNgccOptionKeys>;
@ -183,7 +165,7 @@ export function getSharedSetup(options: NgccOptions): SharedSetup&RequiredNgccOp
compileAllFormats = true,
createNewEntryPointFormats = false,
logger = new ConsoleLogger(LogLevel.info),
pathMappings,
pathMappings = getPathMappingsFromTsConfig(tsConfig, projectPath),
async = false,
errorOnFailedEntryPoint = false,
enableI18nLegacyMessageIdFormat = true,
@ -191,9 +173,7 @@ export function getSharedSetup(options: NgccOptions): SharedSetup&RequiredNgccOp
tsConfigPath,
} = options;
pathMappings = options.pathMappings || getPathMappingsFromTsConfig(tsConfig, projectPath);
if (!!options.targetEntryPointPath) {
if (!!targetEntryPointPath) {
// targetEntryPointPath forces us to error if an entry-point fails.
errorOnFailedEntryPoint = true;
}