❌ Don't use barrel imports in React
@_georgemoller118 B takipçi
161.643 görüntüleme
48 Yorum
Yorumlar X (Twitter) üzerindeki orijinal gönderiden alınmıştır

@MrFlashAccount
Barrel exports do not add unused code! Modern bundlers track the deps and eliminate them at the minification phase
44 beğeni

@_georgemoller
assuming you use a modern bundler with tree-shaking enabled tho.
1 beğeni

@_georgemoller
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.
1 beğeni

@MrFlashAccount
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
7 beğeni

@MrFlashAccount
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.
12 beğeni

@TheAlexLichter
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.
6 beğeni

@MrFlashAccount
@_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).
2 beğeni

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

@_georgemoller
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 ↓
14 beğeni

@bygregorr
But isn't the tradeoff sometimes worth it for simpler imports and easier maintenance, especially in larger projects.
4 beğeni

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

@theyole_
Yup. Explicit imports keep bundles lean and clean! Great video, short and to the point!
3 beğeni

@_georgemoller
thanks! glad you liked it
1 beğeni

@KM_Marques
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.
3 beğeni

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

@ashoKumar89
I’ve seen barrels bite hardest in component libraries and design systems, less so in well-configured app builds.
3 beğeni

@_georgemoller
that's true in component libraries they are usually a problem
2 beğeni

@avrldotdev
Thanks for this video, I have some code like this, will refactor immediately
2 beğeni

@_georgemoller
@techdevdaily glad you liked it!
1 beğeni

@BnayaZilber
And it's additional redundant code
2 beğeni
























