refactor(ngcc): moved shared setup into a single function (#36637)
The `main.ts` and `worker.ts` had duplicate logic, which has now been moved to a single function called `getSharedSetup()`. PR Close #36637
This commit is contained in:
		
							parent
							
								
									bb944eecd6
								
							
						
					
					
						commit
						c332d4d916
					
				| @ -7,13 +7,12 @@ | |||||||
|  */ |  */ | ||||||
| import {CachedFileSystem, NodeJSFileSystem, setFileSystem} from '../src/ngtsc/file_system'; | import {CachedFileSystem, NodeJSFileSystem, setFileSystem} from '../src/ngtsc/file_system'; | ||||||
| 
 | 
 | ||||||
| import {AsyncNgccOptions, NgccOptions, SyncNgccOptions} from './src/command_line_options'; |  | ||||||
| import {mainNgcc} from './src/main'; | import {mainNgcc} from './src/main'; | ||||||
|  | import {AsyncNgccOptions, NgccOptions, SyncNgccOptions} from './src/ngcc_options'; | ||||||
| 
 | 
 | ||||||
| export {AsyncNgccOptions, NgccOptions, SyncNgccOptions} from './src/command_line_options'; |  | ||||||
| export {ConsoleLogger} from './src/logging/console_logger'; | export {ConsoleLogger} from './src/logging/console_logger'; | ||||||
| export {Logger, LogLevel} from './src/logging/logger'; | export {Logger, LogLevel} from './src/logging/logger'; | ||||||
| export {PathMappings} from './src/utils'; | export {AsyncNgccOptions, NgccOptions, PathMappings, SyncNgccOptions} from './src/ngcc_options'; | ||||||
| 
 | 
 | ||||||
| export function process(options: AsyncNgccOptions): Promise<void>; | export function process(options: AsyncNgccOptions): Promise<void>; | ||||||
| export function process(options: SyncNgccOptions): void; | export function process(options: SyncNgccOptions): void; | ||||||
|  | |||||||
| @ -8,129 +8,10 @@ | |||||||
|  */ |  */ | ||||||
| import * as yargs from 'yargs'; | import * as yargs from 'yargs'; | ||||||
| 
 | 
 | ||||||
| import {resolve, setFileSystem, CachedFileSystem, NodeJSFileSystem, FileSystem} from '../../src/ngtsc/file_system'; | import {resolve, setFileSystem, CachedFileSystem, NodeJSFileSystem} from '../../src/ngtsc/file_system'; | ||||||
| import {ConsoleLogger} from './logging/console_logger'; | import {ConsoleLogger} from './logging/console_logger'; | ||||||
| import {LogLevel, Logger} from './logging/logger'; | import {LogLevel} from './logging/logger'; | ||||||
| import {PathMappings} from './utils'; | import {NgccOptions} from './ngcc_options'; | ||||||
| 
 |  | ||||||
| /** |  | ||||||
|  * The options to configure the ngcc compiler for synchronous execution. |  | ||||||
|  */ |  | ||||||
| export interface SyncNgccOptions { |  | ||||||
|   /** The absolute path to the `node_modules` folder that contains the packages to process. */ |  | ||||||
|   basePath: string; |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * The path to the primary package to be processed. If not absolute then it must be relative to |  | ||||||
|    * `basePath`. |  | ||||||
|    * |  | ||||||
|    * All its dependencies will need to be processed too. |  | ||||||
|    * |  | ||||||
|    * If this property is provided then `errorOnFailedEntryPoint` is forced to true. |  | ||||||
|    */ |  | ||||||
|   targetEntryPointPath?: string; |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * Which entry-point properties in the package.json to consider when processing an entry-point. |  | ||||||
|    * Each property should hold a path to the particular bundle format for the entry-point. |  | ||||||
|    * Defaults to all the properties in the package.json. |  | ||||||
|    */ |  | ||||||
|   propertiesToConsider?: string[]; |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * Whether to process all formats specified by (`propertiesToConsider`)  or to stop processing |  | ||||||
|    * this entry-point at the first matching format. Defaults to `true`. |  | ||||||
|    */ |  | ||||||
|   compileAllFormats?: boolean; |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * Whether to create new entry-points bundles rather than overwriting the original files. |  | ||||||
|    */ |  | ||||||
|   createNewEntryPointFormats?: boolean; |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * Provide a logger that will be called with log messages. |  | ||||||
|    */ |  | ||||||
|   logger?: Logger; |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * Paths mapping configuration (`paths` and `baseUrl`), as found in `ts.CompilerOptions`. |  | ||||||
|    * These are used to resolve paths to locally built Angular libraries. |  | ||||||
|    * |  | ||||||
|    * Note that `pathMappings` specified here take precedence over any `pathMappings` loaded from a |  | ||||||
|    * TS config file. |  | ||||||
|    */ |  | ||||||
|   pathMappings?: PathMappings; |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * Provide a file-system service that will be used by ngcc for all file interactions. |  | ||||||
|    */ |  | ||||||
|   fileSystem?: FileSystem; |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * Whether the compilation should run and return asynchronously. Allowing asynchronous execution |  | ||||||
|    * may speed up the compilation by utilizing multiple CPU cores (if available). |  | ||||||
|    * |  | ||||||
|    * Default: `false` (i.e. run synchronously) |  | ||||||
|    */ |  | ||||||
|   async?: false; |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * Set to true in order to terminate immediately with an error code if an entry-point fails to be |  | ||||||
|    * processed. |  | ||||||
|    * |  | ||||||
|    * If `targetEntryPointPath` is provided then this property is always true and cannot be |  | ||||||
|    * changed. Otherwise the default is false. |  | ||||||
|    * |  | ||||||
|    * When set to false, ngcc will continue to process entry-points after a failure. In which case it |  | ||||||
|    * will log an error and resume processing other entry-points. |  | ||||||
|    */ |  | ||||||
|   errorOnFailedEntryPoint?: boolean; |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * Render `$localize` messages with legacy format ids. |  | ||||||
|    * |  | ||||||
|    * The default value is `true`. Only set this to `false` if you do not want legacy message ids to |  | ||||||
|    * be rendered. For example, if you are not using legacy message ids in your translation files |  | ||||||
|    * AND are not doing compile-time inlining of translations, in which case the extra message ids |  | ||||||
|    * would add unwanted size to the final source bundle. |  | ||||||
|    * |  | ||||||
|    * It is safe to leave this set to true if you are doing compile-time inlining because the extra |  | ||||||
|    * legacy message ids will all be stripped during translation. |  | ||||||
|    */ |  | ||||||
|   enableI18nLegacyMessageIdFormat?: boolean; |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * Whether to invalidate any entry-point manifest file that is on disk. Instead, walk the |  | ||||||
|    * directory tree looking for entry-points, and then write a new entry-point manifest, if |  | ||||||
|    * possible. |  | ||||||
|    * |  | ||||||
|    * Default: `false` (i.e. the manifest will be used if available) |  | ||||||
|    */ |  | ||||||
|   invalidateEntryPointManifest?: boolean; |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * An absolute path to a TS config file (e.g. `tsconfig.json`) or a directory containing one, that |  | ||||||
|    * will be used to configure module resolution with things like path mappings, if not specified |  | ||||||
|    * explicitly via the `pathMappings` property to `mainNgcc`. |  | ||||||
|    * |  | ||||||
|    * If `undefined`, ngcc will attempt to load a `tsconfig.json` file from the directory above the |  | ||||||
|    * `basePath`. |  | ||||||
|    * |  | ||||||
|    * If `null`, ngcc will not attempt to load any TS config file at all. |  | ||||||
|    */ |  | ||||||
|   tsConfigPath?: string|null; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| /** |  | ||||||
|  * The options to configure the ngcc compiler for asynchronous execution. |  | ||||||
|  */ |  | ||||||
| export type AsyncNgccOptions = Omit<SyncNgccOptions, 'async'>&{async: true}; |  | ||||||
| 
 |  | ||||||
| /** |  | ||||||
|  * The options to configure the ngcc compiler. |  | ||||||
|  */ |  | ||||||
| export type NgccOptions = AsyncNgccOptions|SyncNgccOptions; |  | ||||||
| 
 | 
 | ||||||
| export function parseCommandLineOptions(args: string[]): NgccOptions { | export function parseCommandLineOptions(args: string[]): NgccOptions { | ||||||
|   const options = |   const options = | ||||||
|  | |||||||
| @ -6,7 +6,7 @@ | |||||||
|  * found in the LICENSE file at https://angular.io/license
 |  * found in the LICENSE file at https://angular.io/license
 | ||||||
|  */ |  */ | ||||||
| import {AbsoluteFsPath, FileSystem} from '../../../src/ngtsc/file_system'; | import {AbsoluteFsPath, FileSystem} from '../../../src/ngtsc/file_system'; | ||||||
| import {PathMappings} from '../utils'; | import {PathMappings} from '../ngcc_options'; | ||||||
| import {EsmDependencyHost} from './esm_dependency_host'; | import {EsmDependencyHost} from './esm_dependency_host'; | ||||||
| import {ModuleResolver} from './module_resolver'; | import {ModuleResolver} from './module_resolver'; | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -6,7 +6,8 @@ | |||||||
|  * found in the LICENSE file at https://angular.io/license
 |  * found in the LICENSE file at https://angular.io/license
 | ||||||
|  */ |  */ | ||||||
| import {absoluteFrom, AbsoluteFsPath, dirname, FileSystem, isRoot, join, resolve} from '../../../src/ngtsc/file_system'; | import {absoluteFrom, AbsoluteFsPath, dirname, FileSystem, isRoot, join, resolve} from '../../../src/ngtsc/file_system'; | ||||||
| import {isRelativePath, PathMappings, resolveFileWithPostfixes} from '../utils'; | import {PathMappings} from '../ngcc_options'; | ||||||
|  | import {isRelativePath, resolveFileWithPostfixes} from '../utils'; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * This is a very cut-down implementation of the TypeScript module resolution strategy. |  * This is a very cut-down implementation of the TypeScript module resolution strategy. | ||||||
|  | |||||||
| @ -9,10 +9,10 @@ import {AbsoluteFsPath, FileSystem, PathSegment} from '../../../src/ngtsc/file_s | |||||||
| import {EntryPointWithDependencies} from '../dependencies/dependency_host'; | import {EntryPointWithDependencies} from '../dependencies/dependency_host'; | ||||||
| import {DependencyResolver, SortedEntryPointsInfo} from '../dependencies/dependency_resolver'; | import {DependencyResolver, SortedEntryPointsInfo} from '../dependencies/dependency_resolver'; | ||||||
| import {Logger} from '../logging/logger'; | import {Logger} from '../logging/logger'; | ||||||
|  | import {PathMappings} from '../ngcc_options'; | ||||||
| import {NgccConfiguration} from '../packages/configuration'; | import {NgccConfiguration} from '../packages/configuration'; | ||||||
| import {getEntryPointInfo, INCOMPATIBLE_ENTRY_POINT, NO_ENTRY_POINT} from '../packages/entry_point'; | import {getEntryPointInfo, INCOMPATIBLE_ENTRY_POINT, NO_ENTRY_POINT} from '../packages/entry_point'; | ||||||
| import {EntryPointManifest} from '../packages/entry_point_manifest'; | import {EntryPointManifest} from '../packages/entry_point_manifest'; | ||||||
| import {PathMappings} from '../utils'; |  | ||||||
| import {NGCC_DIRECTORY} from '../writing/new_entry_point_file_writer'; | import {NGCC_DIRECTORY} from '../writing/new_entry_point_file_writer'; | ||||||
| 
 | 
 | ||||||
| import {EntryPointFinder} from './interface'; | import {EntryPointFinder} from './interface'; | ||||||
|  | |||||||
| @ -9,10 +9,10 @@ import {AbsoluteFsPath, FileSystem, join, PathSegment, relative, relativeFrom} f | |||||||
| import {EntryPointWithDependencies} from '../dependencies/dependency_host'; | import {EntryPointWithDependencies} from '../dependencies/dependency_host'; | ||||||
| import {DependencyResolver, SortedEntryPointsInfo} from '../dependencies/dependency_resolver'; | import {DependencyResolver, SortedEntryPointsInfo} from '../dependencies/dependency_resolver'; | ||||||
| import {Logger} from '../logging/logger'; | import {Logger} from '../logging/logger'; | ||||||
|  | import {PathMappings} from '../ngcc_options'; | ||||||
| import {hasBeenProcessed} from '../packages/build_marker'; | import {hasBeenProcessed} from '../packages/build_marker'; | ||||||
| import {NgccConfiguration} from '../packages/configuration'; | import {NgccConfiguration} from '../packages/configuration'; | ||||||
| import {EntryPoint, EntryPointJsonProperty, getEntryPointInfo, INCOMPATIBLE_ENTRY_POINT, NO_ENTRY_POINT} from '../packages/entry_point'; | import {EntryPoint, EntryPointJsonProperty, getEntryPointInfo, INCOMPATIBLE_ENTRY_POINT, NO_ENTRY_POINT} from '../packages/entry_point'; | ||||||
| import {PathMappings} from '../utils'; |  | ||||||
| 
 | 
 | ||||||
| import {EntryPointFinder} from './interface'; | import {EntryPointFinder} from './interface'; | ||||||
| import {getBasePaths} from './utils'; | import {getBasePaths} from './utils'; | ||||||
|  | |||||||
| @ -7,7 +7,7 @@ | |||||||
|  */ |  */ | ||||||
| import {AbsoluteFsPath, getFileSystem, relative, resolve} from '../../../src/ngtsc/file_system'; | import {AbsoluteFsPath, getFileSystem, relative, resolve} from '../../../src/ngtsc/file_system'; | ||||||
| import {Logger} from '../logging/logger'; | import {Logger} from '../logging/logger'; | ||||||
| import {PathMappings} from '../utils'; | import {PathMappings} from '../ngcc_options'; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Extract all the base-paths that we need to search for entry-points. |  * Extract all the base-paths that we need to search for entry-points. | ||||||
|  | |||||||
| @ -9,12 +9,11 @@ | |||||||
| 
 | 
 | ||||||
| import * as cluster from 'cluster'; | import * as cluster from 'cluster'; | ||||||
| 
 | 
 | ||||||
| import {absoluteFrom, CachedFileSystem, getFileSystem, NodeJSFileSystem, setFileSystem} from '../../../../src/ngtsc/file_system'; | import {CachedFileSystem, NodeJSFileSystem, setFileSystem} from '../../../../src/ngtsc/file_system'; | ||||||
| import {readConfiguration} from '../../../../src/perform_compile'; |  | ||||||
| import {parseCommandLineOptions} from '../../command_line_options'; | import {parseCommandLineOptions} from '../../command_line_options'; | ||||||
| import {ConsoleLogger} from '../../logging/console_logger'; | import {ConsoleLogger} from '../../logging/console_logger'; | ||||||
| import {Logger, LogLevel} from '../../logging/logger'; | import {Logger, LogLevel} from '../../logging/logger'; | ||||||
| import {getPathMappingsFromTsConfig} from '../../utils'; | import {getSharedSetup} from '../../ngcc_options'; | ||||||
| import {CreateCompileFn} from '../api'; | import {CreateCompileFn} from '../api'; | ||||||
| import {getCreateCompileFn} from '../create_compile_function'; | import {getCreateCompileFn} from '../create_compile_function'; | ||||||
| import {stringifyTask} from '../tasks/utils'; | import {stringifyTask} from '../tasks/utils'; | ||||||
| @ -25,34 +24,21 @@ import {sendMessageToMaster} from './utils'; | |||||||
| 
 | 
 | ||||||
| // Cluster worker entry point
 | // Cluster worker entry point
 | ||||||
| if (require.main === module) { | if (require.main === module) { | ||||||
|  |   (async () => { | ||||||
|     process.title = 'ngcc (worker)'; |     process.title = 'ngcc (worker)'; | ||||||
|  | 
 | ||||||
|  |     try { | ||||||
|       setFileSystem(new CachedFileSystem(new NodeJSFileSystem())); |       setFileSystem(new CachedFileSystem(new NodeJSFileSystem())); | ||||||
|   let { | 
 | ||||||
|     basePath, |       const { | ||||||
|     targetEntryPointPath, |  | ||||||
|         createNewEntryPointFormats = false, |         createNewEntryPointFormats = false, | ||||||
|         logger = new ConsoleLogger(LogLevel.info), |         logger = new ConsoleLogger(LogLevel.info), | ||||||
|         pathMappings, |         pathMappings, | ||||||
|         errorOnFailedEntryPoint = false, |         errorOnFailedEntryPoint = false, | ||||||
|         enableI18nLegacyMessageIdFormat = true, |         enableI18nLegacyMessageIdFormat = true, | ||||||
|     tsConfigPath |         fileSystem, | ||||||
|   } = parseCommandLineOptions(process.argv.slice(2)); |         tsConfig | ||||||
|   (async () => { |       } = getSharedSetup(parseCommandLineOptions(process.argv.slice(2))); | ||||||
|     try { |  | ||||||
|       if (!!targetEntryPointPath) { |  | ||||||
|         // targetEntryPointPath forces us to error if an entry-point fails.
 |  | ||||||
|         errorOnFailedEntryPoint = true; |  | ||||||
|       } |  | ||||||
| 
 |  | ||||||
|       const fileSystem = getFileSystem(); |  | ||||||
|       const absBasePath = absoluteFrom(basePath); |  | ||||||
|       const projectPath = fileSystem.dirname(absBasePath); |  | ||||||
|       const tsConfig = |  | ||||||
|           tsConfigPath !== null ? readConfiguration(tsConfigPath || projectPath) : null; |  | ||||||
| 
 |  | ||||||
|       if (pathMappings === undefined) { |  | ||||||
|         pathMappings = getPathMappingsFromTsConfig(tsConfig, projectPath); |  | ||||||
|       } |  | ||||||
| 
 | 
 | ||||||
|       // NOTE: To avoid file corruption, `ngcc` invocation only creates _one_ instance of
 |       // NOTE: To avoid file corruption, `ngcc` invocation only creates _one_ instance of
 | ||||||
|       // `PackageJsonUpdater` that actually writes to disk (across all processes).
 |       // `PackageJsonUpdater` that actually writes to disk (across all processes).
 | ||||||
| @ -68,7 +54,7 @@ if (require.main === module) { | |||||||
|       process.exitCode = 0; |       process.exitCode = 0; | ||||||
|     } catch (e) { |     } catch (e) { | ||||||
|       console.error(e.stack || e.message); |       console.error(e.stack || e.message); | ||||||
|       process.exitCode = 1; |       process.exit(1); | ||||||
|     } |     } | ||||||
|   })(); |   })(); | ||||||
| } | } | ||||||
|  | |||||||
| @ -12,9 +12,9 @@ import {replaceTsWithNgInErrors} from '../../../src/ngtsc/diagnostics'; | |||||||
| import {FileSystem} from '../../../src/ngtsc/file_system'; | import {FileSystem} from '../../../src/ngtsc/file_system'; | ||||||
| import {ParsedConfiguration} from '../../../src/perform_compile'; | import {ParsedConfiguration} from '../../../src/perform_compile'; | ||||||
| import {Logger} from '../logging/logger'; | import {Logger} from '../logging/logger'; | ||||||
|  | import {PathMappings} from '../ngcc_options'; | ||||||
| import {getEntryPointFormat} from '../packages/entry_point'; | import {getEntryPointFormat} from '../packages/entry_point'; | ||||||
| import {makeEntryPointBundle} from '../packages/entry_point_bundle'; | import {makeEntryPointBundle} from '../packages/entry_point_bundle'; | ||||||
| import {PathMappings} from '../utils'; |  | ||||||
| import {FileWriter} from '../writing/file_writer'; | import {FileWriter} from '../writing/file_writer'; | ||||||
| import {InPlaceFileWriter} from '../writing/in_place_file_writer'; | import {InPlaceFileWriter} from '../writing/in_place_file_writer'; | ||||||
| import {NewEntryPointFileWriter} from '../writing/new_entry_point_file_writer'; | import {NewEntryPointFileWriter} from '../writing/new_entry_point_file_writer'; | ||||||
|  | |||||||
| @ -10,10 +10,8 @@ | |||||||
| 
 | 
 | ||||||
| import * as os from 'os'; | import * as os from 'os'; | ||||||
| 
 | 
 | ||||||
| import {readConfiguration} from '../..'; | import {AbsoluteFsPath, FileSystem, resolve} from '../../src/ngtsc/file_system'; | ||||||
| import {absoluteFrom, AbsoluteFsPath, dirname, FileSystem, getFileSystem, resolve} from '../../src/ngtsc/file_system'; |  | ||||||
| 
 | 
 | ||||||
| import {AsyncNgccOptions, NgccOptions, SyncNgccOptions} from './command_line_options'; |  | ||||||
| import {CommonJsDependencyHost} from './dependencies/commonjs_dependency_host'; | import {CommonJsDependencyHost} from './dependencies/commonjs_dependency_host'; | ||||||
| import {DependencyResolver} from './dependencies/dependency_resolver'; | import {DependencyResolver} from './dependencies/dependency_resolver'; | ||||||
| import {DtsDependencyHost} from './dependencies/dts_dependency_host'; | import {DtsDependencyHost} from './dependencies/dts_dependency_host'; | ||||||
| @ -33,12 +31,11 @@ import {composeTaskCompletedCallbacks, createLogErrorHandler, createMarkAsProces | |||||||
| import {AsyncLocker} from './locking/async_locker'; | import {AsyncLocker} from './locking/async_locker'; | ||||||
| import {LockFileWithChildProcess} from './locking/lock_file_with_child_process'; | import {LockFileWithChildProcess} from './locking/lock_file_with_child_process'; | ||||||
| import {SyncLocker} from './locking/sync_locker'; | import {SyncLocker} from './locking/sync_locker'; | ||||||
| import {ConsoleLogger} from './logging/console_logger'; | import {Logger} from './logging/logger'; | ||||||
| import {Logger, LogLevel} from './logging/logger'; | import {AsyncNgccOptions, getSharedSetup, NgccOptions, PathMappings, SyncNgccOptions} from './ngcc_options'; | ||||||
| import {NgccConfiguration} from './packages/configuration'; | import {NgccConfiguration} from './packages/configuration'; | ||||||
| import {EntryPointJsonProperty, SUPPORTED_FORMAT_PROPERTIES} from './packages/entry_point'; | import {EntryPointJsonProperty, SUPPORTED_FORMAT_PROPERTIES} from './packages/entry_point'; | ||||||
| import {EntryPointManifest, InvalidatingEntryPointManifest} from './packages/entry_point_manifest'; | import {EntryPointManifest, InvalidatingEntryPointManifest} from './packages/entry_point_manifest'; | ||||||
| import {getPathMappingsFromTsConfig, PathMappings} from './utils'; |  | ||||||
| import {DirectPackageJsonUpdater, PackageJsonUpdater} from './writing/package_json_updater'; | import {DirectPackageJsonUpdater, PackageJsonUpdater} from './writing/package_json_updater'; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
| @ -51,41 +48,26 @@ import {DirectPackageJsonUpdater, PackageJsonUpdater} from './writing/package_js | |||||||
|  */ |  */ | ||||||
| export function mainNgcc(options: AsyncNgccOptions): Promise<void>; | export function mainNgcc(options: AsyncNgccOptions): Promise<void>; | ||||||
| export function mainNgcc(options: SyncNgccOptions): void; | export function mainNgcc(options: SyncNgccOptions): void; | ||||||
| export function mainNgcc({ | export function mainNgcc(options: NgccOptions): void|Promise<void> { | ||||||
|  |   const { | ||||||
|     basePath, |     basePath, | ||||||
|     targetEntryPointPath, |     targetEntryPointPath, | ||||||
|   propertiesToConsider = SUPPORTED_FORMAT_PROPERTIES, |     propertiesToConsider, | ||||||
|   compileAllFormats = true, |     compileAllFormats, | ||||||
|   createNewEntryPointFormats = false, |     createNewEntryPointFormats, | ||||||
|   logger = new ConsoleLogger(LogLevel.info), |     logger, | ||||||
|     pathMappings, |     pathMappings, | ||||||
|   async = false, |     async, | ||||||
|   errorOnFailedEntryPoint = false, |     errorOnFailedEntryPoint, | ||||||
|   enableI18nLegacyMessageIdFormat = true, |     enableI18nLegacyMessageIdFormat, | ||||||
|   invalidateEntryPointManifest = false, |     invalidateEntryPointManifest, | ||||||
|   tsConfigPath |     fileSystem, | ||||||
| }: NgccOptions): void|Promise<void> { |     absBasePath, | ||||||
|   if (!!targetEntryPointPath) { |     projectPath, | ||||||
|     // targetEntryPointPath forces us to error if an entry-point fails.
 |     tsConfig | ||||||
|     errorOnFailedEntryPoint = true; |   } = getSharedSetup(options); | ||||||
|   } |  | ||||||
| 
 | 
 | ||||||
|   // Execute in parallel, if async execution is acceptable and there are more than 1 CPU cores.
 |  | ||||||
|   const inParallel = async && (os.cpus().length > 1); |  | ||||||
| 
 |  | ||||||
|   // Instantiate common utilities that are always used.
 |  | ||||||
|   // NOTE: Avoid eagerly instantiating anything that might not be used when running sync/async or in
 |  | ||||||
|   //       master/worker process.
 |  | ||||||
|   const fileSystem = getFileSystem(); |  | ||||||
|   const absBasePath = absoluteFrom(basePath); |  | ||||||
|   const projectPath = dirname(absBasePath); |  | ||||||
|   const config = new NgccConfiguration(fileSystem, projectPath); |   const config = new NgccConfiguration(fileSystem, projectPath); | ||||||
|   const tsConfig = tsConfigPath !== null ? readConfiguration(tsConfigPath || projectPath) : null; |  | ||||||
| 
 |  | ||||||
|   if (pathMappings === undefined) { |  | ||||||
|     pathMappings = getPathMappingsFromTsConfig(tsConfig, projectPath); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   const dependencyResolver = getDependencyResolver(fileSystem, logger, config, pathMappings); |   const dependencyResolver = getDependencyResolver(fileSystem, logger, config, pathMappings); | ||||||
|   const entryPointManifest = invalidateEntryPointManifest ? |   const entryPointManifest = invalidateEntryPointManifest ? | ||||||
|       new InvalidatingEntryPointManifest(fileSystem, config, logger) : |       new InvalidatingEntryPointManifest(fileSystem, config, logger) : | ||||||
| @ -104,12 +86,16 @@ export function mainNgcc({ | |||||||
|     return; |     return; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   const pkgJsonUpdater = new DirectPackageJsonUpdater(fileSystem); |   // Execute in parallel, if async execution is acceptable and there are more than 1 CPU cores.
 | ||||||
|  |   const inParallel = async && (os.cpus().length > 1); | ||||||
| 
 | 
 | ||||||
|   const analyzeEntryPoints = getAnalyzeEntryPointsFn( |   const analyzeEntryPoints = getAnalyzeEntryPointsFn( | ||||||
|       logger, finder, fileSystem, supportedPropertiesToConsider, compileAllFormats, |       logger, finder, fileSystem, supportedPropertiesToConsider, compileAllFormats, | ||||||
|       propertiesToConsider, inParallel); |       propertiesToConsider, inParallel); | ||||||
| 
 | 
 | ||||||
|  |   // Create an updater that will actually write to disk. In
 | ||||||
|  |   const pkgJsonUpdater = new DirectPackageJsonUpdater(fileSystem); | ||||||
|  | 
 | ||||||
|   // The function for creating the `compile()` function.
 |   // The function for creating the `compile()` function.
 | ||||||
|   const createCompileFn = getCreateCompileFn( |   const createCompileFn = getCreateCompileFn( | ||||||
|       fileSystem, logger, pkgJsonUpdater, createNewEntryPointFormats, errorOnFailedEntryPoint, |       fileSystem, logger, pkgJsonUpdater, createNewEntryPointFormats, errorOnFailedEntryPoint, | ||||||
|  | |||||||
							
								
								
									
										214
									
								
								packages/compiler-cli/ngcc/src/ngcc_options.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										214
									
								
								packages/compiler-cli/ngcc/src/ngcc_options.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,214 @@ | |||||||
|  | /** | ||||||
|  |  * @license | ||||||
|  |  * Copyright Google Inc. All Rights Reserved. | ||||||
|  |  * | ||||||
|  |  * 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 {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'; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * The options to configure the ngcc compiler for synchronous execution. | ||||||
|  |  */ | ||||||
|  | export interface SyncNgccOptions { | ||||||
|  |   /** The absolute path to the `node_modules` folder that contains the packages to process. */ | ||||||
|  |   basePath: string; | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * The path to the primary package to be processed. If not absolute then it must be relative to | ||||||
|  |    * `basePath`. | ||||||
|  |    * | ||||||
|  |    * All its dependencies will need to be processed too. | ||||||
|  |    * | ||||||
|  |    * If this property is provided then `errorOnFailedEntryPoint` is forced to true. | ||||||
|  |    */ | ||||||
|  |   targetEntryPointPath?: string; | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * Which entry-point properties in the package.json to consider when processing an entry-point. | ||||||
|  |    * Each property should hold a path to the particular bundle format for the entry-point. | ||||||
|  |    * Defaults to all the properties in the package.json. | ||||||
|  |    */ | ||||||
|  |   propertiesToConsider?: string[]; | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * Whether to process all formats specified by (`propertiesToConsider`)  or to stop processing | ||||||
|  |    * this entry-point at the first matching format. Defaults to `true`. | ||||||
|  |    */ | ||||||
|  |   compileAllFormats?: boolean; | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * Whether to create new entry-points bundles rather than overwriting the original files. | ||||||
|  |    */ | ||||||
|  |   createNewEntryPointFormats?: boolean; | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * Provide a logger that will be called with log messages. | ||||||
|  |    */ | ||||||
|  |   logger?: Logger; | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * Paths mapping configuration (`paths` and `baseUrl`), as found in `ts.CompilerOptions`. | ||||||
|  |    * These are used to resolve paths to locally built Angular libraries. | ||||||
|  |    * | ||||||
|  |    * Note that `pathMappings` specified here take precedence over any `pathMappings` loaded from a | ||||||
|  |    * TS config file. | ||||||
|  |    */ | ||||||
|  |   pathMappings?: PathMappings; | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * Provide a file-system service that will be used by ngcc for all file interactions. | ||||||
|  |    */ | ||||||
|  |   fileSystem?: FileSystem; | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * Whether the compilation should run and return asynchronously. Allowing asynchronous execution | ||||||
|  |    * may speed up the compilation by utilizing multiple CPU cores (if available). | ||||||
|  |    * | ||||||
|  |    * Default: `false` (i.e. run synchronously) | ||||||
|  |    */ | ||||||
|  |   async?: false; | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * Set to true in order to terminate immediately with an error code if an entry-point fails to be | ||||||
|  |    * processed. | ||||||
|  |    * | ||||||
|  |    * If `targetEntryPointPath` is provided then this property is always true and cannot be | ||||||
|  |    * changed. Otherwise the default is false. | ||||||
|  |    * | ||||||
|  |    * When set to false, ngcc will continue to process entry-points after a failure. In which case it | ||||||
|  |    * will log an error and resume processing other entry-points. | ||||||
|  |    */ | ||||||
|  |   errorOnFailedEntryPoint?: boolean; | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * Render `$localize` messages with legacy format ids. | ||||||
|  |    * | ||||||
|  |    * The default value is `true`. Only set this to `false` if you do not want legacy message ids to | ||||||
|  |    * be rendered. For example, if you are not using legacy message ids in your translation files | ||||||
|  |    * AND are not doing compile-time inlining of translations, in which case the extra message ids | ||||||
|  |    * would add unwanted size to the final source bundle. | ||||||
|  |    * | ||||||
|  |    * It is safe to leave this set to true if you are doing compile-time inlining because the extra | ||||||
|  |    * legacy message ids will all be stripped during translation. | ||||||
|  |    */ | ||||||
|  |   enableI18nLegacyMessageIdFormat?: boolean; | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * Whether to invalidate any entry-point manifest file that is on disk. Instead, walk the | ||||||
|  |    * directory tree looking for entry-points, and then write a new entry-point manifest, if | ||||||
|  |    * possible. | ||||||
|  |    * | ||||||
|  |    * Default: `false` (i.e. the manifest will be used if available) | ||||||
|  |    */ | ||||||
|  |   invalidateEntryPointManifest?: boolean; | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * An absolute path to a TS config file (e.g. `tsconfig.json`) or a directory containing one, that | ||||||
|  |    * will be used to configure module resolution with things like path mappings, if not specified | ||||||
|  |    * explicitly via the `pathMappings` property to `mainNgcc`. | ||||||
|  |    * | ||||||
|  |    * If `undefined`, ngcc will attempt to load a `tsconfig.json` file from the directory above the | ||||||
|  |    * `basePath`. | ||||||
|  |    * | ||||||
|  |    * If `null`, ngcc will not attempt to load any TS config file at all. | ||||||
|  |    */ | ||||||
|  |   tsConfigPath?: string|null; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * The options to configure the ngcc compiler for asynchronous execution. | ||||||
|  |  */ | ||||||
|  | export type AsyncNgccOptions = Omit<SyncNgccOptions, 'async'>&{async: true}; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * The options to configure the ngcc compiler. | ||||||
|  |  */ | ||||||
|  | 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>; | ||||||
|  | export type SharedSetup = { | ||||||
|  |   fileSystem: FileSystem; absBasePath: AbsoluteFsPath; projectPath: AbsoluteFsPath; | ||||||
|  |   tsConfig: ParsedConfiguration | null; | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Instantiate common utilities that are always used and fix up options with defaults, as necessary. | ||||||
|  |  * | ||||||
|  |  * NOTE: Avoid eagerly instantiating anything that might not be used when running sync/async. | ||||||
|  |  */ | ||||||
|  | export function getSharedSetup(options: NgccOptions): SharedSetup&RequiredNgccOptions& | ||||||
|  |     OptionalNgccOptions { | ||||||
|  |   const fileSystem = getFileSystem(); | ||||||
|  |   const absBasePath = absoluteFrom(options.basePath); | ||||||
|  |   const projectPath = fileSystem.dirname(absBasePath); | ||||||
|  |   const tsConfig = | ||||||
|  |       options.tsConfigPath !== null ? readConfiguration(options.tsConfigPath || projectPath) : null; | ||||||
|  | 
 | ||||||
|  |   let { | ||||||
|  |     basePath, | ||||||
|  |     targetEntryPointPath, | ||||||
|  |     propertiesToConsider = SUPPORTED_FORMAT_PROPERTIES, | ||||||
|  |     compileAllFormats = true, | ||||||
|  |     createNewEntryPointFormats = false, | ||||||
|  |     logger = new ConsoleLogger(LogLevel.info), | ||||||
|  |     pathMappings, | ||||||
|  |     async = false, | ||||||
|  |     errorOnFailedEntryPoint = false, | ||||||
|  |     enableI18nLegacyMessageIdFormat = true, | ||||||
|  |     invalidateEntryPointManifest = false, | ||||||
|  |     tsConfigPath, | ||||||
|  |   } = options; | ||||||
|  | 
 | ||||||
|  |   pathMappings = options.pathMappings || getPathMappingsFromTsConfig(tsConfig, projectPath); | ||||||
|  | 
 | ||||||
|  |   if (!!options.targetEntryPointPath) { | ||||||
|  |     // targetEntryPointPath forces us to error if an entry-point fails.
 | ||||||
|  |     errorOnFailedEntryPoint = true; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   return { | ||||||
|  |     basePath, | ||||||
|  |     targetEntryPointPath, | ||||||
|  |     propertiesToConsider, | ||||||
|  |     compileAllFormats, | ||||||
|  |     createNewEntryPointFormats, | ||||||
|  |     logger, | ||||||
|  |     pathMappings, | ||||||
|  |     async, | ||||||
|  |     errorOnFailedEntryPoint, | ||||||
|  |     enableI18nLegacyMessageIdFormat, | ||||||
|  |     invalidateEntryPointManifest, | ||||||
|  |     tsConfigPath, | ||||||
|  |     fileSystem, | ||||||
|  |     absBasePath, | ||||||
|  |     projectPath, | ||||||
|  |     tsConfig, | ||||||
|  |   }; | ||||||
|  | } | ||||||
| @ -7,7 +7,7 @@ | |||||||
|  */ |  */ | ||||||
| import * as ts from 'typescript'; | import * as ts from 'typescript'; | ||||||
| import {AbsoluteFsPath, FileSystem, NgtscCompilerHost} from '../../../src/ngtsc/file_system'; | import {AbsoluteFsPath, FileSystem, NgtscCompilerHost} from '../../../src/ngtsc/file_system'; | ||||||
| import {PathMappings} from '../utils'; | import {PathMappings} from '../ngcc_options'; | ||||||
| import {BundleProgram, makeBundleProgram} from './bundle_program'; | import {BundleProgram, makeBundleProgram} from './bundle_program'; | ||||||
| import {EntryPoint, EntryPointFormat} from './entry_point'; | import {EntryPoint, EntryPointFormat} from './entry_point'; | ||||||
| import {NgccSourcesCompilerHost} from './ngcc_compiler_host'; | import {NgccSourcesCompilerHost} from './ngcc_compiler_host'; | ||||||
|  | |||||||
| @ -5,10 +5,9 @@ | |||||||
|  * Use of this source code is governed by an MIT-style license that can be |  * 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
 |  * found in the LICENSE file at https://angular.io/license
 | ||||||
|  */ |  */ | ||||||
| import {ParsedConfiguration} from '@angular/compiler-cli/src/perform_compile'; |  | ||||||
| import * as ts from 'typescript'; | import * as ts from 'typescript'; | ||||||
| 
 | 
 | ||||||
| import {absoluteFrom, AbsoluteFsPath, FileSystem, isRooted, resolve} from '../../src/ngtsc/file_system'; | import {absoluteFrom, AbsoluteFsPath, FileSystem, isRooted} from '../../src/ngtsc/file_system'; | ||||||
| import {KnownDeclaration} from '../../src/ngtsc/reflection'; | import {KnownDeclaration} from '../../src/ngtsc/reflection'; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
| @ -78,11 +77,6 @@ export function hasNameIdentifier(declaration: ts.Declaration): declaration is t | |||||||
|   return namedDeclaration.name !== undefined && ts.isIdentifier(namedDeclaration.name); |   return namedDeclaration.name !== undefined && ts.isIdentifier(namedDeclaration.name); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export type PathMappings = { |  | ||||||
|   baseUrl: string, |  | ||||||
|   paths: {[key: string]: string[]} |  | ||||||
| }; |  | ||||||
| 
 |  | ||||||
| /** | /** | ||||||
|  * Test whether a path is "relative". |  * Test whether a path is "relative". | ||||||
|  * |  * | ||||||
| @ -186,17 +180,3 @@ export function stripDollarSuffix(value: string): string { | |||||||
| export function stripExtension(fileName: string): string { | export function stripExtension(fileName: string): string { | ||||||
|   return fileName.replace(/\..+$/, ''); |   return fileName.replace(/\..+$/, ''); | ||||||
| } | } | ||||||
| 
 |  | ||||||
| /** |  | ||||||
|  * 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, |  | ||||||
|     }; |  | ||||||
|   } |  | ||||||
| } |  | ||||||
| @ -13,10 +13,10 @@ import {DtsDependencyHost} from '../../src/dependencies/dts_dependency_host'; | |||||||
| import {EsmDependencyHost} from '../../src/dependencies/esm_dependency_host'; | import {EsmDependencyHost} from '../../src/dependencies/esm_dependency_host'; | ||||||
| import {ModuleResolver} from '../../src/dependencies/module_resolver'; | import {ModuleResolver} from '../../src/dependencies/module_resolver'; | ||||||
| import {DirectoryWalkerEntryPointFinder} from '../../src/entry_point_finder/directory_walker_entry_point_finder'; | import {DirectoryWalkerEntryPointFinder} from '../../src/entry_point_finder/directory_walker_entry_point_finder'; | ||||||
|  | import {PathMappings} from '../../src/ngcc_options'; | ||||||
| import {NgccConfiguration} from '../../src/packages/configuration'; | import {NgccConfiguration} from '../../src/packages/configuration'; | ||||||
| import {EntryPoint} from '../../src/packages/entry_point'; | import {EntryPoint} from '../../src/packages/entry_point'; | ||||||
| import {EntryPointManifest, EntryPointManifestFile} from '../../src/packages/entry_point_manifest'; | import {EntryPointManifest, EntryPointManifestFile} from '../../src/packages/entry_point_manifest'; | ||||||
| import {PathMappings} from '../../src/utils'; |  | ||||||
| import {MockLogger} from '../helpers/mock_logger'; | import {MockLogger} from '../helpers/mock_logger'; | ||||||
| 
 | 
 | ||||||
| runInEachFileSystem(() => { | runInEachFileSystem(() => { | ||||||
|  | |||||||
| @ -13,10 +13,10 @@ import {DtsDependencyHost} from '../../src/dependencies/dts_dependency_host'; | |||||||
| import {EsmDependencyHost} from '../../src/dependencies/esm_dependency_host'; | import {EsmDependencyHost} from '../../src/dependencies/esm_dependency_host'; | ||||||
| import {ModuleResolver} from '../../src/dependencies/module_resolver'; | import {ModuleResolver} from '../../src/dependencies/module_resolver'; | ||||||
| import {TargetedEntryPointFinder} from '../../src/entry_point_finder/targeted_entry_point_finder'; | import {TargetedEntryPointFinder} from '../../src/entry_point_finder/targeted_entry_point_finder'; | ||||||
|  | import {PathMappings} from '../../src/ngcc_options'; | ||||||
| import {NGCC_VERSION} from '../../src/packages/build_marker'; | import {NGCC_VERSION} from '../../src/packages/build_marker'; | ||||||
| import {NgccConfiguration} from '../../src/packages/configuration'; | import {NgccConfiguration} from '../../src/packages/configuration'; | ||||||
| import {EntryPoint} from '../../src/packages/entry_point'; | import {EntryPoint} from '../../src/packages/entry_point'; | ||||||
| import {PathMappings} from '../../src/utils'; |  | ||||||
| import {MockLogger} from '../helpers/mock_logger'; | import {MockLogger} from '../helpers/mock_logger'; | ||||||
| 
 | 
 | ||||||
| runInEachFileSystem(() => { | runInEachFileSystem(() => { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user