Are you an LLM? You can read better optimized documentation at /plugins/plugin-barrel.md for this page in Markdown format
@kubb/plugin-barrel
TIP
plugin-barrel ships with Kubb and runs by default. Install it on its own only when you want to tune barrel behavior.
@kubb/plugin-barrel writes the index.ts barrel files. It adds one barrel per plugin output directory and one root barrel at output.path/index.ts. This runs after the build finishes, so you import everything from one entry point, like import { Pet, usePetByIdQuery, petMock } from './gen'.
The plugin is registered by default in defineConfig, so barrels appear with no setup. When it runs, the default output.barrel is { type: 'named' }.
A plugin inherits output.barrel from config.output.barrel when it sets none of its own. Set barrel: false on a plugin to skip its barrel and drop its files from the root barrel.
Installation
shell
bun add -d @kubb/plugin-barrel@betashell
pnpm add -D @kubb/plugin-barrel@betashell
npm install --save-dev @kubb/plugin-barrel@betashell
yarn add -D @kubb/plugin-barrel@betaExample
typescript
import { defineConfig } from 'kubb'
export default defineConfig({
input: { path: './petStore.yaml' },
output: { path: './src/gen' },
plugins: [],
})typescript
// src/gen/index.ts
export { getUser, User } from './api/user'
export { getPost, Post } from './api/post'
export { User } from './api/types/User'
export { useUser } from './hooks/useUser'
// src/gen/api/index.ts
export { getUser, User } from './user'
export { getPost, Post } from './post'
export { User } from './types/User'
// src/gen/api/types/index.ts
export { User } from './User'typescript
import { defineConfig } from 'kubb'
export default defineConfig({
input: { path: './petStore.yaml' },
output: { path: './src/gen', barrel: { type: 'all' } },
plugins: [],
})typescript
// src/gen/index.ts
export * from './api/user'
export * from './api/post'
export * from './api/types/User'
export * from './hooks/useUser'
// src/gen/api/index.ts
export * from './user'
export * from './post'
export * from './types/User'
// src/gen/api/types/index.ts
export * from './User'typescript
import { defineConfig } from 'kubb'
import { pluginTs } from '@kubb/plugin-ts'
// nested lives on a plugin's output.barrel, not on the root output.barrel.
export default defineConfig({
input: { path: './petStore.yaml' },
output: { path: './src/gen' },
plugins: [pluginTs({ output: { path: 'api', barrel: { type: 'all', nested: true } } })],
})typescript
// src/gen/api/index.ts re-exports its files and subdirectories
export * from './user'
export * from './post'
export * from './types'
// src/gen/api/types/index.ts re-exports its files
export * from './User'
// the root src/gen/index.ts still uses the config default ({ type: 'named' })
export { getUser, User } from './api/user'
export { getPost, Post } from './api/post'
export { User } from './api/types/User'typescript
import { defineConfig } from 'kubb'
import { pluginTs } from '@kubb/plugin-ts'
import { pluginZod } from '@kubb/plugin-zod'
// No zod/index.ts is created, and zod files
// are dropped from the root index.ts.
export default defineConfig({
input: { path: './petStore.yaml' },
output: { path: './src/gen' },
plugins: [pluginTs(), pluginZod({ output: { path: 'zod', barrel: false } })],
})typescript
import { } from 'kubb'
// No root index.ts. Each plugin still gets
// its own barrel from its inherited config.
export default ({
: { : './petStore.yaml' },
: { : './src/gen', : false },
: [],
})