2018-11-01 08:21:07 +00: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 MagicString from 'magic-string';
|
2019-03-20 12:10:58 +02:00
|
|
|
import {getIifeBody} from '../host/esm5_host';
|
2018-11-01 08:21:07 +00:00
|
|
|
import {NgccReflectionHost} from '../host/ngcc_host';
|
|
|
|
|
import {CompiledClass} from '../analysis/decoration_analyzer';
|
|
|
|
|
import {EsmRenderer} from './esm_renderer';
|
2018-11-25 21:40:25 +00:00
|
|
|
import {EntryPointBundle} from '../packages/entry_point_bundle';
|
2019-03-29 10:13:14 +00:00
|
|
|
import {Logger} from '../logging/logger';
|
2018-11-01 08:21:07 +00:00
|
|
|
|
|
|
|
|
export class Esm5Renderer extends EsmRenderer {
|
2019-04-28 20:47:56 +01:00
|
|
|
constructor(logger: Logger, host: NgccReflectionHost, isCore: boolean, bundle: EntryPointBundle) {
|
|
|
|
|
super(logger, host, isCore, bundle);
|
2018-11-01 08:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add the definitions to each decorated class
|
|
|
|
|
*/
|
|
|
|
|
addDefinitions(output: MagicString, compiledClass: CompiledClass, definitions: string): void {
|
2019-03-20 12:10:58 +02:00
|
|
|
const iifeBody = getIifeBody(compiledClass.declaration);
|
|
|
|
|
if (!iifeBody) {
|
2018-11-01 08:21:07 +00:00
|
|
|
throw new Error(
|
|
|
|
|
`Compiled class declaration is not inside an IIFE: ${compiledClass.name} in ${compiledClass.declaration.getSourceFile().fileName}`);
|
|
|
|
|
}
|
2019-03-20 12:10:58 +02:00
|
|
|
|
|
|
|
|
const returnStatement = iifeBody.statements.find(ts.isReturnStatement);
|
2018-11-01 08:21:07 +00:00
|
|
|
if (!returnStatement) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`Compiled class wrapper IIFE does not have a return statement: ${compiledClass.name} in ${compiledClass.declaration.getSourceFile().fileName}`);
|
|
|
|
|
}
|
2019-03-20 12:10:58 +02:00
|
|
|
|
2018-11-01 08:21:07 +00:00
|
|
|
const insertionPoint = returnStatement.getFullStart();
|
|
|
|
|
output.appendLeft(insertionPoint, '\n' + definitions);
|
|
|
|
|
}
|
|
|
|
|
}
|