Video wird geladen...

Video konnte nicht geladen werden

Zur Startseite

❌ Don't use barrel imports in React ✅ Import components directly Barrel imports (index.js re-exports) import the entire module even if you need one component. This adds unused code to your bundle ↓

161,603 Aufrufe • vor 8 Monaten •via X (Twitter)

48 Kommentare

Profilbild von Sergei Garin 🇷🇺
Sergei Garin 🇷🇺vor 8 Monaten

Barrel exports do not add unused code! Modern bundlers track the deps and eliminate them at the minification phase

Profilbild von George Moller
George Mollervor 8 Monaten

assuming you use a modern bundler with tree-shaking enabled tho.

Profilbild von George Moller
George Mollervor 8 Monaten

also if a consumer of your lib/package marks it as external, tree-shaking doesn't help. Importing a Button, will result in importing all unused code. As a best practice I avoid barrel files at all costs.

Profilbild von Sergei Garin 🇷🇺
Sergei Garin 🇷🇺vor 8 Monaten

For libs you don’t need top level barrel files, specify “exports” in package json instead. In app it’s fine to use barrel exports

Profilbild von Sergei Garin 🇷🇺
Sergei Garin 🇷🇺vor 8 Monaten

All the hysteria about barrel exports coming from vite, because it doesn’t tree-shake in dev mode, resulting in slower cold-starts. IIRC, rolldown should solve it though.

Profilbild von Alexander Lichter
Alexander Lichtervor 8 Monaten

Not correct! Barrel files are a "problem" across the board. That's why companies like @vercel (specifically for @nextjs) wrote about it. Will it get better in Vite with Rolldown? Yes, mostly! There is potential for optimization that will be implemented (at this very moment actually), but there are still caveats depending on the use case.

Profilbild von Sergei Garin 🇷🇺
Sergei Garin 🇷🇺vor 8 Monaten

@_georgemoller @vercel @nextjs They mentioned they solved the dev mode cold start issue. Also, I think I was a bit misleading in blaming Vite specifically. The barrel files issue exists in every bundler that compiles only requested modules (don’t bundle in dev mode).

Profilbild von Sergei Garin 🇷🇺
Sergei Garin 🇷🇺vor 8 Monaten

@_georgemoller @vercel @nextjs Barrel files are not free, bundlers still need to tree shake them. But they are not bad as people say!

Profilbild von George Moller
George Mollervor 8 Monaten

I've been working professionally with React for more than 8 years and I compiled all my knowledge into 100+ infographics and 70+ video tutorials just like this one. Check them out ↓

Profilbild von Gregor
Gregorvor 8 Monaten

But isn't the tradeoff sometimes worth it for simpler imports and easier maintenance, especially in larger projects.

Profilbild von George Moller
George Mollervor 8 Monaten

I don't think ergonomics is a fear tradeoff in this case, given that it can potentially bloat your (and consumers) bundle.

Profilbild von Jovan
Jovanvor 8 Monaten

Yup. Explicit imports keep bundles lean and clean! Great video, short and to the point!

Profilbild von George Moller
George Mollervor 8 Monaten

thanks! glad you liked it

Profilbild von Kevin Marques
Kevin Marquesvor 8 Monaten

I worked in a company that used a lot of barrel imports. They wondered why the app took 1 minute to hot reload. I searched in the Vitejs documentation and barrel import was the problem.

Profilbild von George Moller
George Mollervor 8 Monaten

yeah as a best practice I just try to avoid them all together, negative impact surpass any ergonomic benefit that it might have.

Profilbild von Ashok Sahoo
Ashok Sahoovor 8 Monaten

I’ve seen barrels bite hardest in component libraries and design systems, less so in well-configured app builds.

Profilbild von George Moller
George Mollervor 8 Monaten

that's true in component libraries they are usually a problem

Profilbild von avrl ☘
avrl ☘vor 8 Monaten

Thanks for this video, I have some code like this, will refactor immediately

Profilbild von George Moller
George Mollervor 8 Monaten

@techdevdaily glad you liked it!

Profilbild von Bnaya Zilberfarb
Bnaya Zilberfarbvor 8 Monaten

And it's additional redundant code

