@kubb/middleware-barrel
Generates `index.ts` re-export files for every plugin output and one root barrel. Ships with Kubb and is enabled by default.
- Downloads
- 7k / mo
- Stars
- 1.7k
- Bundle size
- 106.1 kB
- Updated
- 3d ago
TIP
middleware-barrel ships with Kubb and is enabled automatically when no middleware option is provided. Install it explicitly only when customizing barrel behavior alongside other middlewares.
@kubb/middleware-barrel generates an index.ts for every plugin output directory and one root barrel at output.path/index.ts after the build completes. The result is a single import surface for every consumer — import { Pet, usePetByIdQuery, petMock } from './gen'.
The middleware ships with Kubb and is registered by default in defineConfig, so barrels appear out of the box with no extra configuration. When middlewareBarrel is part of config.middleware, defineConfig also applies a default output.barrel of { type: 'named' }. Custom middleware lists that omit middlewareBarrel leave barrel untouched.
Plugins inherit output.barrel from config.output.barrel when their own value is omitted. Setting barrel: false on a plugin disables that plugin's barrel and excludes its files from the root barrel.
Installation
bun add -d @kubb/middleware-barrel@betapnpm add -D @kubb/middleware-barrel@betanpm install --save-dev @kubb/middleware-barrel@betayarn add -D @kubb/middleware-barrel@betaOptions
barrel
Re-export style used for every generated barrel file. Set via output.barrel in defineConfig (applies to all plugins and the root barrel) or per plugin via that plugin's own output.barrel. It is not an argument to the middleware itself.
At the config level:
{ type: 'all' }—export * from '...'for every generated file.{ type: 'named' }—export { Foo, Bar } from '...'using each file's named exports.false— disables barrel generation entirely.
At the plugin level you also get nested:
{ type: 'named', nested: true }— generates a barrel for every subdirectory, re-exporting only direct children. Lets callers import from any depth.false— disables the plugin's barrel and removes its files from the root barrel.
The { type: 'named' } default kicks in only when middlewareBarrel is part of config.middleware.
| Type: | { type: 'all' | 'named', nested?: boolean } | false |
|---|---|
| Required: | false |
| Default: | { type: 'named' } |
import { defineConfig } from 'kubb'
import { middlewareBarrel } from '@kubb/middleware-barrel'
export default defineConfig({
input: { path: './petStore.yaml' },
output: { path: './src/gen', barrel: { type: 'named' } },
middleware: [middlewareBarrel()],
plugins: [],
})Example
import { defineConfig } from 'kubb'
export default defineConfig({
input: { path: './petStore.yaml' },
output: { path: './src/gen' },
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: { path: './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'
export default defineConfig({
input: { path: './petStore.yaml' },
output: { path: './src/gen', barrel: { type: 'named', nested: true } },
plugins: [],
})// src/gen/index.ts (only exports directories)
export * from './api'
export * from './hooks'
// src/gen/api/index.ts (exports files and subdirs)
export * from './user'
export * from './post'
export * from './types'
// src/gen/api/types/index.ts (exports files)
export * from './User'import { defineConfig } from 'kubb'
import { pluginTs } from '@kubb/plugin-ts'
import { pluginZod } from '@kubb/plugin-zod'
// pluginZod opts out: no zod/index.ts is created,
// and zod files are excluded from the root index.ts.
export default defineConfig({
input: { path: './petStore.yaml' },
output: { path: './src/gen' },
plugins: [
pluginTs(),
pluginZod({ output: { barrel: false } }),
],
})import { defineConfig } from 'kubb'
// No root index.ts is generated, but each plugin
// still gets its own barrel using its inherited config.
export default defineConfig({
input: { path: './petStore.yaml' },
output: { path: './src/gen', barrel: false },
plugins: [],
})