@kubb/parser-ts 5.20.1 → 5.20.3
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/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/docs/options.md +15 -11
- package/docs/overview.md +26 -11
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
package/dist/index.js
CHANGED
package/docs/options.md
CHANGED
|
@@ -1,22 +1,18 @@
|
|
|
1
1
|
# Options
|
|
2
2
|
|
|
3
|
-
`parserTs` and `parserTsx`
|
|
3
|
+
`parserTs()` and `parserTsx()` take one option, `extension`, which sets the extension on the relative imports and exports they write.
|
|
4
4
|
|
|
5
5
|
## Options overview
|
|
6
6
|
|
|
7
7
|
| Option | Purpose | Default |
|
|
8
8
|
| ------------------------- | --------------------------------------------------------------- | --------------- |
|
|
9
|
-
| [`extension`](#extension) |
|
|
9
|
+
| [`extension`](#extension) | Extension written on relative `import` and `export` specifiers. | `{ '.ts': '' }` |
|
|
10
10
|
|
|
11
11
|
## Option details
|
|
12
12
|
|
|
13
13
|
### extension
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
Use `{ '.ts': '.js' }` to emit `.js` specifiers for ESM output compiled to JavaScript. Use `{ '.ts': '.ts' }` to keep `.ts` specifiers when you run the `.ts` files directly in Node.js or compile them with `rewriteRelativeImportExtensions`.
|
|
18
|
-
|
|
19
|
-
The same mapping applies to runtime templates a plugin copies into the output, such as the `.kubb/` runtime files of `@kubb/plugin-fetch`. Their top-level `import` and `export … from` statements are rewritten the same way, so the whole output follows one convention.
|
|
15
|
+
Maps the extension of a generated file to the extension its relative specifiers get. `''` drops the extension. Package specifiers and the file names on disk never change.
|
|
20
16
|
|
|
21
17
|
| | |
|
|
22
18
|
| -------- | ------------------------ |
|
|
@@ -24,7 +20,15 @@ The same mapping applies to runtime templates a plugin copies into the output, s
|
|
|
24
20
|
| Required | `false` |
|
|
25
21
|
| Default | `{ '.ts': '' }` |
|
|
26
22
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
23
|
+
| Value | Writes | Use it when |
|
|
24
|
+
| ------------------------- | ------------------- | --------------------------------------------------------------------------------------------- |
|
|
25
|
+
| `{ '.ts': '' }` (default) | `import './api'` | A bundler or `moduleResolution: 'bundler'` resolves the files. |
|
|
26
|
+
| `{ '.ts': '.js' }` | `import './api.js'` | You compile the output to ESM JavaScript. |
|
|
27
|
+
| `{ '.ts': '.ts' }` | `import './api.ts'` | Node.js runs the `.ts` files directly, or you compile with `rewriteRelativeImportExtensions`. |
|
|
28
|
+
|
|
29
|
+
The same map applies to the runtime templates a plugin copies into the output, such as the `.kubb/` files of `@kubb/plugin-fetch`.
|
|
30
|
+
|
|
31
|
+
> [!NOTE]
|
|
32
|
+
> `parserTsx` reads only its own map. Set the `.tsx` key on it, such as `parserTsx({ extension: { '.tsx': '.js' } })`.
|
|
33
|
+
|
|
34
|
+
A file whose extension has no key also loses its extension. A relative specifier without an extension stays unchanged.
|
package/docs/overview.md
CHANGED
|
@@ -1,24 +1,30 @@
|
|
|
1
1
|
# TypeScript
|
|
2
2
|
|
|
3
|
-
The TypeScript parser
|
|
3
|
+
The TypeScript parser prints the `.ts` and `.js` files that plugins generate, with imports, exports, declarations, and JSDoc.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
- It rewrites relative import paths.
|
|
6
|
+
- The package also exports `parserTsx` for `.tsx` and `.jsx` files, with the same options.
|
|
6
7
|
|
|
7
8
|
## Setup
|
|
8
9
|
|
|
9
|
-
|
|
10
|
+
No setup is needed. `defineConfig` and the bundler integrations register `parserTs()`, `parserTsx()`, and `parserMd()` by default. Set `parsers` only to change an option or to add a parser.
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
> [!WARNING]
|
|
13
|
+
> A custom `parsers` array replaces the default set, so list every parser your plugins need. A file that no parser claims is written as its source text only, without imports, exports, banner, or footer.
|
|
12
14
|
|
|
13
|
-
|
|
15
|
+
## Options worth knowing
|
|
14
16
|
|
|
15
|
-
|
|
17
|
+
| Option | Default | What it does |
|
|
18
|
+
| -------------------------------------------------------------------------------- | --------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
19
|
+
| [`extension`](https://kubb.dev/extensions/kubb-labs/parser-ts/options#extension) | `{ '.ts': '' }` | Sets the extension that relative imports and exports get. Use `{ '.ts': '.js' }` for ESM output and `{ '.ts': '.ts' }` to run `.ts` files directly in Node.js. |
|
|
16
20
|
|
|
17
|
-
|
|
21
|
+
`parserTsx` reads its own `extension` map, so set the `.tsx` key on it too.
|
|
18
22
|
|
|
19
23
|
## Example
|
|
20
24
|
|
|
21
|
-
|
|
25
|
+
This config adds `.js` to relative imports in `.ts` and `.tsx` files and keeps every default parser.
|
|
26
|
+
|
|
27
|
+
::code-group
|
|
22
28
|
|
|
23
29
|
```typescript [kubb.config.ts]
|
|
24
30
|
import { defineConfig } from 'kubb/config'
|
|
@@ -34,8 +40,17 @@ export default defineConfig({
|
|
|
34
40
|
parserTsx({ extension: { '.tsx': '.js' } }),
|
|
35
41
|
parserMd(),
|
|
36
42
|
],
|
|
37
|
-
plugins: [
|
|
38
|
-
pluginTs(),
|
|
39
|
-
],
|
|
43
|
+
plugins: [pluginTs()],
|
|
40
44
|
})
|
|
41
45
|
```
|
|
46
|
+
|
|
47
|
+
```typescript [src/gen/types/Pet.ts]
|
|
48
|
+
import type { Category } from './Category.js'
|
|
49
|
+
|
|
50
|
+
export type Pet = {
|
|
51
|
+
category?: Category
|
|
52
|
+
name: string
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
::
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/parser-ts",
|
|
3
|
-
"version": "5.20.
|
|
3
|
+
"version": "5.20.3",
|
|
4
4
|
"description": "TypeScript and TSX parser for Kubb. Prints AST nodes as TypeScript and TSX source, with imports, exports and JSDoc.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"codegen",
|
|
@@ -64,8 +64,8 @@
|
|
|
64
64
|
"registry": "https://registry.npmjs.org/"
|
|
65
65
|
},
|
|
66
66
|
"dependencies": {
|
|
67
|
-
"@kubb/ast": "^5.20.
|
|
68
|
-
"@kubb/core": "^5.20.
|
|
67
|
+
"@kubb/ast": "^5.20.3",
|
|
68
|
+
"@kubb/core": "^5.20.3",
|
|
69
69
|
"yuku-parser": "^0.19.0"
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|