Profilbild von eren
erenvor 8 Monaten

next.js + turbopack handle this well in most cases

Profilbild von OrcanArtz
OrcanArtzvor 8 Monaten

First time hearing of barrel imports💀

Profilbild von SweatSpots 🏃🏼‍♂️
SweatSpots 🏃🏼‍♂️vor 8 Monaten

This is a good tip. Doing it myself without asking any questions. I feel smarter now.

Profilbild von George Moller
George Mollervor 8 Monaten

@ShadowEmployee_ Glad you found it useful!

Profilbild von Enoch Klu
Enoch Kluvor 8 Monaten

This is a good idea, and I adopted it when building my UI library for React Native: Bleeech. However, with tree shaking, you shouldn't have to worry about this.

Profilbild von Maga - turbotable.ai ▴
Maga - turbotable.ai ▴vor 8 Monaten

@grok is this true for @nextjs 16?

Profilbild von Account suspended
Account suspendedvor 8 Monaten

This is only true if you use a shitty bundler. Import elision is a trivial problem in ESM, the devs are just lazy.

Profilbild von Michael
Michaelvor 8 Monaten

This post was for me. I just had an issue with circular dependency. We use barrel a lot in our nx project. Thanks

Profilbild von George Moller
George Mollervor 8 Monaten

Love it came at the right time Michael! and yes circular dependency is another downside and potential issue of using barrel files.

Profilbild von Michael
Michaelvor 8 Monaten

Hopefully this decreases the bundle size and speeds the app start up time

Profilbild von Oxholm
Oxholmvor 8 Monaten

Is this also the case in latest versions of vite?

Profilbild von Nikita Gritsai
Nikita Gritsaivor 8 Monaten

Tree-shaking not present in 2026 is a crime.

Profilbild von Sam
Samvor 8 Monaten

Just use es linters to stop your devs from exporting multiple components from a single file. Kill your problems at the root.

Profilbild von Cristhofer Quispe
Cristhofer Quispevor 8 Monaten

So using: Import * as React from “react” is not correct? I need to import useEffect, useState independently?

Profilbild von Rahul Rana
Rahul Ranavor 8 Monaten

Yeah we had a time where we faced this issues and solved by exporting the necessary things. Great win on bundle size 💯

Profilbild von gerikov
gerikovvor 8 Monaten

You should use the reexport pattern if the whole component cannot exist without each subcomponent

Profilbild von Yoav Abrahami
Yoav Abrahamivor 8 Monaten

Tree shaking? Vite? Am I missing something?

Profilbild von Roman
Romanvor 8 Monaten

Why are you so bothered about the imports? Doesn't your IDE manage them automatically so you never look at them?

Profilbild von Sparkle
Sparklevor 8 Monaten

@grok is this true for expo project and what settings can be done to tree-shake

Profilbild von Tyler Coffman
Tyler Coffmanvor 8 Monaten

Barrel imports also tend to cause import cycles and especially in jest tests those import cycles make you cry from imports being undefined and the debugging being hell

Profilbild von Aniket Khote
Aniket Khotevor 8 Monaten

Cool, so now I need to refactor all my imports in @bunvelhq 😅

Profilbild von Igor Santos
Igor Santosvor 8 Monaten

I get how barrel imports can be bad in libraries, but what's the point of that in components from your OWN project? If you don't use a component anywhere else, you delete it, simply. In the end, your whole project WILL be imported at some point, so what? ~All generalism is bad~

Profilbild von Stephen Rayner
Stephen Raynervor 8 Monaten

@grok what software is he using to make the videos?

Profilbild von Anthony Luzquiños
Anthony Luzquiñosvor 8 Monaten

@grok, for which React frameworks is this true?

Profilbild von Fede
Fedevor 8 Monaten

@grok is this true in nextjs?

Profilbild von Sebastian Buzdugan
Sebastian Buzduganvor 8 Monaten

tree-shaking usually fixes that with esm setups

Profilbild von Liam
Liamvor 8 Monaten

surely this should be a problem that bundlers solve and developers shouldn't have to worry about?

Profilbild von Mayk Brito
Mayk Britovor 8 Monaten

KISS cause sometimes (always) we tend to forget

Ähnliche Videos