Skip to content

Latest commit

 

History

History
177 lines (125 loc) · 4.16 KB

INSTALLATION.md

File metadata and controls

177 lines (125 loc) · 4.16 KB

ts-reflection

< Back to project

Installation

ts-reflection is a TypeScript transformer - it inspects the types and generates runtime code based on them. It is compatible with rollup, webpack and ttypescript projects and works nicely with jest.

You will first need to install ts-reflection using npm, yarn or similar:

# NPM
npm install --dev ts-reflection

# Yarn
yarn add -D ts-reflection

Webpack

See example here

In order to enable ts-reflection in your Webpack project you need to configure ts-loader or awesome-typescript-loader in you Webpack config.

1. Import the transformer

// Using ES6 imports
import transformer from 'ts-reflection/transformer';

// Or using the old syntax
const transformer = require('ts-reflection/transformer').default;

2. Adjust your ts-loader / awesome-typescript-loader configuration

{
  test: /\.ts(x)?$/,
  loader: 'ts-loader', // Or 'awesome-typescript-loader'
  options: {
    getCustomTransformers: program => ({
      before: [transformer(program)],
    }),
  },
}

Rollup

See example here

In order to enable ts-reflection in your Rollup project you need to configure ts-loader or awesome-typescript-loader in you rollup config.

1. Import the transformer

import transformer from 'ts-reflection/transformer';

2. Option 1: Adjust your @wessberg/rollup-plugin-ts plugin configuration

import ts from '@wessberg/rollup-plugin-ts';

// ...

ts({
  transformers: [
    ({ program }) => ({
      before: transformer(program),
    }),
  ],
}),

2. Option 2: Adjust your rollup-plugin-typescript2 plugin configuration

import typescript from 'rollup-plugin-typescript2';

// ...

typescript({
  transformers: [
    service => ({
      before: [transformer(service.getProgram())],
      after: [],
    }),
  ],
}),

TTypeScript

See example here

1. Install ttypescript

# NPM
npm install --dev ttypescript

# Yarn
yarn add -D ttypescript

2. Add ts-reflection transformer

In order to enable ts-reflection in your TTypescript project you need to configure plugins in your tsconfig.json.

{
  "compilerOptions": {
    "plugins": [
      { "transform": "ts-reflection/transformer" }
    ]
  }
}

Jest

See example here

In order to enable ts-reflection in your Jest tests you need to switch to ttypescript compiler.

1. Configure ttypescript

See the instructions above.

2. Set ttypescript as your compiler

In your jest.config.js (or package.json):

module.exports = {
  preset: 'ts-jest',
  globals: {
    'ts-jest': {
      compiler: 'ttypescript',
    },
  },
};

ts-node

See example here

1. Configure ttypescript

See the instructions above.

2. Set ttypescript as your compiler

Either using command line:

$ ts-node --compiler ttypescript ...

Or the programmatic API:

require('ts-node').register({
  compiler: 'ttypescript'
})