fix(webpack): make NativeClass transformer backwards compatible

This commit is contained in:
Igor Randjelovic
2022-11-14 13:13:06 +01:00
parent 9091e43f03
commit 59624a4ebe
2 changed files with 15 additions and 13 deletions

View File

@@ -8,5 +8,3 @@ module.exports = (env) => {
return webpack.resolveConfig();
};

View File

@@ -1,20 +1,24 @@
import ts from 'typescript';
/**
* A TypeScript transform that compiles classes marked with @NativeClass as es5 & commonjs
*
* @param ctx
* A TypeScript transform that compiles classes marked with `@NativeClass` as es5
*/
export default function (ctx: ts.TransformationContext) {
function isNativeClassExtension(node: ts.ClassDeclaration) {
return (
ts.canHaveDecorators(node) &&
ts.getDecorators(node) &&
ts.getDecorators(node).filter((d) => {
const fullText = d.getFullText().trim();
return fullText.indexOf('@NativeClass') > -1;
}).length > 0
);
let decorators: Readonly<ts.Decorator[]>;
if ('canHaveDecorators' in ts && ts.canHaveDecorators(node)) {
// use the newer decorators API when using a newer typescript version
decorators = ts.getDecorators(node);
} else {
// fallback to old behavior on older typescript versions
decorators = node.decorators;
}
return !!decorators?.some((d) => {
const fullText = d.getFullText().trim();
return fullText.indexOf('@NativeClass') > -1;
});
}
function visitNode(node: ts.Node): ts.Node {
if (ts.isClassDeclaration(node) && isNativeClassExtension(node)) {