JSPM

  • Created
  • Published
  • Downloads 355610
  • Score
    100M100P100Q163315F
  • License MIT

Patch typescript to support custom transformers in tsconfig.json

Package Exports

  • ts-patch

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ts-patch) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

npm version Build Status Coverage Status

ts-patch

Description

ts-patch is a tool which patches typescript to allow custom transformers (plugins) to be specified in tsconfig.json.

Its logic is based on ttypescript. (Credit and thanks to cevek for the excellent work!)

Features

  • Easy to patch or unpatch any version of typescript (2.7+)
  • One step setup - no complicated install process
  • Optionally, enable persistence, which re-patches typescript automatically if it is updated
  • Advanced options for patching individual files, specific locations, etc. (see ts-patch /?)

Installation

npm i -g ts-patch

Patch

ts-patch install

For more options, use: ts-patch /?

Transformers Usage

tsconfig.json

Add transformers to compilerOptions in plugin array:

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

Plugin Options

  • transform - path to transformer or module name
  • type (optional) - Plugin entry point format (see below for options)
  • import (optional) - Name of exported transform plugin in transformer module.
  • after (optional) - Apply transformer after all others
  • afterDeclarations (optional) - Apply transformer to d.ts files (supported in TypeScript 2.9+)
  • [custom options] - Supply additional options to transformer

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

Plugin Types

program (default)

Factory signature (program as first argument):

(program: ts.Program, config: PluginConfig | undefined, helpers: { ts: typeof ts, addDiagnostic: (diag: ts.Diagnostic) => void }) => ts.TransformerFactory
where 
ts.TransformerFactory = (context: ts.TransformationContext) => (sourceFile: ts.SourceFile) => ts.SourceFile

Config Example: { "transform": "transformer-module" }. Note: addDiagnostic() can only add Diagnostic entries to EmitResult.diagnostic It cannot be used to alter semantic diagnostics

config

Signature with transformer's config:

(config: PluginConfig) => ts.TransformerFactory

Config Example: { "transform": "transformer-module", type: "config" }.

checker

Signature with ts.TypeChecker:

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

Config Example: { "transform": "transformer-module", type: "checker" }.

raw

Signature without factory wrapper:

ts.TransformerFactory

Config Example: { "transform": "transformer-module", type: "raw" }.

compilerOptions

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

Config Example: { "transform": "transformer-module", type: "compilerOptions" }.

Examples

{
    "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" }
        ]
    }
}

Transformers

You can write transformers in TypeScript or JavaScript

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

Example Transformers:

{ "transform": "ts-optchain/transform" }

{transform: "typescript-is/lib/transform-inline/transformer"}

{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", import: "transform", type: "config"}

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

{transform: "typescript-transform-jsx" }

{transform: "typescript-transform-paths" }

{transform: "typescript-transform-macros" }

{transform: "ts-transformer-minify-privates" }

{transform: "typescript-plugin-styled-components", type: "config"}

{ "transform": "@zoltu/typescript-transformer-append-js-extension" }

Authors

License

This project is licensed under the MIT License