feature-slicing by ccheney/robust-skills
npx skills add https://github.com/ccheney/robust-skills --skill feature-slicing一种前端架构方法论,具有严格的层级结构和导入规则,用于构建可扩展、可维护的应用程序。FSD 按业务领域而非技术角色来组织代码。
官方文档: feature-sliced.design | GitHub: feature-sliced
模块只能从严格位于其下方的层级导入。绝不能横向或向上导入。
app → pages → widgets → features → entities → shared
↓ ↓ ↓ ↓ ↓ ✓
✓ ✓ ✓ ✓ ✓ (仅外部依赖)
| 违规类型 | 示例 | 修复方法 |
|---|---|---|
| 跨切片导入(同层) | features/auth → features/user |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
提取到 entities/ 或 shared/ |
| 向上导入 | entities/user → features/auth | 将共享代码向下移动 |
| 共享层向上导入 | shared/ → entities/ | 共享层应无内部依赖 |
例外: app/ 和 shared/ 没有切片,因此允许它们内部进行交叉导入。
| 层级 | 目的 | 包含切片 | 必需 |
|---|---|---|---|
app/ | 初始化、路由、提供者、全局样式 | 否 | 是 |
pages/ | 基于路由的页面(每个路由一个切片) | 是 | 是 |
widgets/ | 复杂的可复用 UI 块(页眉、侧边栏) | 是 | 否 |
features/ | 具有业务价值的用户交互(登录、结账) | 是 | 否 |
entities/ | 业务领域模型(用户、产品、订单) | 是 | 否 |
shared/ | 与项目无关的基础设施(UI 套件、API 客户端、工具函数) | 否 | 是 |
最小化设置: app/、pages/、shared/ — 随着复杂度增加再添加其他层级。
代码放置:
├─ 应用范围的配置、提供者、路由 → app/
├─ 完整页面 / 路由组件 → pages/
├─ 复杂的可复用 UI 块 → widgets/
├─ 具有业务价值的用户操作 → features/
├─ 业务领域对象(数据模型) → entities/
└─ 可复用的、与领域无关的代码 → shared/
| 实体(名词) | 功能(动词) |
|---|---|
user — 用户数据模型 | auth — 登录/登出操作 |
product — 产品信息 | add-to-cart — 添加到购物车 |
comment — 评论数据 | write-comment — 创建评论 |
order — 订单记录 | checkout — 完成购买 |
规则: 实体代表具有标识的事物。功能代表具有副作用的操作。
片段(切片内部):
├─ ui/ → React 组件、样式
├─ api/ → 后端调用、数据获取、DTOs
├─ model/ → 类型、模式、存储、业务逻辑
├─ lib/ → 切片特定的工具函数
└─ config/ → 功能标志、常量
命名: 使用基于目的的命名(api/、model/),而非基于本质的命名(hooks/、types/)。
src/
├── app/ # 应用层(无切片)
│ ├── providers/ # React 上下文、QueryClient、主题
│ ├── routes/ # 路由配置
│ └── styles/ # 全局 CSS、主题令牌
├── pages/ # 页面切片
│ └── {page-name}/
│ ├── ui/ # 页面组件
│ ├── api/ # 加载器、服务端操作
│ ├── model/ # 页面特定状态
│ └── index.ts # 公共 API
├── widgets/ # 小部件切片
│ └── {widget-name}/
│ ├── ui/ # 组合 UI
│ └── index.ts
├── features/ # 功能切片
│ └── {feature-name}/
│ ├── ui/ # 功能 UI
│ ├── api/ # 功能 API 调用
│ ├── model/ # 状态、模式
│ └── index.ts
├── entities/ # 实体切片
│ └── {entity-name}/
│ ├── ui/ # 实体 UI(卡片、头像)
│ ├── api/ # CRUD 操作
│ ├── model/ # 类型、映射器、验证
│ └── index.ts
└── shared/ # 共享层(无切片)
├── ui/ # 设计系统组件
├── api/ # API 客户端、拦截器
├── lib/ # 工具函数(日期、验证)
├── config/ # 环境、常量
├── routes/ # 路由路径常量
└── i18n/ # 翻译
每个切片必须通过 index.ts 暴露一个公共 API。外部代码只能从这个文件导入。
// 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';
// ✅ 正确
import { UserCard, type User } from '@/entities/user';
// ❌ 错误
import { UserCard } from '@/entities/user/ui/UserCard';
避免通配符导出 — 它们会暴露内部实现并影响 Tree-shaking:
// ❌
export * from './ui';
// ✅
export { UserCard } from './ui/UserCard';
当实体之间确实需要相互引用时,使用 @x 表示法:
entities/
├── product/
│ ├── @x/
│ │ └── order.ts # 专门为 order 实体准备的 API
│ └── index.ts
└── order/
└── model/types.ts # 从 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';
指导原则: 保持跨导入最小化。如果引用非常广泛,考虑合并实体。
| 反模式 | 问题 | 修复方法 |
|---|---|---|
| 跨切片导入 | features/a → features/b | 将共享逻辑向下提取 |
| 通用片段命名 | components/、hooks/ | 使用 ui/、lib/、model/ |
| 通配符导出 | export * from './button' | 显式命名导出 |
| 业务逻辑放在共享层 | 领域逻辑放在 shared/lib | 移动到 entities/ |
| 单次使用的小部件 | 小部件仅被一个页面使用 | 保留在页面切片内 |
| 跳过公共 API | 从内部路径导入 | 始终使用 index.ts |
| 将所有东西都做成功能 | 所有交互都作为功能 | 仅复用操作 |
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
}
}
| 文件 | 目的 |
|---|---|
| references/LAYERS.md | 完整的层级规范、流程图 |
| references/PUBLIC-API.md | 导出模式、@x 表示法、Tree-shaking |
| references/IMPLEMENTATION.md | 代码模式:实体、功能、React Query |
| references/NEXTJS.md | App Router 集成、页面重新导出 |
| references/MIGRATION.md | 增量迁移策略 |
| references/CHEATSHEET.md | 快速参考、导入矩阵 |
每周安装量
63
代码仓库
GitHub 星标数
21
首次出现
2026年1月21日
安全审计
安装于
github-copilot56
gemini-cli52
opencode51
codex50
kimi-cli44
amp44
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
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 | 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 |
Minimal setup: app/, pages/, shared/ — add other layers as complexity grows.
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/
| 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.
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/).
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
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';
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-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' |
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
}
}
| 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 |
Weekly Installs
63
Repository
GitHub Stars
21
First Seen
Jan 21, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
github-copilot56
gemini-cli52
opencode51
codex50
kimi-cli44
amp44
站立会议模板:敏捷开发每日站会指南与工具(含远程团队异步模板)
10,500 周安装
GitHuman AI代码审查工具 - 可视化审查AI生成代码变更,支持行内评论和待办事项管理
148 周安装
社交媒体内容创作与管理技能:LinkedIn/X/Reddit/Discord 平台自动化工具
145 周安装
使用 bslib 构建现代化 Shiny 应用:Bootstrap 5 仪表盘与 UI 组件库
142 周安装
Python PDF处理全指南:合并拆分、文本表格提取、创建PDF文件
142 周安装
Mux视频平台开发指南:视频流、直播、播放器集成与AI工作流
83 周安装
Next.js App Router国际化方案:next-intl多语言路由配置指南
144 周安装
entities/ |
| Business domain models (user, product, order) |
| Yes |
| No |
shared/ | Project-agnostic infrastructure (UI kit, API client, utils) | No | Yes |
| 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 |