@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.
Files changed (237) hide show
  1. package/.agent/.shared/ui-ux-pro-max/data/charts.csv +26 -0
  2. package/.agent/.shared/ui-ux-pro-max/data/colors.csv +97 -0
  3. package/.agent/.shared/ui-ux-pro-max/data/icons.csv +101 -0
  4. package/.agent/.shared/ui-ux-pro-max/data/landing.csv +31 -0
  5. package/.agent/.shared/ui-ux-pro-max/data/products.csv +97 -0
  6. package/.agent/.shared/ui-ux-pro-max/data/prompts.csv +24 -0
  7. package/.agent/.shared/ui-ux-pro-max/data/react-performance.csv +45 -0
  8. package/.agent/.shared/ui-ux-pro-max/data/stacks/flutter.csv +53 -0
  9. package/.agent/.shared/ui-ux-pro-max/data/stacks/html-tailwind.csv +56 -0
  10. package/.agent/.shared/ui-ux-pro-max/data/stacks/jetpack-compose.csv +53 -0
  11. package/.agent/.shared/ui-ux-pro-max/data/stacks/nextjs.csv +53 -0
  12. package/.agent/.shared/ui-ux-pro-max/data/stacks/nuxt-ui.csv +51 -0
  13. package/.agent/.shared/ui-ux-pro-max/data/stacks/nuxtjs.csv +59 -0
  14. package/.agent/.shared/ui-ux-pro-max/data/stacks/react-native.csv +52 -0
  15. package/.agent/.shared/ui-ux-pro-max/data/stacks/react.csv +54 -0
  16. package/.agent/.shared/ui-ux-pro-max/data/stacks/shadcn.csv +61 -0
  17. package/.agent/.shared/ui-ux-pro-max/data/stacks/svelte.csv +54 -0
  18. package/.agent/.shared/ui-ux-pro-max/data/stacks/swiftui.csv +51 -0
  19. package/.agent/.shared/ui-ux-pro-max/data/stacks/vue.csv +50 -0
  20. package/.agent/.shared/ui-ux-pro-max/data/styles.csv +59 -0
  21. package/.agent/.shared/ui-ux-pro-max/data/typography.csv +58 -0
  22. package/.agent/.shared/ui-ux-pro-max/data/ui-reasoning.csv +101 -0
  23. package/.agent/.shared/ui-ux-pro-max/data/ux-guidelines.csv +100 -0
  24. package/.agent/.shared/ui-ux-pro-max/data/web-interface.csv +31 -0
  25. package/.agent/.shared/ui-ux-pro-max/scripts/core.py +258 -0
  26. package/.agent/.shared/ui-ux-pro-max/scripts/design_system.py +1067 -0
  27. package/.agent/.shared/ui-ux-pro-max/scripts/search.py +106 -0
  28. package/.agent/ARCHITECTURE.md +285 -0
  29. package/.agent/agents/backend-specialist.md +268 -0
  30. package/.agent/agents/code-archaeologist.md +106 -0
  31. package/.agent/agents/database-architect.md +225 -0
  32. package/.agent/agents/debugger.md +225 -0
  33. package/.agent/agents/devops-engineer.md +242 -0
  34. package/.agent/agents/documentation-writer.md +104 -0
  35. package/.agent/agents/explorer-agent.md +73 -0
  36. package/.agent/agents/frontend-specialist.md +618 -0
  37. package/.agent/agents/game-developer.md +162 -0
  38. package/.agent/agents/mobile-developer.md +382 -0
  39. package/.agent/agents/orchestrator.md +438 -0
  40. package/.agent/agents/penetration-tester.md +188 -0
  41. package/.agent/agents/performance-optimizer.md +187 -0
  42. package/.agent/agents/product-manager.md +112 -0
  43. package/.agent/agents/product-owner.md +95 -0
  44. package/.agent/agents/project-planner.md +405 -0
  45. package/.agent/agents/qa-automation-engineer.md +103 -0
  46. package/.agent/agents/security-auditor.md +170 -0
  47. package/.agent/agents/seo-specialist.md +111 -0
  48. package/.agent/agents/test-engineer.md +158 -0
  49. package/.agent/mcp_config.json +12 -0
  50. package/.agent/rules/GEMINI.md +273 -0
  51. package/.agent/scripts/auto_preview.py +148 -0
  52. package/.agent/scripts/checklist.py +217 -0
  53. package/.agent/scripts/session_manager.py +120 -0
  54. package/.agent/scripts/verify_all.py +327 -0
  55. package/.agent/skills/api-patterns/SKILL.md +84 -0
  56. package/.agent/skills/api-patterns/api-style.md +42 -0
  57. package/.agent/skills/api-patterns/auth.md +24 -0
  58. package/.agent/skills/api-patterns/documentation.md +26 -0
  59. package/.agent/skills/api-patterns/graphql.md +41 -0
  60. package/.agent/skills/api-patterns/rate-limiting.md +31 -0
  61. package/.agent/skills/api-patterns/response.md +37 -0
  62. package/.agent/skills/api-patterns/rest.md +40 -0
  63. package/.agent/skills/api-patterns/scripts/api_validator.py +211 -0
  64. package/.agent/skills/api-patterns/security-testing.md +122 -0
  65. package/.agent/skills/api-patterns/trpc.md +41 -0
  66. package/.agent/skills/api-patterns/versioning.md +22 -0
  67. package/.agent/skills/app-builder/SKILL.md +75 -0
  68. package/.agent/skills/app-builder/agent-coordination.md +74 -0
  69. package/.agent/skills/app-builder/feature-building.md +53 -0
  70. package/.agent/skills/app-builder/project-detection.md +34 -0
  71. package/.agent/skills/app-builder/scaffolding.md +118 -0
  72. package/.agent/skills/app-builder/tech-stack.md +40 -0
  73. package/.agent/skills/app-builder/templates/SKILL.md +39 -0
  74. package/.agent/skills/app-builder/templates/astro-static/TEMPLATE.md +76 -0
  75. package/.agent/skills/app-builder/templates/chrome-extension/TEMPLATE.md +92 -0
  76. package/.agent/skills/app-builder/templates/cli-tool/TEMPLATE.md +88 -0
  77. package/.agent/skills/app-builder/templates/electron-desktop/TEMPLATE.md +88 -0
  78. package/.agent/skills/app-builder/templates/express-api/TEMPLATE.md +83 -0
  79. package/.agent/skills/app-builder/templates/flutter-app/TEMPLATE.md +90 -0
  80. package/.agent/skills/app-builder/templates/monorepo-turborepo/TEMPLATE.md +90 -0
  81. package/.agent/skills/app-builder/templates/nextjs-fullstack/TEMPLATE.md +122 -0
  82. package/.agent/skills/app-builder/templates/nextjs-saas/TEMPLATE.md +122 -0
  83. package/.agent/skills/app-builder/templates/nextjs-static/TEMPLATE.md +169 -0
  84. package/.agent/skills/app-builder/templates/nuxt-app/TEMPLATE.md +134 -0
  85. package/.agent/skills/app-builder/templates/python-fastapi/TEMPLATE.md +83 -0
  86. package/.agent/skills/app-builder/templates/react-native-app/TEMPLATE.md +119 -0
  87. package/.agent/skills/architecture/SKILL.md +57 -0
  88. package/.agent/skills/architecture/context-discovery.md +43 -0
  89. package/.agent/skills/architecture/examples.md +94 -0
  90. package/.agent/skills/architecture/pattern-selection.md +68 -0
  91. package/.agent/skills/architecture/patterns-reference.md +50 -0
  92. package/.agent/skills/architecture/trade-off-analysis.md +77 -0
  93. package/.agent/skills/bash-linux/SKILL.md +201 -0
  94. package/.agent/skills/behavioral-modes/SKILL.md +264 -0
  95. package/.agent/skills/brainstorming/SKILL.md +164 -0
  96. package/.agent/skills/brainstorming/dynamic-questioning.md +359 -0
  97. package/.agent/skills/clean-code/SKILL.md +200 -0
  98. package/.agent/skills/code-review-checklist/SKILL.md +125 -0
  99. package/.agent/skills/database-design/SKILL.md +54 -0
  100. package/.agent/skills/database-design/database-selection.md +43 -0
  101. package/.agent/skills/database-design/indexing.md +39 -0
  102. package/.agent/skills/database-design/migrations.md +50 -0
  103. package/.agent/skills/database-design/optimization.md +36 -0
  104. package/.agent/skills/database-design/orm-selection.md +30 -0
  105. package/.agent/skills/database-design/schema-design.md +56 -0
  106. package/.agent/skills/database-design/scripts/schema_validator.py +172 -0
  107. package/.agent/skills/deployment-procedures/SKILL.md +241 -0
  108. package/.agent/skills/doc.md +177 -0
  109. package/.agent/skills/documentation-templates/SKILL.md +194 -0
  110. package/.agent/skills/frontend-design/SKILL.md +418 -0
  111. package/.agent/skills/frontend-design/animation-guide.md +331 -0
  112. package/.agent/skills/frontend-design/color-system.md +307 -0
  113. package/.agent/skills/frontend-design/decision-trees.md +418 -0
  114. package/.agent/skills/frontend-design/motion-graphics.md +306 -0
  115. package/.agent/skills/frontend-design/scripts/accessibility_checker.py +183 -0
  116. package/.agent/skills/frontend-design/scripts/ux_audit.py +727 -0
  117. package/.agent/skills/frontend-design/typography-system.md +345 -0
  118. package/.agent/skills/frontend-design/ux-psychology.md +1118 -0
  119. package/.agent/skills/frontend-design/visual-effects.md +383 -0
  120. package/.agent/skills/game-development/2d-games/SKILL.md +119 -0
  121. package/.agent/skills/game-development/3d-games/SKILL.md +135 -0
  122. package/.agent/skills/game-development/SKILL.md +167 -0
  123. package/.agent/skills/game-development/game-art/SKILL.md +185 -0
  124. package/.agent/skills/game-development/game-audio/SKILL.md +190 -0
  125. package/.agent/skills/game-development/game-design/SKILL.md +129 -0
  126. package/.agent/skills/game-development/mobile-games/SKILL.md +108 -0
  127. package/.agent/skills/game-development/multiplayer/SKILL.md +132 -0
  128. package/.agent/skills/game-development/pc-games/SKILL.md +144 -0
  129. package/.agent/skills/game-development/vr-ar/SKILL.md +123 -0
  130. package/.agent/skills/game-development/web-games/SKILL.md +150 -0
  131. package/.agent/skills/geo-fundamentals/SKILL.md +155 -0
  132. package/.agent/skills/geo-fundamentals/scripts/geo_checker.py +289 -0
  133. package/.agent/skills/i18n-localization/SKILL.md +154 -0
  134. package/.agent/skills/i18n-localization/scripts/i18n_checker.py +241 -0
  135. package/.agent/skills/intelligent-routing/SKILL.md +335 -0
  136. package/.agent/skills/lint-and-validate/SKILL.md +44 -0
  137. package/.agent/skills/lint-and-validate/scripts/lint_runner.py +184 -0
  138. package/.agent/skills/lint-and-validate/scripts/type_coverage.py +173 -0
  139. package/.agent/skills/mcp-builder/SKILL.md +176 -0
  140. package/.agent/skills/mobile-design/SKILL.md +394 -0
  141. package/.agent/skills/mobile-design/decision-trees.md +516 -0
  142. package/.agent/skills/mobile-design/mobile-backend.md +491 -0
  143. package/.agent/skills/mobile-design/mobile-color-system.md +420 -0
  144. package/.agent/skills/mobile-design/mobile-debugging.md +122 -0
  145. package/.agent/skills/mobile-design/mobile-design-thinking.md +355 -0
  146. package/.agent/skills/mobile-design/mobile-navigation.md +458 -0
  147. package/.agent/skills/mobile-design/mobile-performance.md +767 -0
  148. package/.agent/skills/mobile-design/mobile-testing.md +356 -0
  149. package/.agent/skills/mobile-design/mobile-typography.md +432 -0
  150. package/.agent/skills/mobile-design/platform-android.md +666 -0
  151. package/.agent/skills/mobile-design/platform-ios.md +561 -0
  152. package/.agent/skills/mobile-design/scripts/mobile_audit.py +670 -0
  153. package/.agent/skills/mobile-design/touch-psychology.md +537 -0
  154. package/.agent/skills/nextjs-react-expert/1-async-eliminating-waterfalls.md +311 -0
  155. package/.agent/skills/nextjs-react-expert/2-bundle-bundle-size-optimization.md +241 -0
  156. package/.agent/skills/nextjs-react-expert/3-server-server-side-performance.md +489 -0
  157. package/.agent/skills/nextjs-react-expert/4-client-client-side-data-fetching.md +263 -0
  158. package/.agent/skills/nextjs-react-expert/5-rerender-re-render-optimization.md +581 -0
  159. package/.agent/skills/nextjs-react-expert/6-rendering-rendering-performance.md +431 -0
  160. package/.agent/skills/nextjs-react-expert/7-js-javascript-performance.md +683 -0
  161. package/.agent/skills/nextjs-react-expert/8-advanced-advanced-patterns.md +149 -0
  162. package/.agent/skills/nextjs-react-expert/SKILL.md +286 -0
  163. package/.agent/skills/nextjs-react-expert/scripts/convert_rules.py +222 -0
  164. package/.agent/skills/nextjs-react-expert/scripts/react_performance_checker.py +252 -0
  165. package/.agent/skills/nodejs-best-practices/SKILL.md +333 -0
  166. package/.agent/skills/parallel-agents/SKILL.md +194 -0
  167. package/.agent/skills/performance-profiling/SKILL.md +149 -0
  168. package/.agent/skills/performance-profiling/scripts/lighthouse_audit.py +76 -0
  169. package/.agent/skills/plan-writing/SKILL.md +152 -0
  170. package/.agent/skills/powershell-windows/SKILL.md +166 -0
  171. package/.agent/skills/python-patterns/SKILL.md +441 -0
  172. package/.agent/skills/red-team-tactics/SKILL.md +203 -0
  173. package/.agent/skills/rust-pro/SKILL.md +190 -0
  174. package/.agent/skills/seo-fundamentals/SKILL.md +135 -0
  175. package/.agent/skills/seo-fundamentals/scripts/seo_checker.py +215 -0
  176. package/.agent/skills/server-management/SKILL.md +161 -0
  177. package/.agent/skills/systematic-debugging/SKILL.md +114 -0
  178. package/.agent/skills/tailwind-patterns/SKILL.md +269 -0
  179. package/.agent/skills/tdd-workflow/SKILL.md +149 -0
  180. package/.agent/skills/testing-patterns/SKILL.md +178 -0
  181. package/.agent/skills/testing-patterns/scripts/test_runner.py +219 -0
  182. package/.agent/skills/vulnerability-scanner/SKILL.md +276 -0
  183. package/.agent/skills/vulnerability-scanner/checklists.md +131 -0
  184. package/.agent/skills/vulnerability-scanner/scripts/security_scan.py +459 -0
  185. package/.agent/skills/web-design-guidelines/SKILL.md +57 -0
  186. package/.agent/skills/webapp-testing/SKILL.md +187 -0
  187. package/.agent/skills/webapp-testing/scripts/playwright_runner.py +173 -0
  188. package/.agent/workflows/brainstorm.md +113 -0
  189. package/.agent/workflows/create.md +59 -0
  190. package/.agent/workflows/debug.md +103 -0
  191. package/.agent/workflows/deploy.md +176 -0
  192. package/.agent/workflows/enhance.md +63 -0
  193. package/.agent/workflows/orchestrate.md +242 -0
  194. package/.agent/workflows/plan.md +89 -0
  195. package/.agent/workflows/preview.md +80 -0
  196. package/.agent/workflows/restore-localize-compat.md +525 -0
  197. package/.agent/workflows/status.md +86 -0
  198. package/.agent/workflows/test.md +144 -0
  199. package/.agent/workflows/ui-ux-pro-max.md +295 -0
  200. package/AGENT_FLOW.md +609 -0
  201. package/CHANGELOG.md +68 -0
  202. package/LICENSE +21 -0
  203. package/README.md +260 -0
  204. package/bin/adapters/base.js +63 -0
  205. package/bin/adapters/codex.js +391 -0
  206. package/bin/adapters/gemini.js +137 -0
  207. package/bin/ag-kit.js +1336 -0
  208. package/bin/core/builder.js +80 -0
  209. package/bin/core/generator.js +59 -0
  210. package/bin/core/resource-loader.js +64 -0
  211. package/bin/core/transformer.js +208 -0
  212. package/bin/interactive.js +65 -0
  213. package/bin/utils/atomic-writer.js +97 -0
  214. package/bin/utils/git-helper.js +68 -0
  215. package/bin/utils/managed-block.js +65 -0
  216. package/bin/utils/manifest.js +241 -0
  217. package/bin/utils.js +82 -0
  218. package/docs/codex-rules-template.md +36 -0
  219. package/docs/mapping-spec.md +68 -0
  220. package/docs/multi-target-adapter.md +80 -0
  221. package/docs/official/README.md +53 -0
  222. package/docs/official/antigravity/agent-modes-settings.md +64 -0
  223. package/docs/official/antigravity/rules-workflows.md +96 -0
  224. package/docs/official/antigravity/skills.md +147 -0
  225. package/docs/official/codex/agents-md.md +119 -0
  226. package/docs/official/codex/config-advanced.md +358 -0
  227. package/docs/official/codex/config-basic.md +141 -0
  228. package/docs/official/codex/config-reference.md +223 -0
  229. package/docs/official/codex/config-sample.md +216 -0
  230. package/docs/official/codex/mcp.md +107 -0
  231. package/docs/official/codex/rules.md +79 -0
  232. package/docs/official/codex/skills.md +114 -0
  233. package/docs/official/sources-index.md +32 -0
  234. package/docs/operations.md +145 -0
  235. package/docs/terminology-style-guide.md +69 -0
  236. package/package.json +51 -0
  237. package/scripts/postinstall-check.js +112 -0
