Gezinmeyi atla
❌ Don't use barrel imports in React
0:00 / 0:35

❌ Don't use barrel imports in React

George Moller

@_georgemoller118 B takipçi

X profili
772 beğeniİndirOrijinal gönderi

161.643 görüntüleme

❌ 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 ↓

48 Yorum

Yorumlar X (Twitter) üzerindeki orijinal gönderiden alınmıştır

  • Sergei Garin 🇷🇺 profil fotoğrafı

    @MrFlashAccount

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

    44 beğeni

  • George Moller profil fotoğrafı

    @_georgemoller

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

    1 beğeni

  • George Moller profil fotoğrafı

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

  • Sergei Garin 🇷🇺 profil fotoğrafı

    @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

  • Sergei Garin 🇷🇺 profil fotoğrafı

    @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

  • Alexander Lichter profil fotoğrafı

    @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

  • Sergei Garin 🇷🇺 profil fotoğrafı

    @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

  • Sergei Garin 🇷🇺 profil fotoğrafı

    @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

  • George Moller profil fotoğrafı

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

  • Gregor profil fotoğrafı

    @bygregorr

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

    4 beğeni

  • George Moller profil fotoğrafı

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

  • Jovan profil fotoğrafı

    @theyole_

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

    3 beğeni

  • George Moller profil fotoğrafı

    @_georgemoller

    thanks! glad you liked it

    1 beğeni

  • Kevin Marques profil fotoğrafı

    @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

  • George Moller profil fotoğrafı

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

  • Ashok Sahoo profil fotoğrafı

    @ashoKumar89

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

    3 beğeni

  • George Moller profil fotoğrafı

    @_georgemoller

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

    2 beğeni

  • avrl ☘ profil fotoğrafı

    @avrldotdev

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

    2 beğeni

  • George Moller profil fotoğrafı

    @_georgemoller

    @techdevdaily glad you liked it!

    1 beğeni

  • Bnaya Zilberfarb profil fotoğrafı

    @BnayaZilber

    And it's additional redundant code

    2 beğeni