Video yükleniyor...

Video Yüklenemedi

Ana Sayfaya Dön

❌ 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 görüntüleme • 8 ay önce •via X (Twitter)

48 Yorum

Sergei Garin 🇷🇺 profil fotoğrafı
Sergei Garin 🇷🇺8 ay önce

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

George Moller profil fotoğrafı
George Moller8 ay önce

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

George Moller profil fotoğrafı
George Moller8 ay önce

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.

Sergei Garin 🇷🇺 profil fotoğrafı
Sergei Garin 🇷🇺8 ay önce

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

Sergei Garin 🇷🇺 profil fotoğrafı
Sergei Garin 🇷🇺8 ay önce

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.

Alexander Lichter profil fotoğrafı
Alexander Lichter8 ay önce

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.

Sergei Garin 🇷🇺 profil fotoğrafı
Sergei Garin 🇷🇺8 ay önce

@_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).

Sergei Garin 🇷🇺 profil fotoğrafı
Sergei Garin 🇷🇺8 ay önce

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

George Moller profil fotoğrafı
George Moller8 ay önce

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 ↓

Gregor profil fotoğrafı
Gregor8 ay önce

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

George Moller profil fotoğrafı
George Moller8 ay önce

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

Jovan profil fotoğrafı
Jovan8 ay önce

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

George Moller profil fotoğrafı
George Moller8 ay önce

thanks! glad you liked it

Kevin Marques profil fotoğrafı
Kevin Marques8 ay önce

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.

George Moller profil fotoğrafı
George Moller8 ay önce

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

Ashok Sahoo profil fotoğrafı
Ashok Sahoo8 ay önce

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

George Moller profil fotoğrafı
George Moller8 ay önce

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

avrl ☘ profil fotoğrafı
avrl ☘8 ay önce

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

George Moller profil fotoğrafı
George Moller8 ay önce

@techdevdaily glad you liked it!

Bnaya Zilberfarb profil fotoğrafı
Bnaya Zilberfarb8 ay önce

And it's additional redundant code

eren profil fotoğrafı
eren8 ay önce

next.js + turbopack handle this well in most cases

OrcanArtz profil fotoğrafı
OrcanArtz8 ay önce

First time hearing of barrel imports💀

SweatSpots 🏃🏼‍♂️ profil fotoğrafı
SweatSpots 🏃🏼‍♂️8 ay önce

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

George Moller profil fotoğrafı
George Moller8 ay önce

@ShadowEmployee_ Glad you found it useful!

Enoch Klu profil fotoğrafı
Enoch Klu8 ay önce

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.

Maga - turbotable.ai ▴ profil fotoğrafı
Maga - turbotable.ai ▴8 ay önce

@grok is this true for @nextjs 16?

Account suspended profil fotoğrafı
Account suspended8 ay önce

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

Michael profil fotoğrafı
Michael8 ay önce

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

George Moller profil fotoğrafı
George Moller8 ay önce

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

Michael profil fotoğrafı
Michael8 ay önce

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

Oxholm profil fotoğrafı
Oxholm8 ay önce

Is this also the case in latest versions of vite?

Nikita Gritsai profil fotoğrafı
Nikita Gritsai8 ay önce

Tree-shaking not present in 2026 is a crime.

Sam profil fotoğrafı
Sam8 ay önce

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

Cristhofer Quispe profil fotoğrafı
Cristhofer Quispe8 ay önce

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

Rahul Rana profil fotoğrafı
Rahul Rana8 ay önce

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

gerikov profil fotoğrafı
gerikov8 ay önce

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

Yoav Abrahami profil fotoğrafı
Yoav Abrahami8 ay önce

Tree shaking? Vite? Am I missing something?

Roman profil fotoğrafı
Roman8 ay önce

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

Sparkle profil fotoğrafı
Sparkle8 ay önce

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

Tyler Coffman profil fotoğrafı
Tyler Coffman8 ay önce

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

Aniket Khote profil fotoğrafı
Aniket Khote8 ay önce

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

Igor Santos profil fotoğrafı
Igor Santos8 ay önce

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~

Stephen Rayner profil fotoğrafı
Stephen Rayner8 ay önce

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

Anthony Luzquiños profil fotoğrafı
Anthony Luzquiños8 ay önce

@grok, for which React frameworks is this true?

Fede profil fotoğrafı
Fede8 ay önce

@grok is this true in nextjs?

Sebastian Buzdugan profil fotoğrafı
Sebastian Buzdugan8 ay önce

tree-shaking usually fixes that with esm setups

Liam profil fotoğrafı
Liam8 ay önce

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

Mayk Brito profil fotoğrafı
Mayk Brito8 ay önce

KISS cause sometimes (always) we tend to forget

Benzer Videolar