Skip to content

artifact-project/tx-reflector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TX Reflector

TypeScript transformer for code generation.
See also Interface-based instructions.

Install

npm i --save-dev tx-reflector

API

  • getInterfaces(target: Function | object): string[]
  • getInterfaces<T>(target: T): string[]
  • getRawInterfaces<T>(target: T): Interface[]
  • getComponentInterfaces(XClass: Function): string[]

Usage

import {getInterfaces, getRawInterfaces, Interface} from 'tx-reflector';

const data: IData = {value: 'foo'};
const interfaces: string[] = getInterfaces(data); // OR getInterfaces<IData>(anything);
const rawInterfaces: Interface[] = getRawInterfaces(data);;

// After compilation:
//   var interfaces = ["IData", "IAbstractData"];
//   var rawInterfaces = [
//      {
//         name: "IData",
//         entries: [{name: "value", type: "string", optional: false}],
//      }, {
//         name: "IAbstractData",
//         entries: [{name: "source", type: "string", optional: false}],
//      },
//   ];

React and like

import {getComponentInterfaces} from 'tx-reflector';

interface IBtnProps extends IComponent, IClickable {
	value: string;
}

class Btn extends React.Component<IBtnProps> {
}


const interfaces = getComponentInterfaces(Btn);

// After compilation:
//   var interfaces = ["IBtnProps", "IComponent", "IClickable"];

Webpack

Use awesome-typescript-loader or ts-loader 2.3+.

// webpack.config.js
const {default:txReflector} = require('tx-reflector/src/transformer/transformer');

module.exports = {
	// ...
	module: {
		// ...
		{
			test: /\.tsx?$/,
			loader: 'awesome-typescript-loader', // or ts-loader
			options: {
				getCustomTransformers() {
					return {
						before: [txReflector],
						after: [],
					};
				}
			}
		},
	},
	// ...
};

Jest (only with TS 2.4+)

// .jest/tsPreprocessor.js
const tsc = require('typescript');
const tsConfig = require('../tsconfig.json');
const {default:txReflector} = require('tx-reflector/src/transformer/transformer');

module.exports = {
	process(src, path) {
		if (path.endsWith('.ts') || path.endsWith('.tsx')) {
			const result = tsc.transpileModule(src, {
				compilerOptions: tsConfig.compilerOptions,
				fileName: path,
				transformers: {
					before: [txReflector],
					after: [],
				},
			});

			return result.outputText;
		}

		return src;
	},
};

Development