@linyjs/plugin-docs 0.1.0

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 (252) hide show
  1. package/README.md +97 -0
  2. package/docs/api-reference/index.md +341 -0
  3. package/docs/best-practices/error-handling.md +462 -0
  4. package/docs/best-practices/naming-conventions.md +495 -0
  5. package/docs/best-practices/performance.md +532 -0
  6. package/docs/best-practices/security.md +620 -0
  7. package/docs/faq.md +323 -0
  8. package/docs/getting-started/first-plugin.md +358 -0
  9. package/docs/getting-started/installation.md +244 -0
  10. package/docs/getting-started/quick-start.md +174 -0
  11. package/docs/guides/client-module.md +598 -0
  12. package/docs/guides/data-persistence.md +536 -0
  13. package/docs/guides/permissions.md +440 -0
  14. package/docs/guides/server-module.md +371 -0
  15. package/docs/guides/ui-slots.md +475 -0
  16. package/docs/migration/v0-to-v1.md +354 -0
  17. package/examples/analytics-plugin/README.md +416 -0
  18. package/examples/analytics-plugin/dist/client/index.d.ts +36 -0
  19. package/examples/analytics-plugin/dist/client/index.js +173 -0
  20. package/examples/analytics-plugin/dist/index.d.ts +64 -0
  21. package/examples/analytics-plugin/dist/index.js +72 -0
  22. package/examples/analytics-plugin/dist/server/index.d.ts +119 -0
  23. package/examples/analytics-plugin/dist/server/index.js +313 -0
  24. package/examples/analytics-plugin/package.json +28 -0
  25. package/examples/analytics-plugin/src/client/index.tsx +410 -0
  26. package/examples/analytics-plugin/src/index.ts +71 -0
  27. package/examples/analytics-plugin/src/server/index.ts +348 -0
  28. package/examples/analytics-plugin/tsconfig.json +21 -0
  29. package/examples/blog-plugin/README.md +316 -0
  30. package/examples/blog-plugin/dist/client/index.d.ts +22 -0
  31. package/examples/blog-plugin/dist/client/index.js +197 -0
  32. package/examples/blog-plugin/dist/server/index.d.ts +5 -0
  33. package/examples/blog-plugin/dist/server/index.js +220 -0
  34. package/examples/blog-plugin/package.json +24 -0
  35. package/examples/blog-plugin/src/client/index.tsx +405 -0
  36. package/examples/blog-plugin/src/server/index.ts +240 -0
  37. package/examples/blog-plugin/tsconfig.json +16 -0
  38. package/examples/hello-world/README.md +101 -0
  39. package/examples/hello-world/dist/client/index.d.ts +5 -0
  40. package/examples/hello-world/dist/client/index.js +80 -0
  41. package/examples/hello-world/dist/server/index.d.ts +5 -0
  42. package/examples/hello-world/dist/server/index.js +38 -0
  43. package/examples/hello-world/node_modules/.package-lock.json +61 -0
  44. package/examples/hello-world/node_modules/@linyjs/client-module-interface/HOOKS_USAGE.md +281 -0
  45. package/examples/hello-world/node_modules/@linyjs/client-module-interface/IMPLEMENTATION_SUMMARY.md +166 -0
  46. package/examples/hello-world/node_modules/@linyjs/client-module-interface/QUICK_REFERENCE.md +130 -0
  47. package/examples/hello-world/node_modules/@linyjs/client-module-interface/README.md +214 -0
  48. package/examples/hello-world/node_modules/@linyjs/client-module-interface/package.json +26 -0
  49. package/examples/hello-world/node_modules/@linyjs/client-module-interface/src/application.ts +50 -0
  50. package/examples/hello-world/node_modules/@linyjs/client-module-interface/src/comparison-example.tsx +229 -0
  51. package/examples/hello-world/node_modules/@linyjs/client-module-interface/src/di.ts +127 -0
  52. package/examples/hello-world/node_modules/@linyjs/client-module-interface/src/domain.ts +52 -0
  53. package/examples/hello-world/node_modules/@linyjs/client-module-interface/src/hooks-example.tsx +275 -0
  54. package/examples/hello-world/node_modules/@linyjs/client-module-interface/src/hooks.ts +175 -0
  55. package/examples/hello-world/node_modules/@linyjs/client-module-interface/src/index.ts +87 -0
  56. package/examples/hello-world/node_modules/@linyjs/client-module-interface/src/interfaces.ts +117 -0
  57. package/examples/hello-world/node_modules/@linyjs/client-module-interface/src/router.ts +49 -0
  58. package/examples/hello-world/node_modules/@linyjs/client-module-interface/tsconfig.json +10 -0
  59. package/examples/hello-world/node_modules/@linyjs/server-module-interface/package.json +21 -0
  60. package/examples/hello-world/node_modules/@linyjs/server-module-interface/src/application.ts +21 -0
  61. package/examples/hello-world/node_modules/@linyjs/server-module-interface/src/di.ts +127 -0
  62. package/examples/hello-world/node_modules/@linyjs/server-module-interface/src/domain.ts +75 -0
  63. package/examples/hello-world/node_modules/@linyjs/server-module-interface/src/index.ts +55 -0
  64. package/examples/hello-world/node_modules/@linyjs/server-module-interface/src/interfaces.ts +58 -0
  65. package/examples/hello-world/node_modules/@linyjs/server-module-interface/tsconfig.json +8 -0
  66. package/examples/hello-world/node_modules/@types/react/LICENSE +21 -0
  67. package/examples/hello-world/node_modules/@types/react/README.md +15 -0
  68. package/examples/hello-world/node_modules/@types/react/canary.d.ts +129 -0
  69. package/examples/hello-world/node_modules/@types/react/compiler-runtime.d.ts +4 -0
  70. package/examples/hello-world/node_modules/@types/react/experimental.d.ts +184 -0
  71. package/examples/hello-world/node_modules/@types/react/global.d.ts +166 -0
  72. package/examples/hello-world/node_modules/@types/react/index.d.ts +4369 -0
  73. package/examples/hello-world/node_modules/@types/react/jsx-dev-runtime.d.ts +33 -0
  74. package/examples/hello-world/node_modules/@types/react/jsx-runtime.d.ts +24 -0
  75. package/examples/hello-world/node_modules/@types/react/package.json +210 -0
  76. package/examples/hello-world/node_modules/@types/react/ts5.0/canary.d.ts +129 -0
  77. package/examples/hello-world/node_modules/@types/react/ts5.0/experimental.d.ts +184 -0
  78. package/examples/hello-world/node_modules/@types/react/ts5.0/global.d.ts +166 -0
  79. package/examples/hello-world/node_modules/@types/react/ts5.0/index.d.ts +4356 -0
  80. package/examples/hello-world/node_modules/@types/react/ts5.0/jsx-dev-runtime.d.ts +33 -0
  81. package/examples/hello-world/node_modules/@types/react/ts5.0/jsx-runtime.d.ts +24 -0
  82. package/examples/hello-world/node_modules/csstype/LICENSE +19 -0
  83. package/examples/hello-world/node_modules/csstype/README.md +291 -0
  84. package/examples/hello-world/node_modules/csstype/index.d.ts +22569 -0
  85. package/examples/hello-world/node_modules/csstype/index.js.flow +6863 -0
  86. package/examples/hello-world/node_modules/csstype/package.json +70 -0
  87. package/examples/hello-world/node_modules/react/LICENSE +21 -0
  88. package/examples/hello-world/node_modules/react/README.md +37 -0
  89. package/examples/hello-world/node_modules/react/cjs/react-compiler-runtime.development.js +24 -0
  90. package/examples/hello-world/node_modules/react/cjs/react-compiler-runtime.production.js +16 -0
  91. package/examples/hello-world/node_modules/react/cjs/react-compiler-runtime.profiling.js +16 -0
  92. package/examples/hello-world/node_modules/react/cjs/react-jsx-dev-runtime.development.js +338 -0
  93. package/examples/hello-world/node_modules/react/cjs/react-jsx-dev-runtime.production.js +14 -0
  94. package/examples/hello-world/node_modules/react/cjs/react-jsx-dev-runtime.profiling.js +14 -0
  95. package/examples/hello-world/node_modules/react/cjs/react-jsx-dev-runtime.react-server.development.js +370 -0
  96. package/examples/hello-world/node_modules/react/cjs/react-jsx-dev-runtime.react-server.production.js +40 -0
  97. package/examples/hello-world/node_modules/react/cjs/react-jsx-runtime.development.js +352 -0
  98. package/examples/hello-world/node_modules/react/cjs/react-jsx-runtime.production.js +34 -0
  99. package/examples/hello-world/node_modules/react/cjs/react-jsx-runtime.profiling.js +34 -0
  100. package/examples/hello-world/node_modules/react/cjs/react-jsx-runtime.react-server.development.js +370 -0
  101. package/examples/hello-world/node_modules/react/cjs/react-jsx-runtime.react-server.production.js +40 -0
  102. package/examples/hello-world/node_modules/react/cjs/react.development.js +1284 -0
  103. package/examples/hello-world/node_modules/react/cjs/react.production.js +542 -0
  104. package/examples/hello-world/node_modules/react/cjs/react.react-server.development.js +848 -0
  105. package/examples/hello-world/node_modules/react/cjs/react.react-server.production.js +423 -0
  106. package/examples/hello-world/node_modules/react/compiler-runtime.js +14 -0
  107. package/examples/hello-world/node_modules/react/index.js +7 -0
  108. package/examples/hello-world/node_modules/react/jsx-dev-runtime.js +7 -0
  109. package/examples/hello-world/node_modules/react/jsx-dev-runtime.react-server.js +7 -0
  110. package/examples/hello-world/node_modules/react/jsx-runtime.js +7 -0
  111. package/examples/hello-world/node_modules/react/jsx-runtime.react-server.js +7 -0
  112. package/examples/hello-world/node_modules/react/package.json +51 -0
  113. package/examples/hello-world/node_modules/react/react.react-server.js +7 -0
  114. package/examples/hello-world/node_modules/typescript/LICENSE.txt +55 -0
  115. package/examples/hello-world/node_modules/typescript/README.md +50 -0
  116. package/examples/hello-world/node_modules/typescript/SECURITY.md +41 -0
  117. package/examples/hello-world/node_modules/typescript/ThirdPartyNoticeText.txt +193 -0
  118. package/examples/hello-world/node_modules/typescript/bin/tsc +2 -0
  119. package/examples/hello-world/node_modules/typescript/bin/tsserver +2 -0
  120. package/examples/hello-world/node_modules/typescript/lib/_tsc.js +133818 -0
  121. package/examples/hello-world/node_modules/typescript/lib/_tsserver.js +659 -0
  122. package/examples/hello-world/node_modules/typescript/lib/_typingsInstaller.js +222 -0
  123. package/examples/hello-world/node_modules/typescript/lib/cs/diagnosticMessages.generated.json +2122 -0
  124. package/examples/hello-world/node_modules/typescript/lib/de/diagnosticMessages.generated.json +2122 -0
  125. package/examples/hello-world/node_modules/typescript/lib/es/diagnosticMessages.generated.json +2122 -0
  126. package/examples/hello-world/node_modules/typescript/lib/fr/diagnosticMessages.generated.json +2122 -0
  127. package/examples/hello-world/node_modules/typescript/lib/it/diagnosticMessages.generated.json +2122 -0
  128. package/examples/hello-world/node_modules/typescript/lib/ja/diagnosticMessages.generated.json +2122 -0
  129. package/examples/hello-world/node_modules/typescript/lib/ko/diagnosticMessages.generated.json +2122 -0
  130. package/examples/hello-world/node_modules/typescript/lib/lib.d.ts +22 -0
  131. package/examples/hello-world/node_modules/typescript/lib/lib.decorators.d.ts +384 -0
  132. package/examples/hello-world/node_modules/typescript/lib/lib.decorators.legacy.d.ts +22 -0
  133. package/examples/hello-world/node_modules/typescript/lib/lib.dom.asynciterable.d.ts +41 -0
  134. package/examples/hello-world/node_modules/typescript/lib/lib.dom.d.ts +39429 -0
  135. package/examples/hello-world/node_modules/typescript/lib/lib.dom.iterable.d.ts +571 -0
  136. package/examples/hello-world/node_modules/typescript/lib/lib.es2015.collection.d.ts +147 -0
  137. package/examples/hello-world/node_modules/typescript/lib/lib.es2015.core.d.ts +597 -0
  138. package/examples/hello-world/node_modules/typescript/lib/lib.es2015.d.ts +28 -0
  139. package/examples/hello-world/node_modules/typescript/lib/lib.es2015.generator.d.ts +77 -0
  140. package/examples/hello-world/node_modules/typescript/lib/lib.es2015.iterable.d.ts +605 -0
  141. package/examples/hello-world/node_modules/typescript/lib/lib.es2015.promise.d.ts +81 -0
  142. package/examples/hello-world/node_modules/typescript/lib/lib.es2015.proxy.d.ts +128 -0
  143. package/examples/hello-world/node_modules/typescript/lib/lib.es2015.reflect.d.ts +144 -0
  144. package/examples/hello-world/node_modules/typescript/lib/lib.es2015.symbol.d.ts +46 -0
  145. package/examples/hello-world/node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts +326 -0
  146. package/examples/hello-world/node_modules/typescript/lib/lib.es2016.array.include.d.ts +116 -0
  147. package/examples/hello-world/node_modules/typescript/lib/lib.es2016.d.ts +21 -0
  148. package/examples/hello-world/node_modules/typescript/lib/lib.es2016.full.d.ts +23 -0
  149. package/examples/hello-world/node_modules/typescript/lib/lib.es2016.intl.d.ts +31 -0
  150. package/examples/hello-world/node_modules/typescript/lib/lib.es2017.arraybuffer.d.ts +21 -0
  151. package/examples/hello-world/node_modules/typescript/lib/lib.es2017.d.ts +26 -0
  152. package/examples/hello-world/node_modules/typescript/lib/lib.es2017.date.d.ts +31 -0
  153. package/examples/hello-world/node_modules/typescript/lib/lib.es2017.full.d.ts +23 -0
  154. package/examples/hello-world/node_modules/typescript/lib/lib.es2017.intl.d.ts +44 -0
  155. package/examples/hello-world/node_modules/typescript/lib/lib.es2017.object.d.ts +49 -0
  156. package/examples/hello-world/node_modules/typescript/lib/lib.es2017.sharedmemory.d.ts +135 -0
  157. package/examples/hello-world/node_modules/typescript/lib/lib.es2017.string.d.ts +45 -0
  158. package/examples/hello-world/node_modules/typescript/lib/lib.es2017.typedarrays.d.ts +53 -0
  159. package/examples/hello-world/node_modules/typescript/lib/lib.es2018.asyncgenerator.d.ts +77 -0
  160. package/examples/hello-world/node_modules/typescript/lib/lib.es2018.asynciterable.d.ts +53 -0
  161. package/examples/hello-world/node_modules/typescript/lib/lib.es2018.d.ts +24 -0
  162. package/examples/hello-world/node_modules/typescript/lib/lib.es2018.full.d.ts +24 -0
  163. package/examples/hello-world/node_modules/typescript/lib/lib.es2018.intl.d.ts +83 -0
  164. package/examples/hello-world/node_modules/typescript/lib/lib.es2018.promise.d.ts +30 -0
  165. package/examples/hello-world/node_modules/typescript/lib/lib.es2018.regexp.d.ts +37 -0
  166. package/examples/hello-world/node_modules/typescript/lib/lib.es2019.array.d.ts +79 -0
  167. package/examples/hello-world/node_modules/typescript/lib/lib.es2019.d.ts +24 -0
  168. package/examples/hello-world/node_modules/typescript/lib/lib.es2019.full.d.ts +24 -0
  169. package/examples/hello-world/node_modules/typescript/lib/lib.es2019.intl.d.ts +23 -0
  170. package/examples/hello-world/node_modules/typescript/lib/lib.es2019.object.d.ts +33 -0
  171. package/examples/hello-world/node_modules/typescript/lib/lib.es2019.string.d.ts +37 -0
  172. package/examples/hello-world/node_modules/typescript/lib/lib.es2019.symbol.d.ts +24 -0
  173. package/examples/hello-world/node_modules/typescript/lib/lib.es2020.bigint.d.ts +765 -0
  174. package/examples/hello-world/node_modules/typescript/lib/lib.es2020.d.ts +27 -0
  175. package/examples/hello-world/node_modules/typescript/lib/lib.es2020.date.d.ts +42 -0
  176. package/examples/hello-world/node_modules/typescript/lib/lib.es2020.full.d.ts +24 -0
  177. package/examples/hello-world/node_modules/typescript/lib/lib.es2020.intl.d.ts +474 -0
  178. package/examples/hello-world/node_modules/typescript/lib/lib.es2020.number.d.ts +28 -0
  179. package/examples/hello-world/node_modules/typescript/lib/lib.es2020.promise.d.ts +47 -0
  180. package/examples/hello-world/node_modules/typescript/lib/lib.es2020.sharedmemory.d.ts +99 -0
  181. package/examples/hello-world/node_modules/typescript/lib/lib.es2020.string.d.ts +44 -0
  182. package/examples/hello-world/node_modules/typescript/lib/lib.es2020.symbol.wellknown.d.ts +41 -0
  183. package/examples/hello-world/node_modules/typescript/lib/lib.es2021.d.ts +23 -0
  184. package/examples/hello-world/node_modules/typescript/lib/lib.es2021.full.d.ts +24 -0
  185. package/examples/hello-world/node_modules/typescript/lib/lib.es2021.intl.d.ts +166 -0
  186. package/examples/hello-world/node_modules/typescript/lib/lib.es2021.promise.d.ts +48 -0
  187. package/examples/hello-world/node_modules/typescript/lib/lib.es2021.string.d.ts +33 -0
  188. package/examples/hello-world/node_modules/typescript/lib/lib.es2021.weakref.d.ts +78 -0
  189. package/examples/hello-world/node_modules/typescript/lib/lib.es2022.array.d.ts +121 -0
  190. package/examples/hello-world/node_modules/typescript/lib/lib.es2022.d.ts +25 -0
  191. package/examples/hello-world/node_modules/typescript/lib/lib.es2022.error.d.ts +75 -0
  192. package/examples/hello-world/node_modules/typescript/lib/lib.es2022.full.d.ts +24 -0
  193. package/examples/hello-world/node_modules/typescript/lib/lib.es2022.intl.d.ts +145 -0
  194. package/examples/hello-world/node_modules/typescript/lib/lib.es2022.object.d.ts +26 -0
  195. package/examples/hello-world/node_modules/typescript/lib/lib.es2022.regexp.d.ts +39 -0
  196. package/examples/hello-world/node_modules/typescript/lib/lib.es2022.string.d.ts +25 -0
  197. package/examples/hello-world/node_modules/typescript/lib/lib.es2023.array.d.ts +924 -0
  198. package/examples/hello-world/node_modules/typescript/lib/lib.es2023.collection.d.ts +21 -0
  199. package/examples/hello-world/node_modules/typescript/lib/lib.es2023.d.ts +22 -0
  200. package/examples/hello-world/node_modules/typescript/lib/lib.es2023.full.d.ts +24 -0
  201. package/examples/hello-world/node_modules/typescript/lib/lib.es2023.intl.d.ts +56 -0
  202. package/examples/hello-world/node_modules/typescript/lib/lib.es2024.arraybuffer.d.ts +65 -0
  203. package/examples/hello-world/node_modules/typescript/lib/lib.es2024.collection.d.ts +29 -0
  204. package/examples/hello-world/node_modules/typescript/lib/lib.es2024.d.ts +26 -0
  205. package/examples/hello-world/node_modules/typescript/lib/lib.es2024.full.d.ts +24 -0
  206. package/examples/hello-world/node_modules/typescript/lib/lib.es2024.object.d.ts +29 -0
  207. package/examples/hello-world/node_modules/typescript/lib/lib.es2024.promise.d.ts +35 -0
  208. package/examples/hello-world/node_modules/typescript/lib/lib.es2024.regexp.d.ts +25 -0
  209. package/examples/hello-world/node_modules/typescript/lib/lib.es2024.sharedmemory.d.ts +68 -0
  210. package/examples/hello-world/node_modules/typescript/lib/lib.es2024.string.d.ts +29 -0
  211. package/examples/hello-world/node_modules/typescript/lib/lib.es5.d.ts +4601 -0
  212. package/examples/hello-world/node_modules/typescript/lib/lib.es6.d.ts +23 -0
  213. package/examples/hello-world/node_modules/typescript/lib/lib.esnext.array.d.ts +35 -0
  214. package/examples/hello-world/node_modules/typescript/lib/lib.esnext.collection.d.ts +96 -0
  215. package/examples/hello-world/node_modules/typescript/lib/lib.esnext.d.ts +29 -0
  216. package/examples/hello-world/node_modules/typescript/lib/lib.esnext.decorators.d.ts +28 -0
  217. package/examples/hello-world/node_modules/typescript/lib/lib.esnext.disposable.d.ts +193 -0
  218. package/examples/hello-world/node_modules/typescript/lib/lib.esnext.error.d.ts +24 -0
  219. package/examples/hello-world/node_modules/typescript/lib/lib.esnext.float16.d.ts +445 -0
  220. package/examples/hello-world/node_modules/typescript/lib/lib.esnext.full.d.ts +24 -0
  221. package/examples/hello-world/node_modules/typescript/lib/lib.esnext.intl.d.ts +21 -0
  222. package/examples/hello-world/node_modules/typescript/lib/lib.esnext.iterator.d.ts +148 -0
  223. package/examples/hello-world/node_modules/typescript/lib/lib.esnext.promise.d.ts +34 -0
  224. package/examples/hello-world/node_modules/typescript/lib/lib.esnext.sharedmemory.d.ts +25 -0
  225. package/examples/hello-world/node_modules/typescript/lib/lib.scripthost.d.ts +322 -0
  226. package/examples/hello-world/node_modules/typescript/lib/lib.webworker.asynciterable.d.ts +41 -0
  227. package/examples/hello-world/node_modules/typescript/lib/lib.webworker.d.ts +13150 -0
  228. package/examples/hello-world/node_modules/typescript/lib/lib.webworker.importscripts.d.ts +23 -0
  229. package/examples/hello-world/node_modules/typescript/lib/lib.webworker.iterable.d.ts +340 -0
  230. package/examples/hello-world/node_modules/typescript/lib/pl/diagnosticMessages.generated.json +2122 -0
  231. package/examples/hello-world/node_modules/typescript/lib/pt-br/diagnosticMessages.generated.json +2122 -0
  232. package/examples/hello-world/node_modules/typescript/lib/ru/diagnosticMessages.generated.json +2122 -0
  233. package/examples/hello-world/node_modules/typescript/lib/tr/diagnosticMessages.generated.json +2122 -0
  234. package/examples/hello-world/node_modules/typescript/lib/tsc.js +8 -0
  235. package/examples/hello-world/node_modules/typescript/lib/tsserver.js +8 -0
  236. package/examples/hello-world/node_modules/typescript/lib/tsserverlibrary.d.ts +17 -0
  237. package/examples/hello-world/node_modules/typescript/lib/tsserverlibrary.js +21 -0
  238. package/examples/hello-world/node_modules/typescript/lib/typesMap.json +497 -0
  239. package/examples/hello-world/node_modules/typescript/lib/typescript.d.ts +11437 -0
  240. package/examples/hello-world/node_modules/typescript/lib/typescript.js +200276 -0
  241. package/examples/hello-world/node_modules/typescript/lib/typingsInstaller.js +8 -0
  242. package/examples/hello-world/node_modules/typescript/lib/watchGuard.js +53 -0
  243. package/examples/hello-world/node_modules/typescript/lib/zh-cn/diagnosticMessages.generated.json +2122 -0
  244. package/examples/hello-world/node_modules/typescript/lib/zh-tw/diagnosticMessages.generated.json +2122 -0
  245. package/examples/hello-world/node_modules/typescript/package.json +120 -0
  246. package/examples/hello-world/package-lock.json +74 -0
  247. package/examples/hello-world/package.json +23 -0
  248. package/examples/hello-world/src/client/index.tsx +110 -0
  249. package/examples/hello-world/src/server/index.ts +42 -0
  250. package/examples/hello-world/tsconfig.json +16 -0
  251. package/package.json +42 -0
  252. package/src/cli.ts +321 -0
