Coder Social home page Coder Social logo

ttypescript's Introduction

npm version Build Status

ttypescript

What it is

Currently TypeScript doesn't support custom transformers in the tsconfig.json, but supports it programmatically.

And there is no way to compile your files using custom transformers using tsc command.

TTypescript (Transformer TypeScript) solves this problem by patching on the fly the compile module to use transformers from tsconfig.json.

Instead of tsc and tsserver, use ttsc and ttsserver wrappers. This wrappers try to use locally installed typescript first.

No version lock-ins - typescript used as peer dependency.

How to install

npm i ttypescript -D

ttypescript uses your installed typescript in your node_modules

How to use

tsconfig.json

Set a transformer path to the tsconfig.json in compilerOptions section plugin array:

{
    "compilerOptions": {
        "plugins": [
            { "transform": "transformer-module" },
        ]
    }
}

plugin entries described in PluginConfig:

export interface PluginConfig {
    /**
     * Path to transformer or transformer module name
     */
    transform?: string;

    /**
     * Plugin entry point format type, default is program
     */
    type?: 'program' | 'config' | 'checker' | 'raw' | 'compilerOptions';

    /**
     * Should transformer applied after all ones
     */
    after?: boolean;

    /**
     * Should transformer applied for d.ts files, supports from TS2.9
     */
    afterDeclarations?: boolean;
    /**
     * any other properties provided to the transformer as config argument
     * */
    [options: string]: any;
}

You just need to add the transform block with optional type, after, afterDeclarations and plugin-related options.

transform can accept npm module or local file path (.ts or .js) related to tsconfig.json

PluginConfig.type

Because currently transformers can run only programmatically, most of them use factory wrapper with different signatures. For the possible to work with any of them you can specify type in the plugin config.

By default will be used a program type.

program

If the transformer has a factory signature using program as first argument:

(program: ts.Program, config?: PluginConfig) => ts.TransformerFactory
where 
ts.TransformerFactory = (context: ts.TransformationContext) => (sourceFile: ts.SourceFile) => ts.SourceFile

Plugin config entry: { "transform": "transformer-module" }.

config

For the signature with transformer's config:

(config: PluginConfig) => ts.TransformerFactory

Plugin config entry: { "transform": "transformer-module", type: "config" }.

checker

For the signature with ts.TypeChecker:

(checker: ts.TypeChecker, config?: PluginConfig) => ts.TransformerFactory

Plugin config entry: { "transform": "transformer-module", type: "checker" }.

raw

For the signature without factory wrapper:

ts.TransformerFactory

Plugin config entry: { "transform": "transformer-module", type: "raw" }.

compilerOptions

(compilerOpts: ts.CompilerOptions, config?: PluginConfig) => ts.TransformerFactory

Plugin config entry: { "transform": "transformer-module", type: "compilerOptions" }.

Don't forget to exclude your transformers in the tsconfig.json

{
    "compilerOptions": {
        "plugins": [
            { "transform": "transformer-module", "someOption1": 123, "someOption2": 321 },
            { "transform": "./transformers/my-transformer.ts" },
            { "transform": "transformer-module", "after": true },
            { "transform": "transformer-module", "afterDeclarations": true },
            { "transform": "transformer-module", "type": "ls" }
        ]
    },
    "exclude": ["node_modules", "transformers/**/*"]
}

Command line

Like usual tsc, all arguments work the same way.

ttsc

ts-node

ts-node --compiler ttypescript index.ts
or
ts-node -C ttypescript index.ts

Webpack

    {
        test: /\.(ts|tsx)$/,
        loader: require.resolve('awesome-typescript-loader'),
        // or
        loader: require.resolve('ts-loader'),
        options: {
            compiler: 'ttypescript'
        }
    }

Rollup

// rollup.config.js
import ttypescript from 'ttypescript'
import tsPlugin from 'rollup-plugin-typescript2'

export default {
    // ...
    plugins: [
        // ...
        tsPlugin({
            typescript: ttypescript
        })
    ]
}

VC Code

If you want to compile your project with VC Code task runner you need to overwrite the config typescript.tsdk to path of the installed ttypescript:

"typescript.tsdk": "/usr/local/lib/node_modules/ttypescript/lib",
or 
"typescript.tsdk": "node_modules/ttypescript/lib",

Transformers

You can use transformers written in ts or js

// transformer1-module
import * as ts from 'typescript'
export default function (program: ts.Program, pluginOptions: {}) {
    return (ctx: ts.TransformationContext) => {
        return (sourceFile: ts.SourceFile) => {
            return sourceFile;
        }
    }
}

Examples of transformers

{transform: "ts-transformer-keys/transformer"}

{transform: "ts-transformer-enumerate/transformer"}

{transform: "ts-transform-graphql-tag/dist/transformer"}

{transform: "ts-transform-img/dist/transform", type: "config"}

{transform: "ts-transform-css-modules/dist/transform", type: "config"}

{transform: "ts-transform-react-intl/dist/transform", type: "config"}

{transform: "ts-nameof", type: "raw"}

Tutorial how to write a typescript transformer

Example

An example project is in the example directory

License

MIT License

ttypescript's People

Contributors

cevek avatar zerkalica avatar slurptheo avatar dsherret avatar hitkodev avatar supernavix avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.