Cory House
Cory House

@housecor

2 Tweets 14 reads Dec 02, 2023
PSA πŸ“’: Avoid Barrels
(A barrel is a file that exports code from other files)
Barrel benefits:
βœ… Shorter import paths
βœ… Support importing many files via one import
βœ… "Hide" modules - Only export some modules for the "public" interface
But, I avoid barrels. Here's why:
🚫 Bloats the bundle. Inhibits tree shaking.
🚫 Increases memory usage.
🚫 Slows tooling (builds, tests, linting). Barrels create more modules to parse.
🚫 Slows code navigation ("Find all references" finds the barrel instead of the actual source).
🚫 Barrels shorten import paths and group related imports. Sounds nice, but it's an outdated benefit because I don't write imports anymore - my editor reliably auto-imports.
People often put each component in a folder to group the files, and use a barrel to shorten the import.
Instead, I suggest eliminating the needless parent folder.
The files are naturally grouped together alphabetically if they start with the component's name.
No barrel or folder needed.

Loading suggestions...