@@ -0,0 +1,311 @@
1
+ # 1. 消除瀑布流
2
+
3
+ > **影响:** 严重(CRITICAL)
4
+ > **重点:** 瀑布流是头号性能杀手。每一次串行 await 都会增加完整网络延迟。消除它们收益最大。
5
+
6
+ ---
7
+
8
+ ## 概览
9
+
10
+ 本节包含 **5 条规则**,专注于消除瀑布流。
11
+
12
+ ---
13
+
14
+ ## 规则 1.1:延迟 await 到需要时
15
+
16
+ **影响:** 高(HIGH)
17
+ **标签:** async(异步), await, conditional(条件), optimization(优化)
18
+
19
+ ## 延迟 await 到需要时
20
+
21
+ 将 `await` 操作移动到实际使用它们的分支,避免阻塞不需要它们的路径。
22
+
23
+ **错误示例(阻塞两个分支):**
24
+
25
+ ```typescript
26
+ async function handleRequest(userId: string, skipProcessing: boolean) {
27
+ const userData = await fetchUserData(userId)
28
+
29
+ if (skipProcessing) {
30
+ // Returns immediately but still waited for userData
31
+ return { skipped: true }
32
+ }
33
+
34
+ // Only this branch uses userData
35
+ return processUserData(userData)
36
+ }
37
+ ```
38
+
39
+ **正确示例(仅在需要时阻塞):**
40
+
41
+ ```typescript
42
+ async function handleRequest(userId: string, skipProcessing: boolean) {
43
+ if (skipProcessing) {
44
+ // Returns immediately without waiting
45
+ return { skipped: true }
46
+ }
47
+
48
+ // Fetch only when needed
49
+ const userData = await fetchUserData(userId)
50
+ return processUserData(userData)
51
+ }
52
+ ```
53
+
54
+ **另一个例子(提前返回优化):**
55
+
56
+ ```typescript
57
+ // Incorrect: always fetches permissions
58
+ async function updateResource(resourceId: string, userId: string) {
59
+ const permissions = await fetchPermissions(userId)
60
+ const resource = await getResource(resourceId)
61
+
62
+ if (!resource) {
63
+ return { error: 'Not found' }
64
+ }
65
+
66
+ if (!permissions.canEdit) {
67
+ return { error: 'Forbidden' }
68
+ }
69
+
70
+ return await updateResourceData(resource, permissions)
71
+ }
72
+
73
+ // Correct: fetches only when needed
74
+ async function updateResource(resourceId: string, userId: string) {
75
+ const resource = await getResource(resourceId)
76
+
77
+ if (!resource) {
78
+ return { error: 'Not found' }
79
+ }
80
+
81
+ const permissions = await fetchPermissions(userId)
82
+
83
+ if (!permissions.canEdit) {
84
+ return { error: 'Forbidden' }
85
+ }
86
+
87
+ return await updateResourceData(resource, permissions)
88
+ }
89
+ ```
90
+
91
+ 当跳过的分支经常发生,或被延后的操作很昂贵时,这种优化价值很大。
92
+
93
+ ---
94
+
95
+ ## 规则 1.2:基于依赖的并行化
96
+
97
+ **影响:** 严重(CRITICAL)
98
+ **标签:** async(异步), parallelization(并行化), dependencies(依赖), better-all
99
+
100
+ ## 基于依赖的并行化
101
+
102
+ 对于存在部分依赖的操作,使用 `better-all` 最大化并行度。它会在最早时刻启动每个任务。
103
+
104
+ **错误示例(profile 不必要地等待 config):**
105
+
106
+ ```typescript
107
+ const [user, config] = await Promise.all([
108
+ fetchUser(),
109
+ fetchConfig()
110
+ ])
111
+ const profile = await fetchProfile(user.id)
112
+ ```
113
+
114
+ **正确示例(config 与 profile 并行):**
115
+
116
+ ```typescript
117
+ import { all } from 'better-all'
118
+
119
+ const { user, config, profile } = await all({
120
+ async user() { return fetchUser() },
121
+ async config() { return fetchConfig() },
122
+ async profile() {
123
+ return fetchProfile((await this.$.user).id)
124
+ }
125
+ })
126
+ ```
127
+
128
+ **不引入额外依赖的替代方案:**
129
+
130
+ 也可以先创建全部 Promise,最后再 `Promise.all()`。
131
+
132
+ ```typescript
133
+ const userPromise = fetchUser()
134
+ const profilePromise = userPromise.then(user => fetchProfile(user.id))
135
+
136
+ const [user, config, profile] = await Promise.all([
137
+ userPromise,
138
+ fetchConfig(),
139
+ profilePromise
140
+ ])
141
+ ```
142
+
143
+ 参考:<https://github.com/shuding/better-all>
144
+
145
+ ---
146
+
147
+ ## 规则 1.3:防止 API 路由中的瀑布链
148
+
149
+ **影响:** 严重(CRITICAL)
150
+ **标签:** api-routes, server-actions, waterfalls, parallelization
151
+
152
+ ## 防止 API 路由中的瀑布链
153
+
154
+ 在 API 路由与 Server Actions 中,立即启动独立操作,即使暂时不 await。
155
+
156
+ **错误示例(config 等待 auth,data 等待两者):**
157
+
158
+ ```typescript
159
+ export async function GET(request: Request) {
160
+ const session = await auth()
161
+ const config = await fetchConfig()
162
+ const data = await fetchData(session.user.id)
163
+ return Response.json({ data, config })
164
+ }
165
+ ```
166
+
167
+ **正确示例(auth 与 config 立即开始):**
168
+
169
+ ```typescript
170
+ export async function GET(request: Request) {
171
+ const sessionPromise = auth()
172
+ const configPromise = fetchConfig()
173
+ const session = await sessionPromise
174
+ const [config, data] = await Promise.all([
175
+ configPromise,
176
+ fetchData(session.user.id)
177
+ ])
178
+ return Response.json({ data, config })
179
+ }
180
+ ```
181
+
182
+ 对更复杂依赖链,使用 `better-all` 自动最大化并行度(见“基于依赖的并行化”)。
183
+
184
+ ---
185
+
186
+ ## 规则 1.4:独立操作使用 Promise.all()
187
+
188
+ **影响:** 严重(CRITICAL)
189
+ **标签:** async(异步), parallelization(并行化), promises, waterfalls
190
+
191
+ ## 独立操作使用 Promise.all()
192
+
193
+ 当异步操作之间无依赖时,用 `Promise.all()` 并发执行。
194
+
195
+ **错误示例(串行执行,3 次往返):**
196
+
197
+ ```typescript
198
+ const user = await fetchUser()
199
+ const posts = await fetchPosts()
200
+ const comments = await fetchComments()
201
+ ```
202
+
203
+ **正确示例(并行执行,1 次往返):**
204
+
205
+ ```typescript
206
+ const [user, posts, comments] = await Promise.all([
207
+ fetchUser(),
208
+ fetchPosts(),
209
+ fetchComments()
210
+ ])
211
+ ```
212
+
213
+ ---
214
+
215
+ ## 规则 1.5:策略性 Suspense 边界
216
+
217
+ **影响:** 高(HIGH)
218
+ **标签:** async(异步), suspense, streaming(流式), layout-shift(布局偏移)
219
+
220
+ ## 策略性 Suspense 边界
221
+
222
+ 不要在 async 组件中先 await 数据再返回 JSX。用 Suspense 边界让外层 UI 先显示、数据后流入。
223
+
224
+ **错误示例(外层被数据阻塞):**
225
+
226
+ ```tsx
227
+ async function Page() {
228
+ const data = await fetchData() // Blocks entire page
229
+
230
+ return (
231
+ <div>
232
+ <div>Sidebar</div>
233
+ <div>Header</div>
234
+ <div>
235
+ <DataDisplay data={data} />
236
+ </div>
237
+ <div>Footer</div>
238
+ </div>
239
+ )
240
+ }
241
+ ```
242
+
243
+ 整个布局会等待数据,即便只有中间区域需要它。
244
+
245
+ **正确示例(外层立即渲染,数据流式进入):**
246
+
247
+ ```tsx
248
+ function Page() {
249
+ return (
250
+ <div>
251
+ <div>Sidebar</div>
252
+ <div>Header</div>
253
+ <div>
254
+ <Suspense fallback={<Skeleton />}>
255
+ <DataDisplay />
256
+ </Suspense>
257
+ </div>
258
+ <div>Footer</div>
259
+ </div>
260
+ )
261
+ }
262
+
263
+ async function DataDisplay() {
264
+ const data = await fetchData() // Only blocks this component
265
+ return <div>{data.content}</div>
266
+ }
267
+ ```
268
+
269
+ Sidebar、Header、Footer 立即渲染,只有 DataDisplay 等待数据。
270
+
271
+ **替代方案(跨组件共享 Promise):**
272
+
273
+ ```tsx
274
+ function Page() {
275
+ // Start fetch immediately, but don't await
276
+ const dataPromise = fetchData()
277
+
278
+ return (
279
+ <div>
280
+ <div>Sidebar</div>
281
+ <div>Header</div>
282
+ <Suspense fallback={<Skeleton />}>
283
+ <DataDisplay dataPromise={dataPromise} />
284
+ <DataSummary dataPromise={dataPromise} />
285
+ </Suspense>
286
+ <div>Footer</div>
287
+ </div>
288
+ )
289
+ }
290
+
291
+ function DataDisplay({ dataPromise }: { dataPromise: Promise<Data> }) {
292
+ const data = use(dataPromise) // Unwraps the promise
293
+ return <div>{data.content}</div>
294
+ }
295
+
296
+ function DataSummary({ dataPromise }: { dataPromise: Promise<Data> }) {
297
+ const data = use(dataPromise) // Reuses the same promise
298
+ return <div>{data.summary}</div>
299
+ }
300
+ ```
301
+
302
+ 两个组件共享同一个 promise,因此只发生一次请求。布局立即渲染,两个组件一起等待。
303
+
304
+ **何时不使用该模式:**
305
+
306
+ - 布局决策所需的关键数据(影响定位)
307
+ - 首屏(Above the fold)对 SEO 至关重要的内容
308
+ - 极小且很快的查询(Suspense 开销不值得)
309
+ - 当你想避免布局偏移(加载 → 内容跳动)
310
+
311
+ **权衡:** 更快的首屏绘制 vs 潜在的布局偏移。根据你的 UX 优先级选择。
@@ -0,0 +1,241 @@
1
+ # 2. 包体积优化
2
+
3
+ > **影响:** 严重(CRITICAL)
4
+ > **重点:** 减小初始包体积可缩短可交互时间(Time to Interactive,TTI)和最大内容绘制(LCP)。
5
+
6
+ ---
7
+
8
+ ## 概览
9
+
10
+ 本节包含 **5 条规则**,专注于包体积优化。
11
+
12
+ ---
13
+
14
+ ## 规则 2.1: 避免桶文件导入
15
+
16
+ **影响:** 严重(CRITICAL)
17
+ **标签:** bundle, imports, tree-shaking, barrel-files, performance
18
+
19
+ ## 避免桶文件导入
20
+
21
+ 直接从源文件导入,而不是从桶文件导入,以避免加载数千个未使用的模块。**桶文件(Barrel File)**是重新导出多个模块的入口点(例如,`index.js` 执行 `export * from './module'`)。
22
+
23
+ 流行的图标和组件库在入口文件中可能有 **多达 10,000 个重新导出**。对于许多 React 包,**仅导入它们就需要 200-800ms**,影响开发速度和生产环境的冷启动。
24
+
25
+ **为什么 Tree-shaking(摇树优化)没用:** 当库被标记为外部(不捆绑)时,捆绑器无法优化它。如果你捆绑它以启用 Tree-shaking,分析整个模块图会使构建变得大大减慢。
26
+
27
+ **错误示范(导入整个库):**
28
+
29
+ ```tsx
30
+ import { Check, X, Menu } from "lucide-react";
31
+ // 加载 1,583 个模块,开发环境额外耗时 ~2.8s
32
+ // 运行时成本: 每次冷启动 200-800ms
33
+
34
+ import { Button, TextField } from "@mui/material";
35
+ // 加载 2,225 个模块,开发环境额外耗时 ~4.2s
36
+ ```
37
+
38
+ **正确示范(仅导入你需要的):**
39
+
40
+ ```tsx
41
+ import Check from "lucide-react/dist/esm/icons/check";
42
+ import X from "lucide-react/dist/esm/icons/x";
43
+ import Menu from "lucide-react/dist/esm/icons/menu";
44
+ // 仅加载 3 个模块 (~2KB vs ~1MB)
45
+
46
+ import Button from "@mui/material/Button";
47
+ import TextField from "@mui/material/TextField";
48
+ // 仅加载你使用的
49
+ ```
50
+
51
+ **替代方案(Next.js 13.5+):**
52
+
53
+ ```js
54
+ // next.config.js - 使用 optimizePackageImports
55
+ module.exports = {
56
+ experimental: {
57
+ optimizePackageImports: ["lucide-react", "@mui/material"],
58
+ },
59
+ };
60
+
61
+ // 此时你可以保持便捷的桶文件导入:
62
+ import { Check, X, Menu } from "lucide-react";
63
+ // 构建时自动转换为直接导入
64
+ ```
65
+
66
+ 直接导入可提供 15-70% 更快的开发启动速度,28% 更快的构建速度,40% 更快的冷启动速度,以及显著更快的 HMR(热更新)。
67
+
68
+ 常见受影响的库: `lucide-react`, `@mui/material`, `@mui/icons-material`, `@tabler/icons-react`, `react-icons`, `@headlessui/react`, `@radix-ui/react-*`, `lodash`, `ramda`, `date-fns`, `rxjs`, `react-use`。
69
+
70
+ 参考: [Next.js 如何优化包导入](https://vercel.com/blog/how-we-optimized-package-imports-in-next-js)
71
+
72
+ ---
73
+
74
+ ## 规则 2.2: 条件模块加载
75
+
76
+ **影响:** 高(HIGH)
77
+ **标签:** bundle, conditional-loading, lazy-loading
78
+
79
+ ## 条件模块加载
80
+
81
+ 仅当功能激活时才加载大数据或模块。
82
+
83
+ **示例(懒加载动画帧):**
84
+
85
+ ```tsx
86
+ function AnimationPlayer({
87
+ enabled,
88
+ setEnabled,
89
+ }: {
90
+ enabled: boolean;
91
+ setEnabled: React.Dispatch<React.SetStateAction<boolean>>;
92
+ }) {
93
+ const [frames, setFrames] = useState<Frame[] | null>(null);
94
+
95
+ useEffect(() => {
96
+ if (enabled && !frames && typeof window !== "undefined") {
97
+ import("./animation-frames.js")
98
+ .then((mod) => setFrames(mod.frames))
99
+ .catch(() => setEnabled(false));
100
+ }
101
+ }, [enabled, frames, setEnabled]);
102
+
103
+ if (!frames) return <Skeleton />;
104
+ return <Canvas frames={frames} />;
105
+ }
106
+ ```
107
+
108
+ `typeof window !== 'undefined'` 检查防止了此模块被捆绑到 SSR(服务端渲染)中,优化了服务器包体积和构建速度。
109
+
110
+ ---
111
+
112
+ ## 规则 2.3: 推迟非关键的第三方库
113
+
114
+ **影响:** 中(MEDIUM)
115
+ **标签:** bundle, third-party, analytics, defer
116
+
117
+ ## 推迟非关键的第三方库
118
+
119
+ 分析、日志和错误追踪不会阻塞用户交互。在水合(Hydration)之后加载它们。
120
+
121
+ **错误示范(阻塞初始包):**
122
+
123
+ ```tsx
124
+ import { Analytics } from "@vercel/analytics/react";
125
+
126
+ export default function RootLayout({ children }) {
127
+ return (
128
+ <html>
129
+ <body>
130
+ {children}
131
+ <Analytics />
132
+ </body>
133
+ </html>
134
+ );
135
+ }
136
+ ```
137
+
138
+ **正确示范(水合后加载):**
139
+
140
+ ```tsx
141
+ import dynamic from "next/dynamic";
142
+
143
+ const Analytics = dynamic(
144
+ () => import("@vercel/analytics/react").then((m) => m.Analytics),
145
+ { ssr: false },
146
+ );
147
+
148
+ export default function RootLayout({ children }) {
149
+ return (
150
+ <html>
151
+ <body>
152
+ {children}
153
+ <Analytics />
154
+ </body>
155
+ </html>
156
+ );
157
+ }
158
+ ```
159
+
160
+ ---
161
+
162
+ ## 规则 2.4: 对重型组件使用动态导入
163
+
164
+ **影响:** 严重(CRITICAL)
165
+ **标签:** bundle, dynamic-import, code-splitting, next-dynamic
166
+
167
+ ## 对重型组件使用动态导入
168
+
169
+ 使用 `next/dynamic` 懒加载初始渲染不需要的大型组件。
170
+
171
+ **错误示范(Monaco 与主 chunk 捆绑在一起 ~300KB):**
172
+
173
+ ```tsx
174
+ import { MonacoEditor } from "./monaco-editor";
175
+
176
+ function CodePanel({ code }: { code: string }) {
177
+ return <MonacoEditor value={code} />;
178
+ }
179
+ ```
180
+
181
+ **正确示范(Monaco 按需加载):**
182
+
183
+ ```tsx
184
+ import dynamic from "next/dynamic";
185
+
186
+ const MonacoEditor = dynamic(
187
+ () => import("./monaco-editor").then((m) => m.MonacoEditor),
188
+ { ssr: false },
189
+ );
190
+
191
+ function CodePanel({ code }: { code: string }) {
192
+ return <MonacoEditor value={code} />;
193
+ }
194
+ ```
195
+
196
+ ---
197
+
198
+ ## 规则 2.5: 基于用户意图预加载
199
+
200
+ **影响:** 中(MEDIUM)
201
+ **标签:** bundle, preload, user-intent, hover
202
+
203
+ ## 基于用户意图预加载
204
+
205
+ 在需要之前预加载繁重的包,以减少感知延迟。
206
+
207
+ **示例(悬停/聚焦时预加载):**
208
+
209
+ ```tsx
210
+ function EditorButton({ onClick }: { onClick: () => void }) {
211
+ const preload = () => {
212
+ if (typeof window !== "undefined") {
213
+ void import("./monaco-editor");
214
+ }
215
+ };
216
+
217
+ return (
218
+ <button onMouseEnter={preload} onFocus={preload} onClick={onClick}>
219
+ 打开编辑器
220
+ </button>
221
+ );
222
+ }
223
+ ```
224
+
225
+ **示例(当功能标志启用时预加载):**
226
+
227
+ ```tsx
228
+ function FlagsProvider({ children, flags }: Props) {
229
+ useEffect(() => {
230
+ if (flags.editorEnabled && typeof window !== "undefined") {
231
+ void import("./monaco-editor").then((mod) => mod.init());
232
+ }
233
+ }, [flags.editorEnabled]);
234
+
235
+ return (
236
+ <FlagsContext.Provider value={flags}>{children}</FlagsContext.Provider>
237
+ );
238
+ }
239
+ ```
240
+
241
+ `typeof window !== 'undefined'` 检查防止了预加载模块被捆绑到 SSR(服务端渲染)中。