ttsc 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2 -2
- package/LICENSE +21 -21
- package/README.md +396 -396
- package/bin/tsc +2 -2
- package/bin/tsserver +2 -2
- package/lib/PluginCreator.d.ts +67 -67
- package/lib/PluginCreator.js +190 -190
- package/lib/cancelationToken.js +2 -2
- package/lib/loadTypescript.d.ts +7 -7
- package/lib/loadTypescript.js +56 -56
- package/lib/patchCreateProgram.d.ts +21 -21
- package/lib/patchCreateProgram.js +184 -186
- package/lib/tsc.d.ts +1 -1
- package/lib/tsc.js +24 -24
- package/lib/tsserver.js +2 -2
- package/lib/tsserverlibrary.d.ts +3 -3
- package/lib/tsserverlibrary.js +4 -4
- package/lib/typescript.d.ts +3 -3
- package/lib/typescript.js +4 -5
- package/lib/typescriptServices.d.ts +3 -3
- package/lib/typescriptServices.js +4 -4
- package/lib/typingsInstaller.js +2 -2
- package/lib/watchGuard.js +2 -2
- package/package.json +65 -65
package/README.md
CHANGED
|
@@ -1,396 +1,396 @@
|
|
|
1
|
-
# ttsc
|
|
2
|
-
|
|
3
|
-
[](https://github.com/solar-religion/ttsc/blob/main/LICENSE)
|
|
4
|
-
[](https://www.npmjs.com/package/ttsc)
|
|
5
|
-
[](https://www.npmjs.com/package/ttsc)
|
|
6
|
-
|
|
7
|
-
This repository was created due to a perceived lack of management of `ttypescript`.
|
|
8
|
-
|
|
9
|
-
Also, we don't currently support `ts-node` and `cache systems`, as they weren't supported in the original, and we need time to figure out a way around that.
|
|
10
|
-
|
|
11
|
-
And it supports TS5.
|
|
12
|
-
|
|
13
|
-
[](https://badge.fury.io/js/ttypescript) [](https://travis-ci.org/cevek/ttypescript)
|
|
14
|
-
|
|
15
|
-
# ttypescript
|
|
16
|
-
|
|
17
|
-
## Existing contributors
|
|
18
|
-
|
|
19
|
-
- @cevek
|
|
20
|
-
- @zerkalica
|
|
21
|
-
- @SalvatorePreviti
|
|
22
|
-
- @timocov
|
|
23
|
-
- @SlurpTheo
|
|
24
|
-
- @samchon
|
|
25
|
-
- @henriquehbr
|
|
26
|
-
- @JiangWeixian
|
|
27
|
-
- @mdmitry01
|
|
28
|
-
- @jirutka
|
|
29
|
-
- @alexgorbatchev
|
|
30
|
-
- @oscard0m
|
|
31
|
-
- @sveyret
|
|
32
|
-
- @Jack-Works
|
|
33
|
-
- @MicahZoltu
|
|
34
|
-
- @Igorbek
|
|
35
|
-
- @gfx
|
|
36
|
-
- @danielpza
|
|
37
|
-
- @mikeyhew
|
|
38
|
-
- @huafu
|
|
39
|
-
- @dsherret
|
|
40
|
-
- @SupernaviX
|
|
41
|
-
- @HitkoDev
|
|
42
|
-
|
|
43
|
-
## What it is
|
|
44
|
-
|
|
45
|
-
Currently TypeScript doesn't support custom transformers in the tsconfig.json, but supports it programmatically.
|
|
46
|
-
|
|
47
|
-
And there is no way to compile your files using custom transformers using `tsc` command.
|
|
48
|
-
|
|
49
|
-
TTypescript (Transformer TypeScript) solves this problem by patching on the fly the compile module to use transformers from `tsconfig.json`.
|
|
50
|
-
|
|
51
|
-
<!---
|
|
52
|
-
TTypescript is a drop-in replacement for all typescript modules, located in ``` node_modules/typescript/lib ``` directory:
|
|
53
|
-
|
|
54
|
-
```ts
|
|
55
|
-
import * as ts from 'ttypescript'
|
|
56
|
-
import * as tsServer from 'ttypescript/lib/tsserver'
|
|
57
|
-
import * as watchGuard from 'ttypescript/lib/watchGuard'
|
|
58
|
-
```
|
|
59
|
-
--->
|
|
60
|
-
|
|
61
|
-
Instead of tsc and tsserver, use ttsc and ttsserver wrappers. This wrappers try to use locally installed typescript first.
|
|
62
|
-
|
|
63
|
-
No version lock-ins - typescript used as peer dependency.
|
|
64
|
-
|
|
65
|
-
## How to install
|
|
66
|
-
|
|
67
|
-
```
|
|
68
|
-
npm i ttypescript -D
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
ttypescript uses your installed `typescript` in your `node_modules`
|
|
72
|
-
|
|
73
|
-
## How to use
|
|
74
|
-
|
|
75
|
-
### tsconfig.json
|
|
76
|
-
|
|
77
|
-
Set a transformer path to the `tsconfig.json` in `compilerOptions` section `plugin` array:
|
|
78
|
-
|
|
79
|
-
```
|
|
80
|
-
{
|
|
81
|
-
"compilerOptions": {
|
|
82
|
-
"plugins": [
|
|
83
|
-
{ "transform": "transformer-module" },
|
|
84
|
-
]
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
```
|
|
88
|
-
|
|
89
|
-
plugin entries described in `PluginConfig`:
|
|
90
|
-
|
|
91
|
-
```ts
|
|
92
|
-
export interface PluginConfig {
|
|
93
|
-
/**
|
|
94
|
-
* Path to transformer or transformer module name
|
|
95
|
-
*/
|
|
96
|
-
transform?: string;
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* The optional name of the exported transform plugin in the transform module.
|
|
100
|
-
*/
|
|
101
|
-
import?: string;
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Plugin entry point format type, default is program
|
|
105
|
-
*/
|
|
106
|
-
type?: 'program' | 'config' | 'checker' | 'raw' | 'compilerOptions';
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Should transformer applied after all ones
|
|
110
|
-
*/
|
|
111
|
-
after?: boolean;
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Should transformer applied for d.ts files, supports from TS2.9
|
|
115
|
-
*/
|
|
116
|
-
afterDeclarations?: boolean;
|
|
117
|
-
/**
|
|
118
|
-
* any other properties provided to the transformer as config argument
|
|
119
|
-
* */
|
|
120
|
-
[options: string]: any;
|
|
121
|
-
}
|
|
122
|
-
```
|
|
123
|
-
|
|
124
|
-
You just need to add the `transform` block with optional `import`, `type`, `after`, `afterDeclarations` and plugin-related options.
|
|
125
|
-
|
|
126
|
-
`transform` can accept npm module or local file path (.ts or .js) related to `tsconfig.json`
|
|
127
|
-
|
|
128
|
-
### PluginConfig.type
|
|
129
|
-
|
|
130
|
-
Because currently transformers can run only programmatically, most of them use factory wrapper with different signatures.
|
|
131
|
-
For the possible to work with any of them you can specify `type` in the plugin config.
|
|
132
|
-
|
|
133
|
-
By default will be used a `program` type.
|
|
134
|
-
|
|
135
|
-
<!---
|
|
136
|
-
|
|
137
|
-
Most of transformers exports ``` ts.TransformerFactory<ts.SourceFile> ```.
|
|
138
|
-
But better to export TransformerBasePlugin, described below. In most cases it's a plugin responsibility to when run a transformer.
|
|
139
|
-
|
|
140
|
-
```ts
|
|
141
|
-
export interface TransformerBasePlugin {
|
|
142
|
-
before?: ts.TransformerFactory<ts.SourceFile>;
|
|
143
|
-
after?: ts.TransformerFactory<ts.SourceFile>;
|
|
144
|
-
afterDeclarations?: ts.TransformerFactory<ts.SourceFile>;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
export type TransformerPlugin = TransformerBasePlugin | ts.TransformerFactory<ts.SourceFile>;
|
|
148
|
-
```
|
|
149
|
-
--->
|
|
150
|
-
|
|
151
|
-
#### program
|
|
152
|
-
|
|
153
|
-
If the transformer has a factory signature using `program` as first argument:
|
|
154
|
-
|
|
155
|
-
```ts
|
|
156
|
-
(program: ts.Program, config?: PluginConfig) => ts.TransformerFactory;
|
|
157
|
-
where;
|
|
158
|
-
ts.TransformerFactory = (context: ts.TransformationContext) => (sourceFile: ts.SourceFile) => ts.SourceFile;
|
|
159
|
-
```
|
|
160
|
-
|
|
161
|
-
Plugin config entry: `{ "transform": "transformer-module" }`.
|
|
162
|
-
|
|
163
|
-
#### config
|
|
164
|
-
|
|
165
|
-
For the signature with transformer's config:
|
|
166
|
-
|
|
167
|
-
```ts
|
|
168
|
-
(config: PluginConfig) => ts.TransformerFactory;
|
|
169
|
-
```
|
|
170
|
-
|
|
171
|
-
Plugin config entry: `{ "transform": "transformer-module", type: "config" }`.
|
|
172
|
-
|
|
173
|
-
#### checker
|
|
174
|
-
|
|
175
|
-
For the signature with ts.TypeChecker:
|
|
176
|
-
|
|
177
|
-
```ts
|
|
178
|
-
(checker: ts.TypeChecker, config?: PluginConfig) => ts.TransformerFactory;
|
|
179
|
-
```
|
|
180
|
-
|
|
181
|
-
Plugin config entry: `{ "transform": "transformer-module", type: "checker" }`.
|
|
182
|
-
|
|
183
|
-
#### raw
|
|
184
|
-
|
|
185
|
-
For the signature without factory wrapper:
|
|
186
|
-
|
|
187
|
-
```ts
|
|
188
|
-
ts.TransformerFactory;
|
|
189
|
-
```
|
|
190
|
-
|
|
191
|
-
Plugin config entry: `{ "transform": "transformer-module", type: "raw" }`.
|
|
192
|
-
|
|
193
|
-
#### compilerOptions
|
|
194
|
-
|
|
195
|
-
```ts
|
|
196
|
-
(compilerOpts: ts.CompilerOptions, config?: PluginConfig) => ts.TransformerFactory;
|
|
197
|
-
```
|
|
198
|
-
|
|
199
|
-
Plugin config entry: `{ "transform": "transformer-module", type: "compilerOptions" }`.
|
|
200
|
-
|
|
201
|
-
```json
|
|
202
|
-
{
|
|
203
|
-
"compilerOptions": {
|
|
204
|
-
"plugins": [
|
|
205
|
-
{ "transform": "transformer-module", "someOption1": 123, "someOption2": 321 },
|
|
206
|
-
{ "transform": "./transformers/my-transformer.ts" },
|
|
207
|
-
{ "transform": "transformer-module", "after": true },
|
|
208
|
-
{ "transform": "transformer-module", "afterDeclarations": true },
|
|
209
|
-
{ "transform": "transformer-module", "type": "ls" }
|
|
210
|
-
]
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
```
|
|
214
|
-
|
|
215
|
-
### Command line
|
|
216
|
-
|
|
217
|
-
Like usual `tsc`, all arguments work the same way.
|
|
218
|
-
|
|
219
|
-
```
|
|
220
|
-
ttsc
|
|
221
|
-
```
|
|
222
|
-
|
|
223
|
-
### ts-node
|
|
224
|
-
|
|
225
|
-
```
|
|
226
|
-
ts-node --compiler ttypescript index.ts
|
|
227
|
-
or
|
|
228
|
-
ts-node -C ttypescript index.ts
|
|
229
|
-
```
|
|
230
|
-
|
|
231
|
-
### Parcel
|
|
232
|
-
|
|
233
|
-
Just install a parcel plugin
|
|
234
|
-
|
|
235
|
-
```
|
|
236
|
-
npm i parcel-plugin-ttypescript
|
|
237
|
-
```
|
|
238
|
-
|
|
239
|
-
### Webpack
|
|
240
|
-
|
|
241
|
-
```js
|
|
242
|
-
{
|
|
243
|
-
test: /\.(ts|tsx)$/,
|
|
244
|
-
loader: require.resolve('awesome-typescript-loader'),
|
|
245
|
-
// or
|
|
246
|
-
loader: require.resolve('ts-loader'),
|
|
247
|
-
options: {
|
|
248
|
-
compiler: 'ttypescript'
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
```
|
|
252
|
-
|
|
253
|
-
### Rollup
|
|
254
|
-
|
|
255
|
-
```js
|
|
256
|
-
// rollup.config.js
|
|
257
|
-
import ttypescript from 'ttypescript';
|
|
258
|
-
import tsPlugin from 'rollup-plugin-typescript2';
|
|
259
|
-
|
|
260
|
-
export default {
|
|
261
|
-
// ...
|
|
262
|
-
plugins: [
|
|
263
|
-
// ...
|
|
264
|
-
tsPlugin({
|
|
265
|
-
typescript: ttypescript,
|
|
266
|
-
}),
|
|
267
|
-
],
|
|
268
|
-
};
|
|
269
|
-
```
|
|
270
|
-
|
|
271
|
-
### VS Code
|
|
272
|
-
|
|
273
|
-
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`:
|
|
274
|
-
|
|
275
|
-
```
|
|
276
|
-
"typescript.tsdk": "/usr/local/lib/node_modules/ttypescript/lib",
|
|
277
|
-
or
|
|
278
|
-
"typescript.tsdk": "node_modules/ttypescript/lib",
|
|
279
|
-
```
|
|
280
|
-
|
|
281
|
-
### Jest, ts-jest
|
|
282
|
-
|
|
283
|
-
```js
|
|
284
|
-
module.exports = {
|
|
285
|
-
// [...]
|
|
286
|
-
globals: {
|
|
287
|
-
'ts-jest': {
|
|
288
|
-
compiler: 'ttypescript',
|
|
289
|
-
},
|
|
290
|
-
},
|
|
291
|
-
};
|
|
292
|
-
```
|
|
293
|
-
|
|
294
|
-
or in `package.json`
|
|
295
|
-
|
|
296
|
-
```json
|
|
297
|
-
{
|
|
298
|
-
"jest": {
|
|
299
|
-
"globals": {
|
|
300
|
-
"ts-jest": {
|
|
301
|
-
"compiler": "ttypescript"
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
```
|
|
307
|
-
|
|
308
|
-
## Transformers
|
|
309
|
-
|
|
310
|
-
You can use transformers written in ts or js
|
|
311
|
-
|
|
312
|
-
```ts
|
|
313
|
-
// transformer1-module
|
|
314
|
-
import * as ts from 'typescript';
|
|
315
|
-
export default function (program: ts.Program, pluginOptions: {}) {
|
|
316
|
-
return (ctx: ts.TransformationContext) => {
|
|
317
|
-
return (sourceFile: ts.SourceFile) => {
|
|
318
|
-
function visitor(node: ts.Node): ts.Node {
|
|
319
|
-
// if (ts.isCallExpression(node)) {
|
|
320
|
-
// return ts.createLiteral('call');
|
|
321
|
-
// }
|
|
322
|
-
return ts.visitEachChild(node, visitor, ctx);
|
|
323
|
-
}
|
|
324
|
-
return ts.visitEachChild(sourceFile, visitor, ctx);
|
|
325
|
-
};
|
|
326
|
-
};
|
|
327
|
-
}
|
|
328
|
-
```
|
|
329
|
-
|
|
330
|
-
Examples of transformers:
|
|
331
|
-
|
|
332
|
-
[`{ "transform": "ts-nameof", type: "raw"}`](https://github.com/dsherret/ts-nameof)
|
|
333
|
-
|
|
334
|
-
[`{ "transform": "ts-optchain/transform" }`](https://github.com/rimeto/ts-optchain)
|
|
335
|
-
|
|
336
|
-
[`{ "transform": "ts-transform-asset" }`](https://github.com/slune-org/ts-transform-asset)
|
|
337
|
-
|
|
338
|
-
[`{ "transform": "ts-transform-auto-require" }`](https://github.com/slune-org/ts-transform-auto-require)
|
|
339
|
-
|
|
340
|
-
[`{ "transform": "ts-transform-css-modules/dist/transform", type: "config" }`](https://github.com/longlho/ts-transform-css-modules)
|
|
341
|
-
|
|
342
|
-
[`{ "transform": "ts-transform-graphql-tag/dist/transformer" }`](https://github.com/firede/ts-transform-graphql-tag)
|
|
343
|
-
|
|
344
|
-
[`{ "transform": "ts-transform-img/dist/transform", type: "config" }`](https://github.com/longlho/ts-transform-img)
|
|
345
|
-
|
|
346
|
-
[`{ "transform": "ts-transform-react-intl/dist/transform", import: "transform", type: "config" }`](https://github.com/longlho/ts-transform-react-intl)
|
|
347
|
-
|
|
348
|
-
[`{ "transform": "ts-transformer-enumerate/transformer" }`](https://github.com/kimamula/ts-transformer-enumerate)
|
|
349
|
-
|
|
350
|
-
[`{ "transform": "ts-transformer-keys/transformer" }`](https://github.com/kimamula/ts-transformer-keys)
|
|
351
|
-
|
|
352
|
-
[`{ "transform": "ts-transformer-minify-privates" }`](https://github.com/timocov/ts-transformer-minify-privates)
|
|
353
|
-
|
|
354
|
-
[`{ "transform": "typescript-is/lib/transform-inline/transformer" }`](https://github.com/woutervh-/typescript-is)
|
|
355
|
-
|
|
356
|
-
[`{ "transform": "typescript-plugin-styled-components", type: "config" }`](https://github.com/Igorbek/typescript-plugin-styled-components#ttypescript-compiler)
|
|
357
|
-
|
|
358
|
-
[`{ "transform": "typescript-transform-jsx" }`](https://github.com/LeDDGroup/typescript-transform-jsx)
|
|
359
|
-
|
|
360
|
-
[`{ "transform": "typescript-transform-macros" }`](https://github.com/LeDDGroup/typescript-transform-macros)
|
|
361
|
-
|
|
362
|
-
[`{ "transform": "typescript-transform-paths" }`](https://github.com/LeDDGroup/typescript-transform-paths)
|
|
363
|
-
|
|
364
|
-
[`{ "transform": "@zerollup/ts-transform-paths" }`](https://github.com/zerkalica/zerollup/tree/master/packages/ts-transform-paths)
|
|
365
|
-
|
|
366
|
-
[`{ "transform": "@zoltu/typescript-transformer-append-js-extension" }`](https://github.com/Zoltu/typescript-transformer-append-js-extension)
|
|
367
|
-
|
|
368
|
-
[`{ "transform": "@magic-works/ttypescript-browser-like-import-transformer" }`](https://github.com/Jack-Works/ttypescript-browser-like-import-transformer)
|
|
369
|
-
|
|
370
|
-
[`{ "transform": "typescript-transform-react-jsx-source" }`](https://github.com/alexgorbatchev/typescript-transform-react-jsx-source)
|
|
371
|
-
|
|
372
|
-
[`{ "transform": "ts-transformer-remove-named-export" }`](https://github.com/dominguezcelada/ts-transformer-remove-named-export)
|
|
373
|
-
|
|
374
|
-
[`{ "transform": "ts-transformer-export-default-name" }`](https://github.com/jirutka/ts-transformer-export-default-name)
|
|
375
|
-
|
|
376
|
-
[`{ "transform": "ts-transformer-inline-file/transformer" }`](https://github.com/jirutka/ts-transformer-inline-file)
|
|
377
|
-
|
|
378
|
-
[`{ "transform": "ts-transformer-strip-const-enums", "entrySourceFiles": ["./src/index.ts" }`](https://github.com/timocov/ts-transformer-strip-const-enums)
|
|
379
|
-
|
|
380
|
-
[`{ "transform": "ts-transformer-properties-rename", "entrySourceFiles": ["./src/index.ts"] }`](https://github.com/timocov/ts-transformer-properties-rename)
|
|
381
|
-
|
|
382
|
-
[`{ "transform": "tsc-progress", "name": "TSC", "color": "green" }`](https://github.com/JiangWeixian/tsc-progress)
|
|
383
|
-
|
|
384
|
-
[Tutorial how to write a typescript transformer](https://dev.doctorevidence.com/how-to-write-a-typescript-transform-plugin-fc5308fdd943)
|
|
385
|
-
|
|
386
|
-
- [Tutorial how to write a typescript transformer](https://dev.doctorevidence.com/how-to-write-a-typescript-transform-plugin-fc5308fdd943)
|
|
387
|
-
- [Transformer framework](https://github.com/slune-org/simple-ts-transform)
|
|
388
|
-
- [Unit testing transformer compiler](https://github.com/slune-org/ts-transform-test-compiler)
|
|
389
|
-
|
|
390
|
-
## Example
|
|
391
|
-
|
|
392
|
-
An example project is in the [example](./packages/ttypescript-examples) directory
|
|
393
|
-
|
|
394
|
-
## License
|
|
395
|
-
|
|
396
|
-
MIT License
|
|
1
|
+
# ttsc
|
|
2
|
+
|
|
3
|
+
[](https://github.com/solar-religion/ttsc/blob/main/LICENSE)
|
|
4
|
+
[](https://www.npmjs.com/package/ttsc)
|
|
5
|
+
[](https://www.npmjs.com/package/ttsc)
|
|
6
|
+
|
|
7
|
+
This repository was created due to a perceived lack of management of `ttypescript`.
|
|
8
|
+
|
|
9
|
+
Also, we don't currently support `ts-node` and `cache systems`, as they weren't supported in the original, and we need time to figure out a way around that.
|
|
10
|
+
|
|
11
|
+
And it supports TS5.
|
|
12
|
+
|
|
13
|
+
[](https://badge.fury.io/js/ttypescript) [](https://travis-ci.org/cevek/ttypescript)
|
|
14
|
+
|
|
15
|
+
# ttypescript
|
|
16
|
+
|
|
17
|
+
## Existing contributors
|
|
18
|
+
|
|
19
|
+
- @cevek
|
|
20
|
+
- @zerkalica
|
|
21
|
+
- @SalvatorePreviti
|
|
22
|
+
- @timocov
|
|
23
|
+
- @SlurpTheo
|
|
24
|
+
- @samchon
|
|
25
|
+
- @henriquehbr
|
|
26
|
+
- @JiangWeixian
|
|
27
|
+
- @mdmitry01
|
|
28
|
+
- @jirutka
|
|
29
|
+
- @alexgorbatchev
|
|
30
|
+
- @oscard0m
|
|
31
|
+
- @sveyret
|
|
32
|
+
- @Jack-Works
|
|
33
|
+
- @MicahZoltu
|
|
34
|
+
- @Igorbek
|
|
35
|
+
- @gfx
|
|
36
|
+
- @danielpza
|
|
37
|
+
- @mikeyhew
|
|
38
|
+
- @huafu
|
|
39
|
+
- @dsherret
|
|
40
|
+
- @SupernaviX
|
|
41
|
+
- @HitkoDev
|
|
42
|
+
|
|
43
|
+
## What it is
|
|
44
|
+
|
|
45
|
+
Currently TypeScript doesn't support custom transformers in the tsconfig.json, but supports it programmatically.
|
|
46
|
+
|
|
47
|
+
And there is no way to compile your files using custom transformers using `tsc` command.
|
|
48
|
+
|
|
49
|
+
TTypescript (Transformer TypeScript) solves this problem by patching on the fly the compile module to use transformers from `tsconfig.json`.
|
|
50
|
+
|
|
51
|
+
<!---
|
|
52
|
+
TTypescript is a drop-in replacement for all typescript modules, located in ``` node_modules/typescript/lib ``` directory:
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
import * as ts from 'ttypescript'
|
|
56
|
+
import * as tsServer from 'ttypescript/lib/tsserver'
|
|
57
|
+
import * as watchGuard from 'ttypescript/lib/watchGuard'
|
|
58
|
+
```
|
|
59
|
+
--->
|
|
60
|
+
|
|
61
|
+
Instead of tsc and tsserver, use ttsc and ttsserver wrappers. This wrappers try to use locally installed typescript first.
|
|
62
|
+
|
|
63
|
+
No version lock-ins - typescript used as peer dependency.
|
|
64
|
+
|
|
65
|
+
## How to install
|
|
66
|
+
|
|
67
|
+
```
|
|
68
|
+
npm i ttypescript -D
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
ttypescript uses your installed `typescript` in your `node_modules`
|
|
72
|
+
|
|
73
|
+
## How to use
|
|
74
|
+
|
|
75
|
+
### tsconfig.json
|
|
76
|
+
|
|
77
|
+
Set a transformer path to the `tsconfig.json` in `compilerOptions` section `plugin` array:
|
|
78
|
+
|
|
79
|
+
```
|
|
80
|
+
{
|
|
81
|
+
"compilerOptions": {
|
|
82
|
+
"plugins": [
|
|
83
|
+
{ "transform": "transformer-module" },
|
|
84
|
+
]
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
plugin entries described in `PluginConfig`:
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
export interface PluginConfig {
|
|
93
|
+
/**
|
|
94
|
+
* Path to transformer or transformer module name
|
|
95
|
+
*/
|
|
96
|
+
transform?: string;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* The optional name of the exported transform plugin in the transform module.
|
|
100
|
+
*/
|
|
101
|
+
import?: string;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Plugin entry point format type, default is program
|
|
105
|
+
*/
|
|
106
|
+
type?: 'program' | 'config' | 'checker' | 'raw' | 'compilerOptions';
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Should transformer applied after all ones
|
|
110
|
+
*/
|
|
111
|
+
after?: boolean;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Should transformer applied for d.ts files, supports from TS2.9
|
|
115
|
+
*/
|
|
116
|
+
afterDeclarations?: boolean;
|
|
117
|
+
/**
|
|
118
|
+
* any other properties provided to the transformer as config argument
|
|
119
|
+
* */
|
|
120
|
+
[options: string]: any;
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
You just need to add the `transform` block with optional `import`, `type`, `after`, `afterDeclarations` and plugin-related options.
|
|
125
|
+
|
|
126
|
+
`transform` can accept npm module or local file path (.ts or .js) related to `tsconfig.json`
|
|
127
|
+
|
|
128
|
+
### PluginConfig.type
|
|
129
|
+
|
|
130
|
+
Because currently transformers can run only programmatically, most of them use factory wrapper with different signatures.
|
|
131
|
+
For the possible to work with any of them you can specify `type` in the plugin config.
|
|
132
|
+
|
|
133
|
+
By default will be used a `program` type.
|
|
134
|
+
|
|
135
|
+
<!---
|
|
136
|
+
|
|
137
|
+
Most of transformers exports ``` ts.TransformerFactory<ts.SourceFile> ```.
|
|
138
|
+
But better to export TransformerBasePlugin, described below. In most cases it's a plugin responsibility to when run a transformer.
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
export interface TransformerBasePlugin {
|
|
142
|
+
before?: ts.TransformerFactory<ts.SourceFile>;
|
|
143
|
+
after?: ts.TransformerFactory<ts.SourceFile>;
|
|
144
|
+
afterDeclarations?: ts.TransformerFactory<ts.SourceFile>;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export type TransformerPlugin = TransformerBasePlugin | ts.TransformerFactory<ts.SourceFile>;
|
|
148
|
+
```
|
|
149
|
+
--->
|
|
150
|
+
|
|
151
|
+
#### program
|
|
152
|
+
|
|
153
|
+
If the transformer has a factory signature using `program` as first argument:
|
|
154
|
+
|
|
155
|
+
```ts
|
|
156
|
+
(program: ts.Program, config?: PluginConfig) => ts.TransformerFactory;
|
|
157
|
+
where;
|
|
158
|
+
ts.TransformerFactory = (context: ts.TransformationContext) => (sourceFile: ts.SourceFile) => ts.SourceFile;
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
Plugin config entry: `{ "transform": "transformer-module" }`.
|
|
162
|
+
|
|
163
|
+
#### config
|
|
164
|
+
|
|
165
|
+
For the signature with transformer's config:
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
(config: PluginConfig) => ts.TransformerFactory;
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
Plugin config entry: `{ "transform": "transformer-module", type: "config" }`.
|
|
172
|
+
|
|
173
|
+
#### checker
|
|
174
|
+
|
|
175
|
+
For the signature with ts.TypeChecker:
|
|
176
|
+
|
|
177
|
+
```ts
|
|
178
|
+
(checker: ts.TypeChecker, config?: PluginConfig) => ts.TransformerFactory;
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
Plugin config entry: `{ "transform": "transformer-module", type: "checker" }`.
|
|
182
|
+
|
|
183
|
+
#### raw
|
|
184
|
+
|
|
185
|
+
For the signature without factory wrapper:
|
|
186
|
+
|
|
187
|
+
```ts
|
|
188
|
+
ts.TransformerFactory;
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
Plugin config entry: `{ "transform": "transformer-module", type: "raw" }`.
|
|
192
|
+
|
|
193
|
+
#### compilerOptions
|
|
194
|
+
|
|
195
|
+
```ts
|
|
196
|
+
(compilerOpts: ts.CompilerOptions, config?: PluginConfig) => ts.TransformerFactory;
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
Plugin config entry: `{ "transform": "transformer-module", type: "compilerOptions" }`.
|
|
200
|
+
|
|
201
|
+
```json
|
|
202
|
+
{
|
|
203
|
+
"compilerOptions": {
|
|
204
|
+
"plugins": [
|
|
205
|
+
{ "transform": "transformer-module", "someOption1": 123, "someOption2": 321 },
|
|
206
|
+
{ "transform": "./transformers/my-transformer.ts" },
|
|
207
|
+
{ "transform": "transformer-module", "after": true },
|
|
208
|
+
{ "transform": "transformer-module", "afterDeclarations": true },
|
|
209
|
+
{ "transform": "transformer-module", "type": "ls" }
|
|
210
|
+
]
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
### Command line
|
|
216
|
+
|
|
217
|
+
Like usual `tsc`, all arguments work the same way.
|
|
218
|
+
|
|
219
|
+
```
|
|
220
|
+
ttsc
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
### ts-node
|
|
224
|
+
|
|
225
|
+
```
|
|
226
|
+
ts-node --compiler ttypescript index.ts
|
|
227
|
+
or
|
|
228
|
+
ts-node -C ttypescript index.ts
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
### Parcel
|
|
232
|
+
|
|
233
|
+
Just install a parcel plugin
|
|
234
|
+
|
|
235
|
+
```
|
|
236
|
+
npm i parcel-plugin-ttypescript
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### Webpack
|
|
240
|
+
|
|
241
|
+
```js
|
|
242
|
+
{
|
|
243
|
+
test: /\.(ts|tsx)$/,
|
|
244
|
+
loader: require.resolve('awesome-typescript-loader'),
|
|
245
|
+
// or
|
|
246
|
+
loader: require.resolve('ts-loader'),
|
|
247
|
+
options: {
|
|
248
|
+
compiler: 'ttypescript'
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
### Rollup
|
|
254
|
+
|
|
255
|
+
```js
|
|
256
|
+
// rollup.config.js
|
|
257
|
+
import ttypescript from 'ttypescript';
|
|
258
|
+
import tsPlugin from 'rollup-plugin-typescript2';
|
|
259
|
+
|
|
260
|
+
export default {
|
|
261
|
+
// ...
|
|
262
|
+
plugins: [
|
|
263
|
+
// ...
|
|
264
|
+
tsPlugin({
|
|
265
|
+
typescript: ttypescript,
|
|
266
|
+
}),
|
|
267
|
+
],
|
|
268
|
+
};
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
### VS Code
|
|
272
|
+
|
|
273
|
+
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`:
|
|
274
|
+
|
|
275
|
+
```
|
|
276
|
+
"typescript.tsdk": "/usr/local/lib/node_modules/ttypescript/lib",
|
|
277
|
+
or
|
|
278
|
+
"typescript.tsdk": "node_modules/ttypescript/lib",
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
### Jest, ts-jest
|
|
282
|
+
|
|
283
|
+
```js
|
|
284
|
+
module.exports = {
|
|
285
|
+
// [...]
|
|
286
|
+
globals: {
|
|
287
|
+
'ts-jest': {
|
|
288
|
+
compiler: 'ttypescript',
|
|
289
|
+
},
|
|
290
|
+
},
|
|
291
|
+
};
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
or in `package.json`
|
|
295
|
+
|
|
296
|
+
```json
|
|
297
|
+
{
|
|
298
|
+
"jest": {
|
|
299
|
+
"globals": {
|
|
300
|
+
"ts-jest": {
|
|
301
|
+
"compiler": "ttypescript"
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
## Transformers
|
|
309
|
+
|
|
310
|
+
You can use transformers written in ts or js
|
|
311
|
+
|
|
312
|
+
```ts
|
|
313
|
+
// transformer1-module
|
|
314
|
+
import * as ts from 'typescript';
|
|
315
|
+
export default function (program: ts.Program, pluginOptions: {}) {
|
|
316
|
+
return (ctx: ts.TransformationContext) => {
|
|
317
|
+
return (sourceFile: ts.SourceFile) => {
|
|
318
|
+
function visitor(node: ts.Node): ts.Node {
|
|
319
|
+
// if (ts.isCallExpression(node)) {
|
|
320
|
+
// return ts.createLiteral('call');
|
|
321
|
+
// }
|
|
322
|
+
return ts.visitEachChild(node, visitor, ctx);
|
|
323
|
+
}
|
|
324
|
+
return ts.visitEachChild(sourceFile, visitor, ctx);
|
|
325
|
+
};
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
Examples of transformers:
|
|
331
|
+
|
|
332
|
+
[`{ "transform": "ts-nameof", type: "raw"}`](https://github.com/dsherret/ts-nameof)
|
|
333
|
+
|
|
334
|
+
[`{ "transform": "ts-optchain/transform" }`](https://github.com/rimeto/ts-optchain)
|
|
335
|
+
|
|
336
|
+
[`{ "transform": "ts-transform-asset" }`](https://github.com/slune-org/ts-transform-asset)
|
|
337
|
+
|
|
338
|
+
[`{ "transform": "ts-transform-auto-require" }`](https://github.com/slune-org/ts-transform-auto-require)
|
|
339
|
+
|
|
340
|
+
[`{ "transform": "ts-transform-css-modules/dist/transform", type: "config" }`](https://github.com/longlho/ts-transform-css-modules)
|
|
341
|
+
|
|
342
|
+
[`{ "transform": "ts-transform-graphql-tag/dist/transformer" }`](https://github.com/firede/ts-transform-graphql-tag)
|
|
343
|
+
|
|
344
|
+
[`{ "transform": "ts-transform-img/dist/transform", type: "config" }`](https://github.com/longlho/ts-transform-img)
|
|
345
|
+
|
|
346
|
+
[`{ "transform": "ts-transform-react-intl/dist/transform", import: "transform", type: "config" }`](https://github.com/longlho/ts-transform-react-intl)
|
|
347
|
+
|
|
348
|
+
[`{ "transform": "ts-transformer-enumerate/transformer" }`](https://github.com/kimamula/ts-transformer-enumerate)
|
|
349
|
+
|
|
350
|
+
[`{ "transform": "ts-transformer-keys/transformer" }`](https://github.com/kimamula/ts-transformer-keys)
|
|
351
|
+
|
|
352
|
+
[`{ "transform": "ts-transformer-minify-privates" }`](https://github.com/timocov/ts-transformer-minify-privates)
|
|
353
|
+
|
|
354
|
+
[`{ "transform": "typescript-is/lib/transform-inline/transformer" }`](https://github.com/woutervh-/typescript-is)
|
|
355
|
+
|
|
356
|
+
[`{ "transform": "typescript-plugin-styled-components", type: "config" }`](https://github.com/Igorbek/typescript-plugin-styled-components#ttypescript-compiler)
|
|
357
|
+
|
|
358
|
+
[`{ "transform": "typescript-transform-jsx" }`](https://github.com/LeDDGroup/typescript-transform-jsx)
|
|
359
|
+
|
|
360
|
+
[`{ "transform": "typescript-transform-macros" }`](https://github.com/LeDDGroup/typescript-transform-macros)
|
|
361
|
+
|
|
362
|
+
[`{ "transform": "typescript-transform-paths" }`](https://github.com/LeDDGroup/typescript-transform-paths)
|
|
363
|
+
|
|
364
|
+
[`{ "transform": "@zerollup/ts-transform-paths" }`](https://github.com/zerkalica/zerollup/tree/master/packages/ts-transform-paths)
|
|
365
|
+
|
|
366
|
+
[`{ "transform": "@zoltu/typescript-transformer-append-js-extension" }`](https://github.com/Zoltu/typescript-transformer-append-js-extension)
|
|
367
|
+
|
|
368
|
+
[`{ "transform": "@magic-works/ttypescript-browser-like-import-transformer" }`](https://github.com/Jack-Works/ttypescript-browser-like-import-transformer)
|
|
369
|
+
|
|
370
|
+
[`{ "transform": "typescript-transform-react-jsx-source" }`](https://github.com/alexgorbatchev/typescript-transform-react-jsx-source)
|
|
371
|
+
|
|
372
|
+
[`{ "transform": "ts-transformer-remove-named-export" }`](https://github.com/dominguezcelada/ts-transformer-remove-named-export)
|
|
373
|
+
|
|
374
|
+
[`{ "transform": "ts-transformer-export-default-name" }`](https://github.com/jirutka/ts-transformer-export-default-name)
|
|
375
|
+
|
|
376
|
+
[`{ "transform": "ts-transformer-inline-file/transformer" }`](https://github.com/jirutka/ts-transformer-inline-file)
|
|
377
|
+
|
|
378
|
+
[`{ "transform": "ts-transformer-strip-const-enums", "entrySourceFiles": ["./src/index.ts" }`](https://github.com/timocov/ts-transformer-strip-const-enums)
|
|
379
|
+
|
|
380
|
+
[`{ "transform": "ts-transformer-properties-rename", "entrySourceFiles": ["./src/index.ts"] }`](https://github.com/timocov/ts-transformer-properties-rename)
|
|
381
|
+
|
|
382
|
+
[`{ "transform": "tsc-progress", "name": "TSC", "color": "green" }`](https://github.com/JiangWeixian/tsc-progress)
|
|
383
|
+
|
|
384
|
+
[Tutorial how to write a typescript transformer](https://dev.doctorevidence.com/how-to-write-a-typescript-transform-plugin-fc5308fdd943)
|
|
385
|
+
|
|
386
|
+
- [Tutorial how to write a typescript transformer](https://dev.doctorevidence.com/how-to-write-a-typescript-transform-plugin-fc5308fdd943)
|
|
387
|
+
- [Transformer framework](https://github.com/slune-org/simple-ts-transform)
|
|
388
|
+
- [Unit testing transformer compiler](https://github.com/slune-org/ts-transform-test-compiler)
|
|
389
|
+
|
|
390
|
+
## Example
|
|
391
|
+
|
|
392
|
+
An example project is in the [example](./packages/ttypescript-examples) directory
|
|
393
|
+
|
|
394
|
+
## License
|
|
395
|
+
|
|
396
|
+
MIT License
|