DEV Community

Cover image for Barrel files and why you should STOP using them now
Tássio
Tássio

Posted on • Updated on

Barrel files and why you should STOP using them now

Small decisions can also have big impacts on applications. When developers start using barrel files they might seem like a harmless pattern, but they aren't.

[UPDATE Mar, 23th of 2024]:
BEFORE YOU KEEP READING: this article and its metrics were created thinking on final front-end applications. Those that final users navigate such as dev.to and my source project. So, if you have been using Barrel files in any other context, such as packages, it (Barrel files) might fit you as expected. Take a look at this amazing comment from
@kasper573.

What are barrel files?

Barrel files are an easy exportation pattern that allows developers to import different files from a unique path.

Let's see an example:

// ./barrelFiles/used.ts
export const HEY = 'hey used';

// ./barrelFiles/notUsed.ts
export const HEY_NOT_USED = 'hey NOT used';
Enter fullscreen mode Exit fullscreen mode
// ./barrelFiles/index.ts
export { HEY_NOT_USED } from './notUsed';
export { HEY } from './used';

/*
This is a barrel file, which will be used as a unique path to import any files into barrelFiles folder (such as ./used).
*/
Enter fullscreen mode Exit fullscreen mode

Using it:

// ./Component.tsx

import { HEY } from './barrelFiles'; // instead of use './barrelFiles/used'
Enter fullscreen mode Exit fullscreen mode

In the first touch, that looks amazing, doesn't it? They are in terms of Dev Experience, but it costs a lot!

What is the Barrel files cost into the app bundle?

One crucial question you must keep in mind about barrel files: What is imported when I use something from them? Does it import only what I use?

Let's see it with examples. Right, Let's add two more files to our barrel file and add some console.log to see how they work:

// now there are 4 files in barrelFiles folder

// ./barrelFiles/used.ts
export const HEY = 'hey used';
console.log('%c Im used xD', 'color: green;');

// ./barrelFiles/notUsed.ts
export const HEY_NOT_USED = 'hey NOT used';
console.log('%c WAIT! Im NOT used', 'color: red');

// ./barrelFiles/notUsed1.ts
export const HEY_NOT_USED_1 = 'hey NOT used 1';
console.log('%c WAIT! Im NOT used 1', 'color: red');

// ./barrelFiles/notUsed2.ts
export const HEY_NOT_USED_2 = 'hey NOT used 2';
console.log('%c WAIT! Im NOT used 2', 'color: red');
Enter fullscreen mode Exit fullscreen mode

And the barrel file:

// ./barrelFiles/index.ts
export { HEY_NOT_USED } from './notUsed';
export { HEY_NOT_USED_2 } from './notUsed2';
export { HEY_NOT_USED_1 } from './notUsed1';
export { HEY } from './used';
Enter fullscreen mode Exit fullscreen mode

Well, getting back to the questions, it's expected if I import HEY, I only import ./used file content. So let's do this by importing it into my personal project where you find more articles like this:

// ./Component.tsx

import { HEY } from './barrelFiles'; // I wanna use HEY, only
Enter fullscreen mode Exit fullscreen mode

Using barrel files impact on app

Do you see? Even though only HEY is used, it shows all console.log which means all files are imported.

Now, not importing from the barrel file.

// ./Component.tsx

import { HEY } from './barrelFiles/used'; // I wanna use HEY, only
Enter fullscreen mode Exit fullscreen mode

Not using barrel files impact on app

Importing directly one console.log is shown.

You might be wondering: why would there be files not used in my project?

That's a good question. Well, some points about it:

  1. In this example, there are few files, but how about a huge project using dozen barrel files? Likely there would be not used files. If you work on a monorepo, it might have shared folders used for many apps. Creating patterns that avoid team-mattes (and you) doing things wrong is the best thing you can do as a developer;
  2. Even though tools such as Vite and Webpack optmizining bundle and avoid to use re-import chunks, the better way to make sure you opmitizing as much as possible is understanding things work on the application. To understand it, see the next screenshots.

Let's see the bundle difference now (please, as it's a teaching example, the difference is short. Scale it up to understand the impact).

Using the barrel file (189,83kb):
barrel file bundle

No barrel file (189,67kb):
no barrel file

As you can see, there is a final bundle size difference. By not using barrel file, it's smaller.

It's also applicable to third-party packages. Let's use pako as an example - a high-speed zlib port to javascript. For this project, we just wanna use inflate function from pako.

using pako barrel file size: 43.9k
using pako barrel file

using pako with no barrel file: 21.5k
using pako with no barrel file

Incredible, isn't it?

Circular dependency is another problem you might have with barrel files.

Enjoying it? If so, don't forget to give a ❤️. Then, I'll continue creating more content like this

What is the Barrel files cost into the unit tests?

The barrel file on unit tests is even worse, once unit tests engines such as Vitest and Jest don't care about bundle optimization as Vite and Webpack. Let's see by creating two helpers that only use HEY.

// ./helper.ts
import { HEY } from './barrelFiles';
export const helper = () => {
  console.log(HEY);
};

// ./helper2.ts
import { HEY } from './barrelFiles';
export const helper2 = () => {
  console.log(HEY);
};
Enter fullscreen mode Exit fullscreen mode

And now import them into the test files

// ./helper.epec.ts
import { describe, it } from 'vitest';
import { helper } from './helper';

describe('helper', () => {
  it('Should only use HEY', () => {
    helper();
  });
});

// ./helper2.epec.ts
import { describe, it } from 'vitest';
import { helper2 } from './helper2';

describe('helper2', () => {
  it('Should only use HEY', () => {
    helper2();
  });
});
Enter fullscreen mode Exit fullscreen mode

using barrel file on tests time: 2.61s
Unit test with barrel file

As you can see, there is a bunch of not-used console.log, and that gonna happen for EACH test file on the project that uses the barrel file or even use a file that uses the barrel file. Can you imagine the performance impact of it? Well... terrible.

Now, removing the barrel files

not using barrel file on tests time: 1.32s
Unit test without barrel file

Conclusion

[UPDATE Mar, 23th of 2024]:
In this article, you saw a bit more about the barrel file pattern and its impact on final front-end applications.

As I have said before: creating patterns that avoid team-mattes (and you) doing things wrong is the best thing you can do as a developer. That is valid to barrel files once not using them:

  1. Reducing the final bundle and avoiding importing stuff are not used (same applicable to local files and third-party packages);
  2. Improve the unit test performance;

Expensive.

See my other articles

Top comments (6)

Collapse
 
hernanarica profile image
Hernán Arica • Edited

Hace unos meses cuando descubrí este patrón, me pareció genial la forma en la que da una mejora claridad al leer los imports, pero usando react router 6 y un sistema de autenticación, me di cuenta de estos problemas que nombras, ya que la app no funcionaba como yo esperaba, debito a esto.

Collapse
 
tassiofront profile image
Tássio

Gracias por compartir, Hernan! Feliz por clarar 🚀

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/