Menu

Barrel Files (`index.ts`): Why One Import Can Slow Your Build
πŸ“°

Barrel Files (`index.ts`): Why One Import Can Slow Your Build

DEV Community: typescriptΒ·Mohsen FallahnejadΒ·2 days ago
#8KFQYRZO
#devtotypescript#devto#dev#import#barrel
Reading 0:00
15s threshold

You add a tidy folder: ten components, one index.ts that re-exports them all, and a single import line in the app:

import { Button } from "@/components/ui" ;

Enter fullscreen mode

Exit fullscreen mode

You only wanted Button . The dev build still feels heavier than it should.

The mental model: the whole shelf

A barrel file is an index.ts that re-exports a folder so you can import from one path. It's convenient. It's also like asking the library for one book and having the librarian pull every book on that shelf into the room before they hand you the right one.

The bundler's job is to follow every export * from and export { X } from while it figures out what your entry file actually needs. Tree shaking can still drop unused code from the final bundle.…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.