Next.js -- barrel imports (index.ts re-exports)
Unanswered
Norwegian Forest Cat posted this in #help-forum
Norwegian Forest CatOP
We are working on a large Next.js application and doing a performance audit around barrel imports (index.ts re-exports).
From research, barrel files can negatively affect tree-shaking, bundle size, and dev performance, especially in large codebases. However, there are mixed opinions about their usage in smaller modules.
In our project:
* We have many barrel files, especially in backend-related folders at the root level
* These folders re-export a lot of modules
* We are not currently using
* Even if we enable it, it seems it only applies to external packages, not local barrel files
Questions:
1. In a large Next.js app, is it recommended to avoid barrel files completely, or just limit them to specific cases?
2. Does Next.js (Webpack/Turbopack) handle tree-shaking of local barrel files well enough, or should we prefer direct imports for performance-critical paths?
3. Are there any plans or existing tools in Next.js to optimize local barrel imports similar to
4. What is the recommended pattern for balancing DX vs performance in this case?
Would appreciate guidance, especially from real-world experience or Vercel team insights.
From research, barrel files can negatively affect tree-shaking, bundle size, and dev performance, especially in large codebases. However, there are mixed opinions about their usage in smaller modules.
In our project:
* We have many barrel files, especially in backend-related folders at the root level
* These folders re-export a lot of modules
* We are not currently using
optimizePackageImports* Even if we enable it, it seems it only applies to external packages, not local barrel files
Questions:
1. In a large Next.js app, is it recommended to avoid barrel files completely, or just limit them to specific cases?
2. Does Next.js (Webpack/Turbopack) handle tree-shaking of local barrel files well enough, or should we prefer direct imports for performance-critical paths?
3. Are there any plans or existing tools in Next.js to optimize local barrel imports similar to
optimizePackageImports?4. What is the recommended pattern for balancing DX vs performance in this case?
Would appreciate guidance, especially from real-world experience or Vercel team insights.