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:59 -04:00
|
|
|
import {resolve} from 'canonical-path';
|
2019-03-20 09:47:59 -04:00
|
|
|
import {readFileSync} from 'fs';
|
2019-03-20 09:47:59 -04:00
|
|
|
|
2019-03-20 09:47:59 -04:00
|
|
|
import {AbsoluteFsPath} from '../../src/ngtsc/path';
|
2019-03-20 09:47:59 -04:00
|
|
|
|
2019-03-20 09:47:59 -04:00
|
|
|
import {hasBeenProcessed, markAsProcessed} 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';
|
2019-03-20 09:47:59 -04:00
|
|
|
import {FileWriter} from './writing/file_writer';
|
|
|
|
import {InPlaceFileWriter} from './writing/in_place_file_writer';
|
2019-03-20 09:47:59 -04:00
|
|
|
import {NewEntryPointFileWriter} from './writing/new_entry_point_file_writer';
|
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 {
|
2019-03-20 09:47:59 -04:00
|
|
|
/** The absolute path to the `node_modules` folder that contains the packages to process. */
|
|
|
|
basePath: string;
|
2019-03-20 09:47:58 -04:00
|
|
|
/**
|
2019-03-20 09:47:59 -04:00
|
|
|
* The path, relative to `basePath` to the primary package to be processed.
|
|
|
|
*
|
|
|
|
* All its dependencies will need to be processed too.
|
2019-03-20 09:47:58 -04:00
|
|
|
*/
|
2019-03-20 09:47:59 -04:00
|
|
|
targetEntryPointPath?: string;
|
2019-03-20 09:47:58 -04:00
|
|
|
/**
|
2019-03-20 09:47:59 -04:00
|
|
|
* 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.
|
2019-03-20 09:47:58 -04:00
|
|
|
*/
|
2019-03-20 09:47:59 -04:00
|
|
|
propertiesToConsider?: string[];
|
2019-03-20 09:47:58 -04:00
|
|
|
/**
|
2019-03-20 09:47:59 -04:00
|
|
|
* Whether to process all formats specified by (`propertiesToConsider`) or to stop processing
|
|
|
|
* this entry-point at the first matching format. Defaults to `true`.
|
2019-03-20 09:47:58 -04:00
|
|
|
*/
|
|
|
|
compileAllFormats?: boolean;
|
2019-03-20 09:47:59 -04:00
|
|
|
/**
|
|
|
|
* Whether to create new entry-points bundles rather than overwriting the original files.
|
|
|
|
*/
|
|
|
|
createNewEntryPointFormats?: 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(
|
|
|
|
{basePath, targetEntryPointPath, propertiesToConsider = SUPPORTED_FORMAT_PROPERTIES,
|
|
|
|
compileAllFormats = true, createNewEntryPointFormats = false}: NgccOptions): void {
|
2019-03-20 09:47:59 -04:00
|
|
|
const transformer = new Transformer(basePath, basePath);
|
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:59 -04:00
|
|
|
const fileWriter = getFileWriter(createNewEntryPointFormats);
|
2018-08-09 10:59:10 -04:00
|
|
|
|
2019-03-20 09:47:59 -04:00
|
|
|
const absoluteTargetEntryPointPath = targetEntryPointPath ?
|
|
|
|
AbsoluteFsPath.from(resolve(basePath, targetEntryPointPath)) :
|
|
|
|
undefined;
|
|
|
|
const {entryPoints} =
|
|
|
|
finder.findEntryPoints(AbsoluteFsPath.from(basePath), absoluteTargetEntryPointPath);
|
2018-11-25 16:40:25 -05:00
|
|
|
|
2019-03-20 09:47:59 -04:00
|
|
|
if (absoluteTargetEntryPointPath && entryPoints.every(entryPoint => {
|
|
|
|
return entryPoint.path !== absoluteTargetEntryPointPath;
|
|
|
|
})) {
|
|
|
|
// If we get here, then the requested entry-point did not contain anything compiled by
|
|
|
|
// the old Angular compiler. Therefore there is nothing for ngcc to do.
|
|
|
|
// So mark all formats in this entry-point as processed so that clients of ngcc can avoid
|
|
|
|
// triggering ngcc for this entry-point in the future.
|
|
|
|
const packageJsonPath =
|
|
|
|
AbsoluteFsPath.from(resolve(absoluteTargetEntryPointPath, 'package.json'));
|
|
|
|
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
|
|
|
|
propertiesToConsider.forEach(formatProperty => {
|
|
|
|
if (packageJson[formatProperty])
|
|
|
|
markAsProcessed(packageJson, packageJsonPath, formatProperty as EntryPointJsonProperty);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
entryPoints.forEach(entryPoint => {
|
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:59 -04:00
|
|
|
const entryPointPackageJson = entryPoint.packageJson;
|
|
|
|
const entryPointPackageJsonPath = AbsoluteFsPath.from(resolve(entryPoint.path, 'package.json'));
|
2019-03-20 09:47:58 -04:00
|
|
|
|
2019-03-20 09:47:59 -04:00
|
|
|
for (let i = 0; i < propertiesToConsider.length; i++) {
|
2019-03-20 09:47:59 -04:00
|
|
|
const property = propertiesToConsider[i] as EntryPointJsonProperty;
|
2019-03-20 09:47:59 -04:00
|
|
|
const formatPath = entryPointPackageJson[property];
|
2019-03-20 09:47:58 -04:00
|
|
|
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:59 -04:00
|
|
|
if (hasBeenProcessed(entryPointPackageJson, property)) {
|
2019-03-20 09:47:58 -04:00
|
|
|
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(
|
2019-03-20 09:47:59 -04:00
|
|
|
entryPoint.path, formatPath, entryPoint.typings, isCore, property, format,
|
2019-03-20 09:47:58 -04:00
|
|
|
compiledFormats.size === 0);
|
|
|
|
if (bundle) {
|
|
|
|
console.warn(`Compiling ${entryPoint.name} : ${property} as ${format}`);
|
2019-03-20 09:47:59 -04:00
|
|
|
const transformedFiles = transformer.transform(bundle);
|
|
|
|
fileWriter.writeBundle(entryPoint, bundle, transformedFiles);
|
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)) {
|
2019-03-20 09:47:59 -04:00
|
|
|
markAsProcessed(entryPointPackageJson, entryPointPackageJsonPath, 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
|
|
|
}
|
2019-03-20 09:47:59 -04:00
|
|
|
|
2019-03-20 09:47:59 -04:00
|
|
|
function getFileWriter(createNewEntryPointFormats: boolean): FileWriter {
|
|
|
|
return createNewEntryPointFormats ? new NewEntryPointFileWriter() : new InPlaceFileWriter();
|
2019-03-20 09:47:59 -04:00
|
|
|
}
|