tsrx-typegen 0.2.1 → 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/README.md +129 -48
- package/package.json +1 -1
- package/src/cli.mjs +57 -15
- package/src/pack-check.mjs +71 -21
package/README.md
CHANGED
|
@@ -1,45 +1,21 @@
|
|
|
1
1
|
# tsrx-typegen
|
|
2
2
|
|
|
3
|
-
Generate declaration files for packages that export `.tsrx` modules.
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
specifiers.
|
|
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.
|
|
7
7
|
|
|
8
8
|
Install `tsrx-typegen` as a development tool alongside `typescript` and
|
|
9
|
-
`@tsrx/typescript-plugin
|
|
10
|
-
renderer, project config, declaration output directory, and public subpaths.
|
|
11
|
-
The project config selects source variants and enables `declaration`,
|
|
12
|
-
`emitDeclarationOnly`, and `noEmit: false`. The target's `renderer` is checked
|
|
13
|
-
against `compilerOptions.jsxImportSource` when TypeScript sets it; the project
|
|
14
|
-
config continues to select the actual tsrx compiler and platform.
|
|
9
|
+
`@tsrx/typescript-plugin`, then run it against a named target:
|
|
15
10
|
|
|
16
11
|
```sh
|
|
17
|
-
pnpm exec tsrx-typegen --target octane
|
|
18
|
-
pnpm exec tsrx-typegen --
|
|
19
|
-
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
|
|
20
15
|
```
|
|
21
16
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
temporary directory and compares the result with those owned files. It reports
|
|
25
|
-
missing or stale output, unresolved `.tsrx` references, and code export paths
|
|
26
|
-
without declaration targets. It refuses to overwrite an unmanaged declaration
|
|
27
|
-
and removes stale files only when the manifest says it owns them. Same-name
|
|
28
|
-
sources such as `Button.ts` and `Button.tsrx` need separate target configs or an
|
|
29
|
-
explicit declaration override.
|
|
30
|
-
|
|
31
|
-
Treat the output directory, including its ownership manifest, as generated
|
|
32
|
-
build artifacts: ignore it in Git, formatting, and linting. Keep handwritten
|
|
33
|
-
declarations and override sources outside that directory. Include the output
|
|
34
|
-
directory in the package's `files` list so ignored declarations still ship in
|
|
35
|
-
the tarball. Generate before local consumer typechecks, and regenerate in
|
|
36
|
-
`prepack` before validating the package. Workspaces that resolve generated
|
|
37
|
-
types directly also need generation during setup and refreshes during
|
|
38
|
-
development; this repository's workflow is documented in
|
|
39
|
-
[Develop against generated declarations](../../docs/toolchain-notes.md#develop-against-generated-declarations).
|
|
40
|
-
|
|
41
|
-
`--project` selects the target whose project path matches. Use `--target` to
|
|
42
|
-
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:
|
|
43
19
|
|
|
44
20
|
```json
|
|
45
21
|
{
|
|
@@ -51,7 +27,7 @@ select a named target directly. A target can look like this:
|
|
|
51
27
|
"entrypoints": {
|
|
52
28
|
".": {
|
|
53
29
|
"source": "src/index.ts",
|
|
54
|
-
"runtime": "./
|
|
30
|
+
"runtime": "./src/index.ts",
|
|
55
31
|
"types": "./types/generated/index.d.ts"
|
|
56
32
|
}
|
|
57
33
|
}
|
|
@@ -60,6 +36,41 @@ select a named target directly. A target can look like this:
|
|
|
60
36
|
}
|
|
61
37
|
```
|
|
62
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
|
+
|
|
63
74
|
Keep exceptional signatures in handwritten declaration sources outside the
|
|
64
75
|
generated directory, then map their generated output path explicitly:
|
|
65
76
|
|
|
@@ -78,16 +89,54 @@ reference `.tsrx` files.
|
|
|
78
89
|
|
|
79
90
|
Relative imports between generated declarations receive runtime extensions
|
|
80
91
|
from the source map, including extensionless imports. This supports NodeNext
|
|
81
|
-
consumers as well as bundler resolution without requiring
|
|
92
|
+
consumers as well as bundler resolution without requiring
|
|
93
|
+
`allowArbitraryExtensions`. A plain TypeScript consumer then imports the
|
|
94
|
+
package normally:
|
|
95
|
+
|
|
96
|
+
```ts
|
|
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:
|
|
104
|
+
|
|
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
|
+
}
|
|
119
|
+
```
|
|
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.
|
|
82
125
|
|
|
83
126
|
Use separate target records and project configs when public signatures differ.
|
|
84
|
-
Each config must select the
|
|
127
|
+
Each config must select the source variants exposed by its runtime exports.
|
|
85
128
|
`runtime` records the published runtime path or condition map, while `types`
|
|
86
|
-
records the declaration path. The CLI does not edit `package.json`; export
|
|
87
|
-
remain package-owned. The default source-extension mapping is `.tsrx`,
|
|
88
|
-
and `.ts` to `.js`, with `.mts` to `.mjs` and `.cts` to `.cjs`.
|
|
89
|
-
another runtime layout can override these mappings in
|
|
90
|
-
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:
|
|
134
|
+
|
|
135
|
+
```json
|
|
136
|
+
{
|
|
137
|
+
"sourceExtensions": { ".tsrx": ".js", ".mts": ".mjs", ".cts": ".cjs" }
|
|
138
|
+
}
|
|
139
|
+
```
|
|
91
140
|
|
|
92
141
|
## Publish checks
|
|
93
142
|
|
|
@@ -95,8 +144,8 @@ Use `--pack-check` before publishing. It checks every configured target unless
|
|
|
95
144
|
`--target` narrows the selection, packs a temporary tarball with lifecycle
|
|
96
145
|
scripts disabled, and verifies that package and `publishConfig` export paths
|
|
97
146
|
exist in the tarball. It also compares runtime and declaration value exports
|
|
98
|
-
and checks that relative declaration references resolve
|
|
99
|
-
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
|
|
100
149
|
`pnpm` and the system `tar` utility. It checks package structure; keep a plain
|
|
101
150
|
TypeScript consumer test for the module-resolution modes and public prop
|
|
102
151
|
contracts your package supports.
|
|
@@ -109,14 +158,33 @@ selected for generation; a plain `--target` run against them fails. Every code
|
|
|
109
158
|
export path still needs a `types` condition — pointing at a `.d.ts` file or,
|
|
110
159
|
for source-published packages, the `.ts` source itself.
|
|
111
160
|
|
|
161
|
+
```json
|
|
162
|
+
{
|
|
163
|
+
"targets": {
|
|
164
|
+
"manual": {
|
|
165
|
+
"emit": false,
|
|
166
|
+
"renderer": "octane",
|
|
167
|
+
"project": "tsconfig.types.json",
|
|
168
|
+
"outDir": "types/generated",
|
|
169
|
+
"entrypoints": {
|
|
170
|
+
".": {
|
|
171
|
+
"source": "src/index.ts",
|
|
172
|
+
"runtime": "./src/index.ts",
|
|
173
|
+
"types": "./types/index.d.ts"
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
112
181
|
Run the same gate from `prepack` so `pnpm pack` and publication share it:
|
|
113
182
|
|
|
114
183
|
```json
|
|
115
184
|
{
|
|
116
185
|
"scripts": {
|
|
117
186
|
"typegen": "tsrx-typegen --target octane",
|
|
118
|
-
"
|
|
119
|
-
"prepack": "pnpm build && tsrx-typegen --pack-check"
|
|
187
|
+
"prepack": "pnpm typegen && tsrx-typegen --pack-check"
|
|
120
188
|
}
|
|
121
189
|
}
|
|
122
190
|
```
|
|
@@ -125,9 +193,15 @@ When run from a package root that has `tsrx-typegen.json`, `xplat doctor`
|
|
|
125
193
|
delegates to `tsrx-typegen --pack-check` and returns a failing exit status if
|
|
126
194
|
that declaration gate fails.
|
|
127
195
|
|
|
196
|
+
```sh
|
|
197
|
+
pnpm exec xplat doctor
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
## Backend and inference
|
|
201
|
+
|
|
128
202
|
The first supported backend is the classic `tsrx-tsc` path with TypeScript
|
|
129
|
-
5.9.x. TypeScript 7 content-mapper output needs upstream declaration naming
|
|
130
|
-
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
|
|
131
205
|
compiler errors; editor-only partial transforms are not used.
|
|
132
206
|
|
|
133
207
|
This tool preserves TypeScript's declaration inference. Keep exported helpers,
|
|
@@ -137,3 +211,10 @@ packed package with a plain TypeScript consumer; generated declarations may
|
|
|
137
211
|
still express a different contract even when their paths and exports are
|
|
138
212
|
valid. Use explicit overrides for exceptional signatures and keep those
|
|
139
213
|
overrides outside the generated output directory.
|
|
214
|
+
|
|
215
|
+
```ts
|
|
216
|
+
// Source of your component package; use an explicit return type for helpers.
|
|
217
|
+
export function formatCount(count: number): string {
|
|
218
|
+
return `${count} packed`
|
|
219
|
+
}
|
|
220
|
+
```
|
package/package.json
CHANGED
package/src/cli.mjs
CHANGED
|
@@ -529,7 +529,7 @@ function rewriteDeclaration(ts, path, outputDir, extensions, generatedFiles, run
|
|
|
529
529
|
|
|
530
530
|
const declaration = relative(outputDir, target)
|
|
531
531
|
const runtimeExtension = runtimeByDeclaration.get(declaration) ?? '.js'
|
|
532
|
-
const isIndex =
|
|
532
|
+
const isIndex = dirname(target) === targetStem
|
|
533
533
|
const baseSpecifier = specifier.endsWith('/') ? specifier.slice(0, -1) : specifier
|
|
534
534
|
replacement = isIndex
|
|
535
535
|
? `${baseSpecifier}/index${runtimeExtension}`
|
|
@@ -608,19 +608,42 @@ function pathInside(root, file) {
|
|
|
608
608
|
)
|
|
609
609
|
}
|
|
610
610
|
|
|
611
|
-
function
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
continue
|
|
615
|
-
}
|
|
611
|
+
function existingOutputPath(outputDir, file) {
|
|
612
|
+
let current = outputDir
|
|
613
|
+
const actualParts = []
|
|
616
614
|
|
|
615
|
+
for (const part of normalize(file).split(sep)) {
|
|
616
|
+
let entries
|
|
617
617
|
try {
|
|
618
|
-
|
|
619
|
-
throw new Error(`refusing to overwrite unmanaged declaration: ${file}`)
|
|
618
|
+
entries = readdirSync(current)
|
|
620
619
|
} catch (error) {
|
|
621
|
-
if (error.code
|
|
622
|
-
|
|
620
|
+
if (error.code === 'ENOENT') {
|
|
621
|
+
return null
|
|
623
622
|
}
|
|
623
|
+
|
|
624
|
+
throw error
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
const actual = entries.includes(part)
|
|
628
|
+
? part
|
|
629
|
+
: entries.find((entry) => entry.toLowerCase() === part.toLowerCase())
|
|
630
|
+
|
|
631
|
+
if (!actual) {
|
|
632
|
+
return null
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
actualParts.push(actual)
|
|
636
|
+
current = join(current, actual)
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
return actualParts.join(sep)
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
function writeOutput(outputDir, generated, oldFiles, overrideFiles) {
|
|
643
|
+
for (const file of generated.keys()) {
|
|
644
|
+
const existing = existingOutputPath(outputDir, file)
|
|
645
|
+
if (existing && !oldFiles?.has(existing) && !overrideFiles.has(existing)) {
|
|
646
|
+
throw new Error(`refusing to overwrite unmanaged declaration: ${file}`)
|
|
624
647
|
}
|
|
625
648
|
}
|
|
626
649
|
|
|
@@ -723,8 +746,8 @@ function verifyPublicTypes(projectRoot, generated, outDir, target, sourceOutputs
|
|
|
723
746
|
if (target && exportsMap && typeof exportsMap === 'object') {
|
|
724
747
|
const configuredSubpaths = new Set(Object.keys(target.entrypoints))
|
|
725
748
|
for (const [subpath, mapping] of Object.entries(exportsMap)) {
|
|
726
|
-
const runtimeTargets = stringsIn(mapping).filter(
|
|
727
|
-
/\.(?:[cm]?[jt]sx?|tsrx)$/.test(entry),
|
|
749
|
+
const runtimeTargets = stringsIn(mapping).filter(
|
|
750
|
+
(entry) => !extname(entry) || /\.(?:[cm]?[jt]sx?|tsrx)$/.test(entry),
|
|
728
751
|
)
|
|
729
752
|
|
|
730
753
|
if (runtimeTargets.length && !configuredSubpaths.has(subpath)) {
|
|
@@ -743,7 +766,9 @@ function verifyPublicTypes(projectRoot, generated, outDir, target, sourceOutputs
|
|
|
743
766
|
}
|
|
744
767
|
|
|
745
768
|
const source = resolve(projectRoot, entry.source)
|
|
746
|
-
|
|
769
|
+
// TypeScript emits forward-slash paths even on Windows; resolve both
|
|
770
|
+
// sides before comparing them with the platform-native entry path.
|
|
771
|
+
if (![...sourceOutputs.values()].some((included) => resolve(included) === source)) {
|
|
747
772
|
failures.push(
|
|
748
773
|
`${subpath} source is not included by its TypeScript project: ${entry.source}`,
|
|
749
774
|
)
|
|
@@ -769,8 +794,8 @@ function verifyPublicTypes(projectRoot, generated, outDir, target, sourceOutputs
|
|
|
769
794
|
|
|
770
795
|
if (exportsMap && typeof exportsMap === 'object') {
|
|
771
796
|
for (const [subpath, mapping] of Object.entries(exportsMap)) {
|
|
772
|
-
const runtimeTargets = stringsIn(mapping).filter(
|
|
773
|
-
/\.(?:[cm]?[jt]sx?|tsrx)$/.test(target),
|
|
797
|
+
const runtimeTargets = stringsIn(mapping).filter(
|
|
798
|
+
(target) => !extname(target) || /\.(?:[cm]?[jt]sx?|tsrx)$/.test(target),
|
|
774
799
|
)
|
|
775
800
|
|
|
776
801
|
if (!runtimeTargets.length) {
|
|
@@ -900,6 +925,23 @@ function runTarget(projectRoot, args) {
|
|
|
900
925
|
return false
|
|
901
926
|
}
|
|
902
927
|
|
|
928
|
+
// TypeScript does not emit declaration inputs. Preserve included, local
|
|
929
|
+
// declarations beside emitted modules so their references remain closed.
|
|
930
|
+
for (const source of fileNames) {
|
|
931
|
+
if (!/\.d\.[cm]?ts$/.test(source) || !pathInside(rootDir, source)) {
|
|
932
|
+
continue
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
const declaration = relative(rootDir, source)
|
|
936
|
+
if (sourceOutputs.has(declaration)) {
|
|
937
|
+
continue
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
const output = join(outputDir, declaration)
|
|
941
|
+
mkdirSync(dirname(output), { recursive: true })
|
|
942
|
+
writeFileSync(output, readFileSync(source))
|
|
943
|
+
}
|
|
944
|
+
|
|
903
945
|
const generated = normalizeOutput(ts, outputDir, sourceExtensions, runtimeByDeclaration)
|
|
904
946
|
const managedOverrides = applyOverrides(ts, projectRoot, outDir, generated, overrides)
|
|
905
947
|
verifySourceDeclarations(generated, sourceOutputs)
|
package/src/pack-check.mjs
CHANGED
|
@@ -50,7 +50,7 @@ const stringsIn = (value) => {
|
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
const codeTarget = (target) =>
|
|
53
|
-
/\.(?:[cm]?[jt]sx?|tsrx)$/.test(target) && !/\.d\.[cm]?ts$/.test(target)
|
|
53
|
+
(!extname(target) || /\.(?:[cm]?[jt]sx?|tsrx)$/.test(target)) && !/\.d\.[cm]?ts$/.test(target)
|
|
54
54
|
|
|
55
55
|
function exportBranches(exportsMap, fallbackTypes = []) {
|
|
56
56
|
const branches = []
|
|
@@ -58,7 +58,11 @@ function exportBranches(exportsMap, fallbackTypes = []) {
|
|
|
58
58
|
const visit = (value, inheritedTypes) => {
|
|
59
59
|
if (typeof value === 'string') {
|
|
60
60
|
if (codeTarget(value)) {
|
|
61
|
-
branches.push({
|
|
61
|
+
branches.push({
|
|
62
|
+
subpath,
|
|
63
|
+
runtime: value,
|
|
64
|
+
types: inheritedTypes.length ? inheritedTypes : fallbackTypes,
|
|
65
|
+
})
|
|
62
66
|
}
|
|
63
67
|
|
|
64
68
|
return
|
|
@@ -68,15 +72,20 @@ function exportBranches(exportsMap, fallbackTypes = []) {
|
|
|
68
72
|
return
|
|
69
73
|
}
|
|
70
74
|
|
|
71
|
-
|
|
75
|
+
// An earlier condition cannot use a later sibling's type fallback.
|
|
76
|
+
let types = inheritedTypes
|
|
72
77
|
for (const [condition, child] of Object.entries(value)) {
|
|
73
|
-
if (condition
|
|
78
|
+
if (condition === 'types') {
|
|
79
|
+
if (!types.length) {
|
|
80
|
+
types = stringsIn(child)
|
|
81
|
+
}
|
|
82
|
+
} else {
|
|
74
83
|
visit(child, types)
|
|
75
84
|
}
|
|
76
85
|
}
|
|
77
86
|
}
|
|
78
87
|
|
|
79
|
-
visit(mapping,
|
|
88
|
+
visit(mapping, [])
|
|
80
89
|
}
|
|
81
90
|
|
|
82
91
|
return branches
|
|
@@ -104,11 +113,12 @@ function packageFiles(root) {
|
|
|
104
113
|
}
|
|
105
114
|
|
|
106
115
|
function expandTarget(target, files, packageRoot) {
|
|
107
|
-
|
|
116
|
+
const extensionless = !extname(target)
|
|
117
|
+
if (!target.includes('*') && !extensionless) {
|
|
108
118
|
return [{ path: target, capture: undefined }]
|
|
109
119
|
}
|
|
110
120
|
|
|
111
|
-
if ((target.match(/\*/g) ?? []).length
|
|
121
|
+
if ((target.match(/\*/g) ?? []).length > 1) {
|
|
112
122
|
throw new Error(`packed export target uses more than one wildcard: ${target}`)
|
|
113
123
|
}
|
|
114
124
|
|
|
@@ -118,19 +128,31 @@ function expandTarget(target, files, packageRoot) {
|
|
|
118
128
|
|
|
119
129
|
const pattern = target.slice(2)
|
|
120
130
|
const marker = pattern.indexOf('*')
|
|
121
|
-
const prefix = pattern.slice(0, marker)
|
|
122
|
-
const suffix = pattern.slice(marker + 1)
|
|
131
|
+
const prefix = marker === -1 ? pattern : pattern.slice(0, marker)
|
|
132
|
+
const suffix = marker === -1 ? '' : pattern.slice(marker + 1)
|
|
123
133
|
const matches = files.flatMap((file) => {
|
|
124
134
|
const path = relative(packageRoot, file).replaceAll('\\', '/')
|
|
135
|
+
// Extensionless source exports let the app choose platform siblings.
|
|
136
|
+
// Their wildcard captures are module stems, not source extensions.
|
|
137
|
+
if (!codeTarget(path) || (extensionless && !extname(path))) {
|
|
138
|
+
return []
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const matchedPath = extensionless ? path.slice(0, -extname(path).length) : path
|
|
125
142
|
if (
|
|
126
|
-
!
|
|
127
|
-
!
|
|
128
|
-
|
|
143
|
+
!matchedPath.startsWith(prefix) ||
|
|
144
|
+
!matchedPath.endsWith(suffix) ||
|
|
145
|
+
matchedPath.length < prefix.length + suffix.length ||
|
|
146
|
+
(marker === -1 && matchedPath !== pattern)
|
|
129
147
|
) {
|
|
130
148
|
return []
|
|
131
149
|
}
|
|
132
150
|
|
|
133
|
-
const capture =
|
|
151
|
+
const capture =
|
|
152
|
+
marker === -1
|
|
153
|
+
? undefined
|
|
154
|
+
: matchedPath.slice(prefix.length, matchedPath.length - suffix.length)
|
|
155
|
+
|
|
134
156
|
return [{ path: `./${path}`, capture }]
|
|
135
157
|
})
|
|
136
158
|
|
|
@@ -163,13 +185,11 @@ function packedPath(packageRoot, target) {
|
|
|
163
185
|
function sourceFile(ts, file) {
|
|
164
186
|
const extension = extname(file).toLowerCase()
|
|
165
187
|
const scriptKind =
|
|
166
|
-
extension === '.tsx'
|
|
188
|
+
extension === '.tsx' || extension === '.tsrx'
|
|
167
189
|
? ts.ScriptKind.TSX
|
|
168
190
|
: extension === '.jsx'
|
|
169
191
|
? ts.ScriptKind.JSX
|
|
170
|
-
:
|
|
171
|
-
? ts.ScriptKind.Deferred
|
|
172
|
-
: ts.ScriptKind.TS
|
|
192
|
+
: ts.ScriptKind.TS
|
|
173
193
|
|
|
174
194
|
return ts.createSourceFile(
|
|
175
195
|
file,
|
|
@@ -337,7 +357,21 @@ function compilerExports(ts, program, file, cache = new Map(), pending = new Set
|
|
|
337
357
|
// Subtract names whose only export edges are type-only.
|
|
338
358
|
const typeOnly = new Set()
|
|
339
359
|
for (const statement of source.statements) {
|
|
340
|
-
if (!ts.isExportDeclaration(statement)
|
|
360
|
+
if (!ts.isExportDeclaration(statement)) {
|
|
361
|
+
continue
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
if (statement.exportClause && ts.isNamedExports(statement.exportClause)) {
|
|
365
|
+
for (const element of statement.exportClause.elements) {
|
|
366
|
+
if (statement.isTypeOnly || element.isTypeOnly) {
|
|
367
|
+
typeOnly.add(element.name.text)
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
continue
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
if (!statement.isTypeOnly) {
|
|
341
375
|
continue
|
|
342
376
|
}
|
|
343
377
|
|
|
@@ -392,6 +426,20 @@ function moduleResolutionOptions(ts) {
|
|
|
392
426
|
allowImportingTsExtensions: true,
|
|
393
427
|
module: ts.ModuleKind.NodeNext,
|
|
394
428
|
moduleResolution: ts.ModuleResolutionKind.NodeNext,
|
|
429
|
+
// Same precedence as repository typecheck/typegen programs
|
|
430
|
+
// (packages/cli tsconfig-envs.mjs PLATFORM_MODULE_SUFFIXES):
|
|
431
|
+
// extensionless first, then every platform suffix.
|
|
432
|
+
moduleSuffixes: [
|
|
433
|
+
'',
|
|
434
|
+
'.web',
|
|
435
|
+
'.ios',
|
|
436
|
+
'.android',
|
|
437
|
+
'.mobile',
|
|
438
|
+
'.native',
|
|
439
|
+
'.macos',
|
|
440
|
+
'.linux',
|
|
441
|
+
'.windows',
|
|
442
|
+
],
|
|
395
443
|
noEmit: true,
|
|
396
444
|
skipLibCheck: true,
|
|
397
445
|
target: ts.ScriptTarget.Latest,
|
|
@@ -421,6 +469,10 @@ function verifyDeclarationClosure(ts, packageRoot, files, manifest, failures) {
|
|
|
421
469
|
)
|
|
422
470
|
} else if (!pathInside(packageRoot, resolve(resolved.resolvedFileName))) {
|
|
423
471
|
failures.push(`${relative(packageRoot, file)} resolves ${specifier} outside the package`)
|
|
472
|
+
} else if (!/\.d\.[cm]?ts$/.test(resolved.resolvedFileName)) {
|
|
473
|
+
failures.push(
|
|
474
|
+
`${relative(packageRoot, file)} resolves ${specifier} to implementation source; publish its declaration`,
|
|
475
|
+
)
|
|
424
476
|
}
|
|
425
477
|
|
|
426
478
|
continue
|
|
@@ -599,8 +651,6 @@ export function checkPackedPackage(projectRoot, ts) {
|
|
|
599
651
|
mkdirSync(packOutput)
|
|
600
652
|
mkdirSync(extractedRoot)
|
|
601
653
|
try {
|
|
602
|
-
verifyPackedPackage(ts, projectRoot)
|
|
603
|
-
console.log('tsrx-typegen: workspace exports and declaration graph are valid')
|
|
604
654
|
const packed = spawnSync(
|
|
605
655
|
'pnpm',
|
|
606
656
|
['--config.ignore-scripts=true', 'pack', '--pack-destination', packOutput],
|
|
@@ -654,7 +704,7 @@ export function checkPackedPackage(projectRoot, ts) {
|
|
|
654
704
|
}
|
|
655
705
|
|
|
656
706
|
verifyPackedPackage(ts, join(extractedRoot, 'package'))
|
|
657
|
-
console.log('tsrx-typegen: packed
|
|
707
|
+
console.log('tsrx-typegen: packed exports and declaration graph are valid')
|
|
658
708
|
} finally {
|
|
659
709
|
rmSync(temporary, { recursive: true, force: true })
|
|
660
710
|
}
|