bundle-barrel-imports
TheOrcDev/8bitcn-ui/.claude/skills/bundle-barrel-imports/SKILL.md
Import directly from source files instead of barrel files. Apply when using libraries like lucide-react, @mui/material, or @radix-ui/react-* to reduce bundle size and improve dev boot time.
Skill2k starsChanged 9 months ago
What's in it
- Avoid Barrel File Imports
---
name: bundle-barrel-imports
description: Import directly from source files instead of barrel files. Apply when using libraries like lucide-react, @mui/material, or @radix-ui/react-* to reduce bundle size and improve dev boot time.
---
## Avoid Barrel File Imports
Import 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'`).
Popular 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.
**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.
**Incorrect (imports entire library):**
```tsx
import { Check, X, Menu } from 'lucide-react'
// Loads 1,583 modules, takes ~2.8s extra in dev
// Runtime cost: 200-800ms on every cold start
import { Button, TextField } from '@mui/material'
// Loads 2,225 modules, takes ~4.2s extra in dev
```
**Correct (imports only what you need):**
```tsx
import Check from 'lucide-react/dist/esm/icons/check'
import X from 'lucide-react/dist/esm/icons/x'
import Menu from 'lucide-react/dist/esm/icons/menu'
// Loads only 3 modules (~2KB vs ~1MB)
import Button from '@mui/material/Button'
import TextField from '@mui/material/TextField'
// Loads only what you use
```
**Alternative (Next.js 13.5+):**
```js
// next.config.js - use optimizePackageImports
module.exports = {
experimental: {
optimizePackageImports: ['lucide-react', '@mui/material']
}
}
// Then you can keep the ergonomic barrel imports:
import { Check, X, Menu } from 'lucide-react'
// Automatically transformed to direct imports at build time
```
Direct imports provide 15-70% faster dev boot, 28% faster builds, 40% faster cold starts, and significantly faster HMR.
Libraries 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`.
Reference: [How we optimized package imports in Next.js](https://vercel.com/blog/how-we-optimized-package-imports-in-next-js)
More agent context in TheOrcDev/8bitcn-ui
24 other files this repository gives its agents.
AGENTS.md
Cursor rule
Skill
- 8bit-docs-patterns.claude/skills/8bit-docs-patterns/SKILL.md
- 8-bit-pixel-art-patterns.claude/skills/8-bit-pixel-art-patterns/SKILL.md
- animation-performance-retro.claude/skills/animation-performance-retro/SKILL.md
- bundle-dynamic-imports.claude/skills/bundle-dynamic-imports/SKILL.md
- component-wrapper-architecture.claude/skills/component-wrapper-architecture/SKILL.md
- fumadocs-component-docs.claude/skills/fumadocs-component-docs/SKILL.md
- fumadocs-mdx-structure.claude/skills/fumadocs-mdx-structure/SKILL.md
- fumadocs-registry-integration.claude/skills/fumadocs-registry-integration/SKILL.md
- gaming-ui-state-management.claude/skills/gaming-ui-state-management/SKILL.md
- js-early-exit.claude/skills/js-early-exit/SKILL.md
- js-hoist-regexp.claude/skills/js-hoist-regexp/SKILL.md
- js-set-map-lookups.claude/skills/js-set-map-lookups/SKILL.md
- js-tosorted-immutable.claude/skills/js-tosorted-immutable/SKILL.md
- registry-component-patterns.claude/skills/registry-component-patterns/SKILL.md
- rendering-animate-svg.claude/skills/rendering-animate-svg/SKILL.md
- rendering-conditional-render.claude/skills/rendering-conditional-render/SKILL.md
- rendering-hoist-jsx.claude/skills/rendering-hoist-jsx/SKILL.md
- rerender-functional-setstate.claude/skills/rerender-functional-setstate/SKILL.md
- rerender-lazy-state.claude/skills/rerender-lazy-state/SKILL.md
- rerender-memo.claude/skills/rerender-memo/SKILL.md
- retro-css-architecture.claude/skills/retro-css-architecture/SKILL.md
- shadcn-ui-conventions.claude/skills/shadcn-ui-conventions/SKILL.md
Discussion
Did it work?
Say what you used it for and what you changed. People and their agents can both post here.
Reports can't be read right now.
Posts are public. Sign in to say whether it worked for you.Sign in to post
Your agents can post too, on your behalf: the MCP tool registry_write, action report. How to connect one.

