@modern-js/main-doc 2.0.0-beta.2 → 2.0.0-beta.4

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 (345) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/en/docusaurus-plugin-content-docs/current/apis/app/commands/inspect.md +0 -4
  3. package/en/docusaurus-plugin-content-docs/current/apis/app/commands/new.md +0 -2
  4. package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/core/bootstrap.md +17 -3
  5. package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/model/create-app.md +1 -1
  6. package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/model/use-store.md +0 -22
  7. package/en/docusaurus-plugin-content-docs/current/components/init-app.md +42 -0
  8. package/en/docusaurus-plugin-content-docs/current/components/reduck-migration.md +1 -0
  9. package/en/docusaurus-plugin-content-docs/current/configure/app/dev/asset-prefix.md +1 -1
  10. package/en/docusaurus-plugin-content-docs/current/configure/app/dev/hmr.md +1 -1
  11. package/en/docusaurus-plugin-content-docs/current/configure/app/dev/https.md +1 -1
  12. package/en/docusaurus-plugin-content-docs/current/configure/app/dev/port.md +1 -1
  13. package/en/docusaurus-plugin-content-docs/current/configure/app/dev/progress-bar.md +1 -1
  14. package/en/docusaurus-plugin-content-docs/current/configure/app/dev/start-url.md +1 -1
  15. package/en/docusaurus-plugin-content-docs/current/configure/app/experiments/lazy-compilation.md +1 -1
  16. package/en/docusaurus-plugin-content-docs/current/configure/app/html/app-icon.md +1 -1
  17. package/en/docusaurus-plugin-content-docs/current/configure/app/html/crossorigin.md +1 -1
  18. package/en/docusaurus-plugin-content-docs/current/configure/app/html/disable-html-folder.md +1 -1
  19. package/en/docusaurus-plugin-content-docs/current/configure/app/html/favicon-by-entries.md +1 -1
  20. package/en/docusaurus-plugin-content-docs/current/configure/app/html/favicon.md +1 -1
  21. package/en/docusaurus-plugin-content-docs/current/configure/app/html/inject-by-entries.md +1 -1
  22. package/en/docusaurus-plugin-content-docs/current/configure/app/html/inject.md +1 -1
  23. package/en/docusaurus-plugin-content-docs/current/configure/app/html/meta-by-entries.md +1 -1
  24. package/en/docusaurus-plugin-content-docs/current/configure/app/html/meta.md +1 -1
  25. package/en/docusaurus-plugin-content-docs/current/configure/app/html/mount-id.md +1 -1
  26. package/en/docusaurus-plugin-content-docs/current/configure/app/html/template-by-entries.md +1 -1
  27. package/en/docusaurus-plugin-content-docs/current/configure/app/html/template-parameters-by-entries.md +1 -1
  28. package/en/docusaurus-plugin-content-docs/current/configure/app/html/template-parameters.md +1 -1
  29. package/en/docusaurus-plugin-content-docs/current/configure/app/html/template.md +1 -1
  30. package/en/docusaurus-plugin-content-docs/current/configure/app/html/title-by-entries.md +1 -1
  31. package/en/docusaurus-plugin-content-docs/current/configure/app/html/title.md +1 -1
  32. package/en/docusaurus-plugin-content-docs/current/configure/app/output/asset-prefix.md +1 -1
  33. package/en/docusaurus-plugin-content-docs/current/configure/app/output/assets-retry.md +1 -1
  34. package/en/docusaurus-plugin-content-docs/current/configure/app/output/charset.md +1 -1
  35. package/en/docusaurus-plugin-content-docs/current/configure/app/output/clean-dist-path.md +1 -1
  36. package/en/docusaurus-plugin-content-docs/current/configure/app/output/convert-to-rem.md +1 -1
  37. package/en/docusaurus-plugin-content-docs/current/configure/app/output/copy.md +1 -1
  38. package/en/docusaurus-plugin-content-docs/current/configure/app/output/css-module-local-ident-name.md +1 -1
  39. package/en/docusaurus-plugin-content-docs/current/configure/app/output/data-uri-limit.md +1 -1
  40. package/en/docusaurus-plugin-content-docs/current/configure/app/output/disable-css-extract.md +1 -1
  41. package/en/docusaurus-plugin-content-docs/current/configure/app/output/disable-css-module-extension.md +1 -1
  42. package/en/docusaurus-plugin-content-docs/current/configure/app/output/disable-filename-hash.md +1 -1
  43. package/en/docusaurus-plugin-content-docs/current/configure/app/output/disable-inline-runtime-chunk.md +1 -1
  44. package/en/docusaurus-plugin-content-docs/current/configure/app/output/disable-minimize.md +1 -1
  45. package/en/docusaurus-plugin-content-docs/current/configure/app/output/disable-source-map.md +1 -1
  46. package/en/docusaurus-plugin-content-docs/current/configure/app/output/disable-ts-checker.md +1 -1
  47. package/en/docusaurus-plugin-content-docs/current/configure/app/output/dist-path.md +1 -1
  48. package/en/docusaurus-plugin-content-docs/current/configure/app/output/enable-asset-fallback.md +1 -1
  49. package/en/docusaurus-plugin-content-docs/current/configure/app/output/enable-asset-manifest.md +1 -1
  50. package/en/docusaurus-plugin-content-docs/current/configure/app/output/enable-css-module-tsdeclaration.md +1 -1
  51. package/en/docusaurus-plugin-content-docs/current/configure/app/output/enable-inline-scripts.md +1 -1
  52. package/en/docusaurus-plugin-content-docs/current/configure/app/output/enable-inline-styles.md +1 -1
  53. package/en/docusaurus-plugin-content-docs/current/configure/app/output/enable-latest-decorators.md +1 -1
  54. package/en/docusaurus-plugin-content-docs/current/configure/app/output/externals.md +1 -1
  55. package/en/docusaurus-plugin-content-docs/current/configure/app/output/filename.md +1 -1
  56. package/en/docusaurus-plugin-content-docs/current/configure/app/output/legal-comments.md +1 -1
  57. package/en/docusaurus-plugin-content-docs/current/configure/app/output/override-browserslist.md +1 -1
  58. package/en/docusaurus-plugin-content-docs/current/configure/app/output/polyfill.md +1 -1
  59. package/en/docusaurus-plugin-content-docs/current/configure/app/output/svg-default-export.md +1 -1
  60. package/en/docusaurus-plugin-content-docs/current/configure/app/performance/build-cache.md +1 -1
  61. package/en/docusaurus-plugin-content-docs/current/configure/app/performance/bundle-analyze.md +1 -1
  62. package/en/docusaurus-plugin-content-docs/current/configure/app/performance/chunk-split.md +1 -1
  63. package/en/docusaurus-plugin-content-docs/current/configure/app/performance/print-file-size.md +1 -1
  64. package/en/docusaurus-plugin-content-docs/current/configure/app/performance/profile.md +1 -1
  65. package/en/docusaurus-plugin-content-docs/current/configure/app/performance/remove-console.md +1 -1
  66. package/en/docusaurus-plugin-content-docs/current/configure/app/performance/remove-moment-locale.md +1 -1
  67. package/en/docusaurus-plugin-content-docs/current/configure/app/plugins.md +1 -1
  68. package/en/docusaurus-plugin-content-docs/current/configure/app/security/sri.md +1 -1
  69. package/en/docusaurus-plugin-content-docs/current/configure/app/server/routes.md +2 -4
  70. package/en/docusaurus-plugin-content-docs/current/configure/app/source/alias.md +1 -1
  71. package/en/docusaurus-plugin-content-docs/current/configure/app/source/compile-js-data-uri.md +1 -1
  72. package/en/docusaurus-plugin-content-docs/current/configure/app/source/define.md +1 -1
  73. package/en/docusaurus-plugin-content-docs/current/configure/app/source/exclude.md +1 -1
  74. package/en/docusaurus-plugin-content-docs/current/configure/app/source/global-vars.md +1 -1
  75. package/en/docusaurus-plugin-content-docs/current/configure/app/source/include.md +1 -1
  76. package/en/docusaurus-plugin-content-docs/current/configure/app/source/module-scopes.md +1 -1
  77. package/en/docusaurus-plugin-content-docs/current/configure/app/source/pre-entry.md +1 -1
  78. package/en/docusaurus-plugin-content-docs/current/configure/app/source/resolve-extension-prefix.md +1 -1
  79. package/en/docusaurus-plugin-content-docs/current/configure/app/source/resolve-main-fields.md +1 -1
  80. package/en/docusaurus-plugin-content-docs/current/configure/app/tools/autoprefixer.md +1 -1
  81. package/en/docusaurus-plugin-content-docs/current/configure/app/tools/babel.md +1 -1
  82. package/en/docusaurus-plugin-content-docs/current/configure/app/tools/css-extract.md +1 -1
  83. package/en/docusaurus-plugin-content-docs/current/configure/app/tools/css-loader.md +1 -1
  84. package/en/docusaurus-plugin-content-docs/current/configure/app/tools/dev-server.md +1 -1
  85. package/en/docusaurus-plugin-content-docs/current/configure/app/tools/esbuild.md +16 -39
  86. package/en/docusaurus-plugin-content-docs/current/configure/app/tools/html-plugin.md +1 -1
  87. package/en/docusaurus-plugin-content-docs/current/configure/app/tools/inspector.md +1 -1
  88. package/en/docusaurus-plugin-content-docs/current/configure/app/tools/less.md +1 -1
  89. package/en/docusaurus-plugin-content-docs/current/configure/app/tools/minify-css.md +1 -1
  90. package/en/docusaurus-plugin-content-docs/current/configure/app/tools/postcss.md +1 -1
  91. package/en/docusaurus-plugin-content-docs/current/configure/app/tools/pug.md +1 -1
  92. package/en/docusaurus-plugin-content-docs/current/configure/app/tools/sass.md +1 -1
  93. package/en/docusaurus-plugin-content-docs/current/configure/app/tools/style-loader.md +1 -1
  94. package/en/docusaurus-plugin-content-docs/current/configure/app/tools/styled-components.md +1 -1
  95. package/en/docusaurus-plugin-content-docs/current/configure/app/tools/swc.md +42 -0
  96. package/en/docusaurus-plugin-content-docs/current/configure/app/tools/terser.md +1 -1
  97. package/en/docusaurus-plugin-content-docs/current/configure/app/tools/ts-checker.md +1 -1
  98. package/en/docusaurus-plugin-content-docs/current/configure/app/tools/ts-loader.md +1 -1
  99. package/en/docusaurus-plugin-content-docs/current/configure/app/tools/webpack-chain.md +1 -1
  100. package/en/docusaurus-plugin-content-docs/current/configure/app/tools/webpack.md +1 -1
  101. package/en/docusaurus-plugin-content-docs/current/guides/basic-features/builder.md +46 -0
  102. package/en/docusaurus-plugin-content-docs/current/guides/basic-features/css/css-in-js.md +38 -0
  103. package/en/docusaurus-plugin-content-docs/current/guides/basic-features/css/css-modules.md +86 -0
  104. package/en/docusaurus-plugin-content-docs/current/guides/basic-features/css/less-sass.md +17 -0
  105. package/en/docusaurus-plugin-content-docs/current/guides/basic-features/css/postcss.md +81 -0
  106. package/en/docusaurus-plugin-content-docs/current/guides/basic-features/css/tailwindcss.md +95 -0
  107. package/en/docusaurus-plugin-content-docs/current/guides/basic-features/data-fetch.md +66 -0
  108. package/en/docusaurus-plugin-content-docs/current/guides/basic-features/routes.md +270 -0
  109. package/en/docusaurus-plugin-content-docs/current/guides/concept/entries.md +116 -0
  110. package/en/docusaurus-plugin-content-docs/current/guides/concept/lifecycle.md +15 -0
  111. package/en/docusaurus-plugin-content-docs/current/guides/get-started/quick-start.md +162 -0
  112. package/en/docusaurus-plugin-content-docs/current/guides/get-started/upgrade.md +78 -0
  113. package/{zh/tutorials/first-app → en/docusaurus-plugin-content-docs/current/guides}/overview.md +4 -4
  114. package/en/docusaurus-plugin-content-docs/current/tutorials/foundations/introduction.md +1 -1
  115. package/en/docusaurus-plugin-content-docs/current.json +29 -29
  116. package/package.json +3 -3
  117. package/scripts/config.ts +2 -2
  118. package/zh/apis/app/commands/inspect.md +0 -4
  119. package/zh/apis/app/commands/new.md +1 -3
  120. package/zh/apis/app/hooks/src/index_.md +6 -5
  121. package/zh/apis/app/runtime/app/_category_.json +1 -1
  122. package/zh/apis/app/runtime/bff/_category_.json +1 -1
  123. package/zh/apis/app/runtime/core/_category_.json +1 -1
  124. package/zh/apis/app/runtime/core/bootstrap.md +17 -3
  125. package/zh/apis/app/runtime/model/_category_.json +1 -1
  126. package/zh/apis/app/runtime/model/create-app.md +1 -1
  127. package/zh/apis/app/runtime/model/use-store.md +1 -23
  128. package/zh/apis/app/runtime/router/_category_.json +1 -1
  129. package/zh/apis/app/runtime/ssr/_category_.json +1 -1
  130. package/zh/apis/app/runtime/testing/_category_.json +1 -1
  131. package/zh/apis/app/runtime/utility/_category_.json +1 -1
  132. package/zh/apis/app/runtime/web-server/_category_.json +1 -1
  133. package/zh/components/debug-app.md +18 -0
  134. package/zh/components/global-proxy.md +28 -0
  135. package/zh/components/init-app.md +44 -0
  136. package/zh/components/prerequisites.md +19 -0
  137. package/zh/components/reduck-migration.md +1 -0
  138. package/zh/configure/app/dev/asset-prefix.md +1 -1
  139. package/zh/configure/app/dev/hmr.md +1 -1
  140. package/zh/configure/app/dev/https.md +1 -1
  141. package/zh/configure/app/dev/port.md +1 -1
  142. package/zh/configure/app/dev/progress-bar.md +1 -1
  143. package/zh/configure/app/dev/start-url.md +1 -1
  144. package/zh/configure/app/experiments/_category_.json +4 -0
  145. package/zh/configure/app/experiments/lazy-compilation.md +1 -1
  146. package/zh/configure/app/html/_category_.json +4 -0
  147. package/zh/configure/app/html/app-icon.md +1 -1
  148. package/zh/configure/app/html/crossorigin.md +1 -1
  149. package/zh/configure/app/html/disable-html-folder.md +1 -1
  150. package/zh/configure/app/html/favicon-by-entries.md +1 -1
  151. package/zh/configure/app/html/favicon.md +1 -1
  152. package/zh/configure/app/html/inject-by-entries.md +1 -1
  153. package/zh/configure/app/html/inject.md +1 -1
  154. package/zh/configure/app/html/meta-by-entries.md +1 -1
  155. package/zh/configure/app/html/meta.md +1 -1
  156. package/zh/configure/app/html/mount-id.md +1 -1
  157. package/zh/configure/app/html/template-by-entries.md +1 -1
  158. package/zh/configure/app/html/template-parameters-by-entries.md +1 -1
  159. package/zh/configure/app/html/template-parameters.md +1 -1
  160. package/zh/configure/app/html/template.md +1 -1
  161. package/zh/configure/app/html/title-by-entries.md +1 -1
  162. package/zh/configure/app/html/title.md +1 -1
  163. package/zh/configure/app/output/asset-prefix.md +1 -1
  164. package/zh/configure/app/output/assets-retry.md +1 -1
  165. package/zh/configure/app/output/charset.md +1 -1
  166. package/zh/configure/app/output/clean-dist-path.md +1 -1
  167. package/zh/configure/app/output/convert-to-rem.md +1 -1
  168. package/zh/configure/app/output/copy.md +1 -1
  169. package/zh/configure/app/output/css-module-local-ident-name.md +1 -1
  170. package/zh/configure/app/output/data-uri-limit.md +1 -1
  171. package/zh/configure/app/output/disable-css-extract.md +1 -1
  172. package/zh/configure/app/output/disable-css-module-extension.md +1 -1
  173. package/zh/configure/app/output/disable-filename-hash.md +1 -1
  174. package/zh/configure/app/output/disable-inline-runtime-chunk.md +1 -1
  175. package/zh/configure/app/output/disable-minimize.md +1 -1
  176. package/zh/configure/app/output/disable-source-map.md +1 -1
  177. package/zh/configure/app/output/disable-ts-checker.md +1 -1
  178. package/zh/configure/app/output/dist-path.md +1 -1
  179. package/zh/configure/app/output/enable-asset-fallback.md +1 -1
  180. package/zh/configure/app/output/enable-asset-manifest.md +1 -1
  181. package/zh/configure/app/output/enable-css-module-tsdeclaration.md +1 -1
  182. package/zh/configure/app/output/enable-inline-scripts.md +1 -1
  183. package/zh/configure/app/output/enable-inline-styles.md +1 -1
  184. package/zh/configure/app/output/enable-latest-decorators.md +1 -1
  185. package/zh/configure/app/output/externals.md +1 -1
  186. package/zh/configure/app/output/filename.md +1 -1
  187. package/zh/configure/app/output/legal-comments.md +1 -1
  188. package/zh/configure/app/output/override-browserslist.md +1 -1
  189. package/zh/configure/app/output/polyfill.md +1 -1
  190. package/zh/configure/app/output/ssg.md +118 -114
  191. package/zh/configure/app/output/svg-default-export.md +1 -1
  192. package/zh/configure/app/performance/_category_.json +4 -0
  193. package/zh/configure/app/performance/build-cache.md +1 -1
  194. package/zh/configure/app/performance/bundle-analyze.md +1 -1
  195. package/zh/configure/app/performance/chunk-split.md +1 -1
  196. package/zh/configure/app/performance/print-file-size.md +1 -1
  197. package/zh/configure/app/performance/profile.md +1 -1
  198. package/zh/configure/app/performance/remove-console.md +1 -1
  199. package/zh/configure/app/performance/remove-moment-locale.md +1 -1
  200. package/zh/configure/app/plugins.md +1 -1
  201. package/zh/configure/app/runtime/state.md +13 -0
  202. package/zh/configure/app/security/_category_.json +4 -0
  203. package/zh/configure/app/security/sri.md +1 -1
  204. package/zh/configure/app/server/routes.md +2 -4
  205. package/zh/configure/app/server/ssr.md +0 -2
  206. package/zh/configure/app/source/alias.md +1 -1
  207. package/zh/configure/app/source/compile-js-data-uri.md +1 -1
  208. package/zh/configure/app/source/define.md +1 -1
  209. package/zh/configure/app/source/exclude.md +1 -1
  210. package/zh/configure/app/source/global-vars.md +1 -1
  211. package/zh/configure/app/source/include.md +1 -1
  212. package/zh/configure/app/source/module-scopes.md +1 -1
  213. package/zh/configure/app/source/pre-entry.md +1 -1
  214. package/zh/configure/app/source/resolve-extension-prefix.md +1 -1
  215. package/zh/configure/app/source/resolve-main-fields.md +1 -1
  216. package/zh/configure/app/tools/autoprefixer.md +1 -1
  217. package/zh/configure/app/tools/babel.md +1 -1
  218. package/zh/configure/app/tools/css-extract.md +1 -1
  219. package/zh/configure/app/tools/css-loader.md +1 -1
  220. package/zh/configure/app/tools/dev-server.md +1 -1
  221. package/zh/configure/app/tools/esbuild.md +16 -39
  222. package/zh/configure/app/tools/html-plugin.md +1 -1
  223. package/zh/configure/app/tools/inspector.md +1 -1
  224. package/zh/configure/app/tools/less.md +1 -1
  225. package/zh/configure/app/tools/minify-css.md +1 -1
  226. package/zh/configure/app/tools/postcss.md +1 -1
  227. package/zh/configure/app/tools/pug.md +1 -1
  228. package/zh/configure/app/tools/sass.md +1 -1
  229. package/zh/configure/app/tools/style-loader.md +1 -1
  230. package/zh/configure/app/tools/styled-components.md +1 -1
  231. package/zh/configure/app/tools/swc.md +42 -0
  232. package/zh/configure/app/tools/terser.md +1 -1
  233. package/zh/configure/app/tools/ts-checker.md +1 -1
  234. package/zh/configure/app/tools/ts-loader.md +1 -1
  235. package/zh/configure/app/tools/webpack-chain.md +1 -1
  236. package/zh/configure/app/tools/webpack.md +1 -1
  237. package/zh/guides/advanced-features/bff/bff-proxy.md +1 -1
  238. package/zh/guides/advanced-features/compatibility.md +2 -38
  239. package/zh/guides/advanced-features/custom-app.md +23 -19
  240. package/zh/guides/advanced-features/ssg.md +73 -62
  241. package/zh/guides/advanced-features/ssr.md +142 -36
  242. package/zh/guides/advanced-features/testing.md +33 -1
  243. package/zh/guides/advanced-features/web-server.md +2 -2
  244. package/zh/guides/basic-features/builder.md +46 -0
  245. package/zh/guides/basic-features/css/_category_.json +1 -1
  246. package/zh/guides/basic-features/css/less-sass.md +1 -14
  247. package/zh/guides/basic-features/css/tailwindcss.md +2 -6
  248. package/zh/guides/basic-features/data-fetch.md +1 -1
  249. package/zh/guides/basic-features/env-vars.md +34 -0
  250. package/zh/guides/basic-features/html.md +182 -0
  251. package/zh/guides/basic-features/mock.md +3 -9
  252. package/zh/guides/basic-features/proxy.md +2 -27
  253. package/zh/guides/basic-features/routes.md +32 -35
  254. package/zh/guides/concept/entries.md +8 -9
  255. package/zh/guides/get-started/quick-start.md +6 -78
  256. package/zh/guides/get-started/upgrade.md +8 -8
  257. package/zh/guides/topic-detail/framework-plugin/extend.md +3 -4
  258. package/zh/{apis/app/runtime/plugin/hook-api.md → guides/topic-detail/framework-plugin/hook-list.md} +42 -135
  259. package/zh/{apis/app/runtime/plugin → guides/topic-detail/framework-plugin}/hook.md +2 -3
  260. package/zh/guides/topic-detail/framework-plugin/implement.md +21 -10
  261. package/zh/guides/topic-detail/framework-plugin/introduction.md +49 -0
  262. package/{en/docusaurus-plugin-content-docs/current/apis/app/runtime/plugin → zh/guides/topic-detail/framework-plugin}/plugin-api.md +3 -4
  263. package/zh/guides/topic-detail/framework-plugin/relationship.md +2 -3
  264. package/zh/guides/topic-detail/model/quick-start.md +5 -1
  265. package/zh/guides/topic-detail/model/test-model.md +2 -2
  266. package/zh/guides/topic-detail/monorepo/intro.md +1 -1
  267. package/zh/guides/troubleshooting/dependencies.md +0 -69
  268. package/zh/tutorials/first-app/_category_.json +1 -1
  269. package/zh/tutorials/first-app/c01-start.md +94 -0
  270. package/zh/tutorials/first-app/c02-component.md +56 -0
  271. package/zh/tutorials/first-app/c03-css.md +305 -0
  272. package/zh/tutorials/first-app/c04-routes.md +169 -0
  273. package/zh/tutorials/first-app/c05-loader.md +82 -0
  274. package/zh/tutorials/first-app/c06-model.md +256 -0
  275. package/zh/tutorials/first-app/c07-container.md +268 -0
  276. package/zh/tutorials/first-app/c08-entries.md +134 -0
  277. package/zh/tutorials/foundations/introduction.md +2 -2
  278. package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/default-alias.md +0 -25
  279. package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/plugin/_category_.json +0 -4
  280. package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/plugin/abstract.md +0 -26
  281. package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/plugin/hook-api.md +0 -896
  282. package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/plugin/hook.md +0 -170
  283. package/en/docusaurus-plugin-content-docs/current/configure/app/output/enable-modern-mode.md +0 -34
  284. package/zh/apis/app/runtime/default-alias.md +0 -23
  285. package/zh/apis/app/runtime/plugin/_category_.json +0 -4
  286. package/zh/apis/app/runtime/plugin/abstract.md +0 -26
  287. package/zh/apis/app/runtime/plugin/plugin-api.md +0 -117
  288. package/zh/apis/generator/overview.md +0 -32
  289. package/zh/configure/app/output/enable-modern-mode.md +0 -34
  290. package/zh/guides/basic-features/image.md +0 -43
  291. package/zh/guides/topic-detail/compile-speed.md +0 -182
  292. package/zh/guides/topic-detail/framework-plugin/abstract.md +0 -27
  293. package/zh/guides/topic-detail/monorepo/deploy.md +0 -43
  294. package/zh/guides/troubleshooting/compile.md +0 -379
  295. package/zh/tutorials/first-app/c01-getting-started/1.1-prerequisites.md +0 -25
  296. package/zh/tutorials/first-app/c01-getting-started/1.2-minimal-mwa.md +0 -118
  297. package/zh/tutorials/first-app/c01-getting-started/1.3-dev-command.md +0 -29
  298. package/zh/tutorials/first-app/c01-getting-started/1.4-enable-ssr.md +0 -44
  299. package/zh/tutorials/first-app/c01-getting-started/1.5-start-command.md +0 -18
  300. package/zh/tutorials/first-app/c01-getting-started/1.6-create-repo.md +0 -31
  301. package/zh/tutorials/first-app/c01-getting-started/_category_.json +0 -3
  302. package/zh/tutorials/first-app/c02-generator-and-studio/2.1-generator.md +0 -79
  303. package/zh/tutorials/first-app/c02-generator-and-studio/2.2-boilerplates.md +0 -36
  304. package/zh/tutorials/first-app/c02-generator-and-studio/2.3-configuration.md +0 -21
  305. package/zh/tutorials/first-app/c02-generator-and-studio/_category_.json +0 -3
  306. package/zh/tutorials/first-app/c03-ide/3.1-setting-up.md +0 -55
  307. package/zh/tutorials/first-app/c03-ide/3.2-hints-in-ide.md +0 -66
  308. package/zh/tutorials/first-app/c03-ide/3.3-autofix-in-ide.md +0 -11
  309. package/zh/tutorials/first-app/c03-ide/3.4-autofix-in-cli.md +0 -63
  310. package/zh/tutorials/first-app/c03-ide/_category_.json +0 -3
  311. package/zh/tutorials/first-app/c04-es6-plus-and-ts/4.1-use-es6-plus.md +0 -67
  312. package/zh/tutorials/first-app/c04-es6-plus-and-ts/4.2-use-typescript.md +0 -111
  313. package/zh/tutorials/first-app/c04-es6-plus-and-ts/4.3-compatibility.md +0 -67
  314. package/zh/tutorials/first-app/c04-es6-plus-and-ts/_category_.json +0 -3
  315. package/zh/tutorials/first-app/c05-component/5.1-use-ui-library.md +0 -68
  316. package/zh/tutorials/first-app/c05-component/5.2-use-standalone-component.md +0 -92
  317. package/zh/tutorials/first-app/c05-component/_category_.json +0 -3
  318. package/zh/tutorials/first-app/c06-css-and-component/6.1-css-in-js.md +0 -110
  319. package/zh/tutorials/first-app/c06-css-and-component/6.2-utility-class.md +0 -148
  320. package/zh/tutorials/first-app/c06-css-and-component/6.3-postcss.md +0 -84
  321. package/zh/tutorials/first-app/c06-css-and-component/6.4-design-system.md +0 -83
  322. package/zh/tutorials/first-app/c06-css-and-component/6.5-storybook.md +0 -77
  323. package/zh/tutorials/first-app/c06-css-and-component/6.6-testing.md +0 -113
  324. package/zh/tutorials/first-app/c06-css-and-component/_category_.json +0 -3
  325. package/zh/tutorials/first-app/c07-app-entry/7.1-intro.md +0 -64
  326. package/zh/tutorials/first-app/c07-app-entry/7.2-add-entry-in-cli.md +0 -100
  327. package/zh/tutorials/first-app/c07-app-entry/7.3-manage-entries-by-hand.md +0 -74
  328. package/zh/tutorials/first-app/c07-app-entry/_category_.json +0 -3
  329. package/zh/tutorials/first-app/c08-client-side-routing/8.1-code-based-routing.md +0 -153
  330. package/zh/tutorials/first-app/c08-client-side-routing/8.2-file-based-routing.md +0 -310
  331. package/zh/tutorials/first-app/c08-client-side-routing/_category_.json +0 -3
  332. package/zh/tutorials/first-app/c09-bff/9.1-serverless.md +0 -30
  333. package/zh/tutorials/first-app/c09-bff/9.2-enable-bff.md +0 -93
  334. package/zh/tutorials/first-app/c09-bff/9.3-fetch-bff.md +0 -205
  335. package/zh/tutorials/first-app/c09-bff/_category_.json +0 -3
  336. package/zh/tutorials/first-app/c10-model/10.1-application-architecture.md +0 -23
  337. package/zh/tutorials/first-app/c10-model/10.2-add-model.md +0 -185
  338. package/zh/tutorials/first-app/c10-model/10.3-use-model.md +0 -54
  339. package/zh/tutorials/first-app/c10-model/10.4-testing.md +0 -69
  340. package/zh/tutorials/first-app/c10-model/_category_.json +0 -3
  341. package/zh/tutorials/first-app/c11-container/11.1-use-model-with-app-state.md +0 -274
  342. package/zh/tutorials/first-app/c11-container/11.2-add-container.md +0 -106
  343. package/zh/tutorials/first-app/c11-container/11.3-use-loader.md +0 -61
  344. package/zh/tutorials/first-app/c11-container/11.4-testing.md +0 -56
  345. package/zh/tutorials/first-app/c11-container/_category_.json +0 -3
