feature-slicing
npx skills add https://github.com/ccheney/robust-skills --skill feature-slicing
Agent 安装分布
Skill 文档
Feature-Sliced Design Architecture
Frontend architecture methodology with strict layer hierarchy and import rules for scalable, maintainable applications. FSD organizes code by business domain rather than technical role.
Official Docs: feature-sliced.design | GitHub: feature-sliced
THE IMPORT RULE (Critical)
Modules can ONLY import from layers strictly below them. Never sideways or upward.
app â pages â widgets â features â entities â shared
â â â â â â
â â â â â (external only)
| Violation | Example | Fix |
|---|---|---|
| Cross-slice (same layer) | features/auth â features/user |
Extract to entities/ or shared/ |
| Upward import | entities/user â features/auth |
Move shared code down |
| Shared importing up | shared/ â entities/ |
Shared has NO internal deps |
Exception: app/ and shared/ have no slices, so internal cross-imports are allowed within them.
Layer Hierarchy
| Layer | Purpose | Has Slices | Required |
|---|---|---|---|
app/ |
Initialization, routing, providers, global styles | No | Yes |
pages/ |
Route-based screens (one slice per route) | Yes | Yes |
widgets/ |
Complex reusable UI blocks (header, sidebar) | Yes | No |
features/ |
User interactions with business value (login, checkout) | Yes | No |
entities/ |
Business domain models (user, product, order) | Yes | No |
shared/ |
Project-agnostic infrastructure (UI kit, API client, utils) | No | Yes |
Minimal setup: app/, pages/, shared/ â add other layers as complexity grows.
Quick Decision Trees
“Where does this code go?”
Code Placement:
ââ App-wide config, providers, routing â app/
ââ Full page / route component â pages/
ââ Complex reusable UI block â widgets/
ââ User action with business value â features/
ââ Business domain object (data model) â entities/
ââ Reusable, domain-agnostic code â shared/
“Feature or Entity?”
| Entity (noun) | Feature (verb) |
|---|---|
user â user data model |
auth â login/logout actions |
product â product info |
add-to-cart â adding to cart |
comment â comment data |
write-comment â creating comments |
order â order record |
checkout â completing purchase |
Rule: Entities represent THINGS with identity. Features represent ACTIONS with side effects.
“Which segment?”
Segments (within a slice):
ââ ui/ â React components, styles
ââ api/ â Backend calls, data fetching, DTOs
ââ model/ â Types, schemas, stores, business logic
ââ lib/ â Slice-specific utilities
ââ config/ â Feature flags, constants
Naming: Use purpose-driven names (api/, model/) not essence-based (hooks/, types/).
Directory Structure
src/
âââ app/ # App layer (no slices)
â âââ providers/ # React context, QueryClient, theme
â âââ routes/ # Router configuration
â âââ styles/ # Global CSS, theme tokens
âââ pages/ # Page slices
â âââ {page-name}/
â âââ ui/ # Page components
â âââ api/ # Loaders, server actions
â âââ model/ # Page-specific state
â âââ index.ts # Public API
âââ widgets/ # Widget slices
â âââ {widget-name}/
â âââ ui/ # Composed UI
â âââ index.ts
âââ features/ # Feature slices
â âââ {feature-name}/
â âââ ui/ # Feature UI
â âââ api/ # Feature API calls
â âââ model/ # State, schemas
â âââ index.ts
âââ entities/ # Entity slices
â âââ {entity-name}/
â âââ ui/ # Entity UI (Card, Avatar)
â âââ api/ # CRUD operations
â âââ model/ # Types, mappers, validation
â âââ index.ts
âââ shared/ # Shared layer (no slices)
âââ ui/ # Design system components
âââ api/ # API client, interceptors
âââ lib/ # Utilities (dates, validation)
âââ config/ # Environment, constants
âââ routes/ # Route path constants
âââ i18n/ # Translations
Public API Pattern
Every slice MUST expose a public API via index.ts. External code imports ONLY from this file.
// entities/user/index.ts
export { UserCard } from './ui/UserCard';
export { UserAvatar } from './ui/UserAvatar';
export { getUser, updateUser } from './api/userApi';
export type { User, UserRole } from './model/types';
export { userSchema } from './model/schema';
// â
Correct
import { UserCard, type User } from '@/entities/user';
// â Wrong
import { UserCard } from '@/entities/user/ui/UserCard';
Avoid wildcard exports â they expose internals and harm tree-shaking:
// â
export * from './ui';
// â
export { UserCard } from './ui/UserCard';
Cross-Entity References (@x Notation)
When entities legitimately reference each other, use the @x notation:
entities/
âââ product/
â âââ @x/
â â âââ order.ts # API specifically for order entity
â âââ index.ts
âââ order/
âââ model/types.ts # Imports from product/@x/order
// entities/product/@x/order.ts
export type { ProductId } from '../model/types';
// entities/order/model/types.ts
import type { ProductId } from '@/entities/product/@x/order';
Guidelines: Keep cross-imports minimal. Consider merging entities if references are extensive.
Anti-Patterns
| Anti-Pattern | Problem | Fix |
|---|---|---|
| Cross-slice import | features/a â features/b |
Extract shared logic down |
| Generic segments | components/, hooks/ |
Use ui/, lib/, model/ |
| Wildcard exports | export * from './button' |
Explicit named exports |
| Business logic in shared | Domain logic in shared/lib |
Move to entities/ |
| Single-use widgets | Widget used by one page | Keep in page slice |
| Skipping public API | Import from internal paths | Always use index.ts |
| Making everything a feature | All interactions as features | Only reused actions |
TypeScript Configuration
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
}
}
Reference Documentation
| File | Purpose |
|---|---|
| references/LAYERS.md | Complete layer specifications, flowcharts |
| references/PUBLIC-API.md | Export patterns, @x notation, tree-shaking |
| references/IMPLEMENTATION.md | Code patterns: entities, features, React Query |
| references/NEXTJS.md | App Router integration, page re-exports |
| references/MIGRATION.md | Incremental migration strategy |
| references/CHEATSHEET.md | Quick reference, import matrix |
Resources
Official Sources
- Official Documentation: https://feature-sliced.design
- GitHub Organization: https://github.com/feature-sliced
- Official Examples: https://github.com/feature-sliced/examples
- Specification: https://feature-sliced.design/docs/reference
Community
- Awesome FSD: https://github.com/feature-sliced/awesome (curated articles, videos, tools)