@modern-js/main-doc 0.0.0-next-20221116121040

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 (531) hide show
  1. package/.eslintrc.js +12 -0
  2. package/CHANGELOG.md +15 -0
  3. package/LICENSE +144 -0
  4. package/en/configure/app/dev/asset-prefix.md +13 -0
  5. package/en/configure/app/dev/hmr.md +13 -0
  6. package/en/configure/app/dev/https.md +13 -0
  7. package/en/configure/app/dev/port.md +13 -0
  8. package/en/configure/app/dev/progress-bar.md +13 -0
  9. package/en/configure/app/dev/start-url.md +13 -0
  10. package/en/configure/app/html/app-icon.md +13 -0
  11. package/en/configure/app/html/crossorigin.md +13 -0
  12. package/en/configure/app/html/disable-html-folder.md +13 -0
  13. package/en/configure/app/html/favicon-by-entries.md +13 -0
  14. package/en/configure/app/html/favicon.md +13 -0
  15. package/en/configure/app/html/inject-by-entries.md +13 -0
  16. package/en/configure/app/html/inject.md +13 -0
  17. package/en/configure/app/html/meta-by-entries.md +13 -0
  18. package/en/configure/app/html/meta.md +13 -0
  19. package/en/configure/app/html/mount-id.md +13 -0
  20. package/en/configure/app/html/template-by-entries.md +13 -0
  21. package/en/configure/app/html/template-parameters-by-entries.md +13 -0
  22. package/en/configure/app/html/template-parameters.md +13 -0
  23. package/en/configure/app/html/template.md +13 -0
  24. package/en/configure/app/html/title-by-entries.md +13 -0
  25. package/en/configure/app/html/title.md +13 -0
  26. package/en/configure/app/output/asset-prefix.md +13 -0
  27. package/en/configure/app/output/assets-retry.md +13 -0
  28. package/en/configure/app/output/charset.md +13 -0
  29. package/en/configure/app/output/clean-dist-path.md +13 -0
  30. package/en/configure/app/output/convert-to-rem.md +13 -0
  31. package/en/configure/app/output/copy.md +13 -0
  32. package/en/configure/app/output/css-module-local-ident-name.md +13 -0
  33. package/en/configure/app/output/data-uri-limit.md +13 -0
  34. package/en/configure/app/output/disable-css-module-extension.md +13 -0
  35. package/en/configure/app/output/disable-filename-hash.md +13 -0
  36. package/en/configure/app/output/disable-inline-runtime-chunk.md +13 -0
  37. package/en/configure/app/output/disable-minimize.md +13 -0
  38. package/en/configure/app/output/disable-source-map.md +13 -0
  39. package/en/configure/app/output/dist-path.md +13 -0
  40. package/en/configure/app/output/enable-asset-fallback.md +13 -0
  41. package/en/configure/app/output/enable-asset-manifest.md +13 -0
  42. package/en/configure/app/output/enable-css-module-tsdeclaration.md +13 -0
  43. package/en/configure/app/output/enable-inline-scripts.md +13 -0
  44. package/en/configure/app/output/enable-inline-styles.md +13 -0
  45. package/en/configure/app/output/enable-latest-decorators.md +13 -0
  46. package/en/configure/app/output/externals.md +13 -0
  47. package/en/configure/app/output/filename.md +13 -0
  48. package/en/configure/app/output/legal-comments.md +13 -0
  49. package/en/configure/app/output/override-browserslist.md +13 -0
  50. package/en/configure/app/output/polyfill.md +13 -0
  51. package/en/configure/app/output/svg-default-export.md +13 -0
  52. package/en/configure/app/performance/build-cache.md +13 -0
  53. package/en/configure/app/performance/bundle-analyze.md +13 -0
  54. package/en/configure/app/performance/chunk-split.md +13 -0
  55. package/en/configure/app/performance/print-file-size.md +13 -0
  56. package/en/configure/app/performance/profile.md +13 -0
  57. package/en/configure/app/performance/remove-console.md +13 -0
  58. package/en/configure/app/performance/remove-moment-locale.md +13 -0
  59. package/en/configure/app/security/sri.md +13 -0
  60. package/en/configure/app/source/alias.md +13 -0
  61. package/en/configure/app/source/compile-js-data-uri.md +13 -0
  62. package/en/configure/app/source/define.md +13 -0
  63. package/en/configure/app/source/global-vars.md +13 -0
  64. package/en/configure/app/source/include.md +13 -0
  65. package/en/configure/app/source/module-scopes.md +13 -0
  66. package/en/configure/app/source/pre-entry.md +13 -0
  67. package/en/configure/app/source/resolve-extension-prefix.md +13 -0
  68. package/en/configure/app/source/resolve-main-fields.md +13 -0
  69. package/en/configure/app/tools/autoprefixer.md +13 -0
  70. package/en/configure/app/tools/babel.md +13 -0
  71. package/en/configure/app/tools/css-extract.md +13 -0
  72. package/en/configure/app/tools/css-loader.md +13 -0
  73. package/en/configure/app/tools/dev-server.md +13 -0
  74. package/en/configure/app/tools/html-plugin.md +13 -0
  75. package/en/configure/app/tools/inspector.md +13 -0
  76. package/en/configure/app/tools/less.md +13 -0
  77. package/en/configure/app/tools/minify-css.md +13 -0
  78. package/en/configure/app/tools/postcss.md +13 -0
  79. package/en/configure/app/tools/pug.md +13 -0
  80. package/en/configure/app/tools/sass.md +13 -0
  81. package/en/configure/app/tools/style-loader.md +13 -0
  82. package/en/configure/app/tools/styled-components.md +13 -0
  83. package/en/configure/app/tools/terser.md +13 -0
  84. package/en/configure/app/tools/ts-checker.md +13 -0
  85. package/en/configure/app/tools/ts-loader.md +13 -0
  86. package/en/configure/app/tools/webpack-chain.md +13 -0
  87. package/en/configure/app/tools/webpack.md +13 -0
  88. package/en/tutorials/foundations/_category_.json +5 -0
  89. package/en/tutorials/foundations/basic.md +8 -0
  90. package/en/tutorials/foundations/introduction.md +43 -0
  91. package/package.json +30 -0
  92. package/scripts/gen-config.ts +60 -0
  93. package/scripts/sync.ts +36 -0
  94. package/tsconfig.json +7 -0
  95. package/zh/apis/app/commands/_category_.json +8 -0
  96. package/zh/apis/app/commands/build.md +44 -0
  97. package/zh/apis/app/commands/dev.md +31 -0
  98. package/zh/apis/app/commands/index.md +12 -0
  99. package/zh/apis/app/commands/inspect.md +39 -0
  100. package/zh/apis/app/commands/lint.md +22 -0
  101. package/zh/apis/app/commands/new.md +59 -0
  102. package/zh/apis/app/commands/start.md +32 -0
  103. package/zh/apis/app/commands/test.md +36 -0
  104. package/zh/apis/app/commands/upgrade.md +23 -0
  105. package/zh/apis/app/hooks/_category_.json +8 -0
  106. package/zh/apis/app/hooks/api/_category_.json +4 -0
  107. package/zh/apis/app/hooks/api/framework/_category_.json +4 -0
  108. package/zh/apis/app/hooks/api/framework/app.md +100 -0
  109. package/zh/apis/app/hooks/api/framework/lambda.md +79 -0
  110. package/zh/apis/app/hooks/api/functions/_category_.json +4 -0
  111. package/zh/apis/app/hooks/api/functions/api.md +50 -0
  112. package/zh/apis/app/hooks/api/functions/app.md +10 -0
  113. package/zh/apis/app/hooks/api/functions/common.md +10 -0
  114. package/zh/apis/app/hooks/api/test.md +12 -0
  115. package/zh/apis/app/hooks/config/_category_.json +4 -0
  116. package/zh/apis/app/hooks/config/html.md +10 -0
  117. package/zh/apis/app/hooks/config/icon.md +17 -0
  118. package/zh/apis/app/hooks/config/mock.md +12 -0
  119. package/zh/apis/app/hooks/config/public.md +30 -0
  120. package/zh/apis/app/hooks/config/storybook.md +14 -0
  121. package/zh/apis/app/hooks/config/upload.md +48 -0
  122. package/zh/apis/app/hooks/index.md +12 -0
  123. package/zh/apis/app/hooks/modern-config.md +10 -0
  124. package/zh/apis/app/hooks/server/_category_.json +4 -0
  125. package/zh/apis/app/hooks/server/index.md +14 -0
  126. package/zh/apis/app/hooks/server/test.md +12 -0
  127. package/zh/apis/app/hooks/shared.md +8 -0
  128. package/zh/apis/app/hooks/src/_category_.json +4 -0
  129. package/zh/apis/app/hooks/src/app.md +41 -0
  130. package/zh/apis/app/hooks/src/error.md +10 -0
  131. package/zh/apis/app/hooks/src/eslint.md +9 -0
  132. package/zh/apis/app/hooks/src/index.md +35 -0
  133. package/zh/apis/app/hooks/src/pages.md +183 -0
  134. package/zh/apis/app/hooks/src/stories.md +14 -0
  135. package/zh/apis/app/hooks/src/test.md +12 -0
  136. package/zh/apis/app/overview.md +11 -0
  137. package/zh/apis/app/runtime/_category_.json +9 -0
  138. package/zh/apis/app/runtime/app/_category_.json +4 -0
  139. package/zh/apis/app/runtime/app/bootstrap.md +83 -0
  140. package/zh/apis/app/runtime/app/create-app.md +47 -0
  141. package/zh/apis/app/runtime/app/define-config.md +57 -0
  142. package/zh/apis/app/runtime/app/head.md +43 -0
  143. package/zh/apis/app/runtime/app/no-ssr.md +41 -0
  144. package/zh/apis/app/runtime/app/pre-render.md +77 -0
  145. package/zh/apis/app/runtime/app/use-module-app.md +60 -0
  146. package/zh/apis/app/runtime/app/use-module-apps.md +64 -0
  147. package/zh/apis/app/runtime/app/use-runtime-context.md +39 -0
  148. package/zh/apis/app/runtime/bff-server/_category_.json +4 -0
  149. package/zh/apis/app/runtime/bff-server/after-lambda-registed.md +35 -0
  150. package/zh/apis/app/runtime/bff-server/hook.md +120 -0
  151. package/zh/apis/app/runtime/bff-server/use-context.md +70 -0
  152. package/zh/apis/app/runtime/container/_category_.json +4 -0
  153. package/zh/apis/app/runtime/container/router.md +492 -0
  154. package/zh/apis/app/runtime/container/use-loader.md +64 -0
  155. package/zh/apis/app/runtime/default-alias.md +26 -0
  156. package/zh/apis/app/runtime/env.md +68 -0
  157. package/zh/apis/app/runtime/index.md +12 -0
  158. package/zh/apis/app/runtime/model/Provider.md +36 -0
  159. package/zh/apis/app/runtime/model/_category_.json +4 -0
  160. package/zh/apis/app/runtime/model/auto-actions.md +121 -0
  161. package/zh/apis/app/runtime/model/connect.md +134 -0
  162. package/zh/apis/app/runtime/model/create-app.md +74 -0
  163. package/zh/apis/app/runtime/model/create-store.md +60 -0
  164. package/zh/apis/app/runtime/model/handle-effect.md +106 -0
  165. package/zh/apis/app/runtime/model/model_.md +199 -0
  166. package/zh/apis/app/runtime/model/use-local-model.md +28 -0
  167. package/zh/apis/app/runtime/model/use-model.md +87 -0
  168. package/zh/apis/app/runtime/model/use-static-model.md +47 -0
  169. package/zh/apis/app/runtime/model/use-store.md +48 -0
  170. package/zh/apis/app/runtime/plugin/_category_.json +4 -0
  171. package/zh/apis/app/runtime/plugin/abstract.md +26 -0
  172. package/zh/apis/app/runtime/plugin/hook-api.md +896 -0
  173. package/zh/apis/app/runtime/plugin/hook.md +170 -0
  174. package/zh/apis/app/runtime/plugin/plugin-api.md +117 -0
  175. package/zh/apis/app/runtime/testing/_category_.json +4 -0
  176. package/zh/apis/app/runtime/testing/act.md +34 -0
  177. package/zh/apis/app/runtime/testing/cleanup.md +37 -0
  178. package/zh/apis/app/runtime/testing/render.md +52 -0
  179. package/zh/apis/app/runtime/testing/renderApp.md +31 -0
  180. package/zh/apis/app/runtime/utility/_category_.json +4 -0
  181. package/zh/apis/app/runtime/utility/css-in-js.md +60 -0
  182. package/zh/apis/app/runtime/utility/loadable/_category_.json +4 -0
  183. package/zh/apis/app/runtime/utility/loadable/lazy.md +29 -0
  184. package/zh/apis/app/runtime/utility/loadable/loadable-component.md +61 -0
  185. package/zh/apis/app/runtime/utility/loadable/loadable-library.md +67 -0
  186. package/zh/apis/app/runtime/utility/loadable/loadable_.md +110 -0
  187. package/zh/apis/app/runtime/web-server/_category_.json +4 -0
  188. package/zh/apis/app/runtime/web-server/hook.md +121 -0
  189. package/zh/apis/generator/overview.md +32 -0
  190. package/zh/apis/generator/plugin/_category_.json +4 -0
  191. package/zh/apis/generator/plugin/file/_category_.json +4 -0
  192. package/zh/apis/generator/plugin/file/addFile.md +52 -0
  193. package/zh/apis/generator/plugin/file/addHelper.md +26 -0
  194. package/zh/apis/generator/plugin/file/addManyFile.md +55 -0
  195. package/zh/apis/generator/plugin/file/addPartial.md +26 -0
  196. package/zh/apis/generator/plugin/file/introduce.md +37 -0
  197. package/zh/apis/generator/plugin/file/rmDir.md +24 -0
  198. package/zh/apis/generator/plugin/file/rmFile.md +24 -0
  199. package/zh/apis/generator/plugin/file/updateJSONFile.md +52 -0
  200. package/zh/apis/generator/plugin/file/updateModernConfig.md +26 -0
  201. package/zh/apis/generator/plugin/file/updateTextRawFile.md +33 -0
  202. package/zh/apis/generator/plugin/git/_category_.json +4 -0
  203. package/zh/apis/generator/plugin/git/gitAddAndCommit.md +20 -0
  204. package/zh/apis/generator/plugin/git/initGitRepo.md +16 -0
  205. package/zh/apis/generator/plugin/git/isInGitRepo.md +16 -0
  206. package/zh/apis/generator/plugin/hook/_category_.json +4 -0
  207. package/zh/apis/generator/plugin/hook/afterForged.md +35 -0
  208. package/zh/apis/generator/plugin/hook/onForged.md +35 -0
  209. package/zh/apis/generator/plugin/info/_category_.json +4 -0
  210. package/zh/apis/generator/plugin/info/isFileExit.md +22 -0
  211. package/zh/apis/generator/plugin/info/locale.md +17 -0
  212. package/zh/apis/generator/plugin/info/readDir.md +22 -0
  213. package/zh/apis/generator/plugin/input/_category_.json +4 -0
  214. package/zh/apis/generator/plugin/input/addInputAfter.md +48 -0
  215. package/zh/apis/generator/plugin/input/addInputBefore.md +48 -0
  216. package/zh/apis/generator/plugin/input/addOptionAfter.md +45 -0
  217. package/zh/apis/generator/plugin/input/addOptionBefore.md +45 -0
  218. package/zh/apis/generator/plugin/input/setInput.md +42 -0
  219. package/zh/apis/generator/plugin/input/setInputValue.md +28 -0
  220. package/zh/apis/generator/plugin/input/type.md +101 -0
  221. package/zh/apis/generator/plugin/introduce.md +93 -0
  222. package/zh/apis/generator/plugin/new/_category_.json +4 -0
  223. package/zh/apis/generator/plugin/new/createElement.md +33 -0
  224. package/zh/apis/generator/plugin/new/createSubProject.md +35 -0
  225. package/zh/apis/generator/plugin/new/enableFunc.md +46 -0
  226. package/zh/apis/generator/plugin/new/introduce.md +15 -0
  227. package/zh/apis/generator/plugin/npm/_category_.json +4 -0
  228. package/zh/apis/generator/plugin/npm/install.md +16 -0
  229. package/zh/apis/monorepo/commands/_category_.json +8 -0
  230. package/zh/apis/monorepo/commands/bump.md +42 -0
  231. package/zh/apis/monorepo/commands/change.md +58 -0
  232. package/zh/apis/monorepo/commands/clear.md +27 -0
  233. package/zh/apis/monorepo/commands/deploy.md +39 -0
  234. package/zh/apis/monorepo/commands/gen-release-note.md +35 -0
  235. package/zh/apis/monorepo/commands/index.md +12 -0
  236. package/zh/apis/monorepo/commands/lint.md +21 -0
  237. package/zh/apis/monorepo/commands/new.md +41 -0
  238. package/zh/apis/monorepo/commands/pre.md +68 -0
  239. package/zh/apis/monorepo/commands/release.md +20 -0
  240. package/zh/apis/monorepo/commands/upgrade.md +23 -0
  241. package/zh/apis/monorepo/hooks/_category_.json +8 -0
  242. package/zh/apis/monorepo/hooks/apps.md +12 -0
  243. package/zh/apis/monorepo/hooks/code-workspace.md +10 -0
  244. package/zh/apis/monorepo/hooks/examples.md +14 -0
  245. package/zh/apis/monorepo/hooks/features.md +14 -0
  246. package/zh/apis/monorepo/hooks/index.md +12 -0
  247. package/zh/apis/monorepo/hooks/packages.md +14 -0
  248. package/zh/apis/monorepo/hooks/pnpm-workspace.md +10 -0
  249. package/zh/apis/monorepo/hooks/pnpmfile.md +10 -0
  250. package/zh/apis/monorepo/overview.md +11 -0
  251. package/zh/community/index.md +3 -0
  252. package/zh/components/command-tip.md +57 -0
  253. package/zh/components/deploy.md +60 -0
  254. package/zh/components/dev-ide.md +1 -0
  255. package/zh/components/entry-name.md +15 -0
  256. package/zh/components/env-prepare.md +1 -0
  257. package/zh/components/launch-bff-choices.md +6 -0
  258. package/zh/components/reduck-tip.md +5 -0
  259. package/zh/configure/app/bff/_category_.json +4 -0
  260. package/zh/configure/app/bff/fetcher.md +32 -0
  261. package/zh/configure/app/bff/prefix.md +38 -0
  262. package/zh/configure/app/bff/proxy.md +76 -0
  263. package/zh/configure/app/dev/_category_.json +4 -0
  264. package/zh/configure/app/dev/asset-prefix.md +13 -0
  265. package/zh/configure/app/dev/hmr.md +13 -0
  266. package/zh/configure/app/dev/https.md +13 -0
  267. package/zh/configure/app/dev/port.md +13 -0
  268. package/zh/configure/app/dev/progress-bar.md +13 -0
  269. package/zh/configure/app/dev/proxy.md +79 -0
  270. package/zh/configure/app/dev/start-url.md +13 -0
  271. package/zh/configure/app/dev/with-master-app.md +35 -0
  272. package/zh/configure/app/html/app-icon.md +13 -0
  273. package/zh/configure/app/html/crossorigin.md +13 -0
  274. package/zh/configure/app/html/disable-html-folder.md +13 -0
  275. package/zh/configure/app/html/favicon-by-entries.md +13 -0
  276. package/zh/configure/app/html/favicon.md +13 -0
  277. package/zh/configure/app/html/inject-by-entries.md +13 -0
  278. package/zh/configure/app/html/inject.md +13 -0
  279. package/zh/configure/app/html/meta-by-entries.md +13 -0
  280. package/zh/configure/app/html/meta.md +13 -0
  281. package/zh/configure/app/html/mount-id.md +13 -0
  282. package/zh/configure/app/html/template-by-entries.md +13 -0
  283. package/zh/configure/app/html/template-parameters-by-entries.md +13 -0
  284. package/zh/configure/app/html/template-parameters.md +13 -0
  285. package/zh/configure/app/html/template.md +13 -0
  286. package/zh/configure/app/html/title-by-entries.md +13 -0
  287. package/zh/configure/app/html/title.md +13 -0
  288. package/zh/configure/app/output/_category_.json +4 -0
  289. package/zh/configure/app/output/asset-prefix.md +13 -0
  290. package/zh/configure/app/output/assets-retry.md +13 -0
  291. package/zh/configure/app/output/charset.md +13 -0
  292. package/zh/configure/app/output/clean-dist-path.md +13 -0
  293. package/zh/configure/app/output/convert-to-rem.md +13 -0
  294. package/zh/configure/app/output/copy.md +13 -0
  295. package/zh/configure/app/output/css-module-local-ident-name.md +13 -0
  296. package/zh/configure/app/output/data-uri-limit.md +13 -0
  297. package/zh/configure/app/output/disable-css-module-extension.md +13 -0
  298. package/zh/configure/app/output/disable-filename-hash.md +13 -0
  299. package/zh/configure/app/output/disable-inline-runtime-chunk.md +13 -0
  300. package/zh/configure/app/output/disable-minimize.md +13 -0
  301. package/zh/configure/app/output/disable-node-polyfill.md +24 -0
  302. package/zh/configure/app/output/disable-source-map.md +13 -0
  303. package/zh/configure/app/output/dist-path.md +13 -0
  304. package/zh/configure/app/output/enable-asset-fallback.md +13 -0
  305. package/zh/configure/app/output/enable-asset-manifest.md +13 -0
  306. package/zh/configure/app/output/enable-css-module-tsdeclaration.md +13 -0
  307. package/zh/configure/app/output/enable-inline-scripts.md +13 -0
  308. package/zh/configure/app/output/enable-inline-styles.md +13 -0
  309. package/zh/configure/app/output/enable-latest-decorators.md +13 -0
  310. package/zh/configure/app/output/enable-modern-mode.md +26 -0
  311. package/zh/configure/app/output/externals.md +13 -0
  312. package/zh/configure/app/output/filename.md +13 -0
  313. package/zh/configure/app/output/legal-comments.md +13 -0
  314. package/zh/configure/app/output/override-browserslist.md +13 -0
  315. package/zh/configure/app/output/polyfill.md +13 -0
  316. package/zh/configure/app/output/ssg.md +226 -0
  317. package/zh/configure/app/output/svg-default-export.md +13 -0
  318. package/zh/configure/app/performance/build-cache.md +13 -0
  319. package/zh/configure/app/performance/bundle-analyze.md +13 -0
  320. package/zh/configure/app/performance/chunk-split.md +13 -0
  321. package/zh/configure/app/performance/print-file-size.md +13 -0
  322. package/zh/configure/app/performance/profile.md +13 -0
  323. package/zh/configure/app/performance/remove-console.md +13 -0
  324. package/zh/configure/app/performance/remove-moment-locale.md +13 -0
  325. package/zh/configure/app/plugins.md +74 -0
  326. package/zh/configure/app/runtime/_category_.json +4 -0
  327. package/zh/configure/app/runtime/master-app.md +72 -0
  328. package/zh/configure/app/runtime/router.md +54 -0
  329. package/zh/configure/app/runtime/state.md +45 -0
  330. package/zh/configure/app/security/sri.md +13 -0
  331. package/zh/configure/app/server/_category_.json +4 -0
  332. package/zh/configure/app/server/base-url.md +36 -0
  333. package/zh/configure/app/server/port.md +21 -0
  334. package/zh/configure/app/server/public-routes.md +29 -0
  335. package/zh/configure/app/server/routes.md +92 -0
  336. package/zh/configure/app/server/ssr-by-entries.md +32 -0
  337. package/zh/configure/app/server/ssr.md +24 -0
  338. package/zh/configure/app/source/_category_.json +4 -0
  339. package/zh/configure/app/source/alias.md +13 -0
  340. package/zh/configure/app/source/compile-js-data-uri.md +13 -0
  341. package/zh/configure/app/source/config-dir.md +15 -0
  342. package/zh/configure/app/source/define.md +13 -0
  343. package/zh/configure/app/source/design-system.md +1156 -0
  344. package/zh/configure/app/source/disable-default-entries.md +23 -0
  345. package/zh/configure/app/source/enable-async-entry.md +53 -0
  346. package/zh/configure/app/source/entries-dir.md +42 -0
  347. package/zh/configure/app/source/entries.md +55 -0
  348. package/zh/configure/app/source/global-vars.md +13 -0
  349. package/zh/configure/app/source/include.md +13 -0
  350. package/zh/configure/app/source/module-scopes.md +13 -0
  351. package/zh/configure/app/source/pre-entry.md +13 -0
  352. package/zh/configure/app/source/resolve-extension-prefix.md +13 -0
  353. package/zh/configure/app/source/resolve-main-fields.md +13 -0
  354. package/zh/configure/app/testing/_category_.json +4 -0
  355. package/zh/configure/app/testing/transformer.md +17 -0
  356. package/zh/configure/app/tools/_category_.json +4 -0
  357. package/zh/configure/app/tools/autoprefixer.md +13 -0
  358. package/zh/configure/app/tools/babel.md +13 -0
  359. package/zh/configure/app/tools/css-extract.md +13 -0
  360. package/zh/configure/app/tools/css-loader.md +13 -0
  361. package/zh/configure/app/tools/dev-server.md +13 -0
  362. package/zh/configure/app/tools/esbuild.md +61 -0
  363. package/zh/configure/app/tools/html-plugin.md +13 -0
  364. package/zh/configure/app/tools/inspector.md +13 -0
  365. package/zh/configure/app/tools/jest.md +41 -0
  366. package/zh/configure/app/tools/less.md +13 -0
  367. package/zh/configure/app/tools/minify-css.md +13 -0
  368. package/zh/configure/app/tools/postcss.md +13 -0
  369. package/zh/configure/app/tools/pug.md +13 -0
  370. package/zh/configure/app/tools/sass.md +13 -0
  371. package/zh/configure/app/tools/style-loader.md +13 -0
  372. package/zh/configure/app/tools/styled-components.md +13 -0
  373. package/zh/configure/app/tools/tailwindcss.md +48 -0
  374. package/zh/configure/app/tools/terser.md +13 -0
  375. package/zh/configure/app/tools/ts-checker.md +13 -0
  376. package/zh/configure/app/tools/ts-loader.md +13 -0
  377. package/zh/configure/app/tools/webpack-chain.md +13 -0
  378. package/zh/configure/app/tools/webpack.md +13 -0
  379. package/zh/configure/app/usage.md +81 -0
  380. package/zh/configure/generator/introduce.md +113 -0
  381. package/zh/configure/generator/module.md +43 -0
  382. package/zh/configure/generator/monorepo.md +28 -0
  383. package/zh/configure/generator/mwa.md +127 -0
  384. package/zh/guides/advanced-features/_category_.json +9 -0
  385. package/zh/guides/advanced-features/bff/_category_.json +4 -0
  386. package/zh/guides/advanced-features/bff/bff-proxy.md +25 -0
  387. package/zh/guides/advanced-features/bff/bff-server.md +142 -0
  388. package/zh/guides/advanced-features/bff/frameworks.md +348 -0
  389. package/zh/guides/advanced-features/bff/function.md +236 -0
  390. package/zh/guides/advanced-features/code-split.md +68 -0
  391. package/zh/guides/advanced-features/compatibility.md +89 -0
  392. package/zh/guides/advanced-features/custom-app.md +4 -0
  393. package/zh/guides/advanced-features/eslint.md +146 -0
  394. package/zh/guides/advanced-features/index.md +12 -0
  395. package/zh/guides/advanced-features/low-level.md +44 -0
  396. package/zh/guides/advanced-features/ssg.md +151 -0
  397. package/zh/guides/advanced-features/ssr.md +186 -0
  398. package/zh/guides/advanced-features/testing.md +4 -0
  399. package/zh/guides/advanced-features/typescript.md +4 -0
  400. package/zh/guides/advanced-features/web-server.md +135 -0
  401. package/zh/guides/basic-features/_category_.json +9 -0
  402. package/zh/guides/basic-features/alias.md +67 -0
  403. package/zh/guides/basic-features/css/_category_.json +4 -0
  404. package/zh/guides/basic-features/css/css-in-js.md +38 -0
  405. package/zh/guides/basic-features/css/css-modules.md +86 -0
  406. package/zh/guides/basic-features/css/less-sass.md +29 -0
  407. package/zh/guides/basic-features/css/postcss.md +81 -0
  408. package/zh/guides/basic-features/css/tailwindcss.md +98 -0
  409. package/zh/guides/basic-features/data-fetch.md +4 -0
  410. package/zh/guides/basic-features/env-vars.md +83 -0
  411. package/zh/guides/basic-features/html.md +128 -0
  412. package/zh/guides/basic-features/image.md +43 -0
  413. package/zh/guides/basic-features/index.md +12 -0
  414. package/zh/guides/basic-features/mock.md +85 -0
  415. package/zh/guides/basic-features/proxy.md +85 -0
  416. package/zh/guides/basic-features/routes.md +4 -0
  417. package/zh/guides/concept/_category_.json +5 -0
  418. package/zh/guides/concept/entries.md +4 -0
  419. package/zh/guides/concept/integrated.md +4 -0
  420. package/zh/guides/concept/lifecycle.md +4 -0
  421. package/zh/guides/concept/solutions.md +4 -0
  422. package/zh/guides/get-started/_category_.json +5 -0
  423. package/zh/guides/get-started/quick-start.md +4 -0
  424. package/zh/guides/get-started/upgrade.md +4 -0
  425. package/zh/guides/overview.md +11 -0
  426. package/zh/guides/topic-detail/_category_.json +5 -0
  427. package/zh/guides/topic-detail/changesets/_category_.json +4 -0
  428. package/zh/guides/topic-detail/changesets/add.md +125 -0
  429. package/zh/guides/topic-detail/changesets/changelog.md +239 -0
  430. package/zh/guides/topic-detail/changesets/commit.md +272 -0
  431. package/zh/guides/topic-detail/changesets/config.md +146 -0
  432. package/zh/guides/topic-detail/changesets/github.md +168 -0
  433. package/zh/guides/topic-detail/changesets/introduce.md +57 -0
  434. package/zh/guides/topic-detail/changesets/release-note.md +262 -0
  435. package/zh/guides/topic-detail/changesets/release-pre.md +49 -0
  436. package/zh/guides/topic-detail/changesets/release.md +228 -0
  437. package/zh/guides/topic-detail/compile-speed.md +182 -0
  438. package/zh/guides/topic-detail/framework-plugin/_category_.json +4 -0
  439. package/zh/guides/topic-detail/framework-plugin/abstract.md +27 -0
  440. package/zh/guides/topic-detail/framework-plugin/extend.md +163 -0
  441. package/zh/guides/topic-detail/framework-plugin/implement.md +188 -0
  442. package/zh/guides/topic-detail/framework-plugin/relationship.md +119 -0
  443. package/zh/guides/topic-detail/generator-plugin/_category_.json +4 -0
  444. package/zh/guides/topic-detail/generator-plugin/abstract.md +23 -0
  445. package/zh/guides/topic-detail/generator-plugin/develop.md +120 -0
  446. package/zh/guides/topic-detail/generator-plugin/use.md +61 -0
  447. package/zh/guides/topic-detail/micro-frontend/_category_.json +4 -0
  448. package/zh/guides/topic-detail/micro-frontend/communicate.md +39 -0
  449. package/zh/guides/topic-detail/micro-frontend/debugging.md +168 -0
  450. package/zh/guides/topic-detail/micro-frontend/introduction.md +13 -0
  451. package/zh/guides/topic-detail/micro-frontend/mixed-stack.md +24 -0
  452. package/zh/guides/topic-detail/micro-frontend/route-mode.md +110 -0
  453. package/zh/guides/topic-detail/model/_category_.json +4 -0
  454. package/zh/guides/topic-detail/model/auto-actions.md +90 -0
  455. package/zh/guides/topic-detail/model/computed-state.md +151 -0
  456. package/zh/guides/topic-detail/model/define-model.md +66 -0
  457. package/zh/guides/topic-detail/model/faq.md +43 -0
  458. package/zh/guides/topic-detail/model/manage-effects.md +259 -0
  459. package/zh/guides/topic-detail/model/model-communicate.md +219 -0
  460. package/zh/guides/topic-detail/model/performance.md +173 -0
  461. package/zh/guides/topic-detail/model/quick-start.md +112 -0
  462. package/zh/guides/topic-detail/model/redux-integration.md +21 -0
  463. package/zh/guides/topic-detail/model/test-model.md +43 -0
  464. package/zh/guides/topic-detail/model/typescript-best-practice.md +71 -0
  465. package/zh/guides/topic-detail/model/use-model.md +244 -0
  466. package/zh/guides/topic-detail/model/use-out-of-modernjs.md +51 -0
  467. package/zh/guides/topic-detail/monorepo/_category_.json +4 -0
  468. package/zh/guides/topic-detail/monorepo/create-sub-project.md +64 -0
  469. package/zh/guides/topic-detail/monorepo/deploy.md +43 -0
  470. package/zh/guides/topic-detail/monorepo/intro.md +15 -0
  471. package/zh/guides/topic-detail/monorepo/publish.md +68 -0
  472. package/zh/guides/topic-detail/monorepo/sub-project-interface.md +170 -0
  473. package/zh/guides/troubleshooting/_category_.json +5 -0
  474. package/zh/guides/troubleshooting/compile.md +379 -0
  475. package/zh/guides/troubleshooting/dependencies.md +168 -0
  476. package/zh/tutorials/first-app/_category_.json +5 -0
  477. package/zh/tutorials/first-app/c01-getting-started/1.1-prerequisites.md +25 -0
  478. package/zh/tutorials/first-app/c01-getting-started/1.2-minimal-mwa.md +118 -0
  479. package/zh/tutorials/first-app/c01-getting-started/1.3-dev-command.md +29 -0
  480. package/zh/tutorials/first-app/c01-getting-started/1.4-enable-ssr.md +44 -0
  481. package/zh/tutorials/first-app/c01-getting-started/1.5-start-command.md +18 -0
  482. package/zh/tutorials/first-app/c01-getting-started/1.6-create-repo.md +31 -0
  483. package/zh/tutorials/first-app/c01-getting-started/_category_.json +3 -0
  484. package/zh/tutorials/first-app/c02-generator-and-studio/2.1-generator.md +87 -0
  485. package/zh/tutorials/first-app/c02-generator-and-studio/2.2-boilerplates.md +28 -0
  486. package/zh/tutorials/first-app/c02-generator-and-studio/2.3-configuration.md +21 -0
  487. package/zh/tutorials/first-app/c02-generator-and-studio/_category_.json +3 -0
  488. package/zh/tutorials/first-app/c03-ide/3.1-setting-up.md +55 -0
  489. package/zh/tutorials/first-app/c03-ide/3.2-hints-in-ide.md +66 -0
  490. package/zh/tutorials/first-app/c03-ide/3.3-autofix-in-ide.md +11 -0
  491. package/zh/tutorials/first-app/c03-ide/3.4-autofix-in-cli.md +63 -0
  492. package/zh/tutorials/first-app/c03-ide/_category_.json +3 -0
  493. package/zh/tutorials/first-app/c04-es6-plus-and-ts/4.1-use-es6-plus.md +67 -0
  494. package/zh/tutorials/first-app/c04-es6-plus-and-ts/4.2-use-typescript.md +111 -0
  495. package/zh/tutorials/first-app/c04-es6-plus-and-ts/4.3-compatibility.md +67 -0
  496. package/zh/tutorials/first-app/c04-es6-plus-and-ts/_category_.json +3 -0
  497. package/zh/tutorials/first-app/c05-component/5.1-use-ui-library.md +68 -0
  498. package/zh/tutorials/first-app/c05-component/5.2-use-standalone-component.md +92 -0
  499. package/zh/tutorials/first-app/c05-component/_category_.json +3 -0
  500. package/zh/tutorials/first-app/c06-css-and-component/6.1-css-in-js.md +110 -0
  501. package/zh/tutorials/first-app/c06-css-and-component/6.2-utility-class.md +148 -0
  502. package/zh/tutorials/first-app/c06-css-and-component/6.3-postcss.md +84 -0
  503. package/zh/tutorials/first-app/c06-css-and-component/6.4-design-system.md +83 -0
  504. package/zh/tutorials/first-app/c06-css-and-component/6.5-storybook.md +77 -0
  505. package/zh/tutorials/first-app/c06-css-and-component/6.6-testing.md +113 -0
  506. package/zh/tutorials/first-app/c06-css-and-component/_category_.json +3 -0
  507. package/zh/tutorials/first-app/c07-app-entry/7.1-intro.md +64 -0
  508. package/zh/tutorials/first-app/c07-app-entry/7.2-add-entry-in-cli.md +104 -0
  509. package/zh/tutorials/first-app/c07-app-entry/7.3-manage-entries-by-hand.md +74 -0
  510. package/zh/tutorials/first-app/c07-app-entry/_category_.json +3 -0
  511. package/zh/tutorials/first-app/c08-client-side-routing/8.1-code-based-routing.md +153 -0
  512. package/zh/tutorials/first-app/c08-client-side-routing/8.2-file-based-routing.md +318 -0
  513. package/zh/tutorials/first-app/c08-client-side-routing/_category_.json +3 -0
  514. package/zh/tutorials/first-app/c09-bff/9.1-serverless.md +30 -0
  515. package/zh/tutorials/first-app/c09-bff/9.2-enable-bff.md +93 -0
  516. package/zh/tutorials/first-app/c09-bff/9.3-fetch-bff.md +205 -0
  517. package/zh/tutorials/first-app/c09-bff/_category_.json +3 -0
  518. package/zh/tutorials/first-app/c10-model/10.1-application-architecture.md +23 -0
  519. package/zh/tutorials/first-app/c10-model/10.2-add-model.md +185 -0
  520. package/zh/tutorials/first-app/c10-model/10.3-use-model.md +54 -0
  521. package/zh/tutorials/first-app/c10-model/10.4-testing.md +69 -0
  522. package/zh/tutorials/first-app/c10-model/_category_.json +3 -0
  523. package/zh/tutorials/first-app/c11-container/11.1-use-model-with-app-state.md +274 -0
  524. package/zh/tutorials/first-app/c11-container/11.2-add-container.md +106 -0
  525. package/zh/tutorials/first-app/c11-container/11.3-use-loader.md +61 -0
  526. package/zh/tutorials/first-app/c11-container/11.4-testing.md +56 -0
  527. package/zh/tutorials/first-app/c11-container/_category_.json +3 -0
  528. package/zh/tutorials/first-app/overview.md +11 -0
  529. package/zh/tutorials/foundations/_category_.json +5 -0
  530. package/zh/tutorials/foundations/basic.md +8 -0
  531. package/zh/tutorials/foundations/introduction.md +41 -0
