Loading...
Loading...
Compare original and translation side by side
STOP — Do NOT propose a new structure without understanding the current state and its pain points.
注意——未了解当前状态及其痛点之前,请勿提出新的结构方案。
STOP — Do NOT begin migration without documenting the target structure and getting team alignment.
注意——未文档化目标结构并获得团队一致认可之前,请勿开始迁移。
STOP — Do NOT execute migration without verifying tests pass at each incremental step.
注意——未在每一个增量步骤验证测试通过之前,请勿执行迁移。
| Project Size | Team Size | Recommendation | Why |
|---|---|---|---|
| < 20 files | 1-2 devs | Layer-based | Simple, low overhead |
| 20-100 files | 2-5 devs | Hybrid | Balance of simplicity and scalability |
| 100+ files | 5+ devs | Feature-based | Self-contained modules reduce conflicts |
| Multiple apps sharing code | Any | Monorepo | Shared packages with clear boundaries |
| Rapid prototype / MVP | 1-3 devs | Layer-based | Speed over structure, refactor later |
| Enterprise, multiple teams | 10+ devs | Feature-based + Monorepo | Team ownership per feature module |
| 项目规模 | 团队规模 | 推荐方案 | 原因 |
|---|---|---|---|
| < 20个文件 | 1-2名开发者 | 基于层级的架构 | 简单,开销低 |
| 20-100个文件 | 2-5名开发者 | 混合架构 | 平衡简单性和可扩展性 |
| 100+个文件 | 5+名开发者 | 基于特性的架构 | 自包含模块减少冲突 |
| 多个应用共享代码 | 任意规模 | Monorepo | 边界清晰的共享包 |
| 快速原型 / MVP | 1-3名开发者 | 基于层级的架构 | 速度优先于结构,后续再重构 |
| 企业级,多团队协作 | 10+名开发者 | 基于特性的架构 + Monorepo | 每个特性模块由对应团队负责 |
src/
features/
auth/
components/
LoginForm.tsx
SignupForm.tsx
hooks/
useAuth.ts
api/
auth.api.ts
types/
auth.types.ts
utils/
auth.utils.ts
__tests__/
auth.test.ts
index.ts # Public API (barrel export)
dashboard/
components/
hooks/
api/
types/
index.ts
billing/
...
shared/ # Cross-feature shared code
components/
Button.tsx
Modal.tsx
hooks/
useDebounce.ts
utils/
format.ts
types/
common.types.tssrc/
features/
auth/
components/
LoginForm.tsx
SignupForm.tsx
hooks/
useAuth.ts
api/
auth.api.ts
types/
auth.types.ts
utils/
auth.utils.ts
__tests__/
auth.test.ts
index.ts # Public API (barrel export)
dashboard/
components/
hooks/
api/
types/
index.ts
billing/
...
shared/ # Cross-feature shared code
components/
Button.tsx
Modal.tsx
hooks/
useDebounce.ts
utils/
format.ts
types/
common.types.tssrc/
components/
Button.tsx
Modal.tsx
LoginForm.tsx
DashboardCard.tsx
hooks/
useAuth.ts
useDebounce.ts
services/
auth.service.ts
billing.service.ts
utils/
format.ts
validation.ts
types/
auth.types.ts
billing.types.ts
pages/
Home.tsx
Dashboard.tsxsrc/
components/
Button.tsx
Modal.tsx
LoginForm.tsx
DashboardCard.tsx
hooks/
useAuth.ts
useDebounce.ts
services/
auth.service.ts
billing.service.ts
utils/
format.ts
validation.ts
types/
auth.types.ts
billing.types.ts
pages/
Home.tsx
Dashboard.tsxsrc/
app/ # App-level concerns
layout.tsx
providers.tsx
routes.tsx
features/ # Feature modules
auth/
dashboard/
billing/
components/ # Shared UI components
ui/ # Design system atoms
layout/ # Layout components
hooks/ # Shared hooks
lib/ # Shared utilities
types/ # Shared types
config/ # App configuration
styles/ # Global stylessrc/
app/ # App-level concerns
layout.tsx
providers.tsx
routes.tsx
features/ # Feature modules
auth/
dashboard/
billing/
components/ # Shared UI components
ui/ # Design system atoms
layout/ # Layout components
hooks/ # Shared hooks
lib/ # Shared utilities
types/ # Shared types
config/ # App configuration
styles/ # Global stylesroot/
apps/
web/ # Next.js web app
package.json
api/ # API server
package.json
mobile/ # React Native app
package.json
packages/
ui/ # Shared component library
package.json
config/ # Shared configs (ESLint, TypeScript)
eslint/
typescript/
package.json
utils/ # Shared utilities
package.json
types/ # Shared type definitions
package.json
package.json # Root workspace config
turbo.json # Turborepo pipeline config
pnpm-workspace.yamlroot/
apps/
web/ # Next.js web app
package.json
api/ # API server
package.json
mobile/ # React Native app
package.json
packages/
ui/ # Shared component library
package.json
config/ # Shared configs (ESLint, TypeScript)
eslint/
typescript/
package.json
utils/ # Shared utilities
package.json
types/ # Shared type definitions
package.json
package.json # Root workspace config
turbo.json # Turborepo pipeline config
pnpm-workspace.yamlindex.tsindex.ts// packages/config/typescript/base.json
{
"compilerOptions": {
"strict": true,
"moduleResolution": "bundler",
"target": "ES2022"
}
}
// apps/web/tsconfig.json
{
"extends": "@repo/config/typescript/nextjs",
"include": ["src"]
}// packages/config/typescript/base.json
{
"compilerOptions": {
"strict": true,
"moduleResolution": "bundler",
"target": "ES2022"
}
}
// apps/web/tsconfig.json
{
"extends": "@repo/config/typescript/nextjs",
"include": ["src"]
}| Type | Convention | Example |
|---|---|---|
| Components | PascalCase | |
| Hooks | camelCase with | |
| Utilities | camelCase | |
| Types | camelCase with | |
| Tests | same name with | |
| Styles | same name with | |
| Constants | camelCase or UPPER_SNAKE in file | |
| API/Services | camelCase with | |
| Directories | kebab-case | |
| 类型 | 规范 | 示例 |
|---|---|---|
| 组件 | PascalCase | |
| Hooks | 小驼峰,带 | |
| 工具函数 | 小驼峰 | |
| 类型定义 | 小驼峰,带 | |
| 测试文件 | 和对应文件同名,带 | |
| 样式文件 | 和对应文件同名,带 | |
| 常量 | 小驼峰或大写下划线命名 | |
| API/服务文件 | 小驼峰,带 | |
| 目录 | kebab-case | |
undefinedundefinedundefinedundefined// 1. External packages
import React from 'react';
import { useQuery } from '@tanstack/react-query';
// 2. Internal packages (monorepo)
import { Button } from '@repo/ui';
// 3. Feature-level imports
import { useAuth } from '@/features/auth';
// 4. Relative imports (same feature)
import { LoginForm } from './LoginForm';
import { authSchema } from './auth.types';
// 5. Styles
import styles from './Auth.module.css';// 1. External packages
import React from 'react';
import { useQuery } from '@tanstack/react-query';
// 2. Internal packages (monorepo)
import { Button } from '@repo/ui';
// 3. Feature-level imports
import { useAuth } from '@/features/auth';
// 4. Relative imports (same feature)
import { LoginForm } from './LoginForm';
import { authSchema } from './auth.types';
// 5. Styles
import styles from './Auth.module.css';// features/auth/index.ts — Public API
export { LoginForm } from './components/LoginForm';
export { useAuth } from './hooks/useAuth';
export type { User, AuthState } from './types/auth.types';
// Do NOT export internal implementation details
// Do NOT export utility functions used only within the feature// features/auth/index.ts — Public API
export { LoginForm } from './components/LoginForm';
export { useAuth } from './hooks/useAuth';
export type { User, AuthState } from './types/auth.types';
// Do NOT export internal implementation details
// Do NOT export utility functions used only within the feature| Context | Use Barrel? | Why |
|---|---|---|
| Feature module public API | Yes, always | Clean boundary, controlled surface area |
| Shared component library | Yes, always | Single import point for consumers |
| Utility libraries | Yes, always | Discoverability for shared functions |
| Inside a feature (internal) | No | Import directly, avoid indirection |
| Would cause circular dependencies | No | Break the cycle, import directly |
| Hurts tree-shaking (verified) | No | Use direct imports for bundle size |
| 场景 | 是否使用Barrel? | 原因 |
|---|---|---|
| 特性模块公共API | 是,始终使用 | 边界清晰,控制暴露面 |
| 共享组件库 | 是,始终使用 | 为使用者提供单一导入入口 |
| 工具库 | 是,始终使用 | 提升共享函数的可发现性 |
| 特性内部使用 | 否 | 直接导入,避免间接跳转 |
| 会导致循环依赖 | 否 | 打破循环,直接导入 |
| 已验证会影响tree-shaking | 否 | 使用直接导入控制包体积 |
root/
.editorconfig # Editor settings
.eslintrc.js # ESLint config (or eslint.config.js)
.gitignore # Git ignore rules
.prettierrc # Prettier config
.env.example # Environment variable template
docker-compose.yml # Docker composition
Dockerfile # Container build
package.json # Dependencies and scripts
tsconfig.json # TypeScript config
next.config.js # Framework config
tailwind.config.ts # Tailwind config
vitest.config.ts # Test configroot/
.editorconfig # Editor settings
.eslintrc.js # ESLint config (or eslint.config.js)
.gitignore # Git ignore rules
.prettierrc # Prettier config
.env.example # Environment variable template
docker-compose.yml # Docker composition
Dockerfile # Container build
package.json # Dependencies and scripts
tsconfig.json # TypeScript config
next.config.js # Framework config
tailwind.config.ts # Tailwind config
vitest.config.ts # Test config.env # Local defaults (gitignored)
.env.example # Template with dummy values (committed)
.env.local # Local overrides (gitignored)
.env.development # Development-specific (committed or not)
.env.production # Production-specific (committed or not)
.env.test # Test-specific (committed or not).env # Local defaults (gitignored)
.env.example # Template with dummy values (committed)
.env.local # Local overrides (gitignored)
.env.development # Development-specific (committed or not)
.env.production # Production-specific (committed or not)
.env.test # Test-specific (committed or not)docs/
architecture/
adr/ # Architecture Decision Records
001-framework.md
002-database.md
diagrams/
api/ # API documentation
guides/
getting-started.md
deployment.md
contributing.mddocs/
architecture/
adr/ # Architecture Decision Records
001-framework.md
002-database.md
diagrams/
api/ # API documentation
guides/
getting-started.md
deployment.md
contributing.mdts-morphjscodeshiftimport/orderts-morphjscodeshiftimport/order| Anti-Pattern | Why It Fails | What To Do Instead |
|---|---|---|
| Deeply nested folders (> 4 levels) | Hard to navigate, long import paths | Flatten structure, use path aliases |
| Becomes unmaintainable junk drawer | Organize utils by domain or purpose |
| Circular dependencies between features | Build failures, unclear ownership | Features import only from shared or own modules |
| Barrel exports re-exporting everything | Kills tree-shaking, bloats bundles | Export only the public API |
| Inconsistent naming (mixed conventions) | Cognitive load, merge conflicts | Pick one convention, enforce with linter |
| Config scattered across multiple locations | Hard to find and maintain | All config at project root |
| Tests in separate directory tree | Hard to find tests for a file | Co-locate tests with source code |
| 100+ files in one flat folder | Impossible to navigate | Group into sub-modules or features |
| Index files containing logic | Unexpected side effects on import | Index files only re-export |
| Big-bang migration (move everything at once) | High risk, hard to rollback | Incremental moves with tests after each |
| 反模式 | 失败原因 | 替代方案 |
|---|---|---|
| 文件夹嵌套过深(>4层) | 难以导航,导入路径过长 | 扁平化结构,使用路径别名 |
| 变成难以维护的杂物抽屉 | 按领域或用途组织工具函数 |
| 特性之间存在循环依赖 | 构建失败,所有权不清晰 | 特性仅从共享模块或自身模块导入 |
| Barrel export导出所有内容 | 破坏tree-shaking,增大包体积 | 仅导出公共API |
| 命名不一致(混合规范) | 增加认知负担,容易出现合并冲突 | 选择一套规范,通过linter强制执行 |
| 配置散落在多个位置 | 难以查找和维护 | 所有配置放置在项目根目录 |
| 测试文件放在独立的目录树中 | 难以找到对应文件的测试 | 测试文件与源代码同位置放置 |
| 单个扁平文件夹下有100+个文件 | 完全无法导航 | 拆分为子模块或特性分组 |
| Index文件包含业务逻辑 | 导入时出现意外副作用 | Index文件仅用于重新导出 |
| 一次性大爆炸式迁移 | 风险高,难以回滚 | 增量移动,每次移动后进行测试验证 |
| Skill | How It Connects |
|---|---|
| Frontend project structure follows feature-based or hybrid patterns |
| Architecture decisions inform module boundaries and package structure |
| Full-stack projects need coordinated frontend/backend organization |
| Naming conventions and module boundaries support clean code principles |
| Monorepo structure affects CI/CD pipeline configuration |
| Laravel projects follow framework-specific directory conventions |
| 技能 | 关联方式 |
|---|---|
| 前端项目结构遵循基于特性或混合模式 |
| 架构决策决定模块边界和包结构 |
| 全栈项目需要协调前端/后端的组织方式 |
| 命名规范和模块边界符合干净代码原则 |
| Monorepo结构影响CI/CD流水线配置 |
| Laravel项目遵循框架特定的目录规范 |