@kubb/plugin-barrel
TIP
pluginBarrel is registered by default, but generates nothing until you set output.barrel.
@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, but barrels stay off until you configure output.barrel, root or per-plugin. See that reference entry for how the default, inheritance, and overrides work.
Installation
bun add -d @kubb/plugin-barrel@betapnpm add -D @kubb/plugin-barrel@betanpm install --save-dev @kubb/plugin-barrel@betayarn add -D @kubb/plugin-barrel@betaDependencies
@kubb/plugin-barrel has no plugin dependencies. It ships with Kubb and runs by default, reading the files other plugins write to build the barrels, so you never add it to plugins yourself.
Example
import { defineConfig } from 'kubb'
export default defineConfig({
input: './petStore.yaml',
output: { path: './src/gen', barrel: { type: 'named' } },
plugins: [],
})// 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'import { defineConfig } from 'kubb'
export default defineConfig({
input: './petStore.yaml',
output: { path: './src/gen', barrel: { type: 'all' } },
plugins: [],
})// 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'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: './petStore.yaml',
output: { path: './src/gen', barrel: { type: 'named' } },
plugins: [pluginTs({ output: { path: 'api', barrel: { type: 'all', nested: true } } })],
})// 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 root's own output.barrel ({ type: 'named' })
export { getUser, User } from './api/user'
export { getPost, Post } from './api/post'
export { User } from './api/types/User'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: './petStore.yaml',
output: { path: './src/gen', barrel: { type: 'named' } },
plugins: [pluginTs(), pluginZod({ output: { path: 'zod', barrel: false } })],
})import { defineConfig } from 'kubb'
import { pluginTs } from '@kubb/plugin-ts'
// No root index.ts. The pluginTs barrel below still
// generates from its own output.barrel.
export default defineConfig({
input: './petStore.yaml',
output: { path: './src/gen', barrel: false },
plugins: [pluginTs({ output: { barrel: { type: 'named' } } })],
})