2020-04-15 11:40:41 +01: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
|
|
|
|
*/
|
|
|
|
import * as ts from 'typescript';
|
|
|
|
|
|
|
|
import {replaceTsWithNgInErrors} from '../../../src/ngtsc/diagnostics';
|
|
|
|
import {FileSystem} from '../../../src/ngtsc/file_system';
|
|
|
|
import {ParsedConfiguration} from '../../../src/perform_compile';
|
|
|
|
import {Logger} from '../logging/logger';
|
2020-04-16 15:54:40 +01:00
|
|
|
import {PathMappings} from '../ngcc_options';
|
2020-04-15 11:40:41 +01:00
|
|
|
import {getEntryPointFormat} from '../packages/entry_point';
|
|
|
|
import {makeEntryPointBundle} from '../packages/entry_point_bundle';
|
|
|
|
import {FileWriter} from '../writing/file_writer';
|
|
|
|
|
|
|
|
import {CreateCompileFn} from './api';
|
|
|
|
import {Task, TaskProcessingOutcome} from './tasks/api';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The function for creating the `compile()` function.
|
|
|
|
*/
|
|
|
|
export function getCreateCompileFn(
|
2020-04-29 21:28:22 +03:00
|
|
|
fileSystem: FileSystem, logger: Logger, fileWriter: FileWriter,
|
2020-04-15 11:40:41 +01:00
|
|
|
enableI18nLegacyMessageIdFormat: boolean, tsConfig: ParsedConfiguration|null,
|
|
|
|
pathMappings: PathMappings|undefined): CreateCompileFn {
|
2020-04-29 21:28:11 +03:00
|
|
|
return (beforeWritingFiles, onTaskCompleted) => {
|
2020-04-15 11:40:41 +01:00
|
|
|
const {Transformer} = require('../packages/transformer');
|
|
|
|
const transformer = new Transformer(fileSystem, logger, tsConfig);
|
|
|
|
|
|
|
|
return (task: Task) => {
|
|
|
|
const {entryPoint, formatProperty, formatPropertiesToMarkAsProcessed, processDts} = task;
|
|
|
|
|
|
|
|
const isCore = entryPoint.name === '@angular/core'; // Are we compiling the Angular core?
|
|
|
|
const packageJson = entryPoint.packageJson;
|
|
|
|
const formatPath = packageJson[formatProperty];
|
|
|
|
const format = getEntryPointFormat(fileSystem, entryPoint, formatProperty);
|
|
|
|
|
|
|
|
// All properties listed in `propertiesToProcess` are guaranteed to point to a format-path
|
|
|
|
// (i.e. they are defined in `entryPoint.packageJson`). Furthermore, they are also guaranteed
|
|
|
|
// to be among `SUPPORTED_FORMAT_PROPERTIES`.
|
|
|
|
// Based on the above, `formatPath` should always be defined and `getEntryPointFormat()`
|
|
|
|
// should always return a format here (and not `undefined`).
|
|
|
|
if (!formatPath || !format) {
|
|
|
|
// This should never happen.
|
|
|
|
throw new Error(
|
|
|
|
`Invariant violated: No format-path or format for ${entryPoint.path} : ` +
|
|
|
|
`${formatProperty} (formatPath: ${formatPath} | format: ${format})`);
|
|
|
|
}
|
|
|
|
|
2020-04-29 21:28:02 +03:00
|
|
|
logger.info(`Compiling ${entryPoint.name} : ${formatProperty} as ${format}`);
|
|
|
|
|
2020-04-15 11:40:41 +01:00
|
|
|
const bundle = makeEntryPointBundle(
|
|
|
|
fileSystem, entryPoint, formatPath, isCore, format, processDts, pathMappings, true,
|
|
|
|
enableI18nLegacyMessageIdFormat);
|
|
|
|
|
|
|
|
const result = transformer.transform(bundle);
|
|
|
|
if (result.success) {
|
|
|
|
if (result.diagnostics.length > 0) {
|
|
|
|
logger.warn(replaceTsWithNgInErrors(
|
|
|
|
ts.formatDiagnosticsWithColorAndContext(result.diagnostics, bundle.src.host)));
|
|
|
|
}
|
|
|
|
|
2020-04-29 21:28:11 +03:00
|
|
|
const writeBundle = () => {
|
|
|
|
fileWriter.writeBundle(
|
|
|
|
bundle, result.transformedFiles, formatPropertiesToMarkAsProcessed);
|
2020-04-15 11:40:41 +01:00
|
|
|
|
2020-04-29 21:28:11 +03:00
|
|
|
logger.debug(` Successfully compiled ${entryPoint.name} : ${formatProperty}`);
|
|
|
|
onTaskCompleted(task, TaskProcessingOutcome.Processed, null);
|
|
|
|
};
|
|
|
|
|
|
|
|
const beforeWritingResult = beforeWritingFiles(result.transformedFiles);
|
|
|
|
|
|
|
|
return (beforeWritingResult instanceof Promise) ?
|
|
|
|
beforeWritingResult.then(writeBundle) as ReturnType<typeof beforeWritingFiles>:
|
|
|
|
writeBundle();
|
2020-04-15 11:40:41 +01:00
|
|
|
} else {
|
|
|
|
const errors = replaceTsWithNgInErrors(
|
|
|
|
ts.formatDiagnosticsWithColorAndContext(result.diagnostics, bundle.src.host));
|
|
|
|
onTaskCompleted(task, TaskProcessingOutcome.Failed, `compilation errors:\n${errors}`);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|