@@ -0,0 +1,205 @@
1
+ ---
2
+ title: 从 BFF 获取数据
3
+ ---
4
+
5
+ 上一小节中,我们创建了【 函数写法 】的 BFF,这一小节中,我们将用该模式为联系人列表实现一个数据接口。
6
+
7
+ 我们首先把不需要的样板文件清理掉,把 `index.ts` 重命名为 `contacts.ts`,执行以下命令:
8
+
9
+ import Tabs from '@theme/Tabs';
10
+ import TabItem from '@theme/TabItem';
11
+
12
+ <Tabs>
13
+ <TabItem value="macOS" label="macOS" default>
14
+
15
+ ```bash
16
+ rm -r api/info api/_app.ts
17
+ mv api/index.ts api/contacts.ts
18
+ ```
19
+
20
+ </TabItem>
21
+ <TabItem value="Windows" label="Windows">
22
+
23
+ ```powershell
24
+ rm -r api/info
25
+ rm api/_app.ts
26
+ mv api/index.ts api/contacts.ts
27
+ ```
28
+
29
+ </TabItem>
30
+ </Tabs>
31
+
32
+
33
+ 此时 API 路由将变为 `/api/contacts`。
34
+
35
+ 我们使用 [faker](https://github.com/Marak/Faker.js) 来 mock 需要的数据,首先安装依赖:
36
+
37
+ ```bash
38
+ pnpm add faker
39
+ ```
40
+
41
+ 将 `api/contacts.ts` 内容改为:
42
+
43
+ ```js
44
+ import { name, internet } from 'faker';
45
+
46
+ export const get = async () => {
47
+ const mockData = new Array(20).fill(0).map(() => {
48
+ const firstName = name.firstName();
49
+ return {
50
+ name: firstName,
51
+ avatar: `https://avatars.dicebear.com/api/identicon/${firstName}.svg`,
52
+ email: internet.email(),
53
+ };
54
+ });
55
+
56
+ return { code: 200, data: mockData };
57
+ };
58
+ ```
59
+
60
+ :::info 注
61
+ 数据同样可以从远端接口获取,此处仅为了演示。
62
+ :::
63
+
64
+ 执行 `pnpm run dev`,访问 `http://localhost:8080/api/contacts`:
65
+
66
+ ![browser-result](https://lf3-static.bytednsdoc.com/obj/eden-cn/aphqeh7uhohpquloj/modern-js/docs/08/api-result.png)
67
+
68
+ 接下来我们把 `src/contacts/App.tsx` 里硬编码的 `mockData` 改成从 BFF 加载。
69
+
70
+ 我们将业务逻辑迁移到组件中,`App.tsx` 只保留简单的路由部分,执行:
71
+
72
+ <Tabs>
73
+ <TabItem value="macOS" label="macOS" default>
74
+
75
+ ```bash
76
+ mkdir -p src/contacts/components/AllContacts/
77
+ mkdir -p src/contacts/components/ArchivedContacts/
78
+ touch src/contacts/components/AllContacts/index.tsx
79
+ touch src/contacts/components/ArchivedContacts/index.tsx
80
+ ```
81
+
82
+ </TabItem>
83
+ <TabItem value="Windows" label="Windows">
84
+
85
+ ```powershell
86
+ mkdir -p src/contacts/components/AllContacts/
87
+ mkdir -p src/contacts/components/ArchivedContacts/
88
+ ni src/contacts/components/AllContacts/index.tsx
89
+ ni src/contacts/components/ArchivedContacts/index.tsx
90
+ ```
91
+
92
+ </TabItem>
93
+ </Tabs>
94
+
95
+ `ArchivedContacts/index.tsx` 的内容:
96
+
97
+ ```js
98
+ import { List } from 'antd';
99
+ import Item from '../Item';
100
+
101
+ const getAvatar = (users: Array<{ name: string; email: string }>) =>
102
+ users.map(user => ({
103
+ ...user,
104
+ avatar: `https://avatars.dicebear.com/v2/identicon/${user.name}.svg`,
105
+ }));
106
+
107
+ const mockArchivedData = getAvatar([
108
+ { name: 'Thomas', email: 'w.kccip@bllmfbgv.dm' },
109
+ { name: 'Chow', email: 'f.lfqljnlk@ywoefljhc.af' },
110
+ ]);
111
+
112
+ const ArchivedContacts = () => (
113
+ <List
114
+ dataSource={mockArchivedData}
115
+ renderItem={info => <Item key={info.name} info={info} />}
116
+ />
117
+ );
118
+
119
+ export default ArchivedContacts;
120
+ ```
121
+
122
+ `AllContacts/index.tsx` 的内容:
123
+
124
+ ```tsx
125
+ import { useState, useEffect } from 'react';
126
+ import { List } from 'antd';
127
+ import { get as contacts } from '@api/contacts';
128
+ import Item from '../Item';
129
+
130
+ const AllContacts = () => {
131
+ const [list, setList] = useState(
132
+ [] as Array<{ name: string; email: string; avatar: string }>,
133
+ );
134
+ const loadMockData = async () => {
135
+ const { data } = await contacts();
136
+ setList(data);
137
+ };
138
+ useEffect(() => {
139
+ if (!list.length) {
140
+ loadMockData();
141
+ }
142
+ });
143
+ return (
144
+ (list.length && (
145
+ <List
146
+ dataSource={list}
147
+ renderItem={info => <Item key={info.name} info={info} />}
148
+ />
149
+ )) || (
150
+ <div className="p-4 items-center border-gray-200 border-b border-t custom-text-gray">
151
+ Pending...
152
+ </div>
153
+ )
154
+ );
155
+ };
156
+
157
+ export default AllContacts;
158
+ ```
159
+
160
+ 在 Modern.js 中,可以通过调用函数(前后端一体化)的方式,直接调用 BFF 接口,调用时无需关心域名、路径等。
161
+
162
+ :::info 注
163
+ 在使用 `pnpm run new` 创建 BFF 时,Modern.js 已经默认在 `tsconig.json` 中注入了别名。这也是之前提到的,生成器在项目创建之后并不会被抛弃,仍旧可以在开发过程中不断为项目提供新的内容。
164
+ :::
165
+
166
+ 接下来修改 `src/contacts/App.tsx`,把 `List` 组件的调用代码替换成上面的 `AllContacts` 和 `ArchivedContacts`:
167
+
168
+ ```tsx
169
+ <Route path="/" exact={true}>
170
+ <Helmet>
171
+ <title>All</title>
172
+ </Helmet>
173
+ <AllContacts />
174
+ </Route>
175
+ <Route path="/archives" exact={true}>
176
+ <Helmet>
177
+ <title>Archives</title>
178
+ </Helmet>
179
+ <ArchivedContacts />
180
+ </Route>
181
+ ```
182
+
183
+ 删除 `App.tsx` 里的 mock 数据和 `List` 组件,把 `Item` 组件替换成 `ContactList`:
184
+
185
+ ```tsx
186
+ import { useState } from 'react';
187
+ import { Radio, RadioChangeEvent } from 'antd';
188
+ import { Route, Switch, useHistory } from '@modern-js/runtime/router';
189
+ import { Helmet } from '@modern-js/runtime/head';
190
+ import 'tailwindcss/base.css';
191
+ import 'tailwindcss/components.css';
192
+ import 'tailwindcss/utilities.css';
193
+ import './styles/utils.css';
194
+ import AllContacts from './components/AllContacts';
195
+ import ArchivedContacts from './components/ArchivedContacts';
196
+ ```
197
+
198
+ 执行 `pnpm run dev`,再打开页面`http://localhost:8000/contacts`,可以看到页面加载后,会先初始化联系人数据(显示 pending),之后每次切换到 All 列表,也会重新请求联系人数据:
199
+
200
+ ![browser-result](https://lf3-static.bytednsdoc.com/obj/eden-cn/aphqeh7uhohpquloj/modern-js/docs/08/browser-result.png)
201
+
202
+ ---
203
+
204
+ > 本小节的代码可以在[这里查看](https://github.com/modern-js-dev/modern-js-examples/tree/main/tutorials/c09/hello-modern-3)。
205
+
@@ -0,0 +1,3 @@
1
+ {
2
+ "label": "9: 添加 BFF"
3
+ }
@@ -0,0 +1,23 @@
1
+ ---
2
+ title: 应用架构
3
+ ---
4
+
5
+ 上一章节中,我们把硬编码的 `mockData` 改成从 BFF 加载,从 `App.tsx` 里拆分出了 `components/AllContacts.tsx` 和 `components/ArchivedContacts.tsx`,在 `AllContacts` 组件里用 BFF 函数,获取到联系人数据之后,保存在 `AllContacts` 的组件内部状态里,而 `ArchivedContacts` 组件暂时继续使用 mock 数据。
6
+
7
+ 本章节中,为了进一步实现项目功能,我们需要让 `AllContacts` 和 `ArchivedContacts` 都从 BFF 获取数据,还要实现【 Archive 】按钮,点击按钮能把联系人归档。
8
+
9
+ 业务逻辑变复杂之后,相关代码不可避免会变多,如果都写在 AllContacts 组件或其他组件里,都会让这个组件的可读性和可维护性变差,让做不同事情的代码混在一起。
10
+
11
+ 原本可以跟 UI 完全解耦的业务逻辑(比如网络请求、纯数据的操作、状态的管理等)跟 UI 逻辑(比如联系人列表如何展现)混在一起,当需要修改 UI 的时候,不得不跟 UI 无关的业务逻辑代码打交道,反过来也一样,不符合【 [单一职责原则(SRP)](https://zh.wikipedia.org/wiki/%E5%8D%95%E4%B8%80%E5%8A%9F%E8%83%BD%E5%8E%9F%E5%88%99)】,也做不到【 [关注点分离(SoC)](https://zh.wikipedia.org/wiki/%E5%85%B3%E6%B3%A8%E7%82%B9%E5%88%86%E7%A6%BB)】。
12
+
13
+ 从这里开始,我们发现如果这是个真实的项目,继续这样写下去会产生越来越多面条式代码,仅凭 React 组件这一种抽象方式,不足以让代码足够结构化,随着项目不断积累业务逻辑、复杂性和变更,技术债也会积累,最终可能变成没人愿意碰的祖传代码。
14
+
15
+ 像这样的项目,需要更健全的**客户端应用架构**。
16
+
17
+ Modern.js 提供开箱即用的**应用架构**,支持几种扮演不同**角色(role)**、属于不同**分层(layer)**的代码模块类型,可以把业务逻辑解耦到不同类型的代码模块里,做到高[内聚](https://zh.wikipedia.org/wiki/%E5%85%A7%E8%81%9A%E6%80%A7_(%E8%A8%88%E7%AE%97%E6%A9%9F%E7%A7%91%E5%AD%B8))低[耦合](https://zh.wikipedia.org/wiki/%E8%80%A6%E5%90%88%E6%80%A7_(%E8%A8%88%E7%AE%97%E6%A9%9F%E7%A7%91%E5%AD%B8))。
18
+
19
+ 之前介绍的 `App.tsx` 和 `pages/` 都是其中一种代码模块,通过在最顶层对客户端路由的管理,把其他代码模块最终组织到一起,形成应用。
20
+
21
+ 当前项目里 `components/` 目录中的 React 组件,是一种可以称作【 视图组件 】的代码模块,负责 UI、交互上的界面展现。
22
+
23
+ 本章节我们来把 `AllContacts` 组件中可以跟 UI 解耦的业务逻辑,移到一种叫【 Model(业务模型)】的代码模块里。
@@ -0,0 +1,185 @@
1
+ ---
2
+ title: 实现 Model
3
+ ---
4
+
5
+ 创建一个完整的 Model 首先需要定义**状态(state)**,包括状态中数据的名称和初始值。
6
+
7
+ 我们使用 Model 来管理联系人列表的数据,因此定义如下数据状态:
8
+
9
+ ```js
10
+ const state = {
11
+ items: [],
12
+ };
13
+ ```
14
+
15
+ 使用 TS 语法,可以定义更完整的类型信息,比如 items 里每个对象都应该有 `name`、`email` 字段;
16
+
17
+ 为了实现归档功能,还需要创建 `archived` 字段保存这个联系人是否已被归档的状态;
18
+
19
+ 我们还需要一个字段用来访问所有已归档的联系人,可以定义 **computed** 类型的字段,对已有的数据做转换:
20
+
21
+ ```js
22
+ const computed = {
23
+ archived: ({ items }) => {
24
+ return items.filter(item => item.archived);
25
+ },
26
+ };
27
+ ```
28
+
29
+ :::info 注
30
+ 当前版本还未支持 computed,本章节后续部分会先使用其他方式实现 archived 列表,这里只做介绍。
31
+ :::
32
+
33
+ computed 类型字段的定义方式是函数,但使用时可以像普通字段一样通过 state 访问。
34
+
35
+ Modern.js 支持的 Model 模块跟 React 组件一样,是基于 FP(Functional Programming)而不是 OOP(Object-Oriented Programming)的,对状态的管理是基于**不可变数据**的,不会修改状态中的数据,只会从一个状态转移到另一个状态,这样的好处很多,比如保障程序的可靠性、方便调试、方便记录和还原状态等。
36
+
37
+ 由于 JS 没有原生支持不可变数据,为了提高编写这种代码的效率,Modern.js 集成了 [Immer](https://immerjs.github.io/immer/),能够像操作 JS 中常规的可变数据一样,去写这种状态转移的逻辑。
38
+
39
+ 实现 Archive 按钮时,我们需要一个 `archive` 函数,负责修改指定联系人的 `archived` 字段,我们把这种函数都叫作 **action**:
40
+
41
+ ```js
42
+ const actions = {
43
+ archive(draft, payload) {
44
+ const target = draft.items.find(item => item.email === payload);
45
+ if (target) {
46
+ target.archived = true;
47
+ }
48
+ },
49
+ };
50
+ ```
51
+
52
+ action 函数是一种**纯函数**,确定的输入得到确定的输出(转移后的状态),不应该有任何副作用。
53
+
54
+ 函数的第一个参数是 Immer 提供的 Draft State,第二个参数是 action 被调用时传入的参数(后面会介绍怎么调用)。
55
+
56
+ Model 里也可以定义 Side Effect,比如我们需要从 BFF 加载这个联系人列表的数据,这段业务逻辑可以写成:
57
+
58
+ ```js
59
+ const effects = {
60
+ async load() {
61
+ const { data } = await contacts();
62
+ return data;
63
+ },
64
+ };
65
+ ```
66
+
67
+ 一个 Side Effect 有多种实现方式,上面使用的是 Async 函数方式,这种方式是最简单直观的。Modern.js 会根据它返回的 Promise 对象的状态变化,自动触发不同的 action。
68
+
69
+ 因此一个 effect 总共有三个 action,命名里会用 Side Effect 的名称作为命名空间,在这个例子里,分别是:
70
+
71
+ - `load.pending`:等待中
72
+ - `load.fulfilled`:成功,得到结果
73
+ - `load.rejected`:失败,得到错误信息
74
+
75
+ Modern.js 虽然会自动定义和触发这些 action,但默认不会为这些 action 实现具体的业务逻辑(action 直接返回原本的状态,不做任何转换)。
76
+
77
+ 我们尝试自己实现它们:
78
+
79
+ ```js
80
+ import _ from 'lodash';
81
+
82
+ const state = {
83
+ items: [],
84
+ pending: false,
85
+ error: null,
86
+ };
87
+
88
+ const computed = {
89
+ archived: ({ items }) => {
90
+ return items.filter(item => item.archived);
91
+ },
92
+ };
93
+
94
+ const actions = {
95
+ archive(draft, payload) {
96
+ const target = draft.items.find(item => item.email === payload);
97
+ if (target) {
98
+ target.archived = true;
99
+ }
100
+ },
101
+ load: {
102
+ pending(draft) {
103
+ draft.pending = true;
104
+ },
105
+ fulfilled(draft, payload) {
106
+ draft.pending = false;
107
+ _.merge(draft.items, payload);
108
+ },
109
+ rejected(draft, payload) {
110
+ draft.pending = false;
111
+ draft.error = payload;
112
+ },
113
+ },
114
+ };
115
+ ```
116
+
117
+ 上述实现里,成功时,payload 是 promise 的结果;失败时,payload 是错误信息。
118
+
119
+ 从上面这个例子里可以看到,可以用嵌套的写法,实现 `load.pending` 这样名称中包含命名空间的 action。
120
+
121
+ 为了做到高内聚低耦合,一个 Model 的 state、action、side effect 不应该分散在不同文件里。接下来我们把上面的代码连起来,放在同一个 Model 文件里,执行以下命令:
122
+
123
+ ```bash
124
+ mkdir -p src/contacts/models/
125
+ touch src/contacts/models/contacts.ts
126
+ ```
127
+
128
+ `src/contacts/models/contacts.ts` 的内容:
129
+
130
+ ```tsx
131
+ import { model } from '@modern-js/runtime/model';
132
+ import { get as contacts } from '@api/contacts';
133
+
134
+ type State = {
135
+ items: {
136
+ avatar: string;
137
+ name: string;
138
+ email: string;
139
+ archived?: boolean;
140
+ }[];
141
+ pending: boolean;
142
+ error: null | Error;
143
+ };
144
+
145
+ export default model<State>('contacts').define({
146
+ state: {
147
+ items: [],
148
+ pending: false,
149
+ error: null,
150
+ },
151
+ computed: {
152
+ archived: ({ items }: State) => items.filter(item => item.archived),
153
+ },
154
+ actions: {
155
+ archive(draft, payload) {
156
+ const target = draft.items.find(item => item.email === payload)!;
157
+ if (target) {
158
+ target.archived = true;
159
+ }
160
+ },
161
+ load: {
162
+ pending(draft) {
163
+ draft.pending = true;
164
+ },
165
+ rejected(draft, payload) {
166
+ draft.pending = false;
167
+ draft.error = payload;
168
+ },
169
+ fulfilled(draft, p) {
170
+ draft.items = p;
171
+ },
172
+ },
173
+ },
174
+ effects: {
175
+ async load() {
176
+ const { data } = await contacts();
177
+ return data;
178
+ },
179
+ },
180
+ });
181
+ ```
182
+
183
+ 我们把一个包含 state,action 等要素的 plain object 称作【 Model Spec 】,Modern.js 提供了 [Model API](/docs/apis/app/runtime/model/model_),可以根据 Model Spec 生成【 Model 】。
184
+
185
+ 本小节中,我们联系人列表项目需要的 Model 实现。下一小节我们将会学习如何使用 Model。
@@ -0,0 +1,54 @@
1
+ ---
2
+ title: 使用 Model
3
+ ---
4
+
5
+ 上一小节中,我们实现了 Model,已经把 AllContacts 组件中原有的 UI 无关业务逻辑解耦出来。
6
+
7
+ 这一小节中,我们使用这个 Model 直接把 AllContacts 组件重新还原出来,实现变得更简洁清晰:
8
+
9
+ ```js title="src/contacts/components/AllContacts/index.tsx"
10
+ import { useEffect } from 'react';
11
+ import { useLocalModel } from '@modern-js/runtime/model';
12
+ import { List } from 'antd';
13
+ import Item from '../Item';
14
+ import contacts from '../../models/contacts';
15
+
16
+ const AllContacts = () => {
17
+ const [{ items, error, pending }, actions] = useLocalModel(contacts);
18
+
19
+ useEffect(() => {
20
+ if (!items.length && !error && !pending) {
21
+ actions.load();
22
+ }
23
+ });
24
+ return (
25
+ (items.length && (
26
+ <List
27
+ dataSource={items}
28
+ renderItem={info => <Item key={info.name} info={info} />}
29
+ />
30
+ )) || (
31
+ <div className="p-4 items-center border-gray-200 border-b border-t custom-text-gray">
32
+ Pending...
33
+ </div>
34
+ )
35
+ );
36
+ };
37
+
38
+ export default AllContacts;
39
+ ```
40
+
41
+ 以上代码跟上一章节中 `AllContacts` 组件的代码等价。
42
+
43
+ `useLocalModel` 是 Modern.js 提供的 hooks API,可以使用 Model,在组件中提供 Model 中定义的 state,或通过 actions 调用 Model 中定义的 side effect 与 action,从而改变 Model 的 state。
44
+
45
+ Modern.js 的 Model 能够这样使用的原因是 Model 是基于 FP 的,不像 OOP 的 Model 那样自身持有和封装了状态,对外部提供方法访问和修改自身内部的状态。
46
+
47
+ Model 是业务逻辑,是计算过程,本身不创建也不持有状态,只有在被组件用 hooks API 使用后,才在指定的地方(比如这个例子里,是组件内部的 state)创建状态。
48
+
49
+ 执行 `pnpm run dev`,可以看到跟上一章节一样的效果。
50
+
51
+ ---
52
+
53
+ > 本小节的代码可以在[这里查看](https://github.com/modern-js-dev/modern-js-examples/tree/main/tutorials/c10/hello-modern-3)。
54
+
@@ -0,0 +1,69 @@
1
+ ---
2
+ title: 测试 Model
3
+ ---
4
+
5
+ 上一小节中我们学习了如何使用 Model。
6
+
7
+ 这一小节里我们来测试 Model。
8
+
9
+ 跟[测试组件​​​](../c06-css-and-component/6.6-testing.md)中一样,不需要做任何配置,可以直接给 Model 写测试用例。
10
+
11
+ 以 `models/contacts` 为例,我们创建对应的 `.test` 文件:
12
+
13
+ import Tabs from '@theme/Tabs';
14
+ import TabItem from '@theme/TabItem';
15
+
16
+ <Tabs>
17
+ <TabItem value="macOS" label="macOS" default>
18
+
19
+ ```bash
20
+ touch src/contacts/models/contacts.test.ts
21
+ ```
22
+
23
+ </TabItem>
24
+ <TabItem value="Windows" label="Windows">
25
+
26
+ ```powershell
27
+ ni src/contacts/models/contacts.test.ts
28
+ ```
29
+
30
+ </TabItem>
31
+ </Tabs>
32
+
33
+ 在测试用例中可以使用 Modern.js 提供的 Model 模拟,可以调用这个对象的方法写断言。
34
+
35
+ 我们直接使用 createStore API 来编写测试文件,示例内容如下:
36
+
37
+ ```ts
38
+ import { createStore } from '@modern-js/runtime/testing';
39
+ import contactsModel from './contacts';
40
+
41
+ const mockItem = {
42
+ avatar: '',
43
+ name: '李华',
44
+ age: '16',
45
+ email: 'lihua@gmail.com',
46
+ archived: false,
47
+ };
48
+
49
+ describe('test contracts model', () => {
50
+ it('actions works well', async () => {
51
+ const store = createStore();
52
+ const [state, actions] = store.use(contactsModel);
53
+
54
+ state.items.push(mockItem);
55
+ expect(store.use(contactsModel)[0].items.length).toBe(1);
56
+ expect(store.use(contactsModel)[0].items[0].archived).toBeFalsy();
57
+
58
+ await actions.archive('lihua@gmail.com');
59
+ expect(store.use(contactsModel)[0].items[0].archived).toBeTruthy();
60
+ });
61
+ });
62
+ ```
63
+
64
+ 执行 `pnpm run test`,可以看到测试报告。
65
+
66
+ ---
67
+
68
+ > 本小节的代码可以在[这里查看](https://github.com/modern-js-dev/modern-js-examples/tree/main/tutorials/c10/hello-modern-4)。
69
+
@@ -0,0 +1,3 @@
1
+ {
2
+ "label": "10: 添加业务模型"
3
+ }