Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply 62 React and Next.js performance optimization rules from Vercel Engineering
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
rules/bundle-barrel-imports.md
1---2title: Avoid Barrel File Imports3impact: CRITICAL4impactDescription: 200-800ms import cost, slow builds5tags: bundle, imports, tree-shaking, barrel-files, performance6---78## Avoid Barrel File Imports910Import directly from source files instead of barrel files to avoid loading thousands of unused modules. **Barrel files** are entry points that re-export multiple modules (e.g., `index.js` that does `export * from './module'`).1112Popular icon and component libraries can have **up to 10,000 re-exports** in their entry file. For many React packages, **it takes 200-800ms just to import them**, affecting both development speed and production cold starts.1314**Why tree-shaking doesn't help:** When a library is marked as external (not bundled), the bundler can't optimize it. If you bundle it to enable tree-shaking, builds become substantially slower analyzing the entire module graph.1516**Incorrect (imports entire library):**1718```tsx19import { Check, X, Menu } from 'lucide-react'20// Loads 1,583 modules, takes ~2.8s extra in dev21// Runtime cost: 200-800ms on every cold start2223import { Button, TextField } from '@mui/material'24// Loads 2,225 modules, takes ~4.2s extra in dev25```2627**Correct - Next.js 13.5+ (recommended):**2829```js30// next.config.js - automatically optimizes barrel imports at build time31module.exports = {32experimental: {33optimizePackageImports: ['lucide-react', '@mui/material']34}35}36```3738```tsx39// Keep the standard imports - Next.js transforms them to direct imports40import { Check, X, Menu } from 'lucide-react'41// Full TypeScript support, no manual path wrangling42```4344This is the recommended approach because it preserves TypeScript type safety and editor autocompletion while still eliminating the barrel import cost.4546**Correct - Direct imports (non-Next.js projects):**4748```tsx49import Button from '@mui/material/Button'50import TextField from '@mui/material/TextField'51// Loads only what you use52```5354> **TypeScript warning:** Some libraries (notably `lucide-react`) don't ship `.d.ts` files for their deep import paths. Importing from `lucide-react/dist/esm/icons/check` resolves to an implicit `any` type, causing errors under `strict` or `noImplicitAny`. Prefer `optimizePackageImports` when available, or verify the library exports types for its subpaths before using direct imports.5556These optimizations provide 15-70% faster dev boot, 28% faster builds, 40% faster cold starts, and significantly faster HMR.5758Libraries commonly affected: `lucide-react`, `@mui/material`, `@mui/icons-material`, `@tabler/icons-react`, `react-icons`, `@headlessui/react`, `@radix-ui/react-*`, `lodash`, `ramda`, `date-fns`, `rxjs`, `react-use`.5960Reference: [How we optimized package imports in Next.js](https://vercel.com/blog/how-we-optimized-package-imports-in-next-js)61