@@ -0,0 +1,532 @@
1
+ # 性能优化
2
+
3
+ 本指南提供 linyjs 插件开发中的性能优化最佳实践。
4
+
5
+ ## 为什么需要性能优化?
6
+
7
+ - ⚡ **提升用户体验** - 减少加载时间和响应时间
8
+ - 💰 **降低资源消耗** - 节省服务器和客户端计算资源
9
+ - 📱 **提高移动端体验** - 在资源受限设备上保持流畅
10
+ - 🎯 **增加用户留存** - 快速响应的应用更容易获得用户喜爱
11
+
12
+ ## 客户端性能优化
13
+
14
+ ### 懒加载组件
15
+
16
+ ```typescript
17
+ // 延迟加载重型组件
18
+ registry.register({
19
+ id: 'heavy-widget',
20
+ position: 'admin-dashboard',
21
+ component: () => {
22
+ const HeavyComponent = React.lazy(() => import('./HeavyComponent'));
23
+ return (
24
+ <React.Suspense fallback={<div>Loading...</div>}>
25
+ <HeavyComponent />
26
+ </React.Suspense>
27
+ );
28
+ }
29
+ });
30
+ ```
31
+
32
+ ### 避免不必要的重渲染
33
+
34
+ ```typescript
35
+ // 使用 React.memo 避免不必要的重渲染
36
+ import { memo } from 'react';
37
+
38
+ const ExpensiveComponent = memo(({ data }: { data: any }) => {
39
+ // 复杂计算...
40
+ return <div>{/* ... */}</div>;
41
+ });
42
+
43
+ // 使用 useMemo 缓存计算结果
44
+ import { useMemo } from 'react';
45
+
46
+ function DataList({ items }: { items: any[] }) {
47
+ const sortedItems = useMemo(() => {
48
+ return [...items].sort((a, b) => a.name.localeCompare(b.name));
49
+ }, [items]);
50
+
51
+ return (
52
+ <ul>
53
+ {sortedItems.map(item => (
54
+ <li key={item.id}>{item.name}</li>
55
+ ))}
56
+ </ul>
57
+ );
58
+ }
59
+
60
+ // 使用 useCallback 缓存函数
61
+ import { useCallback } from 'react';
62
+
63
+ function InteractiveComponent() {
64
+ const handleClick = useCallback(() => {
65
+ console.log('Button clicked');
66
+ }, []);
67
+
68
+ return <button onClick={handleClick}>Click me</button>;
69
+ }
70
+ ```
71
+
72
+ ### 优化状态管理
73
+
74
+ ```typescript
75
+ // 避免频繁的状态更新
76
+ function OptimizedComponent() {
77
+ const [data, setData] = useState<any[]>([]);
78
+
79
+ // ❌ 避免:批量操作中的多次状态更新
80
+ const badApproach = () => {
81
+ items.forEach(item => {
82
+ setData(prev => [...prev, item]); // 多次更新
83
+ });
84
+ };
85
+
86
+ // ✅ 推荐:单次状态更新
87
+ const goodApproach = () => {
88
+ setData(prev => [...prev, ...items]); // 单次更新
89
+ };
90
+
91
+ return <div>{/* ... */}</div>;
92
+ }
93
+
94
+ // 使用批处理更新
95
+ import { unstable_batchedUpdates } from 'react-dom';
96
+
97
+ function BatchUpdateExample() {
98
+ const [count, setCount] = useState(0);
99
+ const [data, setData] = useState([]);
100
+
101
+ const batchUpdate = () => {
102
+ unstable_batchedUpdates(() => {
103
+ setCount(prev => prev + 1);
104
+ setData(prev => [...prev, 'new item']);
105
+ });
106
+ };
107
+
108
+ return <button onClick={batchUpdate}>Batch Update</button>;
109
+ }
110
+ ```
111
+
112
+ ### 图片和资源优化
113
+
114
+ ```typescript
115
+ // 延迟加载图片
116
+ function LazyImage({ src, alt }: { src: string; alt: string }) {
117
+ return (
118
+ <img
119
+ src={src}
120
+ alt={alt}
121
+ loading="lazy"
122
+ width="300"
123
+ height="200"
124
+ />
125
+ );
126
+ }
127
+
128
+ // 响应式图片
129
+ function ResponsiveImage() {
130
+ return (
131
+ <picture>
132
+ <source
133
+ media="(max-width: 768px)"
134
+ srcSet="/images/mobile-version.jpg"
135
+ />
136
+ <source
137
+ media="(min-width: 769px)"
138
+ srcSet="/images/desktop-version.jpg"
139
+ />
140
+ <img src="/images/fallback.jpg" alt="Responsive content" />
141
+ </picture>
142
+ );
143
+ }
144
+ ```
145
+
146
+ ## 服务端性能优化
147
+
148
+ ### 数据库查询优化
149
+
150
+ ```typescript
151
+ // ✅ 推荐:使用索引和投影
152
+ class OptimizedArticleService {
153
+ constructor(private mongodb: IMongoDBService) {}
154
+
155
+ async getArticles() {
156
+ const db = this.mongodb.getDb();
157
+ const collection = db.collection('articles');
158
+
159
+ // 使用索引字段查询
160
+ return collection
161
+ .find({
162
+ status: 'published',
163
+ createdAt: { $gte: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000) }
164
+ })
165
+ .project({
166
+ title: 1,
167
+ excerpt: 1,
168
+ createdAt: 1,
169
+ // 只选择需要的字段
170
+ })
171
+ .sort({ createdAt: -1 })
172
+ .limit(10)
173
+ .toArray();
174
+ }
175
+
176
+ // 添加索引
177
+ async createIndexes() {
178
+ const db = this.mongodb.getDb();
179
+ const collection = db.collection('articles');
180
+
181
+ await collection.createIndex({ status: 1, createdAt: -1 });
182
+ await collection.createIndex({ title: 'text', content: 'text' });
183
+ }
184
+ }
185
+ ```
186
+
187
+ ### 缓存策略
188
+
189
+ ```typescript
190
+ // 简单的内存缓存
191
+ class CachedService {
192
+ private cache = new Map<string, { data: any; timestamp: number }>();
193
+ private TTL = 5 * 60 * 1000; // 5分钟
194
+
195
+ async getData(key: string, fetcher: () => Promise<any>) {
196
+ const cached = this.cache.get(key);
197
+ const now = Date.now();
198
+
199
+ // 检查缓存是否有效
200
+ if (cached && (now - cached.timestamp) < this.TTL) {
201
+ return cached.data;
202
+ }
203
+
204
+ // 获取新数据
205
+ const data = await fetcher();
206
+ this.cache.set(key, { data, timestamp: now });
207
+
208
+ return data;
209
+ }
210
+
211
+ clearCache() {
212
+ this.cache.clear();
213
+ }
214
+ }
215
+
216
+ // 在处理器中使用缓存
217
+ handler: async (params) => {
218
+ const cachedService = new CachedService();
219
+
220
+ return await cachedService.getData('articles', async () => {
221
+ // 实际的数据获取逻辑
222
+ return await fetchArticles();
223
+ });
224
+ }
225
+ ```
226
+
227
+ ### 异步处理
228
+
229
+ ```typescript
230
+ // 处理耗时操作
231
+ class AsyncProcessingService {
232
+ async processLargeDataset() {
233
+ // 分页处理大数据集
234
+ const BATCH_SIZE = 100;
235
+ let processed = 0;
236
+ let hasMore = true;
237
+
238
+ while (hasMore) {
239
+ const batch = await this.getBatchData(processed, BATCH_SIZE);
240
+
241
+ if (batch.length === 0) {
242
+ hasMore = false;
243
+ break;
244
+ }
245
+
246
+ // 并行处理批次
247
+ await Promise.all(
248
+ batch.map(item => this.processItem(item))
249
+ );
250
+
251
+ processed += batch.length;
252
+
253
+ // 避免阻塞事件循环
254
+ await new Promise(resolve => setTimeout(resolve, 1));
255
+ }
256
+
257
+ return { processed };
258
+ }
259
+
260
+ private async getBatchData(offset: number, limit: number) {
261
+ // 获取数据批次
262
+ const db = this.mongodb.getDb();
263
+ return db.collection('items')
264
+ .find()
265
+ .skip(offset)
266
+ .limit(limit)
267
+ .toArray();
268
+ }
269
+
270
+ private async processItem(item: any) {
271
+ // 处理单个项目
272
+ return new Promise(resolve => {
273
+ setTimeout(() => {
274
+ // 模拟处理
275
+ resolve(item);
276
+ }, 10);
277
+ });
278
+ }
279
+ }
280
+ ```
281
+
282
+ ### 连接池和复用
283
+
284
+ ```typescript
285
+ // MongoDB 连接复用
286
+ class DatabaseService {
287
+ private static instance: DatabaseService;
288
+ private db: any;
289
+
290
+ private constructor() {
291
+ // 私有构造函数
292
+ }
293
+
294
+ static getInstance(): DatabaseService {
295
+ if (!DatabaseService.instance) {
296
+ DatabaseService.instance = new DatabaseService();
297
+ }
298
+ return DatabaseService.instance;
299
+ }
300
+
301
+ async getConnection() {
302
+ if (!this.db) {
303
+ // 初始化连接
304
+ this.db = await connectToDatabase();
305
+ }
306
+ return this.db;
307
+ }
308
+ }
309
+ ```
310
+
311
+ ## 网络优化
312
+
313
+ ### 压缩和最小化
314
+
315
+ ```typescript
316
+ // 服务端启用压缩
317
+ handler: async (params, ctx) => {
318
+ const data = await getData();
319
+
320
+ // 启用 Gzip 压缩
321
+ ctx.response.setHeader('Content-Encoding', 'gzip');
322
+ ctx.response.setHeader('Content-Type', 'application/json');
323
+
324
+ return data;
325
+ }
326
+
327
+ // 客户端请求优化
328
+ async function fetchOptimizedData() {
329
+ const response = await fetch('/api/data', {
330
+ headers: {
331
+ 'Accept-Encoding': 'gzip, deflate',
332
+ 'Accept': 'application/json'
333
+ },
334
+ // 避免不必要的预检请求
335
+ mode: 'cors',
336
+ credentials: 'same-origin'
337
+ });
338
+
339
+ return response.json();
340
+ }
341
+ ```
342
+
343
+ ### 请求批处理和缓存
344
+
345
+ ```typescript
346
+ // 请求批处理
347
+ class BatchRequestService {
348
+ private requests: Map<string, Promise<any>> = new Map();
349
+
350
+ async batchRequest<T>(key: string, fetcher: () => Promise<T>): Promise<T> {
351
+ // 如果已有相同请求,复用
352
+ if (this.requests.has(key)) {
353
+ return this.requests.get(key);
354
+ }
355
+
356
+ const promise = fetcher().finally(() => {
357
+ // 请求完成后清理
358
+ this.requests.delete(key);
359
+ });
360
+
361
+ this.requests.set(key, promise);
362
+ return promise;
363
+ }
364
+ }
365
+
366
+ // 使用批处理服务
367
+ const batchService = new BatchRequestService();
368
+
369
+ // 多个组件可能同时请求相同数据
370
+ const data = await batchService.batchRequest('user-profile', async () => {
371
+ return await fetch('/api/user/profile').then(res => res.json());
372
+ });
373
+ ```
374
+
375
+ ## 构建优化
376
+
377
+ ### 代码分割
378
+
379
+ ```typescript
380
+ // 动态导入实现代码分割
381
+ const AdminDashboard = lazy(() => import('./components/AdminDashboard'));
382
+ const UserSettings = lazy(() => import('./components/UserSettings'));
383
+
384
+ // 路由级别的代码分割
385
+ function App() {
386
+ return (
387
+ <Router>
388
+ <Suspense fallback={<LoadingSpinner />}>
389
+ <Routes>
390
+ <Route path="/admin" element={<AdminDashboard />} />
391
+ <Route path="/settings" element={<UserSettings />} />
392
+ </Routes>
393
+ </Suspense>
394
+ </Router>
395
+ );
396
+ }
397
+ ```
398
+
399
+ ### Tree Shaking
400
+
401
+ ```typescript
402
+ // 推荐:只导出需要的功能
403
+ export const utils = {
404
+ formatDate: (date: Date) => date.toLocaleDateString(),
405
+ formatCurrency: (amount: number) => `$${amount.toFixed(2)}`
406
+ };
407
+
408
+ // 推荐:分别导出每个功能
409
+ export function formatDate(date: Date) {
410
+ return date.toLocaleDateString();
411
+ }
412
+
413
+ export function formatCurrency(amount: number) {
414
+ return `$${amount.toFixed(2)}`;
415
+ }
416
+
417
+ // 导入时只引入需要的功能
418
+ import { formatDate } from './utils';
419
+ ```
420
+
421
+ ## 性能监控
422
+
423
+ ### 客户端性能监控
424
+
425
+ ```typescript
426
+ // 监控关键性能指标
427
+ function monitorPerformance() {
428
+ // 首次内容绘制 (FCP)
429
+ new PerformanceObserver((list) => {
430
+ for (const entry of list.getEntries()) {
431
+ console.log('FCP:', entry.startTime);
432
+ // 发送到监控服务
433
+ reportMetric('fcp', entry.startTime);
434
+ }
435
+ }).observe({ entryTypes: ['paint'] });
436
+
437
+ // 最大内容绘制 (LCP)
438
+ new PerformanceObserver((list) => {
439
+ const entries = list.getEntries();
440
+ const lastEntry = entries[entries.length - 1];
441
+ console.log('LCP:', lastEntry.startTime);
442
+ reportMetric('lcp', lastEntry.startTime);
443
+ }).observe({ entryTypes: ['largest-contentful-paint'] });
444
+ }
445
+
446
+ function reportMetric(name: string, value: number) {
447
+ fetch('/api/metrics', {
448
+ method: 'POST',
449
+ body: JSON.stringify({ name, value, timestamp: Date.now() })
450
+ });
451
+ }
452
+ ```
453
+
454
+ ### 服务端性能监控
455
+
456
+ ```typescript
457
+ // 监控请求处理时间
458
+ class PerformanceMiddleware implements IMiddlewareService {
459
+ async process(payload: any, ctx: any, next: () => Promise<any>) {
460
+ const startTime = Date.now();
461
+
462
+ try {
463
+ const result = await next();
464
+ const duration = Date.now() - startTime;
465
+
466
+ // 记录性能指标
467
+ this.recordMetric('request-duration', duration, {
468
+ method: ctx.request?.method,
469
+ path: ctx.request?.url
470
+ });
471
+
472
+ return result;
473
+ } catch (error) {
474
+ const duration = Date.now() - startTime;
475
+
476
+ // 记录错误指标
477
+ this.recordMetric('request-error-duration', duration, {
478
+ error: error.message,
479
+ method: ctx.request?.method,
480
+ path: ctx.request?.url
481
+ });
482
+
483
+ throw error;
484
+ }
485
+ }
486
+
487
+ private recordMetric(name: string, value: number, tags: any) {
488
+ console.log(`[PERF] ${name}: ${value}ms`, tags);
489
+ // 发送到监控系统
490
+ }
491
+ }
492
+ ```
493
+
494
+ ## 性能优化检查清单
495
+
496
+ ### 开发阶段
497
+
498
+ - ✅ 启用 React 开发工具检测不必要重渲染
499
+ - ✅ 使用 Chrome DevTools 分析性能瓶颈
500
+ - ✅ 监控组件渲染时间
501
+ - ✅ 检查包大小(bundle size)
502
+
503
+ ### 构建阶段
504
+
505
+ - ✅ 启用代码分割
506
+ - ✅ 配置 Tree Shaking
507
+ - ✅ 压缩静态资源
508
+ - ✅ 生成 Source Maps
509
+
510
+ ### 部署阶段
511
+
512
+ - ✅ 启用 Gzip/Brotli 压缩
513
+ - ✅ 配置 CDN 缓存
514
+ - ✅ 使用 HTTP/2
515
+ - ✅ 启用服务端缓存
516
+
517
+ ## 完整示例
518
+
519
+ 查看性能优化实践:
520
+
521
+ - 📁 [Analytics Plugin 示例](../../examples/analytics-plugin/) - 性能监控实现
522
+ - 📁 [Blog Plugin 示例](../../examples/blog-plugin/) - 数据库查询优化
523
+
524
+ ## 相关文档
525
+
526
+ - 📝 [命名规范](./naming-conventions.md)
527
+ - ⚠️ [错误处理](./error-handling.md)
528
+ - 🔒 [安全性](./security.md)
529
+
530
+ ---
531
+
532
+ **上一篇**: [错误处理](./error-handling.md) | **下一篇**: [安全性](./security.md)