Beta You're reading the docs for Kubb v5, which is currently in beta. View the stable v4 docs
Skip to content

Barrel files

A barrel file is an index.ts that re-exports everything from a directory. Consumers of the generated code then import from one place instead of reaching into individual files, which keeps their imports short and stable. Kubb generates these files through @kubb/plugin-barrel.

Quick start

@kubb/plugin-barrel is included by default when you import defineConfig from the kubb package. To change how barrels are generated, add it to your plugins array.

kubb.config.ts
typescript
import {  } from 'kubb'
import {  } from '@kubb/plugin-barrel'

export default ({
  : { : './petStore.yaml' },
  : { : './src/gen', : { : 'named' } },
  : [()],
})

How it works

@kubb/plugin-barrel uses enforce: 'post', so it runs after all regular plugins finish. By that point the output tree is complete, so it can walk it and create an index.ts in each directory, then a root index.ts at the top of output.path that re-exports from every plugin directory.

Exports

Export Purpose
pluginBarrel Plugin factory that emits barrel files based on output.barrel.
pluginBarrelName Stable string identifier ('plugin-barrel').

NOTE

Valid barrel.type values are 'all' and 'named'. At the plugin level, set barrel: { type: 'named', nested: true } for hierarchical barrels, or barrel: false to opt out for that plugin.