@libria/ts-barrels
v1.1.0
Published
Simple TypeScript barrel generator for typescript projects
Maintainers
Readme
@libria/ts-barrels
A simple, fast TypeScript barrel file generator that recursively creates index.ts exports for your project.
What are Barrel Files?
Barrel files (typically index.ts) re-export modules from a directory, enabling cleaner imports:
// Without barrels
import { UserService } from './services/user/UserService';
import { AuthService } from './services/auth/AuthService';
// With barrels
import { UserService, AuthService } from './services';Installation
npm install @libria/ts-barrelsCLI Usage
npx ts-barrels <root> [options]Options
| Option | Description | Default |
|--------|-------------|---------|
| <root> | Root folder to generate barrels in | (required) |
| --all | Generate barrels recursively from leaves to root | false |
| --force | Overwrite existing barrel files | false |
| --name <filename> | Custom barrel filename | index.ts |
Examples
Generate barrels for the entire src directory:
npx ts-barrels src --allGenerate with a custom filename:
npx ts-barrels src --all --name barrel.tsForce regenerate all barrels:
npx ts-barrels src --all --forceIgnore a folder from barrel generation:
npx ts-barrels ignore src/internalProgrammatic Usage
import { generateBarrels } from '@libria/ts-barrels';
await generateBarrels('./src', {
all: true,
force: false,
filename: 'index.ts'
});Skip Comment
To prevent a barrel file from being overwritten, add a skip comment at the top:
// @libria/ts-barrels skip
export * from './custom-export';
export { specific } from './module';Files with the skip comment are always preserved, even when using --force.
Ignoring Folders
To completely exclude a folder (and all its children) from barrel generation, create a .lbbign marker file in that folder:
npx ts-barrels ignore src/internalThis creates a .lbbign file in src/internal. When generating barrels, any folder containing .lbbign will be skipped entirely:
- No barrel file will be created in that folder
- No child folders will be traversed
- The parent barrel will not export from the ignored folder
This is useful for internal modules, generated code, or any directory you want to exclude from your public API.
Generated Output
Given this structure:
src/
features/
auth/
login.ts
logout.ts
users/
create.ts
delete.ts
utils/
helpers.tsRunning npx ts-barrels src --all generates:
src/
index.ts -> export * from './features'; export * from './utils';
features/
index.ts -> export * from './auth'; export * from './users';
auth/
index.ts -> export * from './login'; export * from './logout';
users/
index.ts -> export * from './create'; export * from './delete';
utils/
index.ts -> export * from './helpers';License
MIT
