@kubb/plugin-barrel 5.20.0 → 5.20.2
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/guides/barrel-rules.md +46 -0
- package/docs/options.md +60 -42
- package/docs/overview.md +26 -13
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
package/dist/index.js
CHANGED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# Barrel rules
|
|
2
|
+
|
|
3
|
+
Which barrel setting applies to each plugin, when a plugin gets no barrel, and what each `index.ts` re-exports.
|
|
4
|
+
|
|
5
|
+
## Which barrel setting applies
|
|
6
|
+
|
|
7
|
+
The Barrel plugin runs after every other plugin, so each barrel sees the final set of files. For each plugin, Kubb reads its `output.barrel` first, then the root [`output.barrel`](https://kubb.dev/docs/5.x/reference/configuration#outputbarrel), then `false`.
|
|
8
|
+
|
|
9
|
+
- The root `output.barrel` accepts only `type` and is always flat. It has no `nested` or `namespace` field.
|
|
10
|
+
- Passing your own `output` to a plugin replaces its default `barrel: { type: 'named' }`, so the plugin falls back to the root setting.
|
|
11
|
+
|
|
12
|
+
| Root `output.barrel` | Plugin `output.barrel` | Plugin barrels | Root barrel |
|
|
13
|
+
| -------------------- | ------------------------------------------------------------------------------------------------------ | ---------------------------- | -------------------------------- |
|
|
14
|
+
| `false` (default) | Plugin default `{ type: 'named' }` | Yes | No |
|
|
15
|
+
| `false` (default) | Unset, because your own `output` replaced the default, as in `pluginTs({ output: { path: 'types' } })` | No | No |
|
|
16
|
+
| `{ type }` | Unset | Yes, with the root `type` | Yes |
|
|
17
|
+
| `{ type }` | `{ type, … }` | Yes, with the plugin setting | Yes |
|
|
18
|
+
| `{ type }` | `false` | No | Yes, without this plugin's files |
|
|
19
|
+
|
|
20
|
+
## When a plugin gets no barrel
|
|
21
|
+
|
|
22
|
+
| Case | Result |
|
|
23
|
+
| --------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------ |
|
|
24
|
+
| `output.mode: 'file'` | The root barrel re-exports the single file directly. |
|
|
25
|
+
| The plugin writes to the output root, with no own `output.path` | The root barrel picks up its files. |
|
|
26
|
+
| `output.path` resolves outside the root `output.path` | With a directory barrel, the build fails with `KUBB_PATH_TRAVERSAL`. With `barrel: false` or `mode: 'file'`, Kubb does not check it. |
|
|
27
|
+
|
|
28
|
+
## What goes into a barrel
|
|
29
|
+
|
|
30
|
+
- Every `.ts`, `.tsx`, `.js`, and `.jsx` file below the folder, sorted by name. Other `index.ts` files are not re-exported as leaves.
|
|
31
|
+
- The barrel file is always `index.ts`.
|
|
32
|
+
- With `type: 'named'`, a file without sources is re-exported whole.
|
|
33
|
+
- With `type: 'all'`, a file whose sources are all marked as not indexable is left out.
|
|
34
|
+
- With `nested`, a subfolder barrel is always chained with `export *`, also for `type: 'named'`, because the child barrel already lists the names.
|
|
35
|
+
|
|
36
|
+
A plugin's barrels get its `output.banner` and `output.footer` only when the plugin sets them. They never get the default "Generated by Kubb" notice, and the root barrel gets neither.
|
|
37
|
+
|
|
38
|
+
## Namespaces for tag groups
|
|
39
|
+
|
|
40
|
+
[`namespace`](https://kubb.dev/extensions/kubb-labs/plugin-barrel/options#namespace) exposes each immediate subfolder as a named namespace, next to its standalone exports. Combine it with tag [`group`](https://kubb.dev/docs/5.x/reference/plugin-options#group)s to export each group as a named service.
|
|
41
|
+
|
|
42
|
+
- A namespace takes the PascalCase folder name. A name that does not start with a letter gets an `_` prefix, and a name with no letters or digits becomes `_`.
|
|
43
|
+
- An enabled root barrel forwards the namespaces too.
|
|
44
|
+
- Editors can offer auto-imports for these namespaces when the generated files belong to the TypeScript project.
|
|
45
|
+
|
|
46
|
+
For tag-grouped fetch clients, name the service folders with `group.name`. Leave the fetch `sdk` option unset so React Query can import the standalone operation functions. For a `pet` tag, the operations land in `src/gen/client/petService/`. Both calling styles, `PetService.getPetById()` and `getPetById()`, work next to the generated hooks and query options.
|
package/docs/options.md
CHANGED
|
@@ -1,21 +1,24 @@
|
|
|
1
1
|
# Options
|
|
2
2
|
|
|
3
|
-
`pluginBarrel` takes no arguments.
|
|
3
|
+
`pluginBarrel()` takes no arguments. Configure it with `output.barrel` on `defineConfig` or on one plugin.
|
|
4
|
+
|
|
5
|
+
`output.barrel` on `defineConfig` sets the root barrel and the default every plugin inherits. On a plugin, it overrides that plugin's barrels. [Shared plugin options](https://kubb.dev/docs/5.x/reference/plugin-options#output-barrel) summarize the plugin-level setting.
|
|
4
6
|
|
|
5
7
|
## Options overview
|
|
6
8
|
|
|
7
|
-
| Option
|
|
8
|
-
|
|
|
9
|
-
| [`output.barrel`](#
|
|
10
|
-
|
|
|
11
|
-
| ↳ [`output.barrel.
|
|
12
|
-
| ↳ [`output.barrel.
|
|
9
|
+
| Option | Purpose | Default |
|
|
10
|
+
| -------------------------------------------------- | --------------------------------------------------------------- | --------------------------------------------------------------- |
|
|
11
|
+
| [`output.barrel`](#outputbarrel) on `defineConfig` | Root barrel, and the value plugins inherit. | `false` |
|
|
12
|
+
| [`output.barrel`](#outputbarrel) on a plugin | That plugin's barrels. | `{ type: 'named' }` in each generator plugin's default `output` |
|
|
13
|
+
| ↳ [`output.barrel.type`](#type) | Named exports or a wildcard export. | Required when `output.barrel` is an object |
|
|
14
|
+
| ↳ [`output.barrel.nested`](#nested) | Re-export subfolder barrels instead of leaf files. Plugin only. | `false` |
|
|
15
|
+
| ↳ [`output.barrel.namespace`](#namespace) | Add a named namespace per subfolder. Plugin only. | `false` |
|
|
13
16
|
|
|
14
17
|
## Option details
|
|
15
18
|
|
|
16
19
|
### output.barrel
|
|
17
20
|
|
|
18
|
-
Kubb writes an `index.ts` in every
|
|
21
|
+
Kubb writes an `index.ts` in every folder of a plugin's output, and a root barrel when `output.barrel` is set on `defineConfig`. For two plugins that write to `models` and `clients`:
|
|
19
22
|
|
|
20
23
|
```text
|
|
21
24
|
src/gen
|
|
@@ -40,54 +43,60 @@ src/gen
|
|
|
40
43
|
| Required | `false` |
|
|
41
44
|
| Default | `false` |
|
|
42
45
|
|
|
43
|
-
|
|
46
|
+
- The root `output.barrel` accepts only `type` and is always flat.
|
|
47
|
+
- A plugin without its own `output.barrel` uses the root `type`.
|
|
48
|
+
- `false` on a plugin writes no barrel for it and leaves its files out of the root barrel.
|
|
44
49
|
|
|
45
|
-
|
|
50
|
+
> [!WARNING]
|
|
51
|
+
> Passing your own `output` to a plugin, as in `pluginTs({ output: { path: 'types' } })`, replaces its default `{ type: 'named' }`. With the root `output.barrel` at `false`, that plugin then gets no barrels.
|
|
46
52
|
|
|
47
|
-
|
|
53
|
+
See [Barrel rules](https://kubb.dev/extensions/kubb-labs/plugin-barrel/guides/barrel-rules#which-barrel-setting-applies).
|
|
48
54
|
|
|
49
55
|
### type
|
|
50
56
|
|
|
51
|
-
Export style
|
|
57
|
+
Export style of the barrel files.
|
|
52
58
|
|
|
53
|
-
| |
|
|
54
|
-
| -------- |
|
|
55
|
-
| Type | `'all' \| 'named'`
|
|
56
|
-
| Required | `true`
|
|
59
|
+
| | |
|
|
60
|
+
| -------- | ---------------------------------------- |
|
|
61
|
+
| Type | `'all' \| 'named'` |
|
|
62
|
+
| Required | `true` when `output.barrel` is an object |
|
|
57
63
|
|
|
58
64
|
::field-group
|
|
59
65
|
|
|
60
66
|
:::field{name="'named'"}
|
|
61
|
-
Re-exports each symbol by name
|
|
67
|
+
Re-exports each symbol by name, with values and types in separate statements. It suits tree-shaking and explicit imports.
|
|
62
68
|
:::
|
|
63
69
|
|
|
64
70
|
:::field{name="'all'"}
|
|
65
|
-
|
|
71
|
+
Writes `export *` per file, a smaller barrel.
|
|
66
72
|
:::
|
|
67
73
|
|
|
68
74
|
::
|
|
69
75
|
|
|
76
|
+
For a plugin output with `post.ts`, `types/User.ts`, and `user.ts`:
|
|
77
|
+
|
|
70
78
|
::code-group
|
|
71
79
|
|
|
72
80
|
```typescript ['named']
|
|
73
|
-
// src/gen/index.ts
|
|
74
|
-
export {
|
|
75
|
-
export {
|
|
76
|
-
export { User } from './
|
|
81
|
+
// src/gen/api/index.ts
|
|
82
|
+
export { getPost } from './post'
|
|
83
|
+
export type { Post } from './post'
|
|
84
|
+
export type { User } from './types/User'
|
|
85
|
+
export { getUser } from './user'
|
|
77
86
|
```
|
|
78
87
|
|
|
79
88
|
```typescript ['all']
|
|
80
|
-
// src/gen/index.ts
|
|
81
|
-
export * from './
|
|
82
|
-
export * from './
|
|
83
|
-
export * from './
|
|
89
|
+
// src/gen/api/index.ts
|
|
90
|
+
export * from './post'
|
|
91
|
+
export * from './types/User'
|
|
92
|
+
export * from './user'
|
|
84
93
|
```
|
|
85
94
|
|
|
86
95
|
::
|
|
87
96
|
|
|
88
97
|
### nested
|
|
89
98
|
|
|
90
|
-
Changes what each barrel references.
|
|
99
|
+
Changes what each barrel references. Kubb writes a barrel in every folder either way. This field works on a plugin's `output.barrel` only.
|
|
91
100
|
|
|
92
101
|
| | |
|
|
93
102
|
| -------- | --------- |
|
|
@@ -95,25 +104,30 @@ Changes what each barrel references. A barrel is written for every directory eit
|
|
|
95
104
|
| Required | `false` |
|
|
96
105
|
| Default | `false` |
|
|
97
106
|
|
|
107
|
+
| Value | The plugin's top barrel re-exports | Import from |
|
|
108
|
+
| ----------------- | -------------------------------------------------- | -------------- |
|
|
109
|
+
| `false` (default) | Every leaf file below it | The top folder |
|
|
110
|
+
| `true` | Its own files and the `index.ts` of each subfolder | Any depth |
|
|
111
|
+
|
|
98
112
|
::code-group
|
|
99
113
|
|
|
100
|
-
```typescript [nested: false
|
|
114
|
+
```typescript [nested: false]
|
|
101
115
|
// src/gen/api/index.ts
|
|
102
|
-
export * from './user'
|
|
103
116
|
export * from './post'
|
|
104
117
|
export * from './types/User'
|
|
118
|
+
export * from './user'
|
|
105
119
|
|
|
106
120
|
// src/gen/api/types/index.ts
|
|
107
121
|
export * from './User'
|
|
108
122
|
```
|
|
109
123
|
|
|
110
|
-
```typescript [nested: true
|
|
111
|
-
// src/gen/api/index.ts
|
|
112
|
-
export * from './user'
|
|
124
|
+
```typescript [nested: true]
|
|
125
|
+
// src/gen/api/index.ts
|
|
113
126
|
export * from './post'
|
|
114
127
|
export * from './types/index'
|
|
128
|
+
export * from './user'
|
|
115
129
|
|
|
116
|
-
// src/gen/api/types/index.ts
|
|
130
|
+
// src/gen/api/types/index.ts
|
|
117
131
|
export * from './User'
|
|
118
132
|
```
|
|
119
133
|
|
|
@@ -121,7 +135,7 @@ export * from './User'
|
|
|
121
135
|
|
|
122
136
|
### namespace
|
|
123
137
|
|
|
124
|
-
Exposes each immediate
|
|
138
|
+
Exposes each immediate subfolder as a named namespace, such as `PetService`, next to its standalone exports.
|
|
125
139
|
|
|
126
140
|
| | |
|
|
127
141
|
| -------- | --------- |
|
|
@@ -129,7 +143,13 @@ Exposes each immediate subdirectory as a named namespace, alongside its standalo
|
|
|
129
143
|
| Required | `false` |
|
|
130
144
|
| Default | `false` |
|
|
131
145
|
|
|
132
|
-
|
|
146
|
+
- Works on a plugin's `output.barrel` only, and turns on nested barrels even when `nested` is unset or `false`.
|
|
147
|
+
- Needs the plugin's own `output.path`. A plugin that writes to the output root gets a `KUBB_PLUGIN_WARNING` and no namespaces.
|
|
148
|
+
- A namespace takes the PascalCase folder name. Name the folders with `group.name`.
|
|
149
|
+
|
|
150
|
+
For tag-grouped fetch clients, leave the fetch `sdk` option unset so React Query can import the standalone operation functions:
|
|
151
|
+
|
|
152
|
+
::code-group
|
|
133
153
|
|
|
134
154
|
```typescript [kubb.config.ts]
|
|
135
155
|
import { defineConfig } from 'kubb/config'
|
|
@@ -153,17 +173,11 @@ export default defineConfig({
|
|
|
153
173
|
})
|
|
154
174
|
```
|
|
155
175
|
|
|
156
|
-
`integerType: 'number'` types every `int64` as a `number`, so the example can pass `petId: 123`. A `number` loses precision above `Number.MAX_SAFE_INTEGER`. Keep the default `bigint` for larger ids and pass `123n`.
|
|
157
|
-
|
|
158
|
-
For a `pet` tag, the operations land in `src/gen/client/petService/`. `src/gen/client/index.ts` exports both the operations and their PascalCase namespace:
|
|
159
|
-
|
|
160
176
|
```typescript [src/gen/client/index.ts]
|
|
161
177
|
export * from './petService/index'
|
|
162
178
|
export * as PetService from './petService/index'
|
|
163
179
|
```
|
|
164
180
|
|
|
165
|
-
Both calling styles remain available alongside the generated hooks and query options:
|
|
166
|
-
|
|
167
181
|
```typescript [src/example.ts]
|
|
168
182
|
import { PetService, getPetById } from './gen/client'
|
|
169
183
|
|
|
@@ -171,4 +185,8 @@ await PetService.getPetById({ path: { petId: 123 } })
|
|
|
171
185
|
await getPetById({ path: { petId: 123 } })
|
|
172
186
|
```
|
|
173
187
|
|
|
174
|
-
|
|
188
|
+
::
|
|
189
|
+
|
|
190
|
+
For a `pet` tag, the operations land in `src/gen/client/petService/`. The example sets [`integerType: 'number'`](https://kubb.dev/extensions/kubb-labs/adapter-oas/options#integertype), so it can pass `123` as an `int64` id.
|
|
191
|
+
|
|
192
|
+
See [Namespaces for tag groups](https://kubb.dev/extensions/kubb-labs/plugin-barrel/guides/barrel-rules#namespaces-for-tag-groups).
|
package/docs/overview.md
CHANGED
|
@@ -1,27 +1,34 @@
|
|
|
1
1
|
# Barrel
|
|
2
2
|
|
|
3
|
-
The Barrel plugin writes `index.ts` files that re-export generated code, so you
|
|
3
|
+
The Barrel plugin writes `index.ts` files that re-export the generated code, so you import everything from one entry point.
|
|
4
|
+
|
|
5
|
+
It writes a barrel in every folder of each plugin output that has a barrel setting, plus a root barrel when you set `output.barrel` on `defineConfig`.
|
|
4
6
|
|
|
5
7
|
## Setup
|
|
6
8
|
|
|
7
|
-
|
|
9
|
+
No setup is needed. `defineConfig` adds `pluginBarrel()` to `plugins` when it is missing. Generator plugins such as `pluginTs()` turn on their own barrels in their default `output`, and a plugin without its own `output.barrel` inherits the root setting.
|
|
8
10
|
|
|
9
11
|
## Options worth knowing
|
|
10
12
|
|
|
11
|
-
`pluginBarrel()` takes no arguments.
|
|
13
|
+
`pluginBarrel()` takes no arguments. Configure it with `output.barrel`, on `defineConfig` or on one plugin.
|
|
12
14
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
- `false`
|
|
15
|
+
| Setting | Default | What it does |
|
|
16
|
+
| ------------------------------------------------------------------------------------ | -------- | ---------------------------------------------------------------------------------------------------- |
|
|
17
|
+
| [`type`](https://kubb.dev/extensions/kubb-labs/plugin-barrel/options#type) | Required | `'named'` re-exports each symbol by name, which suits tree-shaking. `'all'` writes `export *`. |
|
|
18
|
+
| [`nested`](https://kubb.dev/extensions/kubb-labs/plugin-barrel/options#nested) | `false` | Plugin only. Each barrel re-exports only its own folder and the barrels below it. |
|
|
19
|
+
| [`namespace`](https://kubb.dev/extensions/kubb-labs/plugin-barrel/options#namespace) | `false` | Plugin only. Turns on nested barrels and adds a named namespace per subfolder, such as `PetService`. |
|
|
20
|
+
| `false` | | On a plugin, writes no barrel and leaves the plugin's files out of the root barrel. |
|
|
17
21
|
|
|
18
|
-
|
|
22
|
+
> [!WARNING]
|
|
23
|
+
> Passing your own `output` to a plugin replaces its default `barrel: { type: 'named' }`, so the plugin falls back to the root setting.
|
|
19
24
|
|
|
20
|
-
See [
|
|
25
|
+
See [Barrel rules](https://kubb.dev/extensions/kubb-labs/plugin-barrel/guides/barrel-rules#which-barrel-setting-applies).
|
|
21
26
|
|
|
22
27
|
## Example
|
|
23
28
|
|
|
24
|
-
|
|
29
|
+
This config writes a root `src/gen/index.ts` that re-exports the generated types by name.
|
|
30
|
+
|
|
31
|
+
::code-group
|
|
25
32
|
|
|
26
33
|
```typescript [kubb.config.ts]
|
|
27
34
|
import { defineConfig } from 'kubb/config'
|
|
@@ -30,8 +37,14 @@ import { pluginTs } from '@kubb/plugin-ts'
|
|
|
30
37
|
export default defineConfig({
|
|
31
38
|
input: './petStore.yaml',
|
|
32
39
|
output: { path: './src/gen', barrel: { type: 'named' } },
|
|
33
|
-
plugins: [
|
|
34
|
-
pluginTs(),
|
|
35
|
-
],
|
|
40
|
+
plugins: [pluginTs()],
|
|
36
41
|
})
|
|
37
42
|
```
|
|
43
|
+
|
|
44
|
+
```typescript [src/gen/index.ts]
|
|
45
|
+
export type { Category } from './types/Category'
|
|
46
|
+
export { petStatusEnum } from './types/Pet'
|
|
47
|
+
export type { Pet, PetStatusEnumKey } from './types/Pet'
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
::
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/plugin-barrel",
|
|
3
|
-
"version": "5.20.
|
|
3
|
+
"version": "5.20.2",
|
|
4
4
|
"description": "Barrel-file plugin for Kubb. Automatically generates index.ts re-export files per plugin output directory.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"barrel",
|
|
@@ -66,7 +66,7 @@
|
|
|
66
66
|
"registry": "https://registry.npmjs.org/"
|
|
67
67
|
},
|
|
68
68
|
"dependencies": {
|
|
69
|
-
"@kubb/kit": "5.20.
|
|
69
|
+
"@kubb/kit": "5.20.2"
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|
|
72
72
|
"@internals/utils": "0.0.1"
|