diff --git a/packages/ttypescript/CHANGELOG.md b/packages/ttypescript/CHANGELOG.md index 3fb51c4..293c568 100644 --- a/packages/ttypescript/CHANGELOG.md +++ b/packages/ttypescript/CHANGELOG.md @@ -1,127 +1,2 @@ # Change Log -All notable changes to this project will be documented in this file. -See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. - - -## [1.5.5](https://github.com/cevek/ttypescript/compare/v1.5.4...v1.5.5) (2018-08-31) - - - - -**Note:** Version bump only for package ttypescript - - -## [1.5.4](https://github.com/cevek/ttypescript/compare/v1.5.3...v1.5.4) (2018-07-31) - - - - -**Note:** Version bump only for package ttypescript - - -## [1.5.3](https://github.com/cevek/ttypescript/compare/v1.5.2...v1.5.3) (2018-07-31) - - - - -**Note:** Version bump only for package ttypescript - - -## [1.5.2](https://github.com/cevek/ttypescript/compare/v1.5.1...v1.5.2) (2018-07-30) - - - - -**Note:** Version bump only for package ttypescript - - -## [1.5.1](https://github.com/cevek/ttypescript/compare/v1.5.0...v1.5.1) (2018-07-30) - - - - -**Note:** Version bump only for package ttypescript - - -# [1.5.0](https://github.com/cevek/ttypescript/compare/v1.4.6...v1.5.0) (2018-07-30) - - -### Bug Fixes - -* Add support latest dev ts 3.0 ([810cab7](https://github.com/cevek/ttypescript/commit/810cab7)) -* Create own test tsconfig.json ([6cd1c05](https://github.com/cevek/ttypescript/commit/6cd1c05)) -* Refactor tsc ([abbdc86](https://github.com/cevek/ttypescript/commit/abbdc86)) -* require typescript in tsc ([9a001db](https://github.com/cevek/ttypescript/commit/9a001db)) - - -### Features - -* Make copy of typescript instead of patch it ([f69752a](https://github.com/cevek/ttypescript/commit/f69752a)) - - - - - -## [1.4.6](https://github.com/cevek/ttypescript/compare/v1.4.5...v1.4.6) (2018-06-01) - - -### Bug Fixes - -* Add support ts for 2.7, 2.8, 2.9, 3.0 versions ([ed039b3](https://github.com/cevek/ttypescript/commit/ed039b3)) -* Remove dev deps from packages ([7255e11](https://github.com/cevek/ttypescript/commit/7255e11)) - - - - - -## [1.4.5](https://github.com/cevek/ttypescript/compare/v1.4.4...v1.4.5) (2018-06-01) - - -### Bug Fixes - -* Add support ts 2.9 ([1a738f7](https://github.com/cevek/ttypescript/commit/1a738f7)) - - - - - -## [1.4.4](https://github.com/cevek/ttypescript/compare/v1.4.3...v1.4.4) (2018-05-31) - - -### Bug Fixes - -* remove config key checks ([629dcbc](https://github.com/cevek/ttypescript/commit/629dcbc)) - - - - - -## [1.4.3](https://github.com/cevek/ttypescript/compare/v1.4.2...v1.4.3) (2018-05-17) - - -### Bug Fixes - -* compiler options for transformers ([3cc1167](https://github.com/cevek/ttypescript/commit/3cc1167)) - - - - - -## [1.4.2](https://github.com/cevek/ttypescript/compare/v1.4.1...v1.4.2) (2018-05-17) - - -### Bug Fixes - -* **build:** es5 target for ttypescript build [#14](https://github.com/cevek/ttypescript/issues/14) ([98ae8d4](https://github.com/cevek/ttypescript/commit/98ae8d4)) - - - - - -## [1.4.1](https://github.com/cevek/ttypescript/compare/v1.4.0...v1.4.1) (2018-05-15) - - -### Bug Fixes - -* **package:** removed examples from build ([4f3f40e](https://github.com/cevek/ttypescript/commit/4f3f40e)) diff --git a/packages/ttypescript/README.md b/packages/ttypescript/README.md index 2ecc94a..59f4ca1 100644 --- a/packages/ttypescript/README.md +++ b/packages/ttypescript/README.md @@ -1 +1,307 @@ + +[![npm version](https://badge.fury.io/js/ttypescript.svg)](https://badge.fury.io/js/ttypescript) [![Build Status](https://travis-ci.org/cevek/ttypescript.svg?branch=master)](https://travis-ci.org/cevek/ttypescript) + # 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`: + +```ts +export interface PluginConfig { + /** + * Path to transformer or transformer module name + */ + transform?: string; + + /** + * The optional name of the exported transform plugin in the transform module. + */ + import?: 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 `import`, `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: +```ts +(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: +```ts +(config: PluginConfig) => ts.TransformerFactory +``` +Plugin config entry: `{ "transform": "transformer-module", type: "config" }`. + +#### checker +For the signature with ts.TypeChecker: +```ts +(checker: ts.TypeChecker, config?: PluginConfig) => ts.TransformerFactory +``` +Plugin config entry: `{ "transform": "transformer-module", type: "checker" }`. + +#### raw +For the signature without factory wrapper: +```ts +ts.TransformerFactory +``` +Plugin config entry: `{ "transform": "transformer-module", type: "raw" }`. + +#### compilerOptions +```ts +(compilerOpts: ts.CompilerOptions, config?: PluginConfig) => ts.TransformerFactory +``` +Plugin config entry: `{ "transform": "transformer-module", type: "compilerOptions" }`. + +```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" } + ] + }, +} +``` + +### 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 +``` + +### Parcel + +Just install a parcel plugin +``` +npm i parcel-plugin-ttypescript +``` + + +### Webpack +```js + { + test: /\.(ts|tsx)$/, + loader: require.resolve('awesome-typescript-loader'), + // or + loader: require.resolve('ts-loader'), + options: { + compiler: 'ttypescript' + } + } +``` + +### Rollup +```js +// rollup.config.js +import ttypescript from 'ttypescript' +import tsPlugin from 'rollup-plugin-typescript2' + +export default { + // ... + plugins: [ + // ... + tsPlugin({ + typescript: ttypescript + }) + ] +} +``` + +### VS Code +If you want to compile your project with VS 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", +``` + +### Jest, ts-jest +```js +module.exports = { + // [...] + globals: { + 'ts-jest': { + compiler: 'ttypescript' + } + } +}; +``` +or in `package.json` +```json +{ + "jest": { + "globals": { + "ts-jest": { + "compiler": "ttypescript" + } + } + } +} +``` + +## Transformers + +You can use transformers written in ts or js + +```ts +// 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); + }; + }; +} + +``` + +Examples of transformers + +[`{ "transform": "ts-optchain/transform" }`](https://github.com/rimeto/ts-optchain) + +[`{transform: "typescript-is/lib/transform-inline/transformer"}`](https://github.com/woutervh-/typescript-is) + +[`{transform: "ts-transformer-keys/transformer"}`](https://github.com/kimamula/ts-transformer-keys) + +[`{transform: "ts-transformer-enumerate/transformer"}`](https://github.com/kimamula/ts-transformer-enumerate) + +[`{transform: "ts-transform-graphql-tag/dist/transformer"}`](https://github.com/firede/ts-transform-graphql-tag) + +[`{transform: "ts-transform-img/dist/transform", type: "config"}`](https://github.com/longlho/ts-transform-img) + +[`{transform: "ts-transform-css-modules/dist/transform", type: "config"}`](https://github.com/longlho/ts-transform-css-modules) + +[`{transform: "ts-transform-react-intl/dist/transform", import: "transform", type: "config"}`](https://github.com/longlho/ts-transform-react-intl) + +[`{transform: "ts-nameof", type: "raw"}`](https://github.com/dsherret/ts-nameof) + +[`{transform: "typescript-transform-jsx" }`](https://github.com/LeDDGroup/typescript-transform-jsx) + +[`{transform: "typescript-transform-paths" }`](https://github.com/LeDDGroup/typescript-transform-paths) + +[`{transform: "typescript-transform-macros" }`](https://github.com/LeDDGroup/typescript-transform-macros) + +[`{transform: "ts-transformer-minify-privates" }`](https://github.com/timocov/ts-transformer-minify-privates) + +[`{transform: "typescript-plugin-styled-components", type: "config"}`](https://github.com/Igorbek/typescript-plugin-styled-components#ttypescript-compiler) + +[`{ "transform": "@zoltu/typescript-transformer-append-js-extension" }`](https://github.com/Zoltu/typescript-transformer-append-js-extension) + + +[Tutorial how to write a typescript transformer](https://dev.doctorevidence.com/how-to-write-a-typescript-transform-plugin-fc5308fdd943) + + +## Example +An example project is in the [example](./packages/ttypescript-examples) directory + +## License +MIT License diff --git a/packages/ttypescript/package.json b/packages/ttypescript/package.json index ce50624..a935d5d 100644 --- a/packages/ttypescript/package.json +++ b/packages/ttypescript/package.json @@ -4,7 +4,7 @@ "ttsc": "./bin/tsc", "ttsserver": "./bin/tsserver" }, - "version": "1.5.7", + "version": "1.5.8", "description": "Over TypeScript tool to use custom transformers in the tsconfig.json", "main": "lib/typescript.js", "files": [ diff --git a/packages/ttypescript/src/patchCreateProgram.ts b/packages/ttypescript/src/patchCreateProgram.ts index 4dc80e0..39fcb24 100644 --- a/packages/ttypescript/src/patchCreateProgram.ts +++ b/packages/ttypescript/src/patchCreateProgram.ts @@ -21,7 +21,6 @@ declare module 'typescript' { } export const transformerErrors = new WeakMap(); - export function addDiagnosticFactory(program: ts.Program) { return (diag: ts.Diagnostic) => { const arr = transformerErrors.get(program) || []; @@ -95,7 +94,8 @@ export function patchCreateProgram(tsm: typeof ts, forceReadConfig = false, proj emitOnlyDtsFiles, mergedTransformers ); - result.diagnostics = [...result.diagnostics, ...transformerErrors.get(program)!]; + // todo: doesn't work with 3.7 + // result.diagnostics = [...result.diagnostics, ...transformerErrors.get(program)!]; return result; }; return program; diff --git a/packages/ttypescript/src/tsc.ts b/packages/ttypescript/src/tsc.ts index 21bc2f9..71cf194 100644 --- a/packages/ttypescript/src/tsc.ts +++ b/packages/ttypescript/src/tsc.ts @@ -2,18 +2,16 @@ import * as fs from 'fs'; import * as resolve from 'resolve'; import { loadTypeScript } from './loadTypescript'; import { dirname } from 'path'; -import { runInThisContext } from 'vm' +import { runInThisContext } from 'vm'; const ts = loadTypeScript('typescript', { folder: process.cwd(), forceConfigLoad: true }); const tscFileName = resolve.sync('typescript/lib/tsc', { basedir: process.cwd() }); const commandLineTsCode = fs .readFileSync(tscFileName, 'utf8') .replace(/^[\s\S]+(\(function \(ts\) \{\s+function countLines[\s\S]+)$/, '$1') - .replace('ts.executeCommandLine(ts.sys.args);', ''); -runInThisContext( - `(function (exports, require, module, __filename, __dirname, ts) {${commandLineTsCode}\n});`, - { filename: tscFileName, lineOffset: 0, displayErrors: true } -).call(ts, ts, require, { exports: ts }, tscFileName, dirname(tscFileName), ts); - -(ts as any).executeCommandLine(ts.sys.args); +runInThisContext(`(function (exports, require, module, __filename, __dirname, ts) {${commandLineTsCode}\n});`, { + filename: tscFileName, + lineOffset: 0, + displayErrors: true, +}).call(ts, ts, require, { exports: ts }, tscFileName, dirname(tscFileName), ts);