tsrx-typegen 0.2.2 → 0.3.1

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/README.md CHANGED
@@ -1,55 +1,21 @@
1
1
  # tsrx-typegen
2
2
 
3
- Generate declaration files for packages that export `.tsrx` modules. The
4
- command uses the consuming package's TypeScript and tsrx compiler, then maps
5
- source-extension imports in the emitted declarations to JavaScript module
6
- specifiers.
7
-
8
- ```sh
9
- pnpm exec tsrx-typegen --target octane
10
- ```
3
+ Generate `.d.ts` declaration files for packages that export `.tsrx` modules.
4
+ Use it when publishing an Octane/tsrx component package: it runs the consuming
5
+ package's own TypeScript and tsrx compiler, then rewrites source-extension
6
+ imports in the emitted declarations to JavaScript module specifiers.
11
7
 
12
8
  Install `tsrx-typegen` as a development tool alongside `typescript` and
13
- `@tsrx/typescript-plugin`. Each target in `tsrx-typegen.json` names its
14
- renderer, project config, declaration output directory, and public subpaths.
15
- The project config selects source variants and enables `declaration`,
16
- `emitDeclarationOnly`, and `noEmit: false`. The target's `renderer` is checked
17
- against `compilerOptions.jsxImportSource` when TypeScript sets it; the project
18
- config continues to select the actual tsrx compiler and platform.
9
+ `@tsrx/typescript-plugin`, then run it against a named target:
19
10
 
20
11
  ```sh
21
- pnpm exec tsrx-typegen --target octane
22
- pnpm exec tsrx-typegen --project tsconfig.types.json --check
23
- pnpm exec tsrx-typegen --pack-check
12
+ pnpm exec tsrx-typegen --target octane # generate declarations
13
+ pnpm exec tsrx-typegen --target octane --check # verify output is fresh
14
+ pnpm exec tsrx-typegen --pack-check # validate the packed tarball
24
15
  ```
25
16
 
