ttsc 0.3.0 → 0.4.0-dev.20260424
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 +216 -302
- 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 +17 -0
- package/lib/index.js +34 -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/package.json +70 -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 +18 -0
- package/src/launcher/ttsc-dev.js +42 -0
- package/src/launcher/ttsc.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/test/native.test.ts +60 -0
- package/test/package.test.ts +123 -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 -184
- 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,310 @@
|
|
|
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` compiler host for build-time transforms.
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
- **Build and check**: run the project through the native TypeScript compiler lane.
|
|
13
|
+
- **Transformer support**: load `compilerOptions.plugins` from `tsconfig.json`.
|
|
14
|
+
- **Library author surface**: publish `ttsc` plugins with native backends or JS output hooks.
|
|
12
15
|
|
|
13
|
-
|
|
16
|
+
`ttsc` is the compiler host used by `ttsx`. Use `ttsc` when you want one project-aware path for type checking, JavaScript emit, transformer loading, and native backend selection.
|
|
14
17
|
|
|
15
|
-
|
|
18
|
+
## Setup
|
|
16
19
|
|
|
17
|
-
|
|
20
|
+
Install the native TypeScript preview package with `ttsc`:
|
|
18
21
|
|
|
19
|
-
|
|
20
|
-
-
|
|
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'
|
|
22
|
+
```bash
|
|
23
|
+
npm i -D ttsc @typescript/native-preview
|
|
58
24
|
```
|
|
59
|
-
--->
|
|
60
|
-
|
|
61
|
-
Instead of tsc and tsserver, use ttsc and ttsserver wrappers. This wrappers try to use locally installed typescript first.
|
|
62
25
|
|
|
63
|
-
|
|
26
|
+
Build, check, or watch the project:
|
|
64
27
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
28
|
+
```bash
|
|
29
|
+
npx ttsc
|
|
30
|
+
npx ttsc --noEmit
|
|
31
|
+
npx ttsc --watch
|
|
69
32
|
```
|
|
70
33
|
|
|
71
|
-
|
|
34
|
+
Transform a single file:
|
|
72
35
|
|
|
73
|
-
|
|
36
|
+
```bash
|
|
37
|
+
npx ttsc transform --file src/index.ts
|
|
38
|
+
```
|
|
74
39
|
|
|
75
|
-
|
|
40
|
+
## Transformer Configuration
|
|
76
41
|
|
|
77
|
-
|
|
42
|
+
`ttsc` reads `compilerOptions.plugins` from `tsconfig.json`.
|
|
78
43
|
|
|
79
|
-
```
|
|
44
|
+
```json
|
|
80
45
|
{
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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;
|
|
46
|
+
"compilerOptions": {
|
|
47
|
+
"plugins": [
|
|
48
|
+
{ "transform": "typia/lib/transform" }
|
|
49
|
+
]
|
|
50
|
+
}
|
|
121
51
|
}
|
|
122
52
|
```
|
|
123
53
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
`transform` can accept npm module or local file path (.ts or .js) related to `tsconfig.json`
|
|
54
|
+
The same configuration is used by `ttsc` and `ttsx`.
|
|
127
55
|
|
|
128
|
-
|
|
56
|
+
```bash
|
|
57
|
+
# compile
|
|
58
|
+
npx ttsc
|
|
129
59
|
|
|
130
|
-
|
|
131
|
-
|
|
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>;
|
|
60
|
+
# execute
|
|
61
|
+
npx ttsx src/index.ts
|
|
148
62
|
```
|
|
149
|
-
--->
|
|
150
63
|
|
|
151
|
-
|
|
64
|
+
This gives compiler-powered libraries one transform path for build-time and runtime execution.
|
|
152
65
|
|
|
153
|
-
|
|
66
|
+
## CLI
|
|
154
67
|
|
|
155
|
-
```
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
68
|
+
```bash
|
|
69
|
+
ttsc [options]
|
|
70
|
+
ttsc transform --file <path> [options]
|
|
71
|
+
ttsc version
|
|
159
72
|
```
|
|
160
73
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
#### config
|
|
164
|
-
|
|
165
|
-
For the signature with transformer's config:
|
|
74
|
+
Common commands:
|
|
166
75
|
|
|
167
|
-
```
|
|
168
|
-
|
|
76
|
+
```bash
|
|
77
|
+
ttsc
|
|
78
|
+
ttsc -p tsconfig.json
|
|
79
|
+
ttsc --noEmit
|
|
80
|
+
ttsc --watch
|
|
81
|
+
ttsc --outDir lib
|
|
82
|
+
ttsc --verbose
|
|
83
|
+
ttsc transform --file src/index.ts
|
|
84
|
+
ttsc transform --file src/index.ts --out tmp/index.js
|
|
169
85
|
```
|
|
170
86
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
87
|
+
Supported options:
|
|
88
|
+
|
|
89
|
+
- `-p, --project <file>`: resolve project settings from this config file.
|
|
90
|
+
- `--tsconfig <file>`: alias for an explicit project config file.
|
|
91
|
+
- `--cwd <dir>`: resolve project-relative paths from another directory.
|
|
92
|
+
- `--emit`: force emitted files during build.
|
|
93
|
+
- `--noEmit`: type check and transform without writing emitted files.
|
|
94
|
+
- `-w, --watch`: rebuild when project files change.
|
|
95
|
+
- `--preserveWatchOutput`: keep watch output on screen.
|
|
96
|
+
- `--outDir <dir>`: override `compilerOptions.outDir`.
|
|
97
|
+
- `--rewrite-mode <mode>`: force a native rewrite backend id.
|
|
98
|
+
- `--quiet`: keep native build output quiet.
|
|
99
|
+
- `--verbose`: print the native build summary and emitted files.
|
|
100
|
+
- `--out <path>`: write single-file transform output to a file.
|
|
101
|
+
- `--binary <path>`: use an explicit native backend binary.
|
|
102
|
+
|
|
103
|
+
Compatibility aliases:
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
ttsc build
|
|
107
|
+
ttsc check
|
|
179
108
|
```
|
|
180
109
|
|
|
181
|
-
|
|
110
|
+
`ttsc build` is the same project build lane as `ttsc`. `ttsc check` is the same as `ttsc --noEmit`.
|
|
182
111
|
|
|
183
|
-
|
|
112
|
+
## JS API
|
|
184
113
|
|
|
185
|
-
|
|
114
|
+
`ttsc` exposes a small JS API for bundlers, runners, and higher-level tools.
|
|
186
115
|
|
|
187
116
|
```ts
|
|
188
|
-
|
|
117
|
+
import { build, check, transform, version } from "ttsc";
|
|
189
118
|
```
|
|
190
119
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
#### compilerOptions
|
|
120
|
+
Transform one file:
|
|
194
121
|
|
|
195
122
|
```ts
|
|
196
|
-
|
|
197
|
-
```
|
|
198
|
-
|
|
199
|
-
Plugin config entry: `{ "transform": "transformer-module", type: "compilerOptions" }`.
|
|
123
|
+
import { transform } from "ttsc";
|
|
200
124
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
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
|
|
125
|
+
const code = transform({
|
|
126
|
+
cwd: "/project",
|
|
127
|
+
file: "/project/src/index.ts",
|
|
128
|
+
tsconfig: "/project/tsconfig.json",
|
|
129
|
+
});
|
|
221
130
|
```
|
|
222
131
|
|
|
223
|
-
|
|
132
|
+
Build or check a project:
|
|
224
133
|
|
|
225
|
-
```
|
|
226
|
-
|
|
227
|
-
or
|
|
228
|
-
ts-node -C ttypescript index.ts
|
|
229
|
-
```
|
|
230
|
-
|
|
231
|
-
### Parcel
|
|
134
|
+
```ts
|
|
135
|
+
import { build, check } from "ttsc";
|
|
232
136
|
|
|
233
|
-
|
|
137
|
+
const built = build({
|
|
138
|
+
cwd: process.cwd(),
|
|
139
|
+
tsconfig: "tsconfig.json",
|
|
140
|
+
emit: true,
|
|
141
|
+
});
|
|
234
142
|
|
|
235
|
-
|
|
236
|
-
|
|
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
|
-
```
|
|
143
|
+
if (built.status !== 0) {
|
|
144
|
+
console.error(built.stderr);
|
|
145
|
+
}
|
|
252
146
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
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
|
-
};
|
|
147
|
+
const checked = check({
|
|
148
|
+
tsconfig: "tsconfig.json",
|
|
149
|
+
});
|
|
269
150
|
```
|
|
270
151
|
|
|
271
|
-
|
|
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
|
-
```
|
|
152
|
+
`transform()` throws when the native compiler exits with an error. `build()` and `check()` return `{ status, stdout, stderr }`, so callers can decide how to surface compiler failures.
|
|
280
153
|
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
```js
|
|
284
|
-
module.exports = {
|
|
285
|
-
// [...]
|
|
286
|
-
globals: {
|
|
287
|
-
'ts-jest': {
|
|
288
|
-
compiler: 'ttypescript',
|
|
289
|
-
},
|
|
290
|
-
},
|
|
291
|
-
};
|
|
292
|
-
```
|
|
154
|
+
## Transformer Library Authors
|
|
293
155
|
|
|
294
|
-
|
|
156
|
+
Transformer libraries publish a normal tsconfig plugin entry:
|
|
295
157
|
|
|
296
158
|
```json
|
|
297
159
|
{
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
}
|
|
304
|
-
}
|
|
160
|
+
"compilerOptions": {
|
|
161
|
+
"plugins": [
|
|
162
|
+
{ "transform": "my-lib/lib/transform" }
|
|
163
|
+
]
|
|
164
|
+
}
|
|
305
165
|
}
|
|
306
166
|
```
|
|
307
167
|
|
|
308
|
-
|
|
168
|
+
The `transform` value may be:
|
|
169
|
+
|
|
170
|
+
- a package specifier.
|
|
171
|
+
- an absolute path.
|
|
172
|
+
- a relative path from the project root.
|
|
309
173
|
|
|
310
|
-
|
|
174
|
+
The plugin module may export a plugin object directly, a default export, a `plugin` export, or a `createTtscPlugin` factory.
|
|
311
175
|
|
|
312
176
|
```ts
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
export default
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
return ts.visitEachChild(sourceFile, visitor, ctx);
|
|
325
|
-
};
|
|
326
|
-
};
|
|
327
|
-
}
|
|
177
|
+
import { definePlugin } from "ttsc";
|
|
178
|
+
|
|
179
|
+
export default definePlugin((config, context) => ({
|
|
180
|
+
name: "my-lib",
|
|
181
|
+
native: {
|
|
182
|
+
mode: "my-lib",
|
|
183
|
+
binary: require.resolve("my-lib-native/bin/ttsc-my-lib.js"),
|
|
184
|
+
contractVersion: 1,
|
|
185
|
+
capabilities: ["rewrite", "diagnostics"],
|
|
186
|
+
},
|
|
187
|
+
}));
|
|
328
188
|
```
|
|
329
189
|
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
[`{ "transform": "ts-nameof", type: "raw"}`](https://github.com/dsherret/ts-nameof)
|
|
190
|
+
The factory receives:
|
|
333
191
|
|
|
334
|
-
|
|
192
|
+
- `config`: the raw `compilerOptions.plugins[]` entry.
|
|
193
|
+
- `context.binary`: the fallback `ttsc` native binary path.
|
|
194
|
+
- `context.cwd`: the invocation working directory.
|
|
195
|
+
- `context.projectRoot`: the resolved project root.
|
|
196
|
+
- `context.tsconfig`: the resolved config file path.
|
|
335
197
|
|
|
336
|
-
|
|
198
|
+
Keep this Node-side entry small. Use it for package discovery, feature flags, native binary selection, and manifest construction. Put compiler-sensitive work in the native backend.
|
|
337
199
|
|
|
338
|
-
|
|
200
|
+
## Native Backend Contract
|
|
339
201
|
|
|
340
|
-
|
|
202
|
+
A native transform plugin describes the backend that owns type analysis and rewrite work.
|
|
341
203
|
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
204
|
+
```ts
|
|
205
|
+
import { definePlugin } from "ttsc";
|
|
206
|
+
|
|
207
|
+
export default definePlugin(() => ({
|
|
208
|
+
name: "my-lib",
|
|
209
|
+
native: {
|
|
210
|
+
mode: "my-lib",
|
|
211
|
+
binary: require.resolve("my-lib-native/bin/ttsc-my-lib.js"),
|
|
212
|
+
contractVersion: 1,
|
|
213
|
+
capabilities: ["rewrite", "diagnostics", "assets"],
|
|
214
|
+
},
|
|
215
|
+
}));
|
|
216
|
+
```
|
|
345
217
|
|
|
346
|
-
|
|
218
|
+
Descriptor shape:
|
|
347
219
|
|
|
348
|
-
|
|
220
|
+
```ts
|
|
221
|
+
interface TtscNativeBackend {
|
|
222
|
+
mode: string;
|
|
223
|
+
binary?: string;
|
|
224
|
+
contractVersion?: 1;
|
|
225
|
+
capabilities?: readonly string[];
|
|
226
|
+
}
|
|
227
|
+
```
|
|
349
228
|
|
|
350
|
-
|
|
229
|
+
- `mode` is the rewrite backend id passed to the native compiler process.
|
|
230
|
+
- `binary` points at a consumer-owned native binary.
|
|
231
|
+
- `contractVersion` pins the plugin to the current host protocol.
|
|
232
|
+
- `capabilities` records the backend responsibilities.
|
|
351
233
|
|
|
352
|
-
|
|
234
|
+
The backend is responsible for the work old TypeScript transformer libraries used to do through in-process compiler APIs:
|
|
353
235
|
|
|
354
|
-
|
|
236
|
+
- loading the project through the `typescript-go` lane.
|
|
237
|
+
- finding marker calls such as `typia.is<T>()`.
|
|
238
|
+
- analyzing types through native checker access or a serialized IR.
|
|
239
|
+
- emitting replacement JavaScript.
|
|
240
|
+
- reporting plugin-specific diagnostics.
|
|
241
|
+
- emitting plugin-owned assets when needed.
|
|
355
242
|
|
|
356
|
-
|
|
243
|
+
## JS Output Hooks
|
|
357
244
|
|
|
358
|
-
|
|
245
|
+
For text-level post-processing, a plugin can provide `transformOutput()`.
|
|
359
246
|
|
|
360
|
-
|
|
247
|
+
```ts
|
|
248
|
+
import { definePlugin } from "ttsc";
|
|
361
249
|
|
|
362
|
-
|
|
250
|
+
export default definePlugin((config) => ({
|
|
251
|
+
name: "banner-plugin",
|
|
252
|
+
transformOutput(context) {
|
|
253
|
+
if (context.command === "build") {
|
|
254
|
+
return `/* generated by ${config.transform} */\n${context.code}`;
|
|
255
|
+
}
|
|
256
|
+
return context.code;
|
|
257
|
+
},
|
|
258
|
+
}));
|
|
259
|
+
```
|
|
363
260
|
|
|
364
|
-
|
|
261
|
+
`transformOutput()` receives JavaScript text after native emit. Use it for:
|
|
365
262
|
|
|
366
|
-
|
|
263
|
+
- banner injection.
|
|
264
|
+
- output string patching.
|
|
265
|
+
- runtime helper import rewrites.
|
|
266
|
+
- consumer-specific output normalization.
|
|
367
267
|
|
|
368
|
-
|
|
268
|
+
Use a native backend for compiler-sensitive work:
|
|
369
269
|
|
|
370
|
-
|
|
270
|
+
- type analysis.
|
|
271
|
+
- AST mutation.
|
|
272
|
+
- call-site recognition.
|
|
273
|
+
- `ts.Program` / `ts.TypeChecker` transformer compatibility.
|
|
371
274
|
|
|
372
|
-
|
|
275
|
+
## Current Constraints
|
|
373
276
|
|
|
374
|
-
|
|
277
|
+
These are current host constraints transformer authors should design around.
|
|
375
278
|
|
|
376
|
-
|
|
279
|
+
- One invocation can select one native `mode` / `binary` pair.
|
|
280
|
+
- Multiple text-only `transformOutput()` plugins can run in order.
|
|
281
|
+
- Passing `plugins` to the JS API replaces the tsconfig plugin list.
|
|
282
|
+
- `transformOutput()` is a JS text hook, not a JS AST hook.
|
|
283
|
+
- The current setup lane is `@typescript/native-preview`.
|
|
377
284
|
|
|
378
|
-
|
|
285
|
+
## Integration Patterns
|
|
379
286
|
|
|
380
|
-
|
|
287
|
+
Consumer library:
|
|
381
288
|
|
|
382
|
-
|
|
289
|
+
- publish a tsconfig plugin entry such as `my-lib/lib/transform`.
|
|
290
|
+
- resolve a consumer-owned native backend binary from that entry.
|
|
291
|
+
- keep type analysis and rewrite logic in the native backend.
|
|
292
|
+
- optionally layer `transformOutput()` for final JavaScript text normalization.
|
|
383
293
|
|
|
384
|
-
|
|
294
|
+
Bundler adapter:
|
|
385
295
|
|
|
386
|
-
-
|
|
387
|
-
-
|
|
388
|
-
-
|
|
296
|
+
- call `transform()` for per-file rewrites.
|
|
297
|
+
- pass `plugins` when the adapter needs deterministic plugin state.
|
|
298
|
+
- let `ttsc` resolve the native backend instead of spawning it manually.
|
|
389
299
|
|
|
390
|
-
|
|
300
|
+
Runner:
|
|
391
301
|
|
|
392
|
-
|
|
302
|
+
- use `ttsx` for direct execution.
|
|
303
|
+
- reuse the same `compilerOptions.plugins` contract.
|
|
304
|
+
- keep build-time and runtime transform behavior aligned.
|
|
393
305
|
|
|
394
|
-
##
|
|
306
|
+
## References
|
|
395
307
|
|
|
396
|
-
|
|
308
|
+
- TypeScript runners: [`ts-node`](https://github.com/TypeStrong/ts-node) and [`tsx`](https://github.com/privatenumber/tsx)
|
|
309
|
+
- Transformer tooling: [`ttypescript`](https://github.com/cevek/ttypescript) and [`ts-patch`](https://github.com/nonara/ts-patch)
|
|
310
|
+
- Inspired by: [`typical`](https://github.com/samchon/typical) and [`tsgonest`](https://github.com/samchon/tsgonest)
|