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 MagicString from 'magic-string';
|
2019-04-28 20:47:57 +01:00
|
|
|
import * as ts from 'typescript';
|
2019-07-18 21:05:32 +01:00
|
|
|
import {CompiledClass} from '../analysis/types';
|
2019-03-20 12:10:58 +02:00
|
|
|
import {getIifeBody} from '../host/esm5_host';
|
2019-04-28 20:48:35 +01:00
|
|
|
import {EsmRenderingFormatter} from './esm_rendering_formatter';
|
2018-11-01 08:21:07 +00:00
|
|
|
|
2019-04-28 20:48:35 +01:00
|
|
|
/**
|
|
|
|
|
* A RenderingFormatter that works with files that use ECMAScript Module `import` and `export`
|
|
|
|
|
* statements, but instead of `class` declarations it uses ES5 `function` wrappers for classes.
|
|
|
|
|
*/
|
|
|
|
|
export class Esm5RenderingFormatter extends EsmRenderingFormatter {
|
2018-11-01 08:21:07 +00:00
|
|
|
/**
|
2019-04-28 20:48:35 +01:00
|
|
|
* Add the definitions inside the IIFE of each decorated class
|
2018-11-01 08:21:07 +00:00
|
|
|
*/
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|