@mison/ag-kit-cn 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.agent/.shared/ui-ux-pro-max/data/charts.csv +26 -0
- package/.agent/.shared/ui-ux-pro-max/data/colors.csv +97 -0
- package/.agent/.shared/ui-ux-pro-max/data/icons.csv +101 -0
- package/.agent/.shared/ui-ux-pro-max/data/landing.csv +31 -0
- package/.agent/.shared/ui-ux-pro-max/data/products.csv +97 -0
- package/.agent/.shared/ui-ux-pro-max/data/prompts.csv +24 -0
- package/.agent/.shared/ui-ux-pro-max/data/react-performance.csv +45 -0
- package/.agent/.shared/ui-ux-pro-max/data/stacks/flutter.csv +53 -0
- package/.agent/.shared/ui-ux-pro-max/data/stacks/html-tailwind.csv +56 -0
- package/.agent/.shared/ui-ux-pro-max/data/stacks/jetpack-compose.csv +53 -0
- package/.agent/.shared/ui-ux-pro-max/data/stacks/nextjs.csv +53 -0
- package/.agent/.shared/ui-ux-pro-max/data/stacks/nuxt-ui.csv +51 -0
- package/.agent/.shared/ui-ux-pro-max/data/stacks/nuxtjs.csv +59 -0
- package/.agent/.shared/ui-ux-pro-max/data/stacks/react-native.csv +52 -0
- package/.agent/.shared/ui-ux-pro-max/data/stacks/react.csv +54 -0
- package/.agent/.shared/ui-ux-pro-max/data/stacks/shadcn.csv +61 -0
- package/.agent/.shared/ui-ux-pro-max/data/stacks/svelte.csv +54 -0
- package/.agent/.shared/ui-ux-pro-max/data/stacks/swiftui.csv +51 -0
- package/.agent/.shared/ui-ux-pro-max/data/stacks/vue.csv +50 -0
- package/.agent/.shared/ui-ux-pro-max/data/styles.csv +59 -0
- package/.agent/.shared/ui-ux-pro-max/data/typography.csv +58 -0
- package/.agent/.shared/ui-ux-pro-max/data/ui-reasoning.csv +101 -0
- package/.agent/.shared/ui-ux-pro-max/data/ux-guidelines.csv +100 -0
- package/.agent/.shared/ui-ux-pro-max/data/web-interface.csv +31 -0
- package/.agent/.shared/ui-ux-pro-max/scripts/core.py +258 -0
- package/.agent/.shared/ui-ux-pro-max/scripts/design_system.py +1067 -0
- package/.agent/.shared/ui-ux-pro-max/scripts/search.py +106 -0
- package/.agent/ARCHITECTURE.md +285 -0
- package/.agent/agents/backend-specialist.md +268 -0
- package/.agent/agents/code-archaeologist.md +106 -0
- package/.agent/agents/database-architect.md +225 -0
- package/.agent/agents/debugger.md +225 -0
- package/.agent/agents/devops-engineer.md +242 -0
- package/.agent/agents/documentation-writer.md +104 -0
- package/.agent/agents/explorer-agent.md +73 -0
- package/.agent/agents/frontend-specialist.md +618 -0
- package/.agent/agents/game-developer.md +162 -0
- package/.agent/agents/mobile-developer.md +382 -0
- package/.agent/agents/orchestrator.md +438 -0
- package/.agent/agents/penetration-tester.md +188 -0
- package/.agent/agents/performance-optimizer.md +187 -0
- package/.agent/agents/product-manager.md +112 -0
- package/.agent/agents/product-owner.md +95 -0
- package/.agent/agents/project-planner.md +405 -0
- package/.agent/agents/qa-automation-engineer.md +103 -0
- package/.agent/agents/security-auditor.md +170 -0
- package/.agent/agents/seo-specialist.md +111 -0
- package/.agent/agents/test-engineer.md +158 -0
- package/.agent/mcp_config.json +12 -0
- package/.agent/rules/GEMINI.md +273 -0
- package/.agent/scripts/auto_preview.py +148 -0
- package/.agent/scripts/checklist.py +217 -0
- package/.agent/scripts/session_manager.py +120 -0
- package/.agent/scripts/verify_all.py +327 -0
- package/.agent/skills/api-patterns/SKILL.md +84 -0
- package/.agent/skills/api-patterns/api-style.md +42 -0
- package/.agent/skills/api-patterns/auth.md +24 -0
- package/.agent/skills/api-patterns/documentation.md +26 -0
- package/.agent/skills/api-patterns/graphql.md +41 -0
- package/.agent/skills/api-patterns/rate-limiting.md +31 -0
- package/.agent/skills/api-patterns/response.md +37 -0
- package/.agent/skills/api-patterns/rest.md +40 -0
- package/.agent/skills/api-patterns/scripts/api_validator.py +211 -0
- package/.agent/skills/api-patterns/security-testing.md +122 -0
- package/.agent/skills/api-patterns/trpc.md +41 -0
- package/.agent/skills/api-patterns/versioning.md +22 -0
- package/.agent/skills/app-builder/SKILL.md +75 -0
- package/.agent/skills/app-builder/agent-coordination.md +74 -0
- package/.agent/skills/app-builder/feature-building.md +53 -0
- package/.agent/skills/app-builder/project-detection.md +34 -0
- package/.agent/skills/app-builder/scaffolding.md +118 -0
- package/.agent/skills/app-builder/tech-stack.md +40 -0
- package/.agent/skills/app-builder/templates/SKILL.md +39 -0
- package/.agent/skills/app-builder/templates/astro-static/TEMPLATE.md +76 -0
- package/.agent/skills/app-builder/templates/chrome-extension/TEMPLATE.md +92 -0
- package/.agent/skills/app-builder/templates/cli-tool/TEMPLATE.md +88 -0
- package/.agent/skills/app-builder/templates/electron-desktop/TEMPLATE.md +88 -0
- package/.agent/skills/app-builder/templates/express-api/TEMPLATE.md +83 -0
- package/.agent/skills/app-builder/templates/flutter-app/TEMPLATE.md +90 -0
- package/.agent/skills/app-builder/templates/monorepo-turborepo/TEMPLATE.md +90 -0
- package/.agent/skills/app-builder/templates/nextjs-fullstack/TEMPLATE.md +122 -0
- package/.agent/skills/app-builder/templates/nextjs-saas/TEMPLATE.md +122 -0
- package/.agent/skills/app-builder/templates/nextjs-static/TEMPLATE.md +169 -0
- package/.agent/skills/app-builder/templates/nuxt-app/TEMPLATE.md +134 -0
- package/.agent/skills/app-builder/templates/python-fastapi/TEMPLATE.md +83 -0
- package/.agent/skills/app-builder/templates/react-native-app/TEMPLATE.md +119 -0
- package/.agent/skills/architecture/SKILL.md +57 -0
- package/.agent/skills/architecture/context-discovery.md +43 -0
- package/.agent/skills/architecture/examples.md +94 -0
- package/.agent/skills/architecture/pattern-selection.md +68 -0
- package/.agent/skills/architecture/patterns-reference.md +50 -0
- package/.agent/skills/architecture/trade-off-analysis.md +77 -0
- package/.agent/skills/bash-linux/SKILL.md +201 -0
- package/.agent/skills/behavioral-modes/SKILL.md +264 -0
- package/.agent/skills/brainstorming/SKILL.md +164 -0
- package/.agent/skills/brainstorming/dynamic-questioning.md +359 -0
- package/.agent/skills/clean-code/SKILL.md +200 -0
- package/.agent/skills/code-review-checklist/SKILL.md +125 -0
- package/.agent/skills/database-design/SKILL.md +54 -0
- package/.agent/skills/database-design/database-selection.md +43 -0
- package/.agent/skills/database-design/indexing.md +39 -0
- package/.agent/skills/database-design/migrations.md +50 -0
- package/.agent/skills/database-design/optimization.md +36 -0
- package/.agent/skills/database-design/orm-selection.md +30 -0
- package/.agent/skills/database-design/schema-design.md +56 -0
- package/.agent/skills/database-design/scripts/schema_validator.py +172 -0
- package/.agent/skills/deployment-procedures/SKILL.md +241 -0
- package/.agent/skills/doc.md +177 -0
- package/.agent/skills/documentation-templates/SKILL.md +194 -0
- package/.agent/skills/frontend-design/SKILL.md +418 -0
- package/.agent/skills/frontend-design/animation-guide.md +331 -0
- package/.agent/skills/frontend-design/color-system.md +307 -0
- package/.agent/skills/frontend-design/decision-trees.md +418 -0
- package/.agent/skills/frontend-design/motion-graphics.md +306 -0
- package/.agent/skills/frontend-design/scripts/accessibility_checker.py +183 -0
- package/.agent/skills/frontend-design/scripts/ux_audit.py +727 -0
- package/.agent/skills/frontend-design/typography-system.md +345 -0
- package/.agent/skills/frontend-design/ux-psychology.md +1118 -0
- package/.agent/skills/frontend-design/visual-effects.md +383 -0
- package/.agent/skills/game-development/2d-games/SKILL.md +119 -0
- package/.agent/skills/game-development/3d-games/SKILL.md +135 -0
- package/.agent/skills/game-development/SKILL.md +167 -0
- package/.agent/skills/game-development/game-art/SKILL.md +185 -0
- package/.agent/skills/game-development/game-audio/SKILL.md +190 -0
- package/.agent/skills/game-development/game-design/SKILL.md +129 -0
- package/.agent/skills/game-development/mobile-games/SKILL.md +108 -0
- package/.agent/skills/game-development/multiplayer/SKILL.md +132 -0
- package/.agent/skills/game-development/pc-games/SKILL.md +144 -0
- package/.agent/skills/game-development/vr-ar/SKILL.md +123 -0
- package/.agent/skills/game-development/web-games/SKILL.md +150 -0
- package/.agent/skills/geo-fundamentals/SKILL.md +155 -0
- package/.agent/skills/geo-fundamentals/scripts/geo_checker.py +289 -0
- package/.agent/skills/i18n-localization/SKILL.md +154 -0
- package/.agent/skills/i18n-localization/scripts/i18n_checker.py +241 -0
- package/.agent/skills/intelligent-routing/SKILL.md +335 -0
- package/.agent/skills/lint-and-validate/SKILL.md +44 -0
- package/.agent/skills/lint-and-validate/scripts/lint_runner.py +184 -0
- package/.agent/skills/lint-and-validate/scripts/type_coverage.py +173 -0
- package/.agent/skills/mcp-builder/SKILL.md +176 -0
- package/.agent/skills/mobile-design/SKILL.md +394 -0
- package/.agent/skills/mobile-design/decision-trees.md +516 -0
- package/.agent/skills/mobile-design/mobile-backend.md +491 -0
- package/.agent/skills/mobile-design/mobile-color-system.md +420 -0
- package/.agent/skills/mobile-design/mobile-debugging.md +122 -0
- package/.agent/skills/mobile-design/mobile-design-thinking.md +355 -0
- package/.agent/skills/mobile-design/mobile-navigation.md +458 -0
- package/.agent/skills/mobile-design/mobile-performance.md +767 -0
- package/.agent/skills/mobile-design/mobile-testing.md +356 -0
- package/.agent/skills/mobile-design/mobile-typography.md +432 -0
- package/.agent/skills/mobile-design/platform-android.md +666 -0
- package/.agent/skills/mobile-design/platform-ios.md +561 -0
- package/.agent/skills/mobile-design/scripts/mobile_audit.py +670 -0
- package/.agent/skills/mobile-design/touch-psychology.md +537 -0
- package/.agent/skills/nextjs-react-expert/1-async-eliminating-waterfalls.md +311 -0
- package/.agent/skills/nextjs-react-expert/2-bundle-bundle-size-optimization.md +241 -0
- package/.agent/skills/nextjs-react-expert/3-server-server-side-performance.md +489 -0
- package/.agent/skills/nextjs-react-expert/4-client-client-side-data-fetching.md +263 -0
- package/.agent/skills/nextjs-react-expert/5-rerender-re-render-optimization.md +581 -0
- package/.agent/skills/nextjs-react-expert/6-rendering-rendering-performance.md +431 -0
- package/.agent/skills/nextjs-react-expert/7-js-javascript-performance.md +683 -0
- package/.agent/skills/nextjs-react-expert/8-advanced-advanced-patterns.md +149 -0
- package/.agent/skills/nextjs-react-expert/SKILL.md +286 -0
- package/.agent/skills/nextjs-react-expert/scripts/convert_rules.py +222 -0
- package/.agent/skills/nextjs-react-expert/scripts/react_performance_checker.py +252 -0
- package/.agent/skills/nodejs-best-practices/SKILL.md +333 -0
- package/.agent/skills/parallel-agents/SKILL.md +194 -0
- package/.agent/skills/performance-profiling/SKILL.md +149 -0
- package/.agent/skills/performance-profiling/scripts/lighthouse_audit.py +76 -0
- package/.agent/skills/plan-writing/SKILL.md +152 -0
- package/.agent/skills/powershell-windows/SKILL.md +166 -0
- package/.agent/skills/python-patterns/SKILL.md +441 -0
- package/.agent/skills/red-team-tactics/SKILL.md +203 -0
- package/.agent/skills/rust-pro/SKILL.md +190 -0
- package/.agent/skills/seo-fundamentals/SKILL.md +135 -0
- package/.agent/skills/seo-fundamentals/scripts/seo_checker.py +215 -0
- package/.agent/skills/server-management/SKILL.md +161 -0
- package/.agent/skills/systematic-debugging/SKILL.md +114 -0
- package/.agent/skills/tailwind-patterns/SKILL.md +269 -0
- package/.agent/skills/tdd-workflow/SKILL.md +149 -0
- package/.agent/skills/testing-patterns/SKILL.md +178 -0
- package/.agent/skills/testing-patterns/scripts/test_runner.py +219 -0
- package/.agent/skills/vulnerability-scanner/SKILL.md +276 -0
- package/.agent/skills/vulnerability-scanner/checklists.md +131 -0
- package/.agent/skills/vulnerability-scanner/scripts/security_scan.py +459 -0
- package/.agent/skills/web-design-guidelines/SKILL.md +57 -0
- package/.agent/skills/webapp-testing/SKILL.md +187 -0
- package/.agent/skills/webapp-testing/scripts/playwright_runner.py +173 -0
- package/.agent/workflows/brainstorm.md +113 -0
- package/.agent/workflows/create.md +59 -0
- package/.agent/workflows/debug.md +103 -0
- package/.agent/workflows/deploy.md +176 -0
- package/.agent/workflows/enhance.md +63 -0
- package/.agent/workflows/orchestrate.md +242 -0
- package/.agent/workflows/plan.md +89 -0
- package/.agent/workflows/preview.md +80 -0
- package/.agent/workflows/restore-localize-compat.md +525 -0
- package/.agent/workflows/status.md +86 -0
- package/.agent/workflows/test.md +144 -0
- package/.agent/workflows/ui-ux-pro-max.md +295 -0
- package/AGENT_FLOW.md +609 -0
- package/CHANGELOG.md +68 -0
- package/LICENSE +21 -0
- package/README.md +260 -0
- package/bin/adapters/base.js +63 -0
- package/bin/adapters/codex.js +391 -0
- package/bin/adapters/gemini.js +137 -0
- package/bin/ag-kit.js +1336 -0
- package/bin/core/builder.js +80 -0
- package/bin/core/generator.js +59 -0
- package/bin/core/resource-loader.js +64 -0
- package/bin/core/transformer.js +208 -0
- package/bin/interactive.js +65 -0
- package/bin/utils/atomic-writer.js +97 -0
- package/bin/utils/git-helper.js +68 -0
- package/bin/utils/managed-block.js +65 -0
- package/bin/utils/manifest.js +241 -0
- package/bin/utils.js +82 -0
- package/docs/codex-rules-template.md +36 -0
- package/docs/mapping-spec.md +68 -0
- package/docs/multi-target-adapter.md +80 -0
- package/docs/official/README.md +53 -0
- package/docs/official/antigravity/agent-modes-settings.md +64 -0
- package/docs/official/antigravity/rules-workflows.md +96 -0
- package/docs/official/antigravity/skills.md +147 -0
- package/docs/official/codex/agents-md.md +119 -0
- package/docs/official/codex/config-advanced.md +358 -0
- package/docs/official/codex/config-basic.md +141 -0
- package/docs/official/codex/config-reference.md +223 -0
- package/docs/official/codex/config-sample.md +216 -0
- package/docs/official/codex/mcp.md +107 -0
- package/docs/official/codex/rules.md +79 -0
- package/docs/official/codex/skills.md +114 -0
- package/docs/official/sources-index.md +32 -0
- package/docs/operations.md +145 -0
- package/docs/terminology-style-guide.md +69 -0
- package/package.json +51 -0
- package/scripts/postinstall-check.js +112 -0
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# 8. 高级模式
|
|
2
|
+
|
|
3
|
+
> **影响:** 可变(VARIABLE)
|
|
4
|
+
> **重点:** 针对需要仔细实现的特定情况的高级模式。
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## 概览
|
|
9
|
+
|
|
10
|
+
本节包含 **3 条规则**,专注于高级模式。
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## 规则 8.1: 初始化应用一次,而不是每次挂载
|
|
15
|
+
|
|
16
|
+
**影响:** 中低(LOW-MEDIUM)
|
|
17
|
+
**标签:** initialization, useEffect, app-startup, side-effects
|
|
18
|
+
|
|
19
|
+
## 初始化应用一次,而不是每次挂载
|
|
20
|
+
|
|
21
|
+
不要将必须在每个应用加载时运行一次的应用级初始化放在组件的 `useEffect([])` 内部。组件可以重新挂载,effect 会重新运行。请改用模块级守卫或入口模块中的顶层初始化。
|
|
22
|
+
|
|
23
|
+
**错误(在开发环境中运行两次,在重新挂载时重新运行):**
|
|
24
|
+
|
|
25
|
+
```tsx
|
|
26
|
+
function Comp() {
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
loadFromStorage();
|
|
29
|
+
checkAuthToken();
|
|
30
|
+
}, []);
|
|
31
|
+
|
|
32
|
+
// ...
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
**正确(每个应用加载一次):**
|
|
37
|
+
|
|
38
|
+
```tsx
|
|
39
|
+
let didInit = false;
|
|
40
|
+
|
|
41
|
+
function Comp() {
|
|
42
|
+
useEffect(() => {
|
|
43
|
+
if (didInit) return;
|
|
44
|
+
didInit = true;
|
|
45
|
+
loadFromStorage();
|
|
46
|
+
checkAuthToken();
|
|
47
|
+
}, []);
|
|
48
|
+
|
|
49
|
+
// ...
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
参考: [初始化应用](https://react.dev/learn/you-might-not-need-an-effect#initializing-the-application)
|
|
54
|
+
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
## 规则 8.2: 将事件处理程序存储在 Ref 中
|
|
58
|
+
|
|
59
|
+
**影响:** 低(LOW)
|
|
60
|
+
**标签:** advanced, hooks, refs, event-handlers, optimization
|
|
61
|
+
|
|
62
|
+
## 将事件处理程序存储在 Ref 中
|
|
63
|
+
|
|
64
|
+
当在不应能在回调更改时重新订阅的 effect 中使用回调时,将回调存储在 ref 中。
|
|
65
|
+
|
|
66
|
+
**错误(每次渲染都重新订阅):**
|
|
67
|
+
|
|
68
|
+
```tsx
|
|
69
|
+
function useWindowEvent(event: string, handler: (e) => void) {
|
|
70
|
+
useEffect(() => {
|
|
71
|
+
window.addEventListener(event, handler);
|
|
72
|
+
return () => window.removeEventListener(event, handler);
|
|
73
|
+
}, [event, handler]);
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
**正确(稳定订阅):**
|
|
78
|
+
|
|
79
|
+
```tsx
|
|
80
|
+
function useWindowEvent(event: string, handler: (e) => void) {
|
|
81
|
+
const handlerRef = useRef(handler);
|
|
82
|
+
useEffect(() => {
|
|
83
|
+
handlerRef.current = handler;
|
|
84
|
+
}, [handler]);
|
|
85
|
+
|
|
86
|
+
useEffect(() => {
|
|
87
|
+
const listener = (e) => handlerRef.current(e);
|
|
88
|
+
window.addEventListener(event, listener);
|
|
89
|
+
return () => window.removeEventListener(event, listener);
|
|
90
|
+
}, [event]);
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
**替代方案: 如果你使用的是最新 React,请使用 `useEffectEvent`:**
|
|
95
|
+
|
|
96
|
+
```tsx
|
|
97
|
+
import { useEffectEvent } from "react";
|
|
98
|
+
|
|
99
|
+
function useWindowEvent(event: string, handler: (e) => void) {
|
|
100
|
+
const onEvent = useEffectEvent(handler);
|
|
101
|
+
|
|
102
|
+
useEffect(() => {
|
|
103
|
+
window.addEventListener(event, onEvent);
|
|
104
|
+
return () => window.removeEventListener(event, onEvent);
|
|
105
|
+
}, [event]);
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
`useEffectEvent` 为相同的模式提供了更清晰的 API:它创建一个稳定的函数引用,始终调用处理程序的最新版本。
|
|
110
|
+
|
|
111
|
+
---
|
|
112
|
+
|
|
113
|
+
## 规则 8.3: 用于稳定回调 Ref 的 useEffectEvent
|
|
114
|
+
|
|
115
|
+
**影响:** 低(LOW)
|
|
116
|
+
**标签:** advanced, hooks, useEffectEvent, refs, optimization
|
|
117
|
+
|
|
118
|
+
## 用于稳定回调 Ref 的 useEffectEvent
|
|
119
|
+
|
|
120
|
+
在回调中访问最新值,而无需将其添加到依赖数组中。防止 Effect 重新运行,同时避免过时的闭包。
|
|
121
|
+
|
|
122
|
+
**错误(Effect 在每次回调更改时重新运行):**
|
|
123
|
+
|
|
124
|
+
```tsx
|
|
125
|
+
function SearchInput({ onSearch }: { onSearch: (q: string) => void }) {
|
|
126
|
+
const [query, setQuery] = useState("");
|
|
127
|
+
|
|
128
|
+
useEffect(() => {
|
|
129
|
+
const timeout = setTimeout(() => onSearch(query), 300);
|
|
130
|
+
return () => clearTimeout(timeout);
|
|
131
|
+
}, [query, onSearch]);
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
**正确(使用 React 的 useEffectEvent):**
|
|
136
|
+
|
|
137
|
+
```tsx
|
|
138
|
+
import { useEffectEvent } from "react";
|
|
139
|
+
|
|
140
|
+
function SearchInput({ onSearch }: { onSearch: (q: string) => void }) {
|
|
141
|
+
const [query, setQuery] = useState("");
|
|
142
|
+
const onSearchEvent = useEffectEvent(onSearch);
|
|
143
|
+
|
|
144
|
+
useEffect(() => {
|
|
145
|
+
const timeout = setTimeout(() => onSearchEvent(query), 300);
|
|
146
|
+
return () => clearTimeout(timeout);
|
|
147
|
+
}, [query]);
|
|
148
|
+
}
|
|
149
|
+
```
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: react-best-practices
|
|
3
|
+
description: 来自 Vercel Engineering 的 React 与 Next.js 性能优化规则。适用于构建组件、优化性能、消除瀑布式请求(waterfalls)、缩减 bundle、性能代码审查与服务端/客户端优化实现。
|
|
4
|
+
allowed-tools: Read, Write, Edit, Glob, Grep, Bash
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# Next.js 与 React 性能专家
|
|
8
|
+
|
|
9
|
+
> **来自 Vercel Engineering** - 按影响优先级整理的 57 条优化规则
|
|
10
|
+
> **哲学:** 先消除 waterfall,再优化 bundle,最后再做微优化。
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## 🎯 选择性阅读规则(强制)
|
|
15
|
+
|
|
16
|
+
**只阅读与当前任务相关的章节!** 先看下面内容地图,再按需加载。
|
|
17
|
+
|
|
18
|
+
> 🔴 **进行性能评审时:先看 CRITICAL(1-2),再看 HIGH/MEDIUM。**
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## 📑 内容地图
|
|
23
|
+
|
|
24
|
+
| 文件 | 影响等级 | 规则数 | 阅读时机 |
|
|
25
|
+
| --------------------------------------- | ------------------- | ------ | -------------------------------------------------------------- |
|
|
26
|
+
| `1-async-eliminating-waterfalls.md` | 🔴 **CRITICAL** | 5 条 | 页面加载慢、串行 API 调用、数据获取 waterfall(瀑布式请求) |
|
|
27
|
+
| `2-bundle-bundle-size-optimization.md` | 🔴 **CRITICAL** | 5 条 | bundle 体积过大、TTI 慢、首屏加载问题 |
|
|
28
|
+
| `3-server-server-side-performance.md` | 🟠 **HIGH** | 7 条 | SSR 缓慢、API Route 优化、服务端 waterfall(瀑布式请求) |
|
|
29
|
+
| `4-client-client-side-data-fetching.md` | 🟡 **MEDIUM-HIGH** | 4 条 | 客户端数据管理、SWR 模式、请求去重 |
|
|
30
|
+
| `5-rerender-re-render-optimization.md` | 🟡 **MEDIUM** | 12 条 | 过度重渲染、React 性能、memoization |
|
|
31
|
+
| `6-rendering-rendering-performance.md` | 🟡 **MEDIUM** | 9 条 | 渲染瓶颈、虚拟列表、图像优化 |
|
|
32
|
+
| `7-js-javascript-performance.md` | ⚪ **LOW-MEDIUM** | 12 条 | 微优化、缓存、循环性能 |
|
|
33
|
+
| `8-advanced-advanced-patterns.md` | 🔵 **VARIABLE** | 3 条 | 高级 React 模式、useLatest、init-once |
|
|
34
|
+
|
|
35
|
+
**总计:8 大类,共 57 条规则**
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## 🚀 快速决策树
|
|
40
|
+
|
|
41
|
+
**你的性能问题属于哪一类?**
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
🐌 页面加载慢 / 可交互时间(Time to Interactive,TTI)长
|
|
45
|
+
→ 阅读第 1 节:消除瀑布式请求(Eliminating Waterfalls)
|
|
46
|
+
→ 阅读第 2 节:包体积优化(Bundle Size Optimization)
|
|
47
|
+
|
|
48
|
+
📦 bundle 体积过大(> 200KB)
|
|
49
|
+
→ 阅读第 2 节:包体积优化(Bundle Size Optimization)
|
|
50
|
+
→ 检查:动态导入(dynamic imports)、桶文件导入(barrel imports)、tree-shaking
|
|
51
|
+
|
|
52
|
+
🖥️ 服务端渲染慢(SSR)
|
|
53
|
+
→ 阅读第 3 节:服务端性能(Server-Side Performance)
|
|
54
|
+
→ 检查:并行数据获取、streaming
|
|
55
|
+
|
|
56
|
+
🔄 重渲染过多 / UI 卡顿
|
|
57
|
+
→ 阅读第 5 节:重渲染优化(Re-render Optimization)
|
|
58
|
+
→ 检查:React.memo、useMemo、useCallback
|
|
59
|
+
|
|
60
|
+
🎨 渲染性能问题
|
|
61
|
+
→ 阅读第 6 节:渲染性能(Rendering Performance)
|
|
62
|
+
→ 检查:虚拟化、layout thrashing
|
|
63
|
+
|
|
64
|
+
🌐 客户端数据获取问题
|
|
65
|
+
→ 阅读第 4 节:客户端数据获取(Client-Side Data Fetching)
|
|
66
|
+
→ 检查:SWR 去重(deduplication)、localStorage
|
|
67
|
+
|
|
68
|
+
✨ 需要高级模式
|
|
69
|
+
→ 阅读第 8 节:高级模式(Advanced Patterns)
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
## 📊 影响优先级指南
|
|
75
|
+
|
|
76
|
+
**进行全量优化时,按以下顺序:**
|
|
77
|
+
|
|
78
|
+
```
|
|
79
|
+
1️⃣ CRITICAL(收益最大,先做):
|
|
80
|
+
├─ 第 1 节:消除瀑布式请求(Eliminating Waterfalls)
|
|
81
|
+
│ └─ 每个 waterfall 都会引入完整网络延迟(100-500ms+)
|
|
82
|
+
└─ 第 2 节:包体积优化(Bundle Size Optimization)
|
|
83
|
+
└─ 直接影响可交互时间(Time to Interactive)与最大内容绘制(LCP)
|
|
84
|
+
|
|
85
|
+
2️⃣ HIGH(显著收益,第二步):
|
|
86
|
+
└─ 第 3 节:服务端性能(Server-Side Performance)
|
|
87
|
+
└─ 消除服务端 waterfall,提升响应速度
|
|
88
|
+
|
|
89
|
+
3️⃣ MEDIUM(中等收益,第三步):
|
|
90
|
+
├─ 第 4 节:客户端数据获取(Client-Side Data Fetching)
|
|
91
|
+
├─ 第 5 节:重渲染优化(Re-render Optimization)
|
|
92
|
+
└─ 第 6 节:渲染性能(Rendering Performance)
|
|
93
|
+
|
|
94
|
+
4️⃣ LOW(打磨项,最后做):
|
|
95
|
+
├─ 第 7 节:JavaScript 性能(JavaScript Performance)
|
|
96
|
+
└─ 第 8 节:高级模式(Advanced Patterns)
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
## 🔗 相关技能
|
|
102
|
+
|
|
103
|
+
| 需求 | 技能 |
|
|
104
|
+
| ----------------------- | --------------------------------- |
|
|
105
|
+
| API 设计模式 | `@[skills/api-patterns]` |
|
|
106
|
+
| 数据库优化 | `@[skills/database-design]` |
|
|
107
|
+
| 测试策略 | `@[skills/testing-patterns]` |
|
|
108
|
+
| UI/UX 设计原则 | `@[skills/frontend-design]` |
|
|
109
|
+
| TypeScript 模式 | `@[skills/typescript-expert]` |
|
|
110
|
+
| 部署与 DevOps | `@[skills/deployment-procedures]` |
|
|
111
|
+
|
|
112
|
+
---
|
|
113
|
+
|
|
114
|
+
## ✅ 性能评审清单
|
|
115
|
+
|
|
116
|
+
上线前检查:
|
|
117
|
+
|
|
118
|
+
**Critical(必须修复):**
|
|
119
|
+
|
|
120
|
+
- [ ] 无串行数据获取(waterfall 瀑布式请求已消除)
|
|
121
|
+
- [ ] 主 bundle(包)< 200KB
|
|
122
|
+
- [ ] 应用代码中无 barrel imports(桶文件导入)
|
|
123
|
+
- [ ] 大组件已使用动态导入(dynamic imports)
|
|
124
|
+
- [ ] 可并行的数据获取已并行化
|
|
125
|
+
|
|
126
|
+
**High Priority(高优先级):**
|
|
127
|
+
|
|
128
|
+
- [ ] 合适场景使用 Server Components(服务端组件)
|
|
129
|
+
- [ ] API Routes(接口路由)已优化(无 N+1 查询)
|
|
130
|
+
- [ ] 数据获取路径配置了 Suspense(挂起)边界
|
|
131
|
+
- [ ] 可静态生成内容已采用 SSG(静态生成)
|
|
132
|
+
|
|
133
|
+
**Medium Priority(中优先级):**
|
|
134
|
+
|
|
135
|
+
- [ ] 高开销计算已 memoize(记忆化)
|
|
136
|
+
- [ ] 大列表(>100 项)已虚拟化
|
|
137
|
+
- [ ] 图片通过 `next/image` 优化
|
|
138
|
+
- [ ] 无不必要重渲染
|
|
139
|
+
|
|
140
|
+
**Low Priority(打磨):**
|
|
141
|
+
|
|
142
|
+
- [ ] 热路径循环已优化
|
|
143
|
+
- [ ] RegExp 已提升至循环外
|
|
144
|
+
- [ ] 循环内属性访问已缓存
|
|
145
|
+
|
|
146
|
+
---
|
|
147
|
+
|
|
148
|
+
## ❌ 反模式
|
|
149
|
+
|
|
150
|
+
**不要这样做(DON'T):**
|
|
151
|
+
|
|
152
|
+
- ❌ 对互不依赖任务使用串行 `await`
|
|
153
|
+
- ❌ 只用一个函数却导入整个库
|
|
154
|
+
- ❌ 在应用代码中使用 barrel exports(`index.ts` 重导出)
|
|
155
|
+
- ❌ 大组件/大库不做动态导入(dynamic imports)
|
|
156
|
+
- ❌ `useEffect` 拉数据却不做去重
|
|
157
|
+
- ❌ 高开销计算不做 memoization(记忆化)
|
|
158
|
+
- ❌ 本可用 server component 却硬用 client component
|
|
159
|
+
|
|
160
|
+
**推荐做法(DO):**
|
|
161
|
+
|
|
162
|
+
- ✅ 使用 `Promise.all()` 并行获取数据
|
|
163
|
+
- ✅ 使用动态导入:`const Comp = dynamic(() => import('./Heavy'))`
|
|
164
|
+
- ✅ 精确导入:`import { specific } from 'library/specific'`
|
|
165
|
+
- ✅ 用 Suspense(挂起)边界提升体验
|
|
166
|
+
- ✅ 利用 React Server Components(服务端组件)
|
|
167
|
+
- ✅ 优化前先测量
|
|
168
|
+
- ✅ 使用 Next.js 内建优化(`next/image`、`next/font`)
|
|
169
|
+
|
|
170
|
+
---
|
|
171
|
+
|
|
172
|
+
## 🎯 如何使用本技能
|
|
173
|
+
|
|
174
|
+
### 新功能开发场景:
|
|
175
|
+
|
|
176
|
+
1. 开发阶段优先检查 **Section 1 & 2**(预防 waterfall 瀑布式请求,控制 bundle 包体积)
|
|
177
|
+
2. 默认优先考虑 server components(服务端组件,Section 3)
|
|
178
|
+
3. 对高开销操作应用 memoization(记忆化,Section 5)
|
|
179
|
+
|
|
180
|
+
### 性能评审场景:
|
|
181
|
+
|
|
182
|
+
1. 从 **Section 1** 开始(waterfall 影响最大)
|
|
183
|
+
2. 再看 **Section 2**(bundle 体积)
|
|
184
|
+
3. 再看 **Section 3**(服务端性能)
|
|
185
|
+
4. 最后按需补充其他章节
|
|
186
|
+
|
|
187
|
+
### 慢性能排障场景:
|
|
188
|
+
|
|
189
|
+
1. 先识别症状(慢加载、卡顿等)
|
|
190
|
+
2. 使用上方快速决策树
|
|
191
|
+
3. 阅读对应章节
|
|
192
|
+
4. 按优先级顺序落地修复
|
|
193
|
+
|
|
194
|
+
---
|
|
195
|
+
|
|
196
|
+
## 📚 学习路径
|
|
197
|
+
|
|
198
|
+
**初级(先抓关键):**
|
|
199
|
+
→ 第 1 节:消除瀑布式请求(Eliminating Waterfalls)
|
|
200
|
+
→ 第 2 节:包体积优化(Bundle Size Optimization)
|
|
201
|
+
|
|
202
|
+
**中级(补齐高优先级):**
|
|
203
|
+
→ 第 3 节:服务端性能(Server-Side Performance)
|
|
204
|
+
→ 第 5 节:重渲染优化(Re-render Optimization)
|
|
205
|
+
|
|
206
|
+
**高级(全量优化):**
|
|
207
|
+
→ 全章节 + 第 8 节:高级模式(Advanced Patterns)
|
|
208
|
+
|
|
209
|
+
---
|
|
210
|
+
|
|
211
|
+
## 🔍 验证脚本
|
|
212
|
+
|
|
213
|
+
| 脚本 | 用途 | 执行命令 |
|
|
214
|
+
| -------------------------------------- | --------------------------- | ------------------------------------------------------------ |
|
|
215
|
+
| `scripts/react_performance_checker.py` | 自动化性能审计 | `python scripts/react_performance_checker.py <project_path>` |
|
|
216
|
+
|
|
217
|
+
---
|
|
218
|
+
|
|
219
|
+
## 📖 章节说明
|
|
220
|
+
|
|
221
|
+
### 第 1 节:消除瀑布式请求(CRITICAL)
|
|
222
|
+
|
|
223
|
+
**影响:** 每个 waterfall(瀑布式请求)会增加 100-500ms+ 延迟
|
|
224
|
+
**关键概念:** 并行获取、`Promise.all()`、Suspense 边界、预加载
|
|
225
|
+
|
|
226
|
+
### 第 2 节:包体积优化(CRITICAL)
|
|
227
|
+
|
|
228
|
+
**影响:** 直接影响 TTI(可交互时间)、LCP(最大内容绘制)
|
|
229
|
+
**关键概念:** 动态导入、tree-shaking(摇树优化)、避免 barrel imports(桶文件导入)
|
|
230
|
+
|
|
231
|
+
### 第 3 节:服务端性能(HIGH)
|
|
232
|
+
|
|
233
|
+
**影响:** 更快服务端响应、更好 SEO
|
|
234
|
+
**关键概念:** 服务端并行获取、streaming、API Route(接口路由)优化
|
|
235
|
+
|
|
236
|
+
### 第 4 节:客户端数据获取(MEDIUM-HIGH)
|
|
237
|
+
|
|
238
|
+
**影响:** 减少冗余请求、提升 UX(用户体验)
|
|
239
|
+
**关键概念:** SWR 去重、localStorage 缓存、事件监听
|
|
240
|
+
|
|
241
|
+
### 第 5 节:重渲染优化(MEDIUM)
|
|
242
|
+
|
|
243
|
+
**影响:** UI 更流畅、减少无效计算
|
|
244
|
+
**关键概念:** `React.memo`、`useMemo`、`useCallback`、组件结构
|
|
245
|
+
|
|
246
|
+
### 第 6 节:渲染性能(MEDIUM)
|
|
247
|
+
|
|
248
|
+
**影响:** 提升渲染效率
|
|
249
|
+
**关键概念:** 虚拟化、图像优化、layout thrashing(布局抖动)
|
|
250
|
+
|
|
251
|
+
### 第 7 节:JavaScript 性能(LOW-MEDIUM)
|
|
252
|
+
|
|
253
|
+
**影响:** 热路径增量优化
|
|
254
|
+
**关键概念:** 循环优化、缓存、RegExp 提升
|
|
255
|
+
|
|
256
|
+
### 第 8 节:高级模式(VARIABLE)
|
|
257
|
+
|
|
258
|
+
**影响:** 特定场景收益明显
|
|
259
|
+
**关键概念:** `useLatest` hook(钩子)、init-once 模式、event handler refs(事件处理引用)
|
|
260
|
+
|
|
261
|
+
---
|
|
262
|
+
|
|
263
|
+
## 🎓 最佳实践总结
|
|
264
|
+
|
|
265
|
+
**黄金法则:**
|
|
266
|
+
|
|
267
|
+
1. **先测量再优化** - 使用 React DevTools Profiler、Chrome DevTools
|
|
268
|
+
2. **先做高收益项** - Waterfalls(瀑布式请求)→ Bundle(包体积)→ Server(服务端)→ Micro(微优化)
|
|
269
|
+
3. **避免过度优化** - 聚焦真实瓶颈
|
|
270
|
+
4. **善用平台能力** - Next.js 内建大量优化
|
|
271
|
+
5. **关注真实用户环境** - 不是只看本机结果
|
|
272
|
+
|
|
273
|
+
**性能思维:**
|
|
274
|
+
|
|
275
|
+
- 串行 `await` 可能就是 waterfall(瀑布式请求)
|
|
276
|
+
- 每个 `import` 都可能引入 bundle(包体积)膨胀
|
|
277
|
+
- 每次无意义重渲染都是浪费
|
|
278
|
+
- server components(服务端组件)能减少下发 JS
|
|
279
|
+
- 用数据说话,不靠猜测
|
|
280
|
+
|
|
281
|
+
---
|
|
282
|
+
|
|
283
|
+
**来源:** Vercel Engineering
|
|
284
|
+
**日期:** 2026 年 1 月
|
|
285
|
+
**版本:** 1.0.0
|
|
286
|
+
**规则总数:** 57 条,分 8 大类
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
Conversion Script: React Best Practices → .agent Format
|
|
4
|
+
Merges 59 individual rules into 8 grouped section files
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import os
|
|
8
|
+
import re
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Dict, List, Tuple
|
|
11
|
+
|
|
12
|
+
# Section metadata from _sections.md
|
|
13
|
+
SECTIONS = {
|
|
14
|
+
'async': {
|
|
15
|
+
'number': 1,
|
|
16
|
+
'title': 'Eliminating Waterfalls',
|
|
17
|
+
'impact': 'CRITICAL',
|
|
18
|
+
'description': 'Waterfalls are the #1 performance killer. Each sequential await adds full network latency. Eliminating them yields the largest gains.'
|
|
19
|
+
},
|
|
20
|
+
'bundle': {
|
|
21
|
+
'number': 2,
|
|
22
|
+
'title': 'Bundle Size Optimization',
|
|
23
|
+
'impact': 'CRITICAL',
|
|
24
|
+
'description': 'Reducing initial bundle size improves Time to Interactive and Largest Contentful Paint.'
|
|
25
|
+
},
|
|
26
|
+
'server': {
|
|
27
|
+
'number': 3,
|
|
28
|
+
'title': 'Server-Side Performance',
|
|
29
|
+
'impact': 'HIGH',
|
|
30
|
+
'description': 'Optimizing server-side rendering and data fetching eliminates server-side waterfalls and reduces response times.'
|
|
31
|
+
},
|
|
32
|
+
'client': {
|
|
33
|
+
'number': 4,
|
|
34
|
+
'title': 'Client-Side Data Fetching',
|
|
35
|
+
'impact': 'MEDIUM-HIGH',
|
|
36
|
+
'description': 'Automatic deduplication and efficient data fetching patterns reduce redundant network requests.'
|
|
37
|
+
},
|
|
38
|
+
'rerender': {
|
|
39
|
+
'number': 5,
|
|
40
|
+
'title': 'Re-render Optimization',
|
|
41
|
+
'impact': 'MEDIUM',
|
|
42
|
+
'description': 'Reducing unnecessary re-renders minimizes wasted computation and improves UI responsiveness.'
|
|
43
|
+
},
|
|
44
|
+
'rendering': {
|
|
45
|
+
'number': 6,
|
|
46
|
+
'title': 'Rendering Performance',
|
|
47
|
+
'impact': 'MEDIUM',
|
|
48
|
+
'description': 'Optimizing the rendering process reduces the work the browser needs to do.'
|
|
49
|
+
},
|
|
50
|
+
'js': {
|
|
51
|
+
'number': 7,
|
|
52
|
+
'title': 'JavaScript Performance',
|
|
53
|
+
'impact': 'LOW-MEDIUM',
|
|
54
|
+
'description': 'Micro-optimizations for hot paths can add up to meaningful improvements.'
|
|
55
|
+
},
|
|
56
|
+
'advanced': {
|
|
57
|
+
'number': 8,
|
|
58
|
+
'title': 'Advanced Patterns',
|
|
59
|
+
'impact': 'VARIABLE',
|
|
60
|
+
'description': 'Advanced patterns for specific cases that require careful implementation.'
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def parse_frontmatter(content: str) -> Tuple[Dict, str]:
|
|
66
|
+
"""Parse markdown frontmatter and body"""
|
|
67
|
+
if not content.startswith('---'):
|
|
68
|
+
return {}, content
|
|
69
|
+
|
|
70
|
+
parts = content.split('---', 2)
|
|
71
|
+
if len(parts) < 3:
|
|
72
|
+
return {}, content
|
|
73
|
+
|
|
74
|
+
# Parse YAML frontmatter manually (simple key: value)
|
|
75
|
+
frontmatter = {}
|
|
76
|
+
for line in parts[1].strip().split('\n'):
|
|
77
|
+
if ':' in line:
|
|
78
|
+
key, value = line.split(':', 1)
|
|
79
|
+
frontmatter[key.strip()] = value.strip()
|
|
80
|
+
|
|
81
|
+
body = parts[2].strip()
|
|
82
|
+
return frontmatter, body
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def parse_rule_file(filepath: Path) -> Dict:
|
|
86
|
+
"""Parse a single rule file"""
|
|
87
|
+
with open(filepath, 'r', encoding='utf-8') as f:
|
|
88
|
+
content = f.read()
|
|
89
|
+
|
|
90
|
+
frontmatter, body = parse_frontmatter(content)
|
|
91
|
+
|
|
92
|
+
# Extract prefix from filename
|
|
93
|
+
filename = filepath.stem
|
|
94
|
+
prefix = filename.split('-')[0]
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
'filename': filepath.name,
|
|
98
|
+
'prefix': prefix,
|
|
99
|
+
'title': frontmatter.get('title', filename),
|
|
100
|
+
'impact': frontmatter.get('impact', ''),
|
|
101
|
+
'impactDescription': frontmatter.get('impactDescription', ''),
|
|
102
|
+
'tags': frontmatter.get('tags', ''),
|
|
103
|
+
'body': body,
|
|
104
|
+
'frontmatter': frontmatter
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def group_rules_by_section(rules_dir: Path) -> Dict[str, List[Dict]]:
|
|
109
|
+
"""Group all rules by their section prefix"""
|
|
110
|
+
grouped = {prefix: [] for prefix in SECTIONS.keys()}
|
|
111
|
+
|
|
112
|
+
for rule_file in sorted(rules_dir.glob('*.md')):
|
|
113
|
+
# Skip special files
|
|
114
|
+
if rule_file.name.startswith('_'):
|
|
115
|
+
continue
|
|
116
|
+
|
|
117
|
+
rule = parse_rule_file(rule_file)
|
|
118
|
+
prefix = rule['prefix']
|
|
119
|
+
|
|
120
|
+
if prefix in grouped:
|
|
121
|
+
grouped[prefix].append(rule)
|
|
122
|
+
else:
|
|
123
|
+
print(f"[WARNING] Unknown prefix '{prefix}' in file: {rule_file.name}")
|
|
124
|
+
|
|
125
|
+
return grouped
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def generate_section_file(section_prefix: str, rules: List[Dict], output_dir: Path):
|
|
129
|
+
"""Generate a merged section file"""
|
|
130
|
+
if not rules:
|
|
131
|
+
print(f"[WARNING] No rules found for section: {section_prefix}")
|
|
132
|
+
return
|
|
133
|
+
|
|
134
|
+
section_meta = SECTIONS[section_prefix]
|
|
135
|
+
section_num = section_meta['number']
|
|
136
|
+
section_title = section_meta['title']
|
|
137
|
+
impact = section_meta['impact']
|
|
138
|
+
description = section_meta['description']
|
|
139
|
+
|
|
140
|
+
# Sort rules by title
|
|
141
|
+
rules.sort(key=lambda r: r['title'])
|
|
142
|
+
|
|
143
|
+
# Build content
|
|
144
|
+
content = f"""# {section_num}. {section_title}
|
|
145
|
+
|
|
146
|
+
> **Impact:** {impact}
|
|
147
|
+
> **Focus:** {description}
|
|
148
|
+
|
|
149
|
+
---
|
|
150
|
+
|
|
151
|
+
## Overview
|
|
152
|
+
|
|
153
|
+
This section contains **{len(rules)} rules** focused on {section_title.lower()}.
|
|
154
|
+
|
|
155
|
+
"""
|
|
156
|
+
|
|
157
|
+
# Add each rule
|
|
158
|
+
for i, rule in enumerate(rules, 1):
|
|
159
|
+
rule_id = f"{section_num}.{i}"
|
|
160
|
+
title = rule['title']
|
|
161
|
+
rule_impact = rule['impact']
|
|
162
|
+
tags = rule['tags']
|
|
163
|
+
body = rule['body']
|
|
164
|
+
|
|
165
|
+
content += f"""---
|
|
166
|
+
|
|
167
|
+
## Rule {rule_id}: {title}
|
|
168
|
+
|
|
169
|
+
"""
|
|
170
|
+
|
|
171
|
+
if rule_impact:
|
|
172
|
+
content += f"**Impact:** {rule_impact} \n"
|
|
173
|
+
|
|
174
|
+
if tags:
|
|
175
|
+
content += f"**Tags:** {tags} \n"
|
|
176
|
+
|
|
177
|
+
content += f"\n{body}\n\n"
|
|
178
|
+
|
|
179
|
+
# Write file
|
|
180
|
+
output_file = output_dir / f"{section_num}-{section_prefix}-{section_title.lower().replace(' ', '-')}.md"
|
|
181
|
+
output_file.write_text(content, encoding='utf-8')
|
|
182
|
+
print(f"[OK] Generated: {output_file.name} ({len(rules)} rules)")
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def main():
|
|
186
|
+
"""Main conversion function"""
|
|
187
|
+
# Paths
|
|
188
|
+
base_dir = Path(__file__).parent.parent.parent.parent.parent
|
|
189
|
+
rules_dir = base_dir / "others/agent-skills/skills/react-best-practices/rules"
|
|
190
|
+
output_dir = base_dir / ".agent/skills/react-best-practices"
|
|
191
|
+
|
|
192
|
+
print(f"[*] Reading rules from: {rules_dir}")
|
|
193
|
+
print(f"[*] Output to: {output_dir}")
|
|
194
|
+
print()
|
|
195
|
+
|
|
196
|
+
# Check if rules directory exists
|
|
197
|
+
if not rules_dir.exists():
|
|
198
|
+
print(f"[ERROR] Rules directory not found: {rules_dir}")
|
|
199
|
+
return
|
|
200
|
+
|
|
201
|
+
# Group rules
|
|
202
|
+
print("[*] Grouping rules by section...")
|
|
203
|
+
grouped_rules = group_rules_by_section(rules_dir)
|
|
204
|
+
|
|
205
|
+
# Stats
|
|
206
|
+
total_rules = sum(len(rules) for rules in grouped_rules.values())
|
|
207
|
+
print(f"[*] Found {total_rules} total rules")
|
|
208
|
+
print()
|
|
209
|
+
|
|
210
|
+
# Generate section files
|
|
211
|
+
print("[*] Generating section files...")
|
|
212
|
+
for section_prefix in SECTIONS.keys():
|
|
213
|
+
rules = grouped_rules[section_prefix]
|
|
214
|
+
generate_section_file(section_prefix, rules, output_dir)
|
|
215
|
+
|
|
216
|
+
print()
|
|
217
|
+
print("[SUCCESS] Conversion complete!")
|
|
218
|
+
print(f"[*] Generated 8 section files from {total_rules} rules")
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
if __name__ == '__main__':
|
|
222
|
+
main()
|