Dan Neciu’s Post

🛢️ Barrel imports look clean, but unfortunately That index.ts might be your slowest file 👇 A barrel import is when you create an index.ts that re-exports everything from a folder, so consumers can import from a single path instead of digging into individual files. It's not free though. 1️⃣ One import loads everything — You imported Button but the bundler parses the entire barrel. Modal, Input, and all their deps tag along. Tree-shaking helps sometimes, not always. 2️⃣ Dev server startup tanks — Webpack and Turbopack resolve the full module graph behind every barrel before serving anything. Nest barrels inside barrels and you're resolving hundreds of modules for one component. Next.js added optimizePackageImports specifically for this. 3️⃣ Circular deps get invisible — A imports from the barrel, barrel imports B, B imports A. The cycle is hard to trace because the barrel hid every direct relationship between modules. 4️⃣ Module boundaries disappear — Everything behind index.ts means teams stop thinking about public vs private. Internal details leak and every component ends up depending on everything else. One barrel at the public API boundary of a package is fine. A tiny utils/index.ts with three pure functions won't hurt either. The pain starts with barrels inside features and barrels importing other barrels. Have you ripped out barrel imports from a project, or are you still using them everywhere? #JavaScript #TypeScript #WebDev #React #Frontend

  • text
  • No alternative text description for this image

Seriously? I thought tree shaking would handle this kind of thing.

In a modern setup with tree-shaking, named imports from a barrel file are amazing and do not bloat your bundle

What about treeshaking?

Like
Reply

I'm not sure what your situation was, but simple re-exports should not affect the content of your assembly. Webpack and Rspack operate declaration files, not import entry files. I haven't tested Vite and other bundlers, but I assume this behaviour is common amongst other bundlers as well

Yep, faced this issue while optimizing my bundle size

In React Native you would see so many warnings about circular deps if you're gonna use barrel imports so you immediately realise it's better without them. Web just has a privilege to ignore potential performance issues so nobody cares

Like
Reply

If you have used almost any libraries, they do this for your convenience. Like do you import useMemo from "react/useMemo.js" or just from "react"?

Like
Reply

I’ve already faced this problem. It also slowed down my unit tests

Like
Reply
See more comments

To view or add a comment, sign in

Explore content categories