refactor(build): use typescript es6 imports

This commit is contained in:
Daniel Sogl
2021-09-27 21:04:28 +02:00
parent 04137f44ba
commit 9e38c6b922
9 changed files with 159 additions and 98 deletions
+24 -13
View File
@@ -1,4 +1,14 @@
import * as ts from 'typescript';
import {
createClassDeclaration,
createIdentifier,
createProperty,
createToken,
SourceFile,
SyntaxKind,
TransformationContext,
TransformerFactory,
visitEachChild,
} from 'typescript';
import { Logger } from '../../logger';
import { convertValueToLiteral, getDecorator, getDecoratorArgs, getDecoratorName } from '../helpers';
@@ -16,10 +26,10 @@ function transformClass(cls: any, ngcBuild?: boolean) {
// add plugin decorator args as static properties of the plugin's class
for (const prop in pluginDecoratorArgs) {
pluginStatics.push(
ts.createProperty(
createProperty(
undefined,
[ts.createToken(ts.SyntaxKind.StaticKeyword)],
ts.createIdentifier(prop),
[createToken(SyntaxKind.StaticKeyword)],
createIdentifier(prop),
undefined,
undefined,
convertValueToLiteral(pluginDecoratorArgs[prop])
@@ -28,11 +38,11 @@ function transformClass(cls: any, ngcBuild?: boolean) {
}
}
cls = ts.createClassDeclaration(
cls = createClassDeclaration(
ngcBuild && cls.decorators && cls.decorators.length
? cls.decorators.filter(d => getDecoratorName(d) === 'Injectable')
: undefined, // remove Plugin and Injectable decorators
[ts.createToken(ts.SyntaxKind.ExportKeyword)],
[createToken(SyntaxKind.ExportKeyword)],
cls.name,
cls.typeParameters,
cls.heritageClauses,
@@ -43,14 +53,14 @@ function transformClass(cls: any, ngcBuild?: boolean) {
return cls;
}
function transformClasses(file: ts.SourceFile, ctx: ts.TransformationContext, ngcBuild?: boolean) {
function transformClasses(file: SourceFile, ctx: TransformationContext, ngcBuild?: boolean) {
Logger.silly('Transforming file: ' + file.fileName);
return ts.visitEachChild(
return visitEachChild(
file,
node => {
if (
node.kind !== ts.SyntaxKind.ClassDeclaration ||
(node.modifiers && node.modifiers.find(v => v.kind === ts.SyntaxKind.DeclareKeyword))
node.kind !== SyntaxKind.ClassDeclaration ||
(node.modifiers && node.modifiers.find(v => v.kind === SyntaxKind.DeclareKeyword))
) {
return node;
}
@@ -60,11 +70,12 @@ function transformClasses(file: ts.SourceFile, ctx: ts.TransformationContext, ng
);
}
export function pluginClassTransformer(ngcBuild?: boolean): ts.TransformerFactory<ts.SourceFile> {
return (ctx: ts.TransformationContext) => {
export function pluginClassTransformer(ngcBuild?: boolean): TransformerFactory<SourceFile> {
return (ctx: TransformationContext) => {
return tsSourceFile => {
if (tsSourceFile.fileName.indexOf('src/@awesome-cordova-plugins/plugins') > -1)
if (tsSourceFile.fileName.indexOf('src/@awesome-cordova-plugins/plugins') > -1) {
return transformClasses(tsSourceFile, ctx, ngcBuild);
}
return tsSourceFile;
};
};