Beta You're reading the docs for Kubb v5, which is currently in beta. View the stable v4 docs
Skip to content
Official v5.0.0-beta.51 MIT kubb >=5.0.0 node >=22

@kubb/plugin-barrel

Generates an index.ts barrel for every plugin output and one root barrel, so you import all generated code from a single entry point. Ships with Kubb and runs by default.

barrelindexexportsoutput
Downloads
5.4k / mo
Stars
1.7k
Bundle size
111.4 kB
Updated
3w ago

@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@beta
shell
pnpm add -D @kubb/plugin-barrel@beta
shell
npm install --save-dev @kubb/plugin-barrel@beta
shell
yarn add -D @kubb/plugin-barrel@beta

Example

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 },
  : [],
})

See Also