Загрузка видео...

Не удалось загрузить видео

На главную

❌ 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 просмотров • 8 месяцев назад •via X (Twitter)

Комментарии: 48

Фото профиля Sergei Garin 🇷🇺
Sergei Garin 🇷🇺8 месяцев назад

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

Фото профиля George Moller
George Moller8 месяцев назад

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

Фото профиля George Moller
George Moller8 месяцев назад

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 🇷🇺
Sergei Garin 🇷🇺8 месяцев назад

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 🇷🇺
Sergei Garin 🇷🇺8 месяцев назад

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
Alexander Lichter8 месяцев назад

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 🇷🇺
Sergei Garin 🇷🇺8 месяцев назад

@_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 🇷🇺
Sergei Garin 🇷🇺8 месяцев назад

@_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
George Moller8 месяцев назад

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
Gregor8 месяцев назад

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

Фото профиля George Moller
George Moller8 месяцев назад

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

Фото профиля Jovan
Jovan8 месяцев назад

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

Фото профиля George Moller
George Moller8 месяцев назад

thanks! glad you liked it

Фото профиля Kevin Marques
Kevin Marques8 месяцев назад

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
George Moller8 месяцев назад

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
Ashok Sahoo8 месяцев назад

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

Фото профиля George Moller
George Moller8 месяцев назад

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

Фото профиля avrl ☘
avrl ☘8 месяцев назад

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

Фото профиля George Moller
George Moller8 месяцев назад

@techdevdaily glad you liked it!

Фото профиля Bnaya Zilberfarb
Bnaya Zilberfarb8 месяцев назад

And it's additional redundant code

Фото профиля eren
eren8 месяцев назад

next.js + turbopack handle this well in most cases

Фото профиля OrcanArtz
OrcanArtz8 месяцев назад

First time hearing of barrel imports💀

Фото профиля SweatSpots 🏃🏼‍♂️
SweatSpots 🏃🏼‍♂️8 месяцев назад

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

Фото профиля George Moller
George Moller8 месяцев назад

@ShadowEmployee_ Glad you found it useful!

Фото профиля Enoch Klu
Enoch Klu8 месяцев назад

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 ▴
Maga - turbotable.ai ▴8 месяцев назад

@grok is this true for @nextjs 16?

Фото профиля Account suspended
Account suspended8 месяцев назад

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

Фото профиля Michael
Michael8 месяцев назад

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
George Moller8 месяцев назад

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

Фото профиля Michael
Michael8 месяцев назад

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

Фото профиля Oxholm
Oxholm8 месяцев назад

Is this also the case in latest versions of vite?

Фото профиля Nikita Gritsai
Nikita Gritsai8 месяцев назад

Tree-shaking not present in 2026 is a crime.

Фото профиля Sam
Sam8 месяцев назад

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

Фото профиля Cristhofer Quispe
Cristhofer Quispe8 месяцев назад

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

Фото профиля Rahul Rana
Rahul Rana8 месяцев назад

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

Фото профиля gerikov
gerikov8 месяцев назад

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

Фото профиля Yoav Abrahami
Yoav Abrahami8 месяцев назад

Tree shaking? Vite? Am I missing something?

Фото профиля Roman
Roman8 месяцев назад

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

Фото профиля Sparkle
Sparkle8 месяцев назад

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

Фото профиля Tyler Coffman
Tyler Coffman8 месяцев назад

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
Aniket Khote8 месяцев назад

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

Фото профиля Igor Santos
Igor Santos8 месяцев назад

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
Stephen Rayner8 месяцев назад

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

Фото профиля Anthony Luzquiños
Anthony Luzquiños8 месяцев назад

@grok, for which React frameworks is this true?

Фото профиля Fede
Fede8 месяцев назад

@grok is this true in nextjs?

Фото профиля Sebastian Buzdugan
Sebastian Buzdugan8 месяцев назад

tree-shaking usually fixes that with esm setups

Фото профиля Liam
Liam8 месяцев назад

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

Фото профиля Mayk Brito
Mayk Brito8 месяцев назад

KISS cause sometimes (always) we tend to forget

Похожие видео