ttsc 0.3.1 → 0.4.0-dev.20260425
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/LICENSE +1 -1
- package/README.md +93 -362
- package/cmd/ttsc/build.go +128 -0
- package/cmd/ttsc/main.go +222 -0
- package/cmd/ttsc/main_test.go +288 -0
- package/cmd/ttsc/transform.go +166 -0
- package/driver/host.go +31 -0
- package/driver/program.go +298 -0
- package/driver/rewrite.go +315 -0
- package/go.mod +48 -0
- package/go.sum +22 -0
- package/lib/api.d.ts +91 -0
- package/lib/api.js +325 -0
- package/lib/api.js.map +1 -0
- package/lib/cli.d.ts +1 -0
- package/lib/cli.js +481 -0
- package/lib/cli.js.map +1 -0
- package/lib/index.d.ts +18 -0
- package/lib/index.js +35 -0
- package/lib/index.js.map +1 -0
- package/lib/native.d.ts +15 -0
- package/lib/native.js +34 -0
- package/lib/native.js.map +1 -0
- package/lib/platform.d.ts +66 -0
- package/lib/platform.js +170 -0
- package/lib/platform.js.map +1 -0
- package/lib/plugin.d.ts +47 -0
- package/lib/plugin.js +150 -0
- package/lib/plugin.js.map +1 -0
- package/lib/project.d.ts +32 -0
- package/lib/project.js +303 -0
- package/lib/project.js.map +1 -0
- package/lib/runner/cli.d.ts +1 -0
- package/lib/runner/cli.js +284 -0
- package/lib/runner/cli.js.map +1 -0
- package/lib/runner/register.d.ts +13 -0
- package/lib/runner/register.js +424 -0
- package/lib/runner/register.js.map +1 -0
- package/package.json +72 -63
- package/shim/ast/extra-shim.json +4 -0
- package/shim/ast/go.mod +5 -0
- package/shim/ast/shim.go +117 -0
- package/shim/bundled/extra-shim.json +1 -0
- package/shim/bundled/go.mod +5 -0
- package/shim/bundled/shim.go +19 -0
- package/shim/checker/extra-shim.json +31 -0
- package/shim/checker/go.mod +5 -0
- package/shim/checker/shim.go +146 -0
- package/shim/compiler/extra-shim.json +4 -0
- package/shim/compiler/go.mod +5 -0
- package/shim/compiler/shim.go +50 -0
- package/shim/core/extra-shim.json +4 -0
- package/shim/core/go.mod +5 -0
- package/shim/core/shim.go +11 -0
- package/shim/diagnosticwriter/go.mod +5 -0
- package/shim/diagnosticwriter/shim.go +29 -0
- package/shim/parser/extra-shim.json +4 -0
- package/shim/parser/go.mod +5 -0
- package/shim/parser/shim.go +55 -0
- package/shim/scanner/extra-shim.json +4 -0
- package/shim/scanner/go.mod +5 -0
- package/shim/scanner/shim.go +96 -0
- package/shim/tsoptions/extra-shim.json +4 -0
- package/shim/tsoptions/go.mod +5 -0
- package/shim/tsoptions/shim.go +123 -0
- package/shim/tspath/extra-shim.json +4 -0
- package/shim/tspath/go.mod +5 -0
- package/shim/tspath/shim.go +165 -0
- package/shim/vfs/cachedvfs/extra-shim.json +4 -0
- package/shim/vfs/cachedvfs/go.mod +5 -0
- package/shim/vfs/cachedvfs/shim.go +12 -0
- package/shim/vfs/extra-shim.json +4 -0
- package/shim/vfs/go.mod +5 -0
- package/shim/vfs/osvfs/extra-shim.json +4 -0
- package/shim/vfs/osvfs/go.mod +5 -0
- package/shim/vfs/osvfs/shim.go +13 -0
- package/shim/vfs/shim.go +19 -0
- package/src/api.ts +416 -0
- package/src/cli.ts +492 -0
- package/src/index.ts +19 -0
- package/src/launcher/ttsc-dev.js +42 -0
- package/src/launcher/ttsc.js +19 -0
- package/src/launcher/ttsx.js +19 -0
- package/src/native.ts +60 -0
- package/src/platform.ts +163 -0
- package/src/plugin.ts +241 -0
- package/src/project.ts +330 -0
- package/src/runner/cli.ts +286 -0
- package/src/runner/register.ts +487 -0
- package/test/native.test.ts +60 -0
- package/test/package.test.ts +131 -0
- package/test/platform.test.ts +58 -0
- package/test/project.test.ts +195 -0
- package/tools/gen_shims/go.mod +14 -0
- package/tools/gen_shims/go.sum +12 -0
- package/tools/gen_shims/main.go +446 -0
- package/CHANGELOG.md +0 -2
- package/bin/tsc +0 -2
- package/bin/tsserver +0 -2
- package/lib/PluginCreator.d.ts +0 -67
- package/lib/PluginCreator.js +0 -190
- package/lib/cancelationToken.d.ts +0 -0
- package/lib/cancelationToken.js +0 -2
- package/lib/loadTypescript.d.ts +0 -7
- package/lib/loadTypescript.js +0 -56
- package/lib/patchCreateProgram.d.ts +0 -21
- package/lib/patchCreateProgram.js +0 -190
- package/lib/tsc.d.ts +0 -1
- package/lib/tsc.js +0 -24
- package/lib/tsserver.d.ts +0 -0
- package/lib/tsserver.js +0 -2
- package/lib/tsserverlibrary.d.ts +0 -3
- package/lib/tsserverlibrary.js +0 -4
- package/lib/typescript.d.ts +0 -3
- package/lib/typescript.js +0 -4
- package/lib/typescriptServices.d.ts +0 -3
- package/lib/typescriptServices.js +0 -4
- package/lib/typingsInstaller.d.ts +0 -0
- package/lib/typingsInstaller.js +0 -2
- package/lib/watchGuard.d.ts +0 -0
- package/lib/watchGuard.js +0 -2
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,396 +1,127 @@
|
|
|
1
|
-
# ttsc
|
|
1
|
+
# `ttsc`
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
[](https://www.npmjs.com/package/ttsc)
|
|
5
|
-
[](https://www.npmjs.com/package/ttsc)
|
|
3
|
+

|
|
6
4
|
|
|
7
|
-
|
|
5
|
+
[](https://github.com/samchon/ttsc/blob/master/LICENSE)
|
|
6
|
+
[](https://www.npmjs.com/package/ttsc)
|
|
7
|
+
[](https://www.npmjs.com/package/ttsc)
|
|
8
|
+
[](https://github.com/samchon/ttsc/actions?query=workflow%3Atest)
|
|
8
9
|
|
|
9
|
-
|
|
10
|
+
A `typescript-go` toolchain for compiler-powered transforms and type-safe execution.
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
- **`ttsc`**: build, check, and transform.
|
|
13
|
+
- **`ttsx`**: execute TypeScript with type checking.
|
|
14
|
+
- 10x faster than `ts-node`.
|
|
15
|
+
- type checking that `tsx` does not provide.
|
|
16
|
+
- **transformer support**: compiler-powered libraries, such as `typia`.
|
|
12
17
|
|
|
13
|
-
|
|
18
|
+
> `ttsx` (CLI command) can run existing `typescript@6` projects.
|
|
14
19
|
|
|
15
|
-
|
|
20
|
+
## Setup
|
|
16
21
|
|
|
17
|
-
|
|
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
|
|
22
|
+
Install the native TypeScript preview package with `ttsc`:
|
|
66
23
|
|
|
24
|
+
```bash
|
|
25
|
+
npm i -D ttsc @typescript/native-preview
|
|
67
26
|
```
|
|
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
27
|
|
|
77
|
-
|
|
28
|
+
Run TypeScript directly with `ttsx` (CLI command):
|
|
78
29
|
|
|
30
|
+
```bash
|
|
31
|
+
npx ttsx src/index.ts
|
|
79
32
|
```
|
|
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
33
|
|
|
108
|
-
|
|
109
|
-
* Should transformer applied after all ones
|
|
110
|
-
*/
|
|
111
|
-
after?: boolean;
|
|
34
|
+
Build, check, or watch the project with `ttsc`:
|
|
112
35
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* any other properties provided to the transformer as config argument
|
|
119
|
-
* */
|
|
120
|
-
[options: string]: any;
|
|
121
|
-
}
|
|
36
|
+
```bash
|
|
37
|
+
npx ttsc
|
|
38
|
+
npx ttsc --noEmit
|
|
39
|
+
npx ttsc --watch
|
|
122
40
|
```
|
|
123
41
|
|
|
124
|
-
|
|
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
|
-
```
|
|
42
|
+
## Transformer Configuration
|
|
198
43
|
|
|
199
|
-
|
|
44
|
+
`ttsc` reads `compilerOptions.plugins` from `tsconfig.json`.
|
|
200
45
|
|
|
201
46
|
```json
|
|
202
47
|
{
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
{ "transform": "transformer-module", "afterDeclarations": true },
|
|
209
|
-
{ "transform": "transformer-module", "type": "ls" }
|
|
210
|
-
]
|
|
211
|
-
}
|
|
48
|
+
"compilerOptions": {
|
|
49
|
+
"plugins": [
|
|
50
|
+
{ "transform": "typia/lib/transform" }
|
|
51
|
+
]
|
|
52
|
+
}
|
|
212
53
|
}
|
|
213
54
|
```
|
|
214
55
|
|
|
215
|
-
|
|
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
|
-
```
|
|
56
|
+
The same configuration is used by both `ttsc` commands.
|
|
280
57
|
|
|
281
|
-
|
|
58
|
+
```bash
|
|
59
|
+
# compile
|
|
60
|
+
npx ttsc
|
|
282
61
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
// [...]
|
|
286
|
-
globals: {
|
|
287
|
-
'ts-jest': {
|
|
288
|
-
compiler: 'ttypescript',
|
|
289
|
-
},
|
|
290
|
-
},
|
|
291
|
-
};
|
|
62
|
+
# execute
|
|
63
|
+
npx ttsx src/index.ts
|
|
292
64
|
```
|
|
293
65
|
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
```json
|
|
297
|
-
{
|
|
298
|
-
"jest": {
|
|
299
|
-
"globals": {
|
|
300
|
-
"ts-jest": {
|
|
301
|
-
"compiler": "ttypescript"
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
```
|
|
66
|
+
This gives compiler-powered libraries one transform path for both build-time and runtime execution.
|
|
307
67
|
|
|
308
|
-
##
|
|
68
|
+
## What Is a Transform?
|
|
309
69
|
|
|
310
|
-
|
|
70
|
+
A transform uses TypeScript types to generate JavaScript before runtime.
|
|
311
71
|
|
|
312
72
|
```ts
|
|
313
|
-
|
|
314
|
-
import
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
73
|
+
import typia, { tags } from "typia";
|
|
74
|
+
import { v4 } from "uuid";
|
|
75
|
+
|
|
76
|
+
const matched: boolean = typia.is<IMember>({
|
|
77
|
+
id: v4(),
|
|
78
|
+
email: "samchon.github@gmail.com",
|
|
79
|
+
age: 30,
|
|
80
|
+
});
|
|
81
|
+
console.log(matched); // true
|
|
82
|
+
|
|
83
|
+
interface IMember {
|
|
84
|
+
id: string & tags.Format<"uuid">;
|
|
85
|
+
email: string & tags.Format<"email">;
|
|
86
|
+
age: number &
|
|
87
|
+
tags.Type<"uint32"> &
|
|
88
|
+
tags.ExclusiveMinimum<19> &
|
|
89
|
+
tags.Maximum<100>;
|
|
327
90
|
}
|
|
328
91
|
```
|
|
329
92
|
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
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
|
|
93
|
+
It is transformed into dedicated JavaScript:
|
|
94
|
+
|
|
95
|
+
> ```js
|
|
96
|
+
> import typia from "typia";
|
|
97
|
+
> import * as __typia_transform__isFormatEmail from "typia/lib/internal/_isFormatEmail";
|
|
98
|
+
> import * as __typia_transform__isFormatUuid from "typia/lib/internal/_isFormatUuid";
|
|
99
|
+
> import * as __typia_transform__isTypeUint32 from "typia/lib/internal/_isTypeUint32";
|
|
100
|
+
> import { v4 } from "uuid";
|
|
101
|
+
>
|
|
102
|
+
> const matched = (() => {
|
|
103
|
+
> const _io0 = (input) =>
|
|
104
|
+
> "string" === typeof input.id &&
|
|
105
|
+
> __typia_transform__isFormatUuid._isFormatUuid(input.id) &&
|
|
106
|
+
> "string" === typeof input.email &&
|
|
107
|
+
> __typia_transform__isFormatEmail._isFormatEmail(input.email) &&
|
|
108
|
+
> "number" === typeof input.age &&
|
|
109
|
+
> __typia_transform__isTypeUint32._isTypeUint32(input.age) &&
|
|
110
|
+
> 19 < input.age &&
|
|
111
|
+
> input.age <= 100;
|
|
112
|
+
> return (input) => "object" === typeof input && null !== input && _io0(input);
|
|
113
|
+
> })()({
|
|
114
|
+
> id: v4(),
|
|
115
|
+
> email: "samchon.github@gmai19l.com",
|
|
116
|
+
> age: 30,
|
|
117
|
+
> });
|
|
118
|
+
> console.log(matched); // true
|
|
119
|
+
> ```
|
|
120
|
+
|
|
121
|
+
`ttsc` runs this transform during build, and `ttsx` runs through the same transform path during execution.
|
|
122
|
+
|
|
123
|
+
## References
|
|
124
|
+
|
|
125
|
+
- TypeScript runners: [`ts-node`](https://github.com/TypeStrong/ts-node) and [`tsx`](https://github.com/privatenumber/tsx)
|
|
126
|
+
- Transformer tooling: [`ttypescript`](https://github.com/cevek/ttypescript) and [`ts-patch`](https://github.com/nonara/ts-patch)
|
|
127
|
+
- Inspired by: [`typical`](https://github.com/elliots/typical) and [`tsgonest`](https://github.com/tsgonest/tsgonest)
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
package main
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"encoding/json"
|
|
5
|
+
"flag"
|
|
6
|
+
"fmt"
|
|
7
|
+
"os"
|
|
8
|
+
"path/filepath"
|
|
9
|
+
|
|
10
|
+
shimcompiler "github.com/microsoft/typescript-go/shim/compiler"
|
|
11
|
+
|
|
12
|
+
"github.com/samchon/ttsc/packages/ttsc/driver"
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
// runBuild implements the `ttsc` project-build lane. Two modes:
|
|
16
|
+
//
|
|
17
|
+
// - default (dry run): analyze quietly, surfacing only diagnostics/errors.
|
|
18
|
+
// - --emit: also run tsgo's emit pipeline, patching every recognized native
|
|
19
|
+
// consumer call in the resulting .js files.
|
|
20
|
+
func runBuild(args []string) int {
|
|
21
|
+
fs := flag.NewFlagSet("build", flag.ContinueOnError)
|
|
22
|
+
fs.SetOutput(stderr)
|
|
23
|
+
tsconfigPath := fs.String("tsconfig", "tsconfig.json", "path to tsconfig.json")
|
|
24
|
+
cwdOverride := fs.String("cwd", "", "override the working directory (defaults to process cwd)")
|
|
25
|
+
quiet := fs.Bool("quiet", true, "suppress the per-call diagnostic summary")
|
|
26
|
+
verbose := fs.Bool("verbose", false, "print the per-call diagnostic summary")
|
|
27
|
+
emit := fs.Bool("emit", false, "force emitted .js files (runs tsgo + ttsc rewrite)")
|
|
28
|
+
noEmit := fs.Bool("noEmit", false, "force analysis-only run with no file writes")
|
|
29
|
+
outDir := fs.String("outDir", "", "override compilerOptions.outDir for this build")
|
|
30
|
+
manifestPath := fs.String("manifest", "", "write emitted file list as JSON to this path")
|
|
31
|
+
rewriteMode := fs.String("rewrite-mode", "none", "native rewrite backend id")
|
|
32
|
+
if err := fs.Parse(args); err != nil {
|
|
33
|
+
return 2
|
|
34
|
+
}
|
|
35
|
+
if *emit && *noEmit {
|
|
36
|
+
fmt.Fprintln(stderr, "ttsc: --emit and --noEmit are mutually exclusive")
|
|
37
|
+
return 2
|
|
38
|
+
}
|
|
39
|
+
if *verbose {
|
|
40
|
+
*quiet = false
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
cwd := *cwdOverride
|
|
44
|
+
if cwd == "" {
|
|
45
|
+
var err error
|
|
46
|
+
cwd, err = os.Getwd()
|
|
47
|
+
if err != nil {
|
|
48
|
+
fmt.Fprintf(stderr, "ttsc: could not get working directory: %v\n", err)
|
|
49
|
+
return 2
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
prog, diags, err := driver.LoadProgram(cwd, *tsconfigPath, driver.LoadProgramOptions{
|
|
54
|
+
ForceEmit: *emit,
|
|
55
|
+
ForceNoEmit: *noEmit,
|
|
56
|
+
OutDir: *outDir,
|
|
57
|
+
})
|
|
58
|
+
if err != nil {
|
|
59
|
+
fmt.Fprintf(stderr, "ttsc: %v\n", err)
|
|
60
|
+
return 2
|
|
61
|
+
}
|
|
62
|
+
if len(diags) > 0 {
|
|
63
|
+
driver.WritePrettyDiagnostics(stderr, diags, cwd)
|
|
64
|
+
return 2
|
|
65
|
+
}
|
|
66
|
+
defer prog.Close()
|
|
67
|
+
if diags := prog.Diagnostics(); len(diags) > 0 {
|
|
68
|
+
driver.WritePrettyDiagnostics(stderr, diags, cwd)
|
|
69
|
+
return 2
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
rewrites := driver.NewRewriteSet()
|
|
73
|
+
shouldEmit := !prog.ParsedConfig.ParsedConfig.CompilerOptions.NoEmit.IsTrue()
|
|
74
|
+
if *rewriteMode != "none" {
|
|
75
|
+
fmt.Fprintf(stderr, "ttsc: rewrite backend %q is not built into the standalone ttsc binary\n", *rewriteMode)
|
|
76
|
+
fmt.Fprintln(stderr, "ttsc: run through the JS host with the matching compiler plugin so it can select the consumer-provided native binary")
|
|
77
|
+
return 2
|
|
78
|
+
}
|
|
79
|
+
if !*quiet {
|
|
80
|
+
fmt.Fprintf(stdout, "// ttsc: tsconfig=%s cwd=%s sites=%d emit=%v rewrite=%s\n", *tsconfigPath, cwd, 0, shouldEmit, *rewriteMode)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if shouldEmit {
|
|
84
|
+
writeFile := shimcompiler.WriteFile(
|
|
85
|
+
func(fileName, text string, _ *shimcompiler.WriteFileData) error {
|
|
86
|
+
return driver.DefaultWriteFile(fileName, text)
|
|
87
|
+
},
|
|
88
|
+
)
|
|
89
|
+
res, eDiags, err := prog.EmitAll(rewrites, writeFile)
|
|
90
|
+
if err != nil {
|
|
91
|
+
fmt.Fprintf(stderr, "ttsc: emit failed: %v\n", err)
|
|
92
|
+
return 3
|
|
93
|
+
}
|
|
94
|
+
for _, d := range eDiags {
|
|
95
|
+
fmt.Fprintln(stderr, " -", d.String())
|
|
96
|
+
}
|
|
97
|
+
if !*quiet {
|
|
98
|
+
fmt.Fprintf(stdout, "// ttsc: emitted=%d files\n", len(res.EmittedFiles))
|
|
99
|
+
for _, f := range res.EmittedFiles {
|
|
100
|
+
rel := f
|
|
101
|
+
if abs, err := filepath.Rel(cwd, f); err == nil {
|
|
102
|
+
rel = abs
|
|
103
|
+
}
|
|
104
|
+
fmt.Fprintln(stdout, " +", rel)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if *manifestPath != "" {
|
|
108
|
+
data, err := json.Marshal(res.EmittedFiles)
|
|
109
|
+
if err != nil {
|
|
110
|
+
fmt.Fprintf(stderr, "ttsc: manifest marshal failed: %v\n", err)
|
|
111
|
+
return 3
|
|
112
|
+
}
|
|
113
|
+
if err := os.MkdirAll(filepath.Dir(*manifestPath), 0o755); err != nil {
|
|
114
|
+
fmt.Fprintf(stderr, "ttsc: manifest mkdir failed: %v\n", err)
|
|
115
|
+
return 3
|
|
116
|
+
}
|
|
117
|
+
if err := os.WriteFile(*manifestPath, data, 0o644); err != nil {
|
|
118
|
+
fmt.Fprintf(stderr, "ttsc: manifest write failed: %v\n", err)
|
|
119
|
+
return 3
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if !*quiet {
|
|
125
|
+
fmt.Fprintf(stdout, "// ttsc: recognized=%d total=%d rewrites=%d\n", 0, 0, rewrites.Len())
|
|
126
|
+
}
|
|
127
|
+
return 0
|
|
128
|
+
}
|