How do 'export *' and re-export barrel files (index.ts aggregating a directory's exports) interact with tree-shaking and TypeScript's type checking performance?
Short answer
Barrel files that re-export everything from many submodules make imports more convenient, but can hurt both bundler tree-shaking (some bundlers struggle to eliminate unused re-exported code through a barrel) and TypeScript compiler performance (the compiler must resolve and…
Advertisement
AModel answer
Barrel files that re-export everything from many submodules make imports more convenient, but can hurt both bundler tree-shaking (some bundlers struggle to eliminate unused re-exported code through a barrel) and TypeScript compiler performance (the compiler must resolve and type-check the entire re-export graph even if a consumer only imports one specific named export).
// index.ts (barrel file)
export * from './Button';
export * from './Modal';
export * from './Tooltip';
// A consumer importing only { Button } may still pull in extra work/analysis for Modal and TooltipReal-world example
Debugging why a bundle unexpectedly includes an entire component library when only one component was actually imported.
Likely follow-ups
- What alternative import strategies help mitigate these barrel-file downsides in a large component library?
Related topics