refactor(ivy): ngcc - clean up the public API and export hasBeenProcessed helper (#29092)

Now the public API does not contain internal types, such as `AbsoluteFsPath` and
`EntryPointJsonProperty`. Instead we just accept strings and then guard them in
`mainNgcc` as appropriate.

A new public API function (`hasBeenProcessed`) has been exported to allow programmatic
checking of the build marker when the package.json contents are already known.

PR Close #29092
This commit is contained in:
Pete Bacon Darwin
2019-03-20 13:47:59 +00:00
committed by Matias Niemelä
parent 7ea0d1bd3a
commit 55ea8da6eb
5 changed files with 68 additions and 48 deletions

View File

@ -6,7 +6,9 @@
* found in the LICENSE file at https://angular.io/license
*/
import {AbsoluteFsPath} from '../../src/ngtsc/path';
import {resolve} from 'canonical-path';
import {AbsoluteFsPath, PathSegment} from '../../src/ngtsc/path';
import {checkMarker, writeMarker} from './packages/build_marker';
import {DependencyHost} from './packages/dependency_host';
@ -21,21 +23,23 @@ import {Transformer} from './packages/transformer';
* The options to configure the ngcc compiler.
*/
export interface NgccOptions {
/** The path to the node_modules folder that contains the packages to compile. */
baseSourcePath: AbsoluteFsPath;
/** The absolute path to the `node_modules` folder that contains the packages to process. */
basePath: string;
/**
* The path, relative to `baseSourcePath` of the primary package to be compiled.
* All its dependencies will need to be compiled too.
* The path, relative to `basePath` to the primary package to be processed.
*
* All its dependencies will need to be processed too.
*/
targetEntryPointPath?: AbsoluteFsPath;
targetEntryPointPath?: string;
/**
* Which entry-point properties in the package.json to consider when compiling.
* Each of properties contain a path to particular bundle format for a given entry-point.
* 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?: EntryPointJsonProperty[];
propertiesToConsider?: string[];
/**
* Whether to compile all specified formats or to stop compiling this entry-point at the first
* matching format. Defaults to `true`.
* 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;
}
@ -50,15 +54,19 @@ const SUPPORTED_FORMATS: EntryPointFormat[] = ['esm5', 'esm2015'];
*
* @param options The options telling ngcc what to compile and how.
*/
export function mainNgcc({baseSourcePath, targetEntryPointPath,
export function mainNgcc({basePath, targetEntryPointPath,
propertiesToConsider = SUPPORTED_FORMAT_PROPERTIES,
compileAllFormats = true}: NgccOptions): void {
const transformer = new Transformer(baseSourcePath, baseSourcePath);
const transformer = new Transformer(basePath, basePath);
const host = new DependencyHost();
const resolver = new DependencyResolver(host);
const finder = new EntryPointFinder(resolver);
const {entryPoints} = finder.findEntryPoints(baseSourcePath, targetEntryPointPath);
const absoluteTargetEntryPointPath = targetEntryPointPath ?
AbsoluteFsPath.from(resolve(basePath, targetEntryPointPath)) :
undefined;
const {entryPoints} =
finder.findEntryPoints(AbsoluteFsPath.from(basePath), absoluteTargetEntryPointPath);
entryPoints.forEach(entryPoint => {
// Are we compiling the Angular core?
@ -67,7 +75,7 @@ export function mainNgcc({baseSourcePath, targetEntryPointPath,
const compiledFormats = new Set<string>();
for (let i = 0; i < propertiesToConsider.length; i++) {
const property = propertiesToConsider[i];
const property = propertiesToConsider[i] as EntryPointJsonProperty;
const formatPath = entryPoint.packageJson[property];
const format = getEntryPointFormat(property);

View File

@ -14,17 +14,20 @@ import {EntryPoint, EntryPointJsonProperty} from './entry_point';
export const NGCC_VERSION = '0.0.0-PLACEHOLDER';
/**
* Check whether there is a build marker for the given entry-point and format property.
* @param entryPoint the entry-point to check for a marker.
* @param format the property in the package.json of the format for which we are checking for a
* marker.
* @returns true if the entry-point and format have already been built with this ngcc version.
* @throws Error if the entry-point and format have already been built with a different ngcc
* Check whether ngcc has already processed a given entry-point format.
*
* The entry-point is defined by the package.json contents provided.
* The format is defined by the provided property name of the path to the bundle in the package.json
*
* @param packageJson The parsed contents of the package.json file for the entry-point.
* @param format The entry-point format property in the package.json to check.
* @returns true if the entry-point and format have already been processed with this ngcc version.
* @throws Error if the entry-point has already been processed with a different ngcc
* version.
*/
export function checkMarker(entryPoint: EntryPoint, format: EntryPointJsonProperty): boolean {
const pkg = entryPoint.packageJson;
const compiledVersion = pkg.__modified_by_ngcc__ && pkg.__modified_by_ngcc__[format];
export function hasBeenProcessed(packageJson: any, format: string): boolean {
const compiledVersion =
packageJson && packageJson.__modified_by_ngcc__ && packageJson.__modified_by_ngcc__[format];
if (compiledVersion && compiledVersion !== NGCC_VERSION) {
throw new Error(
'The ngcc compiler has changed since the last ngcc build.\n' +
@ -33,6 +36,21 @@ export function checkMarker(entryPoint: EntryPoint, format: EntryPointJsonProper
return !!compiledVersion;
}
/**
* Check whether there is a marker for the given entry-point and format property, indicating that
* the given bundle has already been processed.
* @param entryPoint the entry-point to check for a marker.
* @param format the property in the package.json of the format for which we are checking for a
* marker.
* @returns true if the entry-point and format have already been processed with this ngcc version.
* @throws Error if the entry-point and format have already been processed with a different ngcc
* version.
*/
export function checkMarker(entryPoint: EntryPoint, format: EntryPointJsonProperty): boolean {
const pkg = entryPoint.packageJson;
return hasBeenProcessed(pkg, format);
}
/**
* Write a build marker for the given entry-point and format property, to indicate that it has
* been compiled by this version of ngcc.