@@ -1,185 +0,0 @@
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。
@@ -1,54 +0,0 @@
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
-
@@ -1,69 +0,0 @@
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
-
@@ -1,3 +0,0 @@
1
- {
2
- "label": "10: 添加业务模型"
3
- }
@@ -1,274 +0,0 @@
1
- ---
2
- title: ​完整使用 Model
3
- ---
4
-
5
- 上一章节中,我们初步引入**客户端应用架构**,从【 视图组件 】中拆分出【 业务模型(Model)】,`AllContacts` 中不再包含 UI 无关的业务逻辑实现细节,只需要使用 Model,就能实现同样的功能。
6
-
7
- 这一章节中,我们要进一步利用 Model 中实现的业务逻辑,让 `AllContacts` 和 `ArchivedContacts` 都从 BFF 获取数据,实现 Archive 按钮,点击按钮能把联系人归档,只显示在 Archives 列表里,不显示在 All 列表里。
8
-
9
- 先改造 `Item` 组件,增加 Archive 按钮的交互实现:
10
-
11
- ```tsx title="src/contacts/components/Item/index.tsx"
12
- import Avatar from '../Avatar';
13
-
14
- type InfoProps = {
15
- avatar: string;
16
- name: string;
17
- email: string;
18
- archived?: boolean;
19
- };
20
-
21
- const Item = ({
22
- info,
23
- onArchive,
24
- }: {
25
- info: InfoProps;
26
- onArchive?: () => void;
27
- }) => {
28
- const { avatar, name, email, archived } = info;
29
- return (
30
- <div className="flex p-4 items-center border-gray-200 border-b">
31
- <Avatar src={avatar} />
32
- <div className="ml-4 custom-text-gray flex-1 flex justify-between">
33
- <div className="flex-1">
34
- <p>{name}</p>
35
- <p>{email}</p>
36
- </div>
37
- <button
38
- type="button"
39
- disabled={archived}
40
- onClick={onArchive}
41
- className={`text-white font-bold py-2 px-4 rounded-full ${
42
- archived
43
- ? 'bg-gray-400 cursor-default'
44
- : 'bg-blue-500 hover:bg-blue-700'
45
- }`}>
46
- {archived ? 'Archived' : 'Archive'}
47
- </button>
48
- </div>
49
- </div>
50
- );
51
- };
52
-
53
- export default Item;
54
- ```
55
-
56
- `ArchivedContacts` 和 `AllContacts` 需要共用同一套状态(联系人列表数据、联系人是否被归档),并且由于 Archives 列表和 All 列表都可能是第一屏页面(从不同 URL 访问),这两个组件都需要包含加载初始数据的逻辑(如果客户端没有联系人列表数据,就请求 BFF),所以这类两个组件公用的实现逻辑应该合并到一起:
57
-
58
- 我们删除原有的两个组件,创建一个新的 `Contacts` 组件:
59
-
60
- import Tabs from '@theme/Tabs';
61
- import TabItem from '@theme/TabItem';
62
-
63
- <Tabs>
64
- <TabItem value="macOS" label="macOS" default>
65
-
66
- ```bash
67
- rm -r src/contacts/components/*Contacts
68
- mkdir -p src/contacts/components/Contacts/
69
- touch src/contacts/components/Contacts/index.tsx
70
- ```
71
-
72
- </TabItem>
73
- <TabItem value="Windows" label="Windows">
74
-
75
- ```powershell
76
- rm -r src/contacts/components/*Contacts
77
- mkdir -p src/contacts/components/Contacts/
78
- ni src/contacts/components/Contacts/index.tsx
79
- ```
80
-
81
- </TabItem>
82
- </Tabs>
83
-
84
- 修改`components/Contacts/index.tsx` ,内容如下:
85
-
86
- ```tsx title="src/contacts/components/Contacts/index.tsx"
87
- import { useEffect } from 'react';
88
- import { useLocalModel } from '@modern-js/runtime/model';
89
- import { List } from 'antd';
90
- import contacts from '../../models/contacts';
91
- import Item from '../Item';
92
-
93
- const Contacts = ({ source }: { source: 'archived' | 'items' }) => {
94
- const [state, actions] = useLocalModel(contacts);
95
- const { items, error, pending } = state;
96
- useEffect(() => {
97
- if (!items.length && !error && !pending) {
98
- actions.load();
99
- }
100
- });
101
-
102
- const data = state.items.filter(item =>
103
- source === 'archived' ? item.archived : true,
104
- );
105
-
106
- return (
107
- (items.length && (
108
- <List
109
- dataSource={data}
110
- renderItem={info => (
111
- <Item
112
- key={info.email}
113
- info={info}
114
- onArchive={() => {
115
- actions.archive(info.email);
116
- }}
117
- />
118
- )}
119
- />
120
- )) || (
121
- <div className="p-4 items-center border-gray-200 border-b border-t custom-text-gray">
122
- Pending...
123
- </div>
124
- )
125
- );
126
- };
127
-
128
- export default Contacts;
129
- ```
130
-
131
- :::info 注
132
- 由于 computed 功能还未提供,这里先在组件里将传入的数据做预处理。
133
- :::
134
-
135
- 最后改造 `App.tsx`,利用 Contacts 实现 Archives 列表和 All 列表:
136
-
137
- ```tsx title="src/contacts/App.tsx"
138
- import { useState } from 'react';
139
- import { Radio, RadioChangeEvent } from 'antd';
140
- import { Route, useHistory } from '@modern-js/runtime/router';
141
- import { Helmet } from '@modern-js/runtime/head';
142
- import 'tailwindcss/base.css';
143
- import 'tailwindcss/components.css';
144
- import 'tailwindcss/utilities.css';
145
- import './styles/utils.css';
146
- import Contacts from './components/Contacts';
147
-
148
- function App() {
149
- const history = useHistory();
150
- const [currentList, setList] = useState(history.location.pathname || '/');
151
- const handleSetList = (e: RadioChangeEvent) => {
152
- const { value } = e.target;
153
- setList(value);
154
- history.push(value);
155
- };
156
-
157
- return (
158
- <div className="container lg mx-auto">
159
- <div className="h-16 p-2 flex items-center justify-center">
160
- <Radio.Group onChange={handleSetList} value={currentList}>
161
- <Radio value="/">All</Radio>
162
- <Radio value="/archives">Archives</Radio>
163
- </Radio.Group>
164
- </div>
165
- <Route path="/" exact={true}>
166
- <Helmet>
167
- <title>All</title>
168
- </Helmet>
169
- <Contacts source="items" />
170
- </Route>
171
- <Route path="/archives" exact={true}>
172
- <Helmet>
173
- <title>Archives</title>
174
- </Helmet>
175
- <Contacts source="archived" />
176
- </Route>
177
- </div>
178
- );
179
- }
180
-
181
- export default App;
182
- ```
183
-
184
- 执行 `pnpm run dev`,访问 `http://localhost:8080/contacts/`,点击 Archive 按钮后,可以看到按钮置灰:
185
-
186
- ![display](https://lf3-static.bytednsdoc.com/obj/eden-cn/aphqeh7uhohpquloj/modern-js/docs/11/display.png)
187
-
188
- 接下来点击顶部导航,切换到 Archives 列表,我们预期的时候能看到列表里显示刚才归档的联系人,但实际上列表是空的:
189
-
190
- ![display7](https://lf3-static.bytednsdoc.com/obj/eden-cn/aphqeh7uhohpquloj/modern-js/docs/11/display7.png)
191
-
192
- 出现这个问题的原因是,我们继续沿用了上一节的 `useLocalModel` API 来使用 Model,状态被保存到了组件内部的 state 里,而 `All` 列表和 `Archives` 列表中分别调用的 `Contacts` 组件,是两个各自独立的组件:
193
-
194
- ```tsx title="src/contacts/App.tsx"
195
- <Route path="/" exact={true}>
196
- <Helmet>
197
- <title>All</title>
198
- </Helmet>
199
- <Contacts source="items" />
200
- </Route>
201
- <Route path="/archives" exact={true}>
202
- <Helmet>
203
- <title>Archives</title>
204
- </Helmet>
205
- <Contacts source="archived" />
206
- </Route>
207
- ```
208
-
209
- 所以它们有各自独立的内部 state,互相不共享状态,渲染 `Archives` 列表的时候,`items` 仍然是初始状态。
210
-
211
- 要解决这个问题,一种方式把 `useLocalModel` 的逻辑提升到父组件里,把状态分别传给两个 `Contacts` 组件。更清晰、完善的方式,是启用全局唯一的「 应用状态 」,两个 `Contacts` 组件「 连接 」应用状态。
212
-
213
- 在 Modern.js 里实现应用状态管理很简单,只需要把 `useLocalModel` 换成 `useModel`。
214
-
215
- 修改 `components/Contacts/index.tsx` 的内容:
216
-
217
-
218
- ```tsx title="src/contacts/components/Contacts/index.tsx"
219
- import { useEffect } from 'react';
220
- import { useModel } from '@modern-js/runtime/model';
221
- import { List } from 'antd';
222
- import contacts from '../../models/contacts';
223
- import Item from '../Item';
224
-
225
- const Contacts = ({ source }: { source: 'archived' | 'items' }) => {
226
- const [state, actions] = useModel(contacts);
227
- const { items, error, pending } = state;
228
- useEffect(() => {
229
- if (!items.length && !error && !pending) {
230
- actions.load();
231
- }
232
- });
233
-
234
- const data = state.items.filter(item =>
235
- source === 'archived' ? item.archived : true,
236
- );
237
-
238
- return (
239
- (items.length && (
240
- <List
241
- dataSource={data}
242
- renderItem={info => (
243
- <Item
244
- key={info.email}
245
- info={info}
246
- onArchive={() => {
247
- actions.archive(info.email);
248
- }}
249
- />
250
- )}
251
- />
252
- )) || (
253
- <div className="p-4 items-center border-gray-200 border-b border-t custom-text-gray">
254
- Pending...
255
- </div>
256
- )
257
- );
258
- };
259
-
260
- export default Contacts;
261
- ```
262
-
263
- 重新执行 `pnpm run dev`,重复刚才的操作,可以看到 Archives 列表能正常显示了:
264
-
265
- ![display1](https://lf3-static.bytednsdoc.com/obj/eden-cn/aphqeh7uhohpquloj/modern-js/docs/11/display1.png)
266
-
267
- :::info 注
268
- useModel API 还可以设置 Selector,只连接这个 Model 定义的状态中的局部。
269
- :::
270
-
271
- ---
272
-
273
- > 本小节的代码可以在[这里查看](https://github.com/modern-js-dev/modern-js-examples/tree/main/tutorials/c11/hello-modern)。
274
-
@@ -1,106 +0,0 @@
1
- ---
2
- title: 容器组件(Container)
3
- ---
4
-
5
- 前两个章节中,我们把项目中的业务逻辑拆分成了两个 layer,一个是【 视图组件 】,另一个是 【 业务模块 】。
6
-
7
- 【 视图组件 】 负责 UI 展示、交互等;【 业务模块 】负责实现 UI 无关的业务逻辑,专门管理状态,既可以是组件状态(局部,不唯一),也可以是应用状态(全局,唯一)。
8
-
9
- 像 `components/Contacts/index.tsx` 这样使用了 `useModel` API 的组件,其实已经在**客户端应用架构**中扮演一种新的角色,负责把 View 和 Model 这两个 layer 连接起来,类似传统 MVC 架构中 Controller 的角色,也类似一种 ViewController。
10
-
11
- 因为这种组件属于一种新的功能模块,在 Modern.js 里我们沿用习惯,把它们称作【 容器组件(Container)】。
12
-
13
- 容器组件推荐放在专门的 `containers/` 目录里,我们执行以下命令:
14
-
15
- import Tabs from '@theme/Tabs';
16
- import TabItem from '@theme/TabItem';
17
-
18
- <Tabs>
19
- <TabItem value="macOS" label="macOS" default>
20
-
21
- ```bash
22
- mkdir -p src/contacts/containers/
23
- mv src/contacts/components/Contacts/index.tsx src/contacts/containers/Contacts.tsx
24
- rm -r src/contacts/components/Contacts/
25
- ```
26
-
27
- </TabItem>
28
- <TabItem value="Windows" label="Windows">
29
-
30
- ```powershell
31
- mkdir -p src/contacts/containers/
32
- mv src/contacts/components/Contacts/index.tsx src/contacts/containers/Contacts.tsx
33
- rm -r src/contacts/components/Contacts/
34
- ```
35
-
36
- </TabItem>
37
- </Tabs>
38
-
39
- 修改 `containers/Contacts.tsx` 的代码:
40
-
41
- ```tsx
42
- import Item from '../components/Item';
43
- import contacts from '../models/contacts';
44
- ```
45
-
46
- 修改 `App.tsx` 的代码:
47
-
48
- ```tsx
49
- import Contacts from './containers/Contacts';
50
- ```
51
-
52
- 重构完成,现在的项目结构是:
53
-
54
- ```md
55
- .
56
- ├── .vscode/
57
- ├── api/
58
- │ ├── .eslintrc.json
59
- │ └── contacts.ts
60
- ├── src/
61
- │ ├── contacts/
62
- │ │ ├── components/
63
- │ │ │ ├── Avatar/
64
- │ │ │ │ ├── index.stories.tsx
65
- │ │ │ │ └── index.tsx
66
- │ │ │ └── Item/
67
- │ │ │ ├── index.test.tsx
68
- │ │ │ └── index.tsx
69
- │ │ ├── containers/
70
- │ │ │ └── Contacts.tsx
71
- │ │ ├── models/
72
- │ │ │ ├── contacts.test.ts
73
- │ │ │ └── contacts.ts
74
- │ │ ├── styles/
75
- │ │ │ └── utils.css
76
- │ │ ├── App.css
77
- │ │ └── App.tsx
78
- │ ├── landing-page/
79
- │ │ └── pages/
80
- │ │ ├── comments/
81
- │ │ │ └── [commentTitle]/
82
- │ │ │ └── index.tsx
83
- │ │ ├── _app.tsx
84
- │ │ ├── docs.tsx
85
- │ │ └── index.tsx
86
- │ ├── .eslintrc.json
87
- │ └── modern-app-env.d.ts
88
- ├── .editorconfig
89
- ├── .gitignore
90
- ├── .npmrc
91
- ├── .nvmrc
92
- ├── README.md
93
- ├── package.json
94
- ├── pnpm-lock.yaml
95
- └── tsconfig.json
96
- ```
97
-
98
- `components/` 里的【 视图组件 】,都是目录形式,如 `Avatar/index.tsx`。而 `containers/` 里的【 容器组件 】,都是单文件形式,如 `contacts.tsx`。**这是我们推荐的一种最佳实践**。
99
-
100
- 在​ [添加 UI 组件(Component)](../c06-css-and-component/6.1-css-in-js.md) 章节提到过,【 视图组件 】用目录形式,是因为【 视图组件 】负责实现 UI 展示和交互细节,可以演变的复杂,用目录形式,可以方便增加子文件,包括专用的资源(图片等)、专用子组件、CSS 文件等,在这个目录内部可以随意重构,只考虑最小局部。
101
-
102
- 而【 容器组件 】只负责连接,是一个胶水层,复杂的业务逻辑和实现细节都交给 View 层和 Model 层去实现,【 容器组件 】自己应该保持简单清晰,不应该包含复杂实现细节,所以不应该有内部结构,采用单文件形式不但更简洁,也能起到约束作用,提醒开发者不要把容器组件写复杂。
103
-
104
- ---
105
-
106
- > 本小节的代码可以在[这里查看](https://github.com/modern-js-dev/modern-js-examples/tree/main/tutorials/c11/hello-modern-2)。