r/reactjs • u/YiyiChu • Jun 30 '26
Discussion Death to barrel files!!! (They wrecked our code splitting)
https://medal.tv/blog/posts/w-key-in-frontend-synergizing-technology-and-productFrontend eng's teardown recap...where removing a component barrel file alone cut 2.6MB, then adopted a NO BARREL FILE afterward. Also flagged legacy Grommet/Styled Components with a custom ESLint rule pointing devs to the Tailwind/Shadcn replacements.
Anyone else fully swearing off barrel files?
64
Upvotes
48
u/Cordes96 Jun 30 '26 edited Jul 01 '26
Barrel files have their place but due to people not understanding bundling they become bloated.
I have kept my apps lightweight and easy with entry points that are just public contract barrel files.
If you properly use lazy loading and tree shaking it won’t be a problem. Bad architecture leads to huge bundle sizes.
Edit: Those who are swearing off barrel files, if you don’t want to use that tool please for your own sanity look into either using the path alias feature with tsconfig or vite and when working in packages look into using the “#/..“ functionality of TS v6 and Node 24.
I started to look at the implementation and it seems to solve a lot of my current uses for my barrel files in my packages.
9
u/cherylswoopz Jun 30 '26
Yes, we recently went through and removed all of our barrel imports. Definitely helped to directly import only what we need
25
u/Sunwukung Jun 30 '26
Yep. It's a real shame, makes imports much noisier but circular dependencies be damned
5
u/yksvaan Jun 30 '26
Codebase would have to be complete garbage at that point regardless of barrel files.
11
u/bzbub2 Jun 30 '26
this is a very haphazard blog post. next time you want to post on something, dig a little deeper...i dont even believe it is true that barrel files would affect code splitting, and need evidence to see that
3
u/yikes_42069 Jun 30 '26
Basically this is clickbait if you're at all interested in the title. The topic of barrel files is only given about 1 line of explanation in the article: glob imports. I expected to see an example of how this fits into the codebase cleanly and honestly some actual analysis of the decision. Glad it worked out for your team though.
4
u/Insensibilities Jun 30 '26 edited Jun 30 '26
I agree Barrel files are just useless code unless it is the root entry point for your libraries. They create unnecessary indirection (which means it is slower, and you have more files overall) and confusion. Just link to the damn file directly.
2
u/juicybot Jun 30 '26
run into nasty server/client collisions with barrel exports too
1
u/TheRealSeeThruHead Jun 30 '26 edited Oct 14 '26
Do est eiusmod reprehenderit tempor commodo culpa labore incididunt sed magna exercitation quis deserunt ex velit laborum. Labore aute id dolor incididunt sed laborum laboris incididunt.
1
u/pierrechaquejour Aug 06 '26
I’m floored that we’re “moving away” from this pattern already. I swear the dev community switches up their “best practices” every year or two just to keep us all on an endless treadmill of refactoring code that works perfectly fine but isn’t the “modern” way of doing the same old things.
0
u/evangelism2 Jun 30 '26
Nope I absolutely love Barrel Files because when implemented correctly, they don't require the same level of tree shaking and they create very organized and easy-to-locate APIs that stop spaghetti code from forming