Design Patterns · Middle · Question 27 of 75
How can barrel files (index.ts re-exporting everything) do harm?
- ✓Correct answer: They can slow down builds and tests and cause circular imports
- 2Barrel files are forbidden in ES modules and don't work in the browser
- 3They make all exported functions private
Explanation
Importing one function from a large barrel can force the bundler and test runner to parse the whole module graph, and mutual re-exports easily form cycles. Fixes: narrow public APIs, direct imports within a module, optimizePackageImports in Next.js.