Skip to content
Barrel

Barrel

Officialv5.8.3

@kubb/plugin-barrel

kubb add plugin-barrel

Options

pluginBarrel takes no arguments. You configure it through output.barrel, either on defineConfig to set the root barrel and the default every plugin inherits, or on a single plugin to override that plugin's barrel. The plugin-level behavior is also summarized under output.barrel in the shared plugin options.

Options overview

OptionPurposeDefault
output.barrelRe-export style for the barrel files.false on defineConfig, { type: 'named' } in every generator plugin's default output
↳ output.barrel.typeNamed exports or a wildcard export.Required when output.barrel is an object
↳ output.barrel.nestedChoose whether barrels reference subdirectory barrels.false

Option details

output.barrel

Kubb writes one root barrel and an index.ts in every directory of a plugin's output. For two plugins that write to models and clients:

src/gen
├── index.ts              root barrel
├── models
│   ├── index.ts          plugin barrel
│   ├── Pet.ts
│   └── User.ts
└── clients
    ├── index.ts          plugin barrel
    ├── pet
    │   ├── index.ts
    │   └── getPetById.ts
    └── store
        ├── index.ts
        └── getInventory.ts
Type{ type: 'all' | 'named', nested?: boolean } | false
Requiredfalse
Defaultfalse

The type field picks the export style. A plugin's output.barrel also accepts nested, so each barrel re-exports the barrels below it instead of the leaf files. The root output.barrel has no nested field and always stays flat.

A plugin called without output uses its own default, which sets barrel: { type: 'named' }. If you pass your own output object, it replaces that default. pluginTs({ output: { path: 'types' } }) then has no barrel and uses config.output.barrel, which is false unless you set it.

Set barrel: { type: 'named' | 'all' } on defineConfig to enable barrels everywhere: a root barrel, and the default every plugin without its own output.barrel inherits. A plugin that sets its own output.barrel overrides that inherited value, including back to false, which also drops its files from the root barrel. A plugin with output.mode: 'file' gets no barrel of its own, and the root barrel re-exports that file directly.

type

Export style for the barrel files. Required whenever output.barrel is set to an object.

Type'all' | 'named'
Requiredtrue
'named'
Re-exports each symbol by name from the file's named exports. Best for tree-shaking and explicit imports.
'all'
Uses export *, a smaller barrel that re-exports everything.
// src/gen/index.ts
export { getUser } from './api/user'
export { getPost, Post } from './api/post'
export { User } from './api/types/User'

nested

Changes what each barrel references. A barrel is written for every directory either way. With nested: false, the plugin's top barrel reaches through to the leaf files. With nested: true, each barrel re-exports only what sits directly inside its directory, including the subdirectory barrels below it, so callers can import from any depth. This field works on a plugin's output.barrel only.

Typeboolean
Requiredfalse
Defaultfalse
// src/gen/api/index.ts
export * from './user'
export * from './post'
export * from './types/User'

// src/gen/api/types/index.ts
export * from './User'