26
- Generation writes only TypeScript declaration files under the config's `outDir` and records
27
- which files it owns in `.tsrx-typegen-manifest.json`. Check mode emits to a
28
- temporary directory and compares the result with those owned files. It reports
29
- missing or stale output, unresolved `.tsrx` references, and code export paths
30
- without declaration targets. It refuses to overwrite an unmanaged declaration
31
- and removes stale files only when the manifest says it owns them. Same-name
32
- sources such as `Button.ts` and `Button.tsrx` need separate target configs or an
33
- explicit declaration override. Case-only source renames replace the prior
34
- manifest-owned declaration, including on case-insensitive filesystems; manual
35
- declarations remain protected.
36
-
37
- ```sh
38
- pnpm exec tsrx-typegen --target octane --check
39
- ```
40
-
41
- Treat the output directory, including its ownership manifest, as generated
42
- build artifacts: ignore it in Git, formatting, and linting. Keep handwritten
43
- declarations and override sources outside that directory. Include the output
44
- directory in the package's `files` list so ignored declarations still ship in
45
- the tarball. Generate before local consumer typechecks, and regenerate in
46
- `prepack` before validating the package. Workspaces that resolve generated
47
- types directly also need generation during setup and refreshes during
48
- development; this repository's workflow is documented in
49
- [Develop against generated declarations](../../docs/notes/toolchain-notes.md#develop-against-generated-declarations).
50
-
51
- `--project` selects the target whose project path matches. Use `--target` to
52
- select a named target directly. A target can look like this:
17
+ Each target in `tsrx-typegen.json` names its renderer, project config,
18
+ declaration output directory, and public subpaths:
53
19
 
54
20
  ```json
55
21
  {
@@ -61,7 +27,7 @@ select a named target directly. A target can look like this:
61
27
  "entrypoints": {
62
28
  ".": {
63
29
  "source": "src/index.ts",
64
- "runtime": "./dist/index.js",
30
+ "runtime": "./src/index.ts",
65
31
  "types": "./types/generated/index.d.ts"
66
32
  }
67
33
  }
@@ -70,6 +36,41 @@ select a named target directly. A target can look like this:
70
36
  }
71
37
  ```
72
38
 
39
+ The project config selects source variants and enables `declaration`,
40
+ `emitDeclarationOnly`, and `noEmit: false`. The target's `renderer` is checked
41
+ against `compilerOptions.jsxImportSource` when TypeScript sets it; the project
42
+ config continues to select the actual tsrx compiler and platform. `--project`
43
+ selects the target whose project path matches; `--target` selects a named
44
+ target directly.
45
+
46
+ ## What generation writes and checks
47
+
48
+ Generation writes only TypeScript declaration files under the config's
49
+ `outDir` and records which files it owns in `.tsrx-typegen-manifest.json`.
50
+ Declaration inputs included by the project under `rootDir` are copied beside
51
+ emitted declarations, with the same import rewriting and freshness checks.
52
+ Check mode emits to a temporary directory and compares the result with those
53
+ owned files. It reports missing or stale output, unresolved `.tsrx`
54
+ references, and code export paths without declaration targets. It refuses to
55
+ overwrite an unmanaged declaration and removes stale files only when the
56
+ manifest says it owns them. Same-name sources such as `Button.ts` and
57
+ `Button.tsrx` need separate target configs or an explicit declaration
58
+ override. Case-only source renames replace the prior manifest-owned
59
+ declaration, including on case-insensitive filesystems; manual declarations
60
+ remain protected.
61
+
62
+ Treat the output directory, including its ownership manifest, as generated
63
+ build artifacts: ignore it in Git, formatting, and linting. Keep handwritten
64
+ declarations and override sources outside that directory. Include the output
65
+ directory in the package's `files` list so ignored declarations still ship in
66
+ the tarball. Generate before local consumer typechecks, and regenerate in
67
+ `prepack` before validating the package. Workspaces that resolve generated
68
+ types directly also need generation during setup and refreshes during
69
+ development; this repository's workflow is documented in
70
+ [Develop against generated declarations](../../.agents/docs/notes/toolchain-notes.md#develop-against-generated-declarations).
71
+
72
+ ## Overrides and import rewriting
73
+
73
74
  Keep exceptional signatures in handwritten declaration sources outside the
74
75
  generated directory, then map their generated output path explicitly:
75
76
 
@@ -86,30 +87,50 @@ manifest; check mode detects edits to either the source mapping or generated
86
87
  copy. Override declarations must use publishable module specifiers and may not
87
88
  reference `.tsrx` files.
88
89
 
89
- ```sh
90
- pnpm exec tsrx-typegen --target octane --check
91
- ```
92
-
93
90
  Relative imports between generated declarations receive runtime extensions
94
91
  from the source map, including extensionless imports. This supports NodeNext
95
- consumers as well as bundler resolution without requiring `allowArbitraryExtensions`.
92
+ consumers as well as bundler resolution without requiring
93
+ `allowArbitraryExtensions`. A plain TypeScript consumer then imports the
94
+ package normally:
96
95
 
97
96
  ```ts
98
- // Plain TypeScript consumer of the package whose exports are configured above.
99
97
  import { Button } from 'my-component-library'
98
+ ```
99
+
100
+ The `.js` specifiers inside declarations resolve to sibling `.d.ts` files
101
+ through TypeScript's extension substitution. They do not require a JavaScript
102
+ package build. Keep runtime exports pointed at authored source and type exports
103
+ pointed at declarations:
100
104
 
101
- // Button is a component exported by your library's src/index.ts.
102
- export { Button }
105
+ ```json
106
+ {
107
+ "exports": {
108
+ ".": {
109
+ "types": "./types/generated/index.d.ts",
110
+ "default": "./src/index.ts"
111
+ },
112
+ "./*": {
113
+ "types": "./types/generated/*.d.ts",
114
+ "default": "./src/*"
115
+ }
116
+ },
117
+ "files": ["src", "types"]
118
+ }
103
119
  ```
104
120
 
121
+ Keep wildcard runtime targets extensionless so the app bundler can select
122
+ `.web.ts`, `.ios.ts`, or `.web.tsrx` siblings. The declaration wildcard uses
123
+ the same module stem, and the pack check verifies each source variant has its
124
+ matching declaration.
125
+
105
126
  Use separate target records and project configs when public signatures differ.
106
- Each config must select the same source variants as its runtime build.
127
+ Each config must select the source variants exposed by its runtime exports.
107
128
  `runtime` records the published runtime path or condition map, while `types`
108
- records the declaration path. The CLI does not edit `package.json`; export maps
109
- remain package-owned. The default source-extension mapping is `.tsrx`, `.tsx`,
110
- and `.ts` to `.js`, with `.mts` to `.mjs` and `.cts` to `.cjs`. Packages with
111
- another runtime layout can override these mappings in `sourceExtensions` at
112
- the target or root level.
129
+ records the declaration path. The CLI does not edit `package.json`; export
130
+ maps remain package-owned. The default source-extension mapping is `.tsrx`,
131
+ `.tsx`, and `.ts` to `.js`, with `.mts` to `.mjs` and `.cts` to `.cjs`.
132
+ Packages with another runtime layout can override these mappings in
133
+ `sourceExtensions` at the target or root level:
113
134
 
114
135
  ```json
115
136
  {
@@ -123,17 +144,12 @@ Use `--pack-check` before publishing. It checks every configured target unless
123
144
  `--target` narrows the selection, packs a temporary tarball with lifecycle
124
145
  scripts disabled, and verifies that package and `publishConfig` export paths
125
146
  exist in the tarball. It also compares runtime and declaration value exports
126
- and checks that relative declaration references resolve inside the package and
127
- bare package imports are declared dependencies or peers. The command needs
147
+ and checks that relative declaration references resolve to declarations inside
148
+ the package and bare package imports are declared dependencies or peers. The command needs
128
149
  `pnpm` and the system `tar` utility. It checks package structure; keep a plain
129
150
  TypeScript consumer test for the module-resolution modes and public prop
130
151
  contracts your package supports.
131
152
 
132
- ```sh
133
- pnpm exec tsrx-typegen --pack-check
134
- pnpm exec tsrx-typegen --pack-check --target octane
135
- ```
136
-
137
153
  A target with `"emit": false` exists only to drive packed-package
138
154
  verification. Packages whose declarations are handwritten or produced by
139
155
  another tool declare one so `--pack-check` skips generation and freshness
@@ -153,7 +169,7 @@ for source-published packages, the `.ts` source itself.
153
169
  "entrypoints": {
154
170
  ".": {
155
171
  "source": "src/index.ts",
156
- "runtime": "./dist/index.js",
172
+ "runtime": "./src/index.ts",
157
173
  "types": "./types/index.d.ts"
158
174
  }
159
175
  }
@@ -168,8 +184,7 @@ Run the same gate from `prepack` so `pnpm pack` and publication share it:
168
184
  {
169
185
  "scripts": {
170
186
  "typegen": "tsrx-typegen --target octane",
171
- "build": "vite build && pnpm typegen",
172
- "prepack": "pnpm build && tsrx-typegen --pack-check"
187
+ "prepack": "pnpm typegen && tsrx-typegen --pack-check"
173
188
  }
174
189
  }
175
190
  ```
@@ -182,9 +197,11 @@ that declaration gate fails.
182
197
  pnpm exec xplat doctor
183
198
  ```
184
199
 
200
+ ## Backend and inference
201
+
185
202
  The first supported backend is the classic `tsrx-tsc` path with TypeScript
186
- 5.9.x. TypeScript 7 content-mapper output needs upstream declaration naming and
187
- specifier support before it can replace this backend. Generation rejects
203
+ 5.9.x. TypeScript 7 content-mapper output needs upstream declaration naming
204
+ and specifier support before it can replace this backend. Generation rejects
188
205
  compiler errors; editor-only partial transforms are not used.
189
206
 
190
207
  This tool preserves TypeScript's declaration inference. Keep exported helpers,
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env node
2
+ // Prefer the emitted dist/ build (published tarballs); fall back to src/ so
3
+ // in-repo typegen needs no build step (Node ≥22.18 strips types).
4
+ import { existsSync } from 'node:fs'
5
+
6
+ const dist = new URL('../dist/cli.js', import.meta.url)
7
+ if (existsSync(dist)) {
8
+ await import(dist.href)
9
+ } else {
10
+ await import(new URL('../src/cli.ts', import.meta.url).href)
11
+ }