Published on

Barrel

Author

A barrel is a way to rollup exports from several modules into a single convenient module. The barrel itself is a module file that re-exports selected exports of other modules.

// demo/foo.ts

export class Foo {}

// demo/bar.ts

export class Bar {}

// demo/baz.ts

export class Baz {}

Without Barrel:

import { Foo } from '../demo/foo'

import { Bar } from '../demo/bar'

import { Baz } from '../demo/baz'

With Barrel:

// demo/index.ts
export * from './foo' // re-export all of its exports
export * from './bar' // re-export all of its exports
export * from './baz' // re-export all of its exports
import { Foo, Bar, Baz } from '../demo' // demo/index.ts is implied