2018-07-16 03:49:56 -04:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
2018-08-09 08:54:20 -04:00
|
|
|
|
2019-03-20 09:47:58 -04:00
|
|
|
import {AbsoluteFsPath} from '../../src/ngtsc/path';
|
2019-03-20 09:47:59 -04:00
|
|
|
|
2019-03-20 09:47:58 -04:00
|
|
|
import {checkMarker, writeMarker} from './packages/build_marker';
|
2018-08-09 10:59:10 -04:00
|
|
|
import {DependencyHost} from './packages/dependency_host';
|
|
|
|
import {DependencyResolver} from './packages/dependency_resolver';
|
2019-03-20 09:47:59 -04:00
|
|
|
import {EntryPointFormat, EntryPointJsonProperty, SUPPORTED_FORMAT_PROPERTIES, getEntryPointFormat} from './packages/entry_point';
|
2018-11-25 16:40:25 -05:00
|
|
|
import {makeEntryPointBundle} from './packages/entry_point_bundle';
|
2018-08-09 10:59:10 -04:00
|
|
|
import {EntryPointFinder} from './packages/entry_point_finder';
|
|
|
|
import {Transformer} from './packages/transformer';
|
2018-07-16 03:49:56 -04:00
|
|
|
|
2019-03-20 09:47:59 -04:00
|
|
|
|
2019-03-20 09:47:58 -04:00
|
|
|
/**
|
|
|
|
* The options to configure the ngcc compiler.
|
|
|
|
*/
|
|
|
|
export interface NgccOptions {
|
|
|
|
/** The path to the node_modules folder that contains the packages to compile. */
|
2019-03-20 09:47:58 -04:00
|
|
|
baseSourcePath: AbsoluteFsPath;
|
2019-03-20 09:47:58 -04:00
|
|
|
/**
|
|
|
|
* The path, relative to `baseSourcePath` of the primary package to be compiled.
|
|
|
|
* All its dependencies will need to be compiled too.
|
|
|
|
*/
|
2019-03-20 09:47:58 -04:00
|
|
|
targetEntryPointPath?: AbsoluteFsPath;
|
2019-03-20 09:47:58 -04:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
propertiesToConsider?: EntryPointJsonProperty[];
|
2019-03-20 09:47:58 -04:00
|
|
|
/**
|
|
|
|
* Whether to compile all specified formats or to stop compiling this entry-point at the first
|
|
|
|
* matching format. Defaults to `true`.
|
|
|
|
*/
|
|
|
|
compileAllFormats?: boolean;
|
2019-03-20 09:47:58 -04:00
|
|
|
}
|
2018-08-09 10:59:10 -04:00
|
|
|
|
2019-03-20 09:47:58 -04:00
|
|
|
const SUPPORTED_FORMATS: EntryPointFormat[] = ['esm5', 'esm2015'];
|
2019-03-20 09:47:58 -04:00
|
|
|
|
2019-03-20 09:47:58 -04:00
|
|
|
/**
|
|
|
|
* This is the main entry-point into ngcc (aNGular Compatibility Compiler).
|
|
|
|
*
|
|
|
|
* You can call this function to process one or more npm packages, to ensure
|
|
|
|
* that they are compatible with the ivy compiler (ngtsc).
|
|
|
|
*
|
|
|
|
* @param options The options telling ngcc what to compile and how.
|
|
|
|
*/
|
2019-03-20 09:47:59 -04:00
|
|
|
export function mainNgcc({baseSourcePath, targetEntryPointPath,
|
|
|
|
propertiesToConsider = SUPPORTED_FORMAT_PROPERTIES,
|
2019-03-20 09:47:58 -04:00
|
|
|
compileAllFormats = true}: NgccOptions): void {
|
2019-03-20 09:47:58 -04:00
|
|
|
const transformer = new Transformer(baseSourcePath, baseSourcePath);
|
2018-08-09 10:59:10 -04:00
|
|
|
const host = new DependencyHost();
|
|
|
|
const resolver = new DependencyResolver(host);
|
|
|
|
const finder = new EntryPointFinder(resolver);
|
|
|
|
|
2019-03-20 09:47:58 -04:00
|
|
|
const {entryPoints} = finder.findEntryPoints(baseSourcePath, targetEntryPointPath);
|
2019-03-20 09:47:58 -04:00
|
|
|
entryPoints.forEach(entryPoint => {
|
2018-11-25 16:40:25 -05:00
|
|
|
|
2019-03-20 09:47:58 -04:00
|
|
|
// Are we compiling the Angular core?
|
|
|
|
const isCore = entryPoint.name === '@angular/core';
|
2018-11-25 16:40:25 -05:00
|
|
|
|
2019-03-20 09:47:58 -04:00
|
|
|
const compiledFormats = new Set<string>();
|
2019-03-20 09:47:58 -04:00
|
|
|
|
2019-03-20 09:47:59 -04:00
|
|
|
for (let i = 0; i < propertiesToConsider.length; i++) {
|
|
|
|
const property = propertiesToConsider[i];
|
2019-03-20 09:47:58 -04:00
|
|
|
const formatPath = entryPoint.packageJson[property];
|
|
|
|
const format = getEntryPointFormat(property);
|
2018-12-04 16:18:00 -05:00
|
|
|
|
2019-03-20 09:47:58 -04:00
|
|
|
// No format then this property is not supposed to be compiled.
|
2019-03-20 09:47:58 -04:00
|
|
|
if (!formatPath || !format || SUPPORTED_FORMATS.indexOf(format) === -1) continue;
|
2019-03-20 09:47:58 -04:00
|
|
|
|
2019-03-20 09:47:58 -04:00
|
|
|
if (checkMarker(entryPoint, property)) {
|
|
|
|
compiledFormats.add(formatPath);
|
|
|
|
console.warn(`Skipping ${entryPoint.name} : ${property} (already compiled).`);
|
|
|
|
continue;
|
|
|
|
}
|
2019-03-20 09:47:58 -04:00
|
|
|
|
2019-03-20 09:47:58 -04:00
|
|
|
// We don't break if this if statement fails because we still want to mark
|
|
|
|
// the property as processed even if its underlying format has been built already.
|
|
|
|
if (!compiledFormats.has(formatPath) && (compileAllFormats || compiledFormats.size === 0)) {
|
2019-03-20 09:47:58 -04:00
|
|
|
const bundle = makeEntryPointBundle(
|
|
|
|
entryPoint.path, formatPath, entryPoint.typings, isCore, format,
|
|
|
|
compiledFormats.size === 0);
|
|
|
|
if (bundle) {
|
|
|
|
console.warn(`Compiling ${entryPoint.name} : ${property} as ${format}`);
|
2019-03-20 09:47:58 -04:00
|
|
|
transformer.transform(bundle);
|
2019-03-20 09:47:58 -04:00
|
|
|
compiledFormats.add(formatPath);
|
2019-03-20 09:47:58 -04:00
|
|
|
} else {
|
2019-03-20 09:47:58 -04:00
|
|
|
console.warn(
|
|
|
|
`Skipping ${entryPoint.name} : ${format} (no valid entry point file for this format).`);
|
2019-03-20 09:47:58 -04:00
|
|
|
}
|
2019-03-20 09:47:58 -04:00
|
|
|
} else if (!compileAllFormats) {
|
2019-03-20 09:47:58 -04:00
|
|
|
console.warn(`Skipping ${entryPoint.name} : ${property} (already compiled).`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Either this format was just compiled or its underlying format was compiled because of a
|
|
|
|
// previous property.
|
|
|
|
if (compiledFormats.has(formatPath)) {
|
|
|
|
writeMarker(entryPoint, property);
|
2019-03-20 09:47:58 -04:00
|
|
|
}
|
2019-03-20 09:47:58 -04:00
|
|
|
}
|
2018-12-04 16:18:00 -05:00
|
|
|
|
2019-03-20 09:47:58 -04:00
|
|
|
if (compiledFormats.size === 0) {
|
2019-03-20 09:47:58 -04:00
|
|
|
throw new Error(
|
2019-03-20 09:47:59 -04:00
|
|
|
`Failed to compile any formats for entry-point at (${entryPoint.path}). Tried ${propertiesToConsider}.`);
|
2019-03-20 09:47:58 -04:00
|
|
|
}
|
2019-03-20 09:47:58 -04:00
|
|
|
});
|
2018-08-09 08:54:20 -04:00
|
|
|
}
|