DEV Community

Koen van Gilst
Koen van Gilst

Posted on • Originally published at blog.koenvangilst.nl on

Of Bundles and Barrels

Until recently I’ve been an unequivocal fan of so called barrel files. They work like this:

// src/components/index.js
export * from './Foo'
export * from './Bar'
export * from './FooBar'
Enter fullscreen mode Exit fullscreen mode

Which allows you to use components like this:

import { Foo, Bar, FooBar } from 'src/components'
Enter fullscreen mode Exit fullscreen mode

Short and sweet! For this reason I’m also a happy user of the VSCode plugin Auto-barrel which helps with creating/updating these barrel files.

I did recently discover, however, that this pattern can have unwanted side effects. Re-exporting all your imports in an index file can result in large bundle sizes.

I gave it a try on one of the projects I’m working on (using Next.js) and lo and behold: Removing the barrel in the /components/ui folder reduced the initial bundle size by more than 35kb. The culprit: In the ui folder was a RichTextEditor component of 35 kB. Since it was re-exported this was included on every page using a component from the ui folder.

So be careful what you put in your barrels, if you care about bundle size!

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay