mirror of
https://github.com/danielsogl/awesome-cordova-plugins.git
synced 2025-01-31 18:49:43 +08:00
refactor(build): use typescript es6 imports
This commit is contained in:
parent
04137f44ba
commit
9e38c6b922
@ -1,7 +1,20 @@
|
|||||||
import { readdirSync } from 'fs-extra';
|
import { readdirSync } from 'fs-extra';
|
||||||
import { camelCase, clone } from 'lodash';
|
import { camelCase, clone } from 'lodash';
|
||||||
import { join, resolve } from 'path';
|
import { join, resolve } from 'path';
|
||||||
import * as ts from 'typescript';
|
import {
|
||||||
|
ArrayLiteralExpression,
|
||||||
|
createArrayLiteral,
|
||||||
|
createLiteral,
|
||||||
|
createNumericLiteral,
|
||||||
|
createObjectLiteral,
|
||||||
|
createPropertyAssignment,
|
||||||
|
Decorator,
|
||||||
|
Expression,
|
||||||
|
Node,
|
||||||
|
ObjectLiteralElementLike,
|
||||||
|
ObjectLiteralExpression,
|
||||||
|
SyntaxKind,
|
||||||
|
} from 'typescript';
|
||||||
|
|
||||||
import { Logger } from '../logger';
|
import { Logger } from '../logger';
|
||||||
|
|
||||||
@ -12,13 +25,13 @@ export const COMPILER_OPTIONS = TS_CONFIG.compilerOptions;
|
|||||||
export const PLUGINS_ROOT = join(ROOT, 'src/@awesome-cordova-plugins/plugins/');
|
export const PLUGINS_ROOT = join(ROOT, 'src/@awesome-cordova-plugins/plugins/');
|
||||||
export const PLUGIN_PATHS = readdirSync(PLUGINS_ROOT).map(d => join(PLUGINS_ROOT, d, 'index.ts'));
|
export const PLUGIN_PATHS = readdirSync(PLUGINS_ROOT).map(d => join(PLUGINS_ROOT, d, 'index.ts'));
|
||||||
|
|
||||||
export function getDecorator(node: ts.Node, index = 0): ts.Decorator {
|
export function getDecorator(node: Node, index = 0): Decorator {
|
||||||
if (node.decorators && node.decorators[index]) {
|
if (node.decorators && node.decorators[index]) {
|
||||||
return node.decorators[index];
|
return node.decorators[index];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function hasDecorator(decoratorName: string, node: ts.Node): boolean {
|
export function hasDecorator(decoratorName: string, node: Node): boolean {
|
||||||
return (
|
return (
|
||||||
node.decorators &&
|
node.decorators &&
|
||||||
node.decorators.length &&
|
node.decorators.length &&
|
||||||
@ -43,24 +56,24 @@ export function getDecoratorArgs(decorator: any) {
|
|||||||
let val: number | boolean;
|
let val: number | boolean;
|
||||||
|
|
||||||
switch (prop.initializer.kind) {
|
switch (prop.initializer.kind) {
|
||||||
case ts.SyntaxKind.StringLiteral:
|
case SyntaxKind.StringLiteral:
|
||||||
case ts.SyntaxKind.Identifier:
|
case SyntaxKind.Identifier:
|
||||||
val = prop.initializer.text;
|
val = prop.initializer.text;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ts.SyntaxKind.ArrayLiteralExpression:
|
case SyntaxKind.ArrayLiteralExpression:
|
||||||
val = prop.initializer.elements.map((e: any) => e.text);
|
val = prop.initializer.elements.map((e: any) => e.text);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ts.SyntaxKind.TrueKeyword:
|
case SyntaxKind.TrueKeyword:
|
||||||
val = true;
|
val = true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ts.SyntaxKind.FalseKeyword:
|
case SyntaxKind.FalseKeyword:
|
||||||
val = false;
|
val = false;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ts.SyntaxKind.NumericLiteral:
|
case SyntaxKind.NumericLiteral:
|
||||||
val = Number(prop.initializer.text);
|
val = Number(prop.initializer.text);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -89,9 +102,9 @@ export function convertValueToLiteral(val: any) {
|
|||||||
return objectToObjectLiteral(val);
|
return objectToObjectLiteral(val);
|
||||||
}
|
}
|
||||||
if (typeof val === 'number') {
|
if (typeof val === 'number') {
|
||||||
return ts.createNumericLiteral(String(val));
|
return createNumericLiteral(String(val));
|
||||||
}
|
}
|
||||||
return ts.createLiteral(val);
|
return createLiteral(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -100,14 +113,12 @@ export function convertValueToLiteral(val: any) {
|
|||||||
* @param obj key value object
|
* @param obj key value object
|
||||||
* @returns Typescript Object Literal Expression
|
* @returns Typescript Object Literal Expression
|
||||||
*/
|
*/
|
||||||
function objectToObjectLiteral(obj: { [key: string]: any }): ts.ObjectLiteralExpression {
|
function objectToObjectLiteral(obj: { [key: string]: any }): ObjectLiteralExpression {
|
||||||
const newProperties: ts.ObjectLiteralElementLike[] = Object.keys(obj).map(
|
const newProperties: ObjectLiteralElementLike[] = Object.keys(obj).map((key: string): ObjectLiteralElementLike => {
|
||||||
(key: string): ts.ObjectLiteralElementLike => {
|
return createPropertyAssignment(createLiteral(key), convertValueToLiteral(obj[key]) as Expression);
|
||||||
return ts.createPropertyAssignment(ts.createLiteral(key), convertValueToLiteral(obj[key]) as ts.Expression);
|
});
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
return ts.createObjectLiteral(newProperties);
|
return createObjectLiteral(newProperties);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -116,9 +127,9 @@ function objectToObjectLiteral(obj: { [key: string]: any }): ts.ObjectLiteralExp
|
|||||||
* @param list array
|
* @param list array
|
||||||
* @returns Typescript Array Literal Expression
|
* @returns Typescript Array Literal Expression
|
||||||
*/
|
*/
|
||||||
function arrayToArrayLiteral(list: any[]): ts.ArrayLiteralExpression {
|
function arrayToArrayLiteral(list: any[]): ArrayLiteralExpression {
|
||||||
const newList: any[] = list.map(convertValueToLiteral);
|
const newList: any[] = list.map(convertValueToLiteral);
|
||||||
return ts.createArrayLiteral(newList);
|
return createArrayLiteral(newList);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getMethodsForDecorator(decoratorName: string) {
|
export function getMethodsForDecorator(decoratorName: string) {
|
||||||
|
@ -4,7 +4,7 @@ import { clone } from 'lodash';
|
|||||||
import { dirname, join, resolve } from 'path';
|
import { dirname, join, resolve } from 'path';
|
||||||
import * as rimraf from 'rimraf';
|
import * as rimraf from 'rimraf';
|
||||||
import * as rollup from 'rollup';
|
import * as rollup from 'rollup';
|
||||||
import * as ts from 'typescript';
|
import { ModuleKind, ModuleResolutionKind, ScriptTarget } from 'typescript';
|
||||||
|
|
||||||
import { COMPILER_OPTIONS, PLUGIN_PATHS, ROOT } from './helpers';
|
import { COMPILER_OPTIONS, PLUGIN_PATHS, ROOT } from './helpers';
|
||||||
import { importsTransformer } from './transformers/imports';
|
import { importsTransformer } from './transformers/imports';
|
||||||
@ -14,9 +14,9 @@ import { generateDeclarations } from './transpile';
|
|||||||
export function getProgram(rootNames: string[] = createSourceFiles()) {
|
export function getProgram(rootNames: string[] = createSourceFiles()) {
|
||||||
const options: CompilerOptions = clone(COMPILER_OPTIONS);
|
const options: CompilerOptions = clone(COMPILER_OPTIONS);
|
||||||
options.basePath = ROOT;
|
options.basePath = ROOT;
|
||||||
options.moduleResolution = ts.ModuleResolutionKind.NodeJs;
|
options.moduleResolution = ModuleResolutionKind.NodeJs;
|
||||||
options.module = ts.ModuleKind.ES2015;
|
options.module = ModuleKind.ES2015;
|
||||||
options.target = ts.ScriptTarget.ES5;
|
options.target = ScriptTarget.ES5;
|
||||||
options.lib = ['dom', 'es2017'];
|
options.lib = ['dom', 'es2017'];
|
||||||
options.inlineSourceMap = true;
|
options.inlineSourceMap = true;
|
||||||
options.importHelpers = true;
|
options.importHelpers = true;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { unlinkSync, writeJSONSync } from 'fs-extra';
|
import { unlinkSync, writeJSONSync } from 'fs-extra';
|
||||||
import { resolve } from 'path';
|
import { resolve } from 'path';
|
||||||
import * as ts from 'typescript';
|
import { ClassDeclaration, SyntaxKind, TransformationContext, visitEachChild } from 'typescript';
|
||||||
|
|
||||||
import { hasDecorator, ROOT } from '../helpers';
|
import { hasDecorator, ROOT } from '../helpers';
|
||||||
|
|
||||||
@ -22,13 +22,13 @@ export const EMIT_PATH = resolve(ROOT, 'injectable-classes.json');
|
|||||||
* window['IonicNative'] object.
|
* window['IonicNative'] object.
|
||||||
*/
|
*/
|
||||||
export function extractInjectables() {
|
export function extractInjectables() {
|
||||||
return (ctx: ts.TransformationContext) => {
|
return (ctx: TransformationContext) => {
|
||||||
return tsSourceFile => {
|
return tsSourceFile => {
|
||||||
if (tsSourceFile.fileName.indexOf('src/@awesome-cordova-plugins/plugins') > -1) {
|
if (tsSourceFile.fileName.indexOf('src/@awesome-cordova-plugins/plugins') > -1) {
|
||||||
ts.visitEachChild(
|
visitEachChild(
|
||||||
tsSourceFile,
|
tsSourceFile,
|
||||||
node => {
|
node => {
|
||||||
if (node.kind !== ts.SyntaxKind.ClassDeclaration) {
|
if (node.kind !== SyntaxKind.ClassDeclaration) {
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,7 +36,7 @@ export function extractInjectables() {
|
|||||||
if (isInjectable) {
|
if (isInjectable) {
|
||||||
injectableClasses.push({
|
injectableClasses.push({
|
||||||
file: tsSourceFile.path,
|
file: tsSourceFile.path,
|
||||||
className: (node as ts.ClassDeclaration).name.text,
|
className: (node as ClassDeclaration).name.text,
|
||||||
dirName: tsSourceFile.path.split(/[\\\/]+/).reverse()[1],
|
dirName: tsSourceFile.path.split(/[\\\/]+/).reverse()[1],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,19 @@
|
|||||||
import * as ts from 'typescript';
|
import { createIdentifier, SourceFile, SyntaxKind, TransformationContext } from 'typescript';
|
||||||
|
|
||||||
import { getMethodsForDecorator } from '../helpers';
|
import { getMethodsForDecorator } from '../helpers';
|
||||||
|
|
||||||
function transformImports(file: ts.SourceFile, ctx: ts.TransformationContext, ngcBuild?: boolean) {
|
function transformImports(file: SourceFile, ctx: TransformationContext, ngcBuild?: boolean) {
|
||||||
// remove angular imports
|
// remove angular imports
|
||||||
if (!ngcBuild) {
|
if (!ngcBuild) {
|
||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
file.statements = (file.statements as any).filter(
|
file.statements = (file.statements as any).filter(
|
||||||
(s: any) => !(s.kind === ts.SyntaxKind.ImportDeclaration && s.moduleSpecifier.text === '@angular/core')
|
(s: any) => !(s.kind === SyntaxKind.ImportDeclaration && s.moduleSpecifier.text === '@angular/core')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// find the @awesome-cordova-plugins/core import statement
|
// find the @awesome-cordova-plugins/core import statement
|
||||||
const importStatement = (file.statements as any).find((s: any) => {
|
const importStatement = (file.statements as any).find((s: any) => {
|
||||||
return s.kind === ts.SyntaxKind.ImportDeclaration && s.moduleSpecifier.text === '@awesome-cordova-plugins/core';
|
return s.kind === SyntaxKind.ImportDeclaration && s.moduleSpecifier.text === '@awesome-cordova-plugins/core';
|
||||||
});
|
});
|
||||||
|
|
||||||
// we're only interested in files containing @awesome-cordova-plugins/core import statement
|
// we're only interested in files containing @awesome-cordova-plugins/core import statement
|
||||||
@ -40,11 +41,11 @@ function transformImports(file: ts.SourceFile, ctx: ts.TransformationContext, ng
|
|||||||
|
|
||||||
decorators.forEach(d => (methods = getMethodsForDecorator(d).concat(methods)));
|
decorators.forEach(d => (methods = getMethodsForDecorator(d).concat(methods)));
|
||||||
|
|
||||||
const methodElements = methods.map(m => ts.createIdentifier(m));
|
const methodElements = methods.map(m => createIdentifier(m));
|
||||||
const methodNames = methodElements.map(el => el.escapedText);
|
const methodNames = methodElements.map(el => el.escapedText);
|
||||||
|
|
||||||
importStatement.importClause.namedBindings.elements = [
|
importStatement.importClause.namedBindings.elements = [
|
||||||
ts.createIdentifier('AwesomeCordovaNativePlugin'),
|
createIdentifier('AwesomeCordovaNativePlugin'),
|
||||||
...methodElements,
|
...methodElements,
|
||||||
...importStatement.importClause.namedBindings.elements.filter(
|
...importStatement.importClause.namedBindings.elements.filter(
|
||||||
el => keep.indexOf(el.name.text) !== -1 && methodNames.indexOf(el.name.text) === -1
|
el => keep.indexOf(el.name.text) !== -1 && methodNames.indexOf(el.name.text) === -1
|
||||||
@ -69,7 +70,7 @@ function transformImports(file: ts.SourceFile, ctx: ts.TransformationContext, ng
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function importsTransformer(ngcBuild?: boolean) {
|
export function importsTransformer(ngcBuild?: boolean) {
|
||||||
return (ctx: ts.TransformationContext) => {
|
return (ctx: TransformationContext) => {
|
||||||
return tsSourceFile => {
|
return tsSourceFile => {
|
||||||
return transformImports(tsSourceFile, ctx, ngcBuild);
|
return transformImports(tsSourceFile, ctx, ngcBuild);
|
||||||
};
|
};
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
import * as ts from 'typescript';
|
import { ClassDeclaration, createConstructor, SyntaxKind } from 'typescript';
|
||||||
|
|
||||||
import { transformMethod } from './methods';
|
import { transformMethod } from './methods';
|
||||||
import { transformProperty } from './properties';
|
import { transformProperty } from './properties';
|
||||||
|
|
||||||
export function transformMembers(cls: ts.ClassDeclaration) {
|
export function transformMembers(cls: ClassDeclaration) {
|
||||||
const propertyIndices: number[] = [];
|
const propertyIndices: number[] = [];
|
||||||
|
|
||||||
const members = cls.members.map((member: any, index: number) => {
|
const members = cls.members.map((member: any, index: number) => {
|
||||||
@ -10,13 +11,13 @@ export function transformMembers(cls: ts.ClassDeclaration) {
|
|||||||
if (!member.decorators || !member.decorators.length) return member;
|
if (!member.decorators || !member.decorators.length) return member;
|
||||||
|
|
||||||
switch (member.kind) {
|
switch (member.kind) {
|
||||||
case ts.SyntaxKind.MethodDeclaration:
|
case SyntaxKind.MethodDeclaration:
|
||||||
return transformMethod(member);
|
return transformMethod(member);
|
||||||
case ts.SyntaxKind.PropertyDeclaration:
|
case SyntaxKind.PropertyDeclaration:
|
||||||
propertyIndices.push(index);
|
propertyIndices.push(index);
|
||||||
return member;
|
return member;
|
||||||
case ts.SyntaxKind.Constructor:
|
case SyntaxKind.Constructor:
|
||||||
return ts.createConstructor(undefined, undefined, member.parameters, member.body);
|
return createConstructor(undefined, undefined, member.parameters, member.body);
|
||||||
default:
|
default:
|
||||||
return member; // in case anything gets here by accident...
|
return member; // in case anything gets here by accident...
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,20 @@
|
|||||||
import * as ts from 'typescript';
|
import {
|
||||||
|
createBinary,
|
||||||
|
createBlock,
|
||||||
|
createCall,
|
||||||
|
createIdentifier,
|
||||||
|
createIf,
|
||||||
|
createImmediatelyInvokedArrowFunction,
|
||||||
|
createLiteral,
|
||||||
|
createMethod,
|
||||||
|
createReturn,
|
||||||
|
createThis,
|
||||||
|
createTrue,
|
||||||
|
Expression,
|
||||||
|
MethodDeclaration,
|
||||||
|
SyntaxKind,
|
||||||
|
} from 'typescript';
|
||||||
|
|
||||||
import { Logger } from '../../logger';
|
import { Logger } from '../../logger';
|
||||||
import {
|
import {
|
||||||
convertValueToLiteral,
|
convertValueToLiteral,
|
||||||
@ -8,7 +24,7 @@ import {
|
|||||||
getMethodsForDecorator,
|
getMethodsForDecorator,
|
||||||
} from '../helpers';
|
} from '../helpers';
|
||||||
|
|
||||||
export function transformMethod(method: ts.MethodDeclaration) {
|
export function transformMethod(method: MethodDeclaration) {
|
||||||
if (!method) return;
|
if (!method) return;
|
||||||
|
|
||||||
const decorator = getDecorator(method),
|
const decorator = getDecorator(method),
|
||||||
@ -16,7 +32,7 @@ export function transformMethod(method: ts.MethodDeclaration) {
|
|||||||
decoratorArgs = getDecoratorArgs(decorator);
|
decoratorArgs = getDecoratorArgs(decorator);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return ts.createMethod(
|
return createMethod(
|
||||||
undefined,
|
undefined,
|
||||||
undefined,
|
undefined,
|
||||||
undefined,
|
undefined,
|
||||||
@ -25,7 +41,7 @@ export function transformMethod(method: ts.MethodDeclaration) {
|
|||||||
method.typeParameters,
|
method.typeParameters,
|
||||||
method.parameters,
|
method.parameters,
|
||||||
method.type,
|
method.type,
|
||||||
ts.createBlock([ts.createReturn(getMethodBlock(method, decoratorName, decoratorArgs))])
|
createBlock([createReturn(getMethodBlock(method, decoratorName, decoratorArgs))])
|
||||||
);
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
Logger.error('Error transforming method: ' + (method.name as any).text);
|
Logger.error('Error transforming method: ' + (method.name as any).text);
|
||||||
@ -33,30 +49,30 @@ export function transformMethod(method: ts.MethodDeclaration) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getMethodBlock(method: ts.MethodDeclaration, decoratorName: string, decoratorArgs: any): ts.Expression {
|
function getMethodBlock(method: MethodDeclaration, decoratorName: string, decoratorArgs: any): Expression {
|
||||||
const decoratorMethod = getMethodsForDecorator(decoratorName)[0];
|
const decoratorMethod = getMethodsForDecorator(decoratorName)[0];
|
||||||
|
|
||||||
switch (decoratorName) {
|
switch (decoratorName) {
|
||||||
case 'CordovaCheck':
|
case 'CordovaCheck':
|
||||||
case 'InstanceCheck':
|
case 'InstanceCheck':
|
||||||
// TODO remove function wrapper
|
// TODO remove function wrapper
|
||||||
return ts.createImmediatelyInvokedArrowFunction([
|
return createImmediatelyInvokedArrowFunction([
|
||||||
ts.createIf(
|
createIf(
|
||||||
ts.createBinary(
|
createBinary(
|
||||||
ts.createCall(ts.createIdentifier(decoratorMethod), undefined, [ts.createThis()]),
|
createCall(createIdentifier(decoratorMethod), undefined, [createThis()]),
|
||||||
ts.SyntaxKind.EqualsEqualsEqualsToken,
|
SyntaxKind.EqualsEqualsEqualsToken,
|
||||||
ts.createTrue()
|
createTrue()
|
||||||
),
|
),
|
||||||
method.body
|
method.body
|
||||||
),
|
),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return ts.createCall(ts.createIdentifier(decoratorMethod), undefined, [
|
return createCall(createIdentifier(decoratorMethod), undefined, [
|
||||||
ts.createThis(),
|
createThis(),
|
||||||
ts.createLiteral(decoratorArgs?.methodName || (method.name as any).text),
|
createLiteral(decoratorArgs?.methodName || (method.name as any).text),
|
||||||
convertValueToLiteral(decoratorArgs),
|
convertValueToLiteral(decoratorArgs),
|
||||||
ts.createIdentifier('arguments'),
|
createIdentifier('arguments'),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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 { Logger } from '../../logger';
|
||||||
import { convertValueToLiteral, getDecorator, getDecoratorArgs, getDecoratorName } from '../helpers';
|
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
|
// add plugin decorator args as static properties of the plugin's class
|
||||||
for (const prop in pluginDecoratorArgs) {
|
for (const prop in pluginDecoratorArgs) {
|
||||||
pluginStatics.push(
|
pluginStatics.push(
|
||||||
ts.createProperty(
|
createProperty(
|
||||||
undefined,
|
undefined,
|
||||||
[ts.createToken(ts.SyntaxKind.StaticKeyword)],
|
[createToken(SyntaxKind.StaticKeyword)],
|
||||||
ts.createIdentifier(prop),
|
createIdentifier(prop),
|
||||||
undefined,
|
undefined,
|
||||||
undefined,
|
undefined,
|
||||||
convertValueToLiteral(pluginDecoratorArgs[prop])
|
convertValueToLiteral(pluginDecoratorArgs[prop])
|
||||||
@ -28,11 +38,11 @@ function transformClass(cls: any, ngcBuild?: boolean) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cls = ts.createClassDeclaration(
|
cls = createClassDeclaration(
|
||||||
ngcBuild && cls.decorators && cls.decorators.length
|
ngcBuild && cls.decorators && cls.decorators.length
|
||||||
? cls.decorators.filter(d => getDecoratorName(d) === 'Injectable')
|
? cls.decorators.filter(d => getDecoratorName(d) === 'Injectable')
|
||||||
: undefined, // remove Plugin and Injectable decorators
|
: undefined, // remove Plugin and Injectable decorators
|
||||||
[ts.createToken(ts.SyntaxKind.ExportKeyword)],
|
[createToken(SyntaxKind.ExportKeyword)],
|
||||||
cls.name,
|
cls.name,
|
||||||
cls.typeParameters,
|
cls.typeParameters,
|
||||||
cls.heritageClauses,
|
cls.heritageClauses,
|
||||||
@ -43,14 +53,14 @@ function transformClass(cls: any, ngcBuild?: boolean) {
|
|||||||
return cls;
|
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);
|
Logger.silly('Transforming file: ' + file.fileName);
|
||||||
return ts.visitEachChild(
|
return visitEachChild(
|
||||||
file,
|
file,
|
||||||
node => {
|
node => {
|
||||||
if (
|
if (
|
||||||
node.kind !== ts.SyntaxKind.ClassDeclaration ||
|
node.kind !== SyntaxKind.ClassDeclaration ||
|
||||||
(node.modifiers && node.modifiers.find(v => v.kind === ts.SyntaxKind.DeclareKeyword))
|
(node.modifiers && node.modifiers.find(v => v.kind === SyntaxKind.DeclareKeyword))
|
||||||
) {
|
) {
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
@ -60,11 +70,12 @@ function transformClasses(file: ts.SourceFile, ctx: ts.TransformationContext, ng
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function pluginClassTransformer(ngcBuild?: boolean): ts.TransformerFactory<ts.SourceFile> {
|
export function pluginClassTransformer(ngcBuild?: boolean): TransformerFactory<SourceFile> {
|
||||||
return (ctx: ts.TransformationContext) => {
|
return (ctx: TransformationContext) => {
|
||||||
return tsSourceFile => {
|
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 transformClasses(tsSourceFile, ctx, ngcBuild);
|
||||||
|
}
|
||||||
return tsSourceFile;
|
return tsSourceFile;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -1,9 +1,21 @@
|
|||||||
import * as ts from 'typescript';
|
import {
|
||||||
|
createBlock,
|
||||||
|
createCall,
|
||||||
|
createGetAccessor,
|
||||||
|
createIdentifier,
|
||||||
|
createLiteral,
|
||||||
|
createParameter,
|
||||||
|
createReturn,
|
||||||
|
createSetAccessor,
|
||||||
|
createStatement,
|
||||||
|
createThis,
|
||||||
|
PropertyDeclaration,
|
||||||
|
} from 'typescript';
|
||||||
|
|
||||||
import { getDecorator, getDecoratorName } from '../helpers';
|
import { getDecorator, getDecoratorName } from '../helpers';
|
||||||
|
|
||||||
export function transformProperty(members: any[], index: number) {
|
export function transformProperty(members: any[], index: number) {
|
||||||
const property = members[index] as ts.PropertyDeclaration,
|
const property = members[index] as PropertyDeclaration,
|
||||||
decorator = getDecorator(property),
|
decorator = getDecorator(property),
|
||||||
decoratorName = getDecoratorName(decorator);
|
decoratorName = getDecoratorName(decorator);
|
||||||
|
|
||||||
@ -22,33 +34,33 @@ export function transformProperty(members: any[], index: number) {
|
|||||||
return property;
|
return property;
|
||||||
}
|
}
|
||||||
|
|
||||||
const getter = ts.createGetAccessor(
|
const getter = createGetAccessor(
|
||||||
undefined,
|
undefined,
|
||||||
undefined,
|
undefined,
|
||||||
property.name,
|
property.name,
|
||||||
undefined,
|
undefined,
|
||||||
property.type,
|
property.type,
|
||||||
ts.createBlock([
|
createBlock([
|
||||||
ts.createReturn(
|
createReturn(
|
||||||
ts.createCall(ts.createIdentifier(type + 'PropertyGet'), undefined, [
|
createCall(createIdentifier(type + 'PropertyGet'), undefined, [
|
||||||
ts.createThis(),
|
createThis(),
|
||||||
ts.createLiteral((property.name as any).text),
|
createLiteral((property.name as any).text),
|
||||||
])
|
])
|
||||||
),
|
),
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
|
|
||||||
const setter = ts.createSetAccessor(
|
const setter = createSetAccessor(
|
||||||
undefined,
|
undefined,
|
||||||
undefined,
|
undefined,
|
||||||
property.name,
|
property.name,
|
||||||
[ts.createParameter(undefined, undefined, undefined, 'value', undefined, property.type)],
|
[createParameter(undefined, undefined, undefined, 'value', undefined, property.type)],
|
||||||
ts.createBlock([
|
createBlock([
|
||||||
ts.createStatement(
|
createStatement(
|
||||||
ts.createCall(ts.createIdentifier(type + 'PropertySet'), undefined, [
|
createCall(createIdentifier(type + 'PropertySet'), undefined, [
|
||||||
ts.createThis(),
|
createThis(),
|
||||||
ts.createLiteral((property.name as any).text),
|
createLiteral((property.name as any).text),
|
||||||
ts.createIdentifier('value'),
|
createIdentifier('value'),
|
||||||
])
|
])
|
||||||
),
|
),
|
||||||
])
|
])
|
||||||
|
@ -1,28 +1,37 @@
|
|||||||
import * as ts from 'typescript';
|
|
||||||
import { pluginClassTransformer } from './transformers/plugin-class';
|
|
||||||
import { importsTransformer } from './transformers/imports';
|
|
||||||
import { clone } from 'lodash';
|
import { clone } from 'lodash';
|
||||||
import { emitInjectableClasses, extractInjectables } from './transformers/extract-injectables';
|
import {
|
||||||
import { COMPILER_OPTIONS, PLUGIN_PATHS, TS_CONFIG } from './helpers';
|
CompilerHost,
|
||||||
|
CompilerOptions,
|
||||||
|
createCompilerHost,
|
||||||
|
createProgram,
|
||||||
|
ModuleKind,
|
||||||
|
ModuleResolutionKind,
|
||||||
|
ScriptTarget,
|
||||||
|
} from 'typescript';
|
||||||
|
|
||||||
let host: ts.CompilerHost;
|
import { COMPILER_OPTIONS, PLUGIN_PATHS, TS_CONFIG } from './helpers';
|
||||||
|
import { emitInjectableClasses, extractInjectables } from './transformers/extract-injectables';
|
||||||
|
import { importsTransformer } from './transformers/imports';
|
||||||
|
import { pluginClassTransformer } from './transformers/plugin-class';
|
||||||
|
|
||||||
|
let host: CompilerHost;
|
||||||
|
|
||||||
export function getCompilerHost() {
|
export function getCompilerHost() {
|
||||||
if (!host) host = ts.createCompilerHost(TS_CONFIG);
|
if (!host) host = createCompilerHost(TS_CONFIG);
|
||||||
return host;
|
return host;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getProgram(declaration = false, pluginPaths: string[] = PLUGIN_PATHS) {
|
export function getProgram(declaration = false, pluginPaths: string[] = PLUGIN_PATHS) {
|
||||||
const compilerOptions: ts.CompilerOptions = clone(COMPILER_OPTIONS);
|
const compilerOptions: CompilerOptions = clone(COMPILER_OPTIONS);
|
||||||
compilerOptions.declaration = declaration;
|
compilerOptions.declaration = declaration;
|
||||||
compilerOptions.moduleResolution = ts.ModuleResolutionKind.NodeJs;
|
compilerOptions.moduleResolution = ModuleResolutionKind.NodeJs;
|
||||||
compilerOptions.target = ts.ScriptTarget.ES5;
|
compilerOptions.target = ScriptTarget.ES5;
|
||||||
compilerOptions.module = ts.ModuleKind.ES2015;
|
compilerOptions.module = ModuleKind.ES2015;
|
||||||
compilerOptions.inlineSourceMap = true;
|
compilerOptions.inlineSourceMap = true;
|
||||||
compilerOptions.inlineSources = true;
|
compilerOptions.inlineSources = true;
|
||||||
compilerOptions.lib = ['lib.dom.d.ts', 'lib.es5.d.ts', 'lib.es2015.d.ts', 'lib.es2016.d.ts', 'lib.es2017.d.ts'];
|
compilerOptions.lib = ['lib.dom.d.ts', 'lib.es5.d.ts', 'lib.es2015.d.ts', 'lib.es2016.d.ts', 'lib.es2017.d.ts'];
|
||||||
|
|
||||||
return ts.createProgram(pluginPaths, compilerOptions, getCompilerHost());
|
return createProgram(pluginPaths, compilerOptions, getCompilerHost());
|
||||||
}
|
}
|
||||||
|
|
||||||
export function generateDeclarations(sourceFiles?: string[]) {
|
export function generateDeclarations(sourceFiles?: string[]) {
|
||||||
|
Loading…
Reference in New Issue
Block a user