Barrel
Officialv5.20.2@kubb/plugin-barrel
Barrel rules
Which barrel setting applies to each plugin, when a plugin gets no barrel, and what each index.ts re-exports.
Which barrel setting applies
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, then false.
- The root
output.barrelaccepts onlytypeand is always flat. It has nonestedornamespacefield. - Passing your own
outputto a plugin replaces its defaultbarrel: { type: 'named' }, so the plugin falls back to the root setting.
Root output.barrel | Plugin output.barrel | Plugin barrels | Root barrel |
|---|---|---|---|
false (default) | Plugin default { type: 'named' } | Yes | No |
false (default) | Unset, because your own output replaced the default, as in pluginTs({ output: { path: 'types' } }) | No | No |
{ type } | Unset | Yes, with the root type | Yes |
{ type } | { type, … } | Yes, with the plugin setting | Yes |
{ type } | false | No | Yes, without this plugin's files |
When a plugin gets no barrel
| Case | Result |
|---|---|
output.mode: 'file' | The root barrel re-exports the single file directly. |
The plugin writes to the output root, with no own output.path | The root barrel picks up its files. |
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. |
What goes into a barrel
- Every
.ts,.tsx,.js, and.jsxfile below the folder, sorted by name. Otherindex.tsfiles are not re-exported as leaves. - The barrel file is always
index.ts. - With
type: 'named', a file without sources is re-exported whole. - With
type: 'all', a file whose sources are all marked as not indexable is left out. - With
nested, a subfolder barrel is always chained withexport *, also fortype: 'named', because the child barrel already lists the names.
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.
Namespaces for tag groups
namespace exposes each immediate subfolder as a named namespace, next to its standalone exports. Combine it with tag groups to export each group as a named service.
- 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_. - An enabled root barrel forwards the namespaces too.
- Editors can offer auto-imports for these namespaces when the generated files belong to the TypeScript project.
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.
