DEV Community

Discussion on: Barrel files and why you should STOP using them now

Collapse
 
kasper573 profile image
kasper573

This is not necessarily true. You can have side effects free packages, which allow bundlers to properly tree shake packages that use barrel files: webpack.js.org/guides/tree-shaking...

Collapse
 
tassiofront profile image
Tássio

Hey, man! Thanks for your comment!

have you validated it? To be honest, I tried it a long time ago and it didn't work as expected. It might be my mistake... Anyway, I would not use Barrel Files due to its test impact. I don't believe Barrel files have so much value for this risk.

Collapse
 
kasper573 profile image
kasper573 • Edited

Yes, I use side effects free packages every day. Third party and my own packages. Three.js and ramda on npm are both side effects free to name a few popular ones.

For tests I definitely agree you shouldn’t be importing via barrel files. A unit test should depend on only its unit, regardless of tree shaking capabilities. I almost exclusively only use barrel files as a definition for what my packages export. Everything else I split up into separate modules so that my codebase can have a proper dependency tree.

I think your article demonstrates important concerns. If one wants a no-build architecture, then being aware of how modules work is very useful, and avoiding barrel files becomes more important. But if one has a build system already then you do have the option to tree shake. I don’t think it’s wise to pass judgement on a whole and say it’s either right or wrong. There are upsides and downsides, and everything depends on context of the team and project.

Thread Thread
 
tassiofront profile image
Tássio • Edited

Nice!! Thanks for the amazing explanation, man!

I will update my article to explain the context. Btw, do you know a nice article about no-build architecture? It is a new definition to me.

Another thing: if you want to create a topic regarding build system (on the barrel files context), I would love to put it in my article and give you the whole credit or if you have an article already, I can add the link here.

Please, find me on LinkedIn to talk if want to. Have a nice day!
linkedin.com/in/tassio-front-end/