@modern-js/main-doc 2.65.5 → 2.66.1-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (131) hide show
  1. package/docs/en/apis/app/hooks/src/app.mdx +20 -34
  2. package/docs/en/apis/app/hooks/src/modern.runtime.mdx +9 -0
  3. package/docs/en/apis/app/runtime/app/define-config.mdx +6 -0
  4. package/docs/en/community/blog/v2-release-note.mdx +1 -1
  5. package/docs/en/components/enable-micro-frontend.mdx +20 -3
  6. package/docs/en/components/micro-runtime-config.mdx +12 -13
  7. package/docs/en/components/reduck-notify.mdx +27 -0
  8. package/docs/en/configure/app/plugins.mdx +2 -2
  9. package/docs/en/configure/app/runtime/0-intro.mdx +63 -91
  10. package/docs/en/configure/app/tools/esbuild.mdx +1 -1
  11. package/docs/en/configure/app/tools/swc.mdx +1 -1
  12. package/docs/en/configure/app/usage.mdx +93 -69
  13. package/docs/en/guides/basic-features/render/_meta.json +1 -1
  14. package/docs/en/guides/basic-features/render/before-render.mdx +115 -0
  15. package/docs/en/guides/basic-features/routes.mdx +0 -95
  16. package/docs/en/guides/topic-detail/micro-frontend/c02-development.mdx +3 -5
  17. package/docs/en/guides/topic-detail/micro-frontend/c03-main-app.mdx +4 -2
  18. package/docs/en/guides/topic-detail/model/auto-actions.mdx +0 -4
  19. package/docs/en/guides/topic-detail/model/computed-state.mdx +0 -5
  20. package/docs/en/guides/topic-detail/model/define-model.mdx +0 -4
  21. package/docs/en/guides/topic-detail/model/faq.mdx +0 -5
  22. package/docs/en/guides/topic-detail/model/manage-effects.mdx +0 -5
  23. package/docs/en/guides/topic-detail/model/model-communicate.mdx +0 -5
  24. package/docs/en/guides/topic-detail/model/performance.mdx +0 -4
  25. package/docs/en/guides/topic-detail/model/quick-start.mdx +5 -7
  26. package/docs/en/guides/topic-detail/model/redux-integration.mdx +0 -4
  27. package/docs/en/guides/topic-detail/model/typescript-best-practice.mdx +0 -4
  28. package/docs/en/guides/topic-detail/model/use-model.mdx +0 -5
  29. package/docs/en/guides/topic-detail/model/use-out-of-modernjs.mdx +0 -4
  30. package/docs/en/plugin/_meta.json +8 -7
  31. package/docs/en/plugin/cli-plugins/_meta.json +1 -1
  32. package/docs/en/plugin/cli-plugins/api.mdx +617 -0
  33. package/docs/en/plugin/cli-plugins/life-cycle.mdx +139 -0
  34. package/docs/en/plugin/cli-plugins/migration.mdx +98 -0
  35. package/docs/en/plugin/introduction.mdx +119 -47
  36. package/docs/en/plugin/official/_meta.json +12 -0
  37. package/docs/en/plugin/official/cli-plugins/_meta.json +1 -0
  38. package/docs/en/plugin/official/cli-plugins.mdx +6 -0
  39. package/docs/en/plugin/official/rsbuild-plugins.mdx +3 -0
  40. package/docs/en/plugin/plugin-system.mdx +237 -0
  41. package/docs/en/plugin/runtime-plugins/_meta.json +1 -0
  42. package/docs/en/plugin/runtime-plugins/api.mdx +165 -0
  43. package/docs/en/plugin/runtime-plugins/life-cycle.mdx +29 -0
  44. package/docs/en/plugin/runtime-plugins/migration.mdx +101 -0
  45. package/docs/en/plugin/server-plugins/api.mdx +3 -0
  46. package/docs/en/plugin/server-plugins/life-cycle.mdx +3 -0
  47. package/docs/zh/apis/app/hooks/src/app.mdx +18 -26
  48. package/docs/zh/apis/app/hooks/src/modern.runtime.mdx +9 -0
  49. package/docs/zh/apis/app/runtime/app/define-config.mdx +5 -0
  50. package/docs/zh/community/blog/v2-release-note.mdx +1 -1
  51. package/docs/zh/components/enable-micro-frontend.mdx +19 -12
  52. package/docs/zh/components/micro-runtime-config.mdx +3 -3
  53. package/docs/zh/components/reduck-notify.mdx +27 -0
  54. package/docs/zh/configure/app/plugins.mdx +2 -2
  55. package/docs/zh/configure/app/runtime/0-intro.mdx +67 -86
  56. package/docs/zh/configure/app/tools/esbuild.mdx +1 -1
  57. package/docs/zh/configure/app/tools/swc.mdx +1 -1
  58. package/docs/zh/configure/app/usage.mdx +44 -21
  59. package/docs/zh/guides/basic-features/render/_meta.json +1 -1
  60. package/docs/zh/guides/basic-features/render/before-render.mdx +115 -0
  61. package/docs/zh/guides/basic-features/routes.mdx +0 -95
  62. package/docs/zh/guides/topic-detail/micro-frontend/c02-development.mdx +3 -5
  63. package/docs/zh/guides/topic-detail/micro-frontend/c03-main-app.mdx +4 -2
  64. package/docs/zh/guides/topic-detail/model/auto-actions.mdx +0 -4
  65. package/docs/zh/guides/topic-detail/model/computed-state.mdx +0 -4
  66. package/docs/zh/guides/topic-detail/model/define-model.mdx +1 -4
  67. package/docs/zh/guides/topic-detail/model/faq.mdx +0 -5
  68. package/docs/zh/guides/topic-detail/model/manage-effects.mdx +0 -4
  69. package/docs/zh/guides/topic-detail/model/model-communicate.mdx +1 -4
  70. package/docs/zh/guides/topic-detail/model/performance.mdx +0 -4
  71. package/docs/zh/guides/topic-detail/model/quick-start.mdx +7 -8
  72. package/docs/zh/guides/topic-detail/model/redux-integration.mdx +0 -4
  73. package/docs/zh/guides/topic-detail/model/typescript-best-practice.mdx +0 -4
  74. package/docs/zh/guides/topic-detail/model/use-model.mdx +0 -5
  75. package/docs/zh/guides/topic-detail/model/use-out-of-modernjs.mdx +0 -4
  76. package/docs/zh/plugin/_meta.json +8 -7
  77. package/docs/zh/plugin/cli-plugins/_meta.json +1 -1
  78. package/docs/zh/plugin/cli-plugins/api.mdx +617 -0
  79. package/docs/zh/plugin/cli-plugins/life-cycle.mdx +139 -0
  80. package/docs/zh/plugin/cli-plugins/migration.mdx +98 -0
  81. package/docs/zh/plugin/introduction.mdx +92 -20
  82. package/docs/zh/plugin/official/_meta.json +12 -0
  83. package/docs/zh/plugin/official/cli-plugins/_meta.json +1 -0
  84. package/docs/zh/plugin/official/cli-plugins.mdx +6 -0
  85. package/docs/zh/plugin/official/rsbuild-plugins.mdx +3 -0
  86. package/docs/zh/plugin/plugin-system.mdx +239 -0
  87. package/docs/zh/plugin/runtime-plugins/_meta.json +1 -0
  88. package/docs/zh/plugin/runtime-plugins/api.mdx +166 -0
  89. package/docs/zh/plugin/runtime-plugins/life-cycle.mdx +29 -0
  90. package/docs/zh/plugin/runtime-plugins/migration.mdx +101 -0
  91. package/docs/zh/plugin/server-plugins/api.mdx +3 -0
  92. package/docs/zh/plugin/server-plugins/life-cycle.mdx +3 -0
  93. package/i18n.json +4 -0
  94. package/package.json +3 -2
  95. package/src/components/Footer/index.tsx +1 -1
  96. package/src/components/Mermaid/index.tsx +60 -0
  97. package/src/components/Mermaid/style.scss +221 -0
  98. package/docs/en/plugin/cli-plugins.mdx +0 -6
  99. package/docs/en/plugin/plugin-system/_meta.json +0 -10
  100. package/docs/en/plugin/plugin-system/extend.mdx +0 -163
  101. package/docs/en/plugin/plugin-system/hook-list.mdx +0 -711
  102. package/docs/en/plugin/plugin-system/hook.mdx +0 -188
  103. package/docs/en/plugin/plugin-system/implement.mdx +0 -243
  104. package/docs/en/plugin/plugin-system/introduction.mdx +0 -95
  105. package/docs/en/plugin/plugin-system/lifecycle.mdx +0 -16
  106. package/docs/en/plugin/plugin-system/plugin-api.mdx +0 -138
  107. package/docs/en/plugin/plugin-system/relationship.mdx +0 -119
  108. package/docs/en/plugin/rsbuild-plugins.mdx +0 -3
  109. package/docs/zh/plugin/cli-plugins.mdx +0 -6
  110. package/docs/zh/plugin/plugin-system/_meta.json +0 -10
  111. package/docs/zh/plugin/plugin-system/extend.mdx +0 -163
  112. package/docs/zh/plugin/plugin-system/hook-list.mdx +0 -715
  113. package/docs/zh/plugin/plugin-system/hook.mdx +0 -173
  114. package/docs/zh/plugin/plugin-system/implement.mdx +0 -250
  115. package/docs/zh/plugin/plugin-system/introduction.mdx +0 -94
  116. package/docs/zh/plugin/plugin-system/lifecycle.mdx +0 -16
  117. package/docs/zh/plugin/plugin-system/plugin-api.mdx +0 -138
  118. package/docs/zh/plugin/plugin-system/relationship.mdx +0 -119
  119. package/docs/zh/plugin/rsbuild-plugins.mdx +0 -4
  120. /package/docs/en/plugin/{cli-plugins → official/cli-plugins}/plugin-bff.mdx +0 -0
  121. /package/docs/en/plugin/{cli-plugins → official/cli-plugins}/plugin-ssg.mdx +0 -0
  122. /package/docs/en/plugin/{cli-plugins → official/cli-plugins}/plugin-swc.mdx +0 -0
  123. /package/docs/en/plugin/{cli-plugins → official/cli-plugins}/plugin-tailwind.mdx +0 -0
  124. /package/docs/en/plugin/{rsbuild-plugins → official/rsbuild-plugins}/_meta.json +0 -0
  125. /package/docs/en/plugin/{rsbuild-plugins → official/rsbuild-plugins}/plugin-esbuild.mdx +0 -0
  126. /package/docs/zh/plugin/{cli-plugins → official/cli-plugins}/plugin-bff.mdx +0 -0
  127. /package/docs/zh/plugin/{cli-plugins → official/cli-plugins}/plugin-ssg.mdx +0 -0
  128. /package/docs/zh/plugin/{cli-plugins → official/cli-plugins}/plugin-swc.mdx +0 -0
  129. /package/docs/zh/plugin/{cli-plugins → official/cli-plugins}/plugin-tailwind.mdx +0 -0
  130. /package/docs/zh/plugin/{rsbuild-plugins → official/rsbuild-plugins}/_meta.json +0 -0
  131. /package/docs/zh/plugin/{rsbuild-plugins → official/rsbuild-plugins}/plugin-esbuild.mdx +0 -0
@@ -1,7 +1,7 @@
1
- ```js title="src/App.tsx"
2
- import { defineConfig } from '@modern-js/runtime';
1
+ ```ts title="src/modern.runtime.ts"
2
+ import { defineRuntimeConfig } from '@modern-js/runtime';
3
3
 
4
- defineConfig(App, {
4
+ export default defineRuntimeConfig({
5
5
  masterApp: {
6
6
  apps: [{
7
7
  name: 'Table',
@@ -0,0 +1,27 @@
1
+ :::warning
2
+ 从 `MAJOR_VERSION.67.0` 起,Modern.js 不再默认提供 Reduck 状态管理能力。如果需要使用 Reduck,您需要安装并注册 state 插件。
3
+
4
+ **安装步骤**
5
+
6
+ 1. 安装 `@modern-js/plugin-state` 依赖:
7
+
8
+ ```bash
9
+ pnpm add @modern-js/plugin-state
10
+ ```
11
+
12
+ 2. 注册 state 插件:
13
+
14
+ ```ts title="modern.config.ts"
15
+ import { defineConfig } from '@modern-js/app-tools';
16
+ import { statePlugin } from '@modern-js/plugin-state';
17
+
18
+ export default defineConfig({
19
+ ...,
20
+ plugins: [
21
+ ...,
22
+ statePlugin(),
23
+ ],
24
+ });
25
+ ```
26
+
27
+ :::
@@ -9,7 +9,7 @@ sidebar_position: 9
9
9
 
10
10
  用于配置自定义的 Modern.js 框架插件。
11
11
 
12
- 自定义插件的编写方式请参考 [如何编写插件](/plugin/plugin-system/implement)。
12
+ 自定义插件的编写方式请参考 [如何编写插件](/plugin/plugin-system)。
13
13
 
14
14
  ## 注意事项
15
15
 
@@ -33,7 +33,7 @@ Modern.js 中内置了三种不同的框架插件:
33
33
 
34
34
  默认情况下,自定义插件会按照 `plugins` 数组的顺序依次执行,Modern.js 内置插件的执行时机早于自定义插件。
35
35
 
36
- 当插件内部使用了控制顺序的相关字段,比如 `pre`、`post` 时,会基于声明的字段对执行顺序进行调整,详见 [插件之间的关系](/plugin/plugin-system/relationship)。
36
+ 当插件内部使用了控制顺序的相关字段,比如 `pre`、`post` 时,会基于声明的字段对执行顺序进行调整,详见 [插件结构](/plugin/plugin-system)。
37
37
 
38
38
  ## 示例
39
39
 
@@ -1,120 +1,101 @@
1
- ---
2
- title: 总览
3
- sidebar_position: 1
4
- ---
1
+ # 介绍
5
2
 
6
- # 总览
3
+ Modern.js 的运行时(Runtime)配置需集中在 `src/modern.runtime.ts` 文件中声明。
7
4
 
8
- 此节将介绍 Runtime 插件的配置。
9
-
10
- ## 配置方式
11
-
12
- ### runtime
13
-
14
- - **类型:** `Object`
5
+ :::info
6
+ 如果项目中还没有此文件,请执行以下命令创建:
15
7
 
16
- runtime 配置方式如下:
8
+ ```bash
9
+ touch src/modern.runtime.ts
10
+ ```
17
11
 
18
- #### 基本用法
12
+ :::
19
13
 
20
- `modern.config.ts` 中配置
14
+ ## 基本配置
21
15
 
22
- ```ts title="modern.config.ts"
23
- import { defineConfig } from '@modern-js/app-tools';
16
+ ```tsx
17
+ import { defineRuntimeConfig } from '@modern-js/runtime';
24
18
 
25
- export default defineConfig({
26
- runtime: {
27
- state: true,
28
- router: true,
19
+ export default defineRuntimeConfig({
20
+ router: {
21
+ // 路由配置
22
+ },
23
+ state: {
24
+ // 状态管理配置
29
25
  },
26
+ // 其他运行时模块...
30
27
  });
31
28
  ```
32
29
 
33
- #### 运行时配置
34
-
35
- 通过 [`defineConfig`](/apis/app/runtime/app/define-config) API 配置:
36
-
37
- :::info
38
- 当 runtime 配置中存在函数时,只能使用该方式进行配置。
39
-
40
- :::
30
+ ## 多入口配置
41
31
 
42
- import { Tabs, Tab as TabItem } from "@theme";
32
+ 对于多入口应用,`defineRuntimeConfig` 函数可以根据入口名称返回特定的配置:
43
33
 
44
- <Tabs>
45
- <TabItem value="layout" label="约定式路由" default>
34
+ ```tsx
35
+ import { defineRuntimeConfig } from '@modern-js/runtime';
46
36
 
47
- ```tsx title="src/routes/layout.tsx"
48
- import type { AppConfig } from '@modern-js/runtime';
49
-
50
- export const config = (): AppConfig => {
51
- return {
52
- router: {
53
- supportHtml5History: false
54
- }
37
+ export default defineRuntimeConfig(entryName => {
38
+ if (entryName === 'main') {
39
+ return {
40
+ router: {
41
+ // "main" 入口的路由配置
42
+ },
43
+ state: {
44
+ // "main" 入口的状态管理配置
45
+ },
46
+ };
55
47
  }
56
- };
57
- ```
58
-
59
- </TabItem>
60
-
61
- <TabItem value="app" label="自控路由">
62
48
 
63
- ```ts title="src/App.tsx"
64
- import { defineConfig } from '@modern-js/runtime';
65
-
66
- const App = () => {
67
- /** */
68
- };
69
-
70
- defineConfig(App, {
71
- router: {
72
- supportHtml5History: false,
73
- },
49
+ // 其他入口的配置
50
+ return {
51
+ masterApp: {
52
+ // 微前端配置示例
53
+ },
54
+ };
74
55
  });
75
-
76
- export default App;
77
56
  ```
78
57
 
79
- </TabItem>
80
- </Tabs>
81
-
82
- :::info
83
- 使用运行时配置,可以解决 Runtime 插件配置需要在运行时才能获取到具体内容问题。
84
-
85
- Runtime 插件运行时配置和直接在 `modern.config.ts` 中的配置默认会进行合并,且运行时配置优先级更高。
86
-
58
+ :::tip
59
+ 使用 `src/modern.runtime.ts` 配置方式不支持导出异步函数,这与 Modern.js 的渲染方式有关。如果需要添加异步逻辑,请使用 **[Runtime 插件 (Runtime Plugin)](/plugin/introduction.html#runtime-插件)**。
87
60
  :::
88
61
 
89
62
  :::warning
90
- defineConfig 中只能定义 Runtime 插件的具体配置内容,确认是否开启插件还需要通过 `modern.config.ts` 中的配置决定。
91
-
63
+ 使用 `src/modern.runtime.ts` 配置方式需要 Modern.js 版本 **MAJOR_VERSION.66.0** 或更高版本。
92
64
  :::
93
65
 
94
- ### runtimeByEntries
66
+ ## 配置方式演进
95
67
 
96
- - **类型:** `Object`
97
- - **默认值:**无
68
+ MAJOR_VERSION.66.0 之前,运行时配置分散在多个位置:
98
69
 
99
- #### 说明
70
+ 1. `modern.config.ts` 中的 `runtime` 和 `runtimeByEntries` 字段
71
+ 2. 各入口的 `App.config` 或 `layout` 文件导出的 `config` 函数
100
72
 
101
- 按入口添加 runtime 配置,选项属性同 runtime 一致,指定值会和 runtime 属性内容做替换合并操作。
73
+ 为提升可维护性,Modern.js 引入了统一的 `src/modern.runtime.ts` 配置入口。
102
74
 
103
- ```ts title="modern.config.ts"
104
- import { defineConfig } from '@modern-js/app-tools';
75
+ ### 旧配置方式(兼容但不推荐)
105
76
 
106
- export default defineConfig({
77
+ ```ts
78
+ // modern.config.ts
79
+ export default {
107
80
  runtime: {
108
- state: false,
81
+ /* ... */
109
82
  },
110
83
  runtimeByEntries: {
111
- entry1: {
112
- state: true, // { state: true }
113
- },
114
- entry2: {
115
- // { state: false, router: true }
116
- router: true,
117
- },
84
+ /* ... */
118
85
  },
119
- });
86
+ };
87
+
88
+ // App.config
89
+ App.config = {
90
+ /* ... */
91
+ };
92
+
93
+ // layout 文件
94
+ export const config = () => {
95
+ /* 动态配置逻辑 */
96
+ };
120
97
  ```
98
+
99
+ ### 迁移建议
100
+
101
+ 强烈建议将所有运行时配置迁移至 `src/modern.runtime.ts`。虽然旧配置方式目前仍兼容,但计划在未来版本中逐步废弃。统一配置入口可避免配置分散,显著提高项目可维护性。
@@ -41,4 +41,4 @@ export default defineConfig({
41
41
  });
42
42
  ```
43
43
 
44
- 完整配置项请参考 [esbuild 插件配置](/plugin/rsbuild-plugins/plugin-esbuild#配置)。
44
+ 完整配置项请参考 [esbuild 插件配置](/plugin/official/rsbuild-plugins/plugin-esbuild#配置)。
@@ -108,4 +108,4 @@ export default defineConfig({
108
108
  });
109
109
  ```
110
110
 
111
- 完整配置项请参考 [SWC 插件配置](/plugin/cli-plugins/plugin-swc.html#配置)。
111
+ 完整配置项请参考 [SWC 插件配置](/plugin/official/cli-plugins/plugin-swc.html#配置)。
@@ -1,12 +1,8 @@
1
- ---
2
- sidebar_position: 0
3
- ---
4
-
5
1
  # 配置使用
6
2
 
7
- Modern.js 中有两种配置,分别是编译时配置和服务端运行时配置。
3
+ Modern.js 中有三种配置,分别是编译时配置、运行时配置和服务端运行时配置。
8
4
 
9
- 编译时配置可以在两个位置进行配置:
5
+ **编译时配置**可以在两个位置进行配置:
10
6
 
11
7
  - 根路径下的 `modern.config.(ts|js|mjs)` 文件
12
8
  - `package.json` 文件
@@ -15,9 +11,14 @@ Modern.js 中有两种配置,分别是编译时配置和服务端运行时配
15
11
  Modern.js 不支持同时在 `package.json` 中和 `modern.config.ts` 中配置同一个配置项,推荐在 `modern.config.ts` 中进行配置。如果 Modern.js 检测到重复配置导致的冲突,将会抛出警告。
16
12
  :::
17
13
 
18
- 服务端运行时配置可以在根路径下的 `modern.server-runtime.config.(ts|js|mjs)` 中进行配置。
14
+ **运行时配置**可以在 `src/modern.runtime.(ts|js|mjs)` 文件中配置。
15
+
16
+ {/* TODO server 配置文件更新 */}
17
+ **服务端运行时配置**可以在根路径下的 `modern.server-runtime.config.(ts|js|mjs)` 中进行配置。
19
18
 
20
- ## 在配置文件中配置
19
+ ## 编译时配置
20
+
21
+ ### 在配置文件中配置
21
22
 
22
23
  Modern.js 的配置文件定义在项目的根目录下,支持 `.ts`, `.js` 和 `.mjs` 格式:
23
24
 
@@ -25,7 +26,7 @@ Modern.js 的配置文件定义在项目的根目录下,支持 `.ts`, `.js`
25
26
  - `modern.config.js`
26
27
  - `modern.config.mjs`
27
28
 
28
- ### modern.config.ts(推荐)
29
+ #### modern.config.ts(推荐)
29
30
 
30
31
  我们推荐使用 `.ts` 格式的配置文件,它提供了友好的 TypeScript 类型提示,从而帮助你避免配置中的错误。
31
32
 
@@ -52,7 +53,7 @@ export default defineConfig({
52
53
  });
53
54
  ```
54
55
 
55
- ### modern.config.js
56
+ #### modern.config.js
56
57
 
57
58
  如果你在开发一个非 TypeScript 项目,可以使用 `.js` 格式的配置文件:
58
59
 
@@ -76,7 +77,7 @@ export default {
76
77
  };
77
78
  ```
78
79
 
79
- ### 导出配置函数
80
+ #### 导出配置函数
80
81
 
81
82
  Modern.js 支持在配置文件中导出一个函数,你可以在函数中动态计算配置,并返回给 Modern.js。
82
83
 
@@ -99,7 +100,7 @@ export default defineConfig(({ env, command }) => ({
99
100
  - 当运行 `modern build` 或 `modern serve` 时,`env` 的值为 `production`。
100
101
  - `command`:对应当前运行的命令,如 `dev`、`start`、`build`、`serve`。
101
102
 
102
- ### 导出异步函数
103
+ #### 导出异步函数
103
104
 
104
105
  Modern.js 也支持在配置文件中导出一个异步函数,你可以在函数中进行一些异步操作:
105
106
 
@@ -117,7 +118,7 @@ export default defineConfig(async ({ env, command }) => {
117
118
  });
118
119
  ```
119
120
 
120
- ### 指定配置文件
121
+ #### 指定配置文件
121
122
 
122
123
  Modern.js 命令行支持通过 `--config` 选项来指定配置文件的名称。
123
124
 
@@ -138,7 +139,7 @@ Modern.js 命令行支持通过 `--config` 选项来指定配置文件的名称
138
139
  $ modern build -c modern.prod.config.js
139
140
  ```
140
141
 
141
- ## 在 package.json 中配置(不推荐)
142
+ ### 在 package.json 中配置(不推荐)
142
143
 
143
144
  除了配置文件外,你也可以在 `package.json` 中的 `modernConfig` 字段下设置配置项,如:
144
145
 
@@ -156,16 +157,16 @@ $ modern build -c modern.prod.config.js
156
157
 
157
158
  由于 JSON 文件格式的限制,`package.json` 中只能定义数字、字符串、布尔值、数组等简单类型的值,当我们需要设置函数类型的值时,建议在 Modern.js 配置文件中进行设置。
158
159
 
159
- ### 注意事项
160
+ #### 注意事项
160
161
 
161
162
  - 不建议同时使用 `package.json` 和 `modern.config.js` 进行配置。如果同时使用了两者并出现配置冲突,Modern.js 会在命令行进行提示错误。
162
163
  - `@modern-js/runtime` 导出了同名的 [defineConfig](/apis/app/runtime/app/define-config) API,请注意区分。
163
164
 
164
- ## 本地调试配置
165
+ ### 本地调试配置
165
166
 
166
167
  为了便于本地调试配置,Modern.js 支持在项目根目录下创建 `modern.config.local.(ts|js|mjs)` 文件,用于覆盖 `modern.config.(ts|js|mjs)` 中的配置选项。
167
168
 
168
- ### 示例
169
+ #### 示例
169
170
 
170
171
  比如,项目的 `modern.config.ts` 中配置了端口号为 `3000`:
171
172
 
@@ -193,7 +194,7 @@ export default defineConfig({
193
194
 
194
195
  `modern.config.local.ts` 文件中的配置会与 `modern.config.ts` 中的配置进行深层合并,并覆盖 `modern.config.ts` 中的配置选项,因此 `server.port` 会被覆盖为 `3001`。
195
196
 
196
- ### 注意事项
197
+ #### 注意事项
197
198
 
198
199
  在使用 `modern.config.local.ts` 时,请注意以下事项:
199
200
 
@@ -205,13 +206,13 @@ export default defineConfig({
205
206
  modern.config.local.*
206
207
  ```
207
208
 
208
- ## 合并多份配置
209
+ ### 合并多份配置
209
210
 
210
211
  在某些情况下,你可能需要将多份配置合并为一份配置,此时你可以使用 `mergeConfig` 工具函数来合并多个配置。
211
212
 
212
213
  `mergeConfig` 函数接受一个数组作为参数,数组中的每一项都是一个配置对象,`mergeConfig` 会将数组中的每一项配置对象进行深层合并,自动将多个函数项合并为数组,最终返回一个合并后的配置对象。
213
214
 
214
- ### 示例
215
+ #### 示例
215
216
 
216
217
  ```ts title="modern.config.ts"
217
218
  import { mergeConfig } from '@modern-js/app-tools';
@@ -249,7 +250,7 @@ const mergedConfig = {
249
250
  };
250
251
  ```
251
252
 
252
- ## 配置类型定义
253
+ ### 配置类型定义
253
254
 
254
255
  Modern.js 导出了 `AppUserConfig` 类型,对应 Modern.js 配置对象的类型:
255
256
 
@@ -274,3 +275,25 @@ const config: AppUserConfig<'rspack'> = {
274
275
  },
275
276
  };
276
277
  ```
278
+
279
+ ## 运行时配置
280
+
281
+ 运行时配置的详细信息可以参考 [Runtime 配置介绍](/configure/app/runtime/0-intro.html)。
282
+
283
+ :::tip
284
+ 如果当前的 Runtime 配置需要在编译时和运行时同时使用,请将相关配置参数添加到插件注册的位置。
285
+
286
+ ```ts title="modern.config.ts"
287
+ import { defineConfig } from '@modern-js/app-tools';
288
+ import { statePlugin } from '@modern-js/plugin-state';
289
+
290
+ export default defineConfig({
291
+ plugins: [
292
+ statePlugin({
293
+ /** 在此处添加参数 */
294
+ }),
295
+ ],
296
+ });
297
+ ```
298
+
299
+ :::
@@ -1 +1 @@
1
- ["ssr", "streaming-ssr", "ssr-cache", "ssg"]
1
+ ["ssr", "streaming-ssr", "ssr-cache", "ssg", "before-render"]
@@ -0,0 +1,115 @@
1
+ # 渲染预处理 (Render Preprocessing)
2
+
3
+ 在某些场景下,应用需要在渲染前执行预处理操作。Modern.js 推荐使用 **[Runtime 插件 (Runtime Plugin)](/plugin/introduction.html#runtime-插件)** 来实现这类逻辑。
4
+
5
+ ## 定义 Runtime 插件
6
+
7
+ ```ts
8
+ import type { RuntimePluginFuture } from '@modern-js/runtime';
9
+
10
+ const myRuntimePlugin = (): RuntimePluginFuture => ({
11
+ name: 'my-runtime-plugin',
12
+ setup: (api) => {
13
+ api.onBeforeRender((context) => {
14
+ // 在渲染前执行的逻辑
15
+ console.log('Before rendering:', context);
16
+ });
17
+ },
18
+ });
19
+
20
+ export default myRuntimePlugin;
21
+ ```
22
+
23
+ ## 注册插件
24
+
25
+ 在项目 `src/modern.runtime.ts` 文件中注册插件:
26
+
27
+ ```ts
28
+ import { defineRuntimeConfig } from '@modern-js/runtime';
29
+ import myRuntimePlugin from './plugins/myRuntimePlugin';
30
+
31
+ export default defineRuntimeConfig({
32
+ plugins: [myRuntimePlugin()],
33
+ });
34
+ ```
35
+
36
+ ## 应用场景 -- 全局数据注入
37
+
38
+ 通过 `onBeforeRender` 钩子的 `context` 参数,可以向应用注入全局数据。应用的组件可以通过 `useRuntimeContext` Hook 访问这些数据。
39
+
40
+ :::info
41
+
42
+ 此功能在以下场景中特别有用:
43
+ * 需要页面级前置数据的应用
44
+ * 自定义数据注入流程
45
+ * 框架迁移场景(例如从 Next.js 迁移)
46
+
47
+ :::
48
+
49
+ **定义数据注入插件**
50
+
51
+ ```ts
52
+ import type { RuntimePluginFuture } from '@modern-js/runtime';
53
+
54
+ const dataInjectionPlugin = (): RuntimePluginFuture => ({
55
+ name: 'data-injection-plugin',
56
+ setup: api => {
57
+ api.onBeforeRender(context => {
58
+ // 向 context 中注入数据
59
+ context.message = 'Hello World';
60
+ });
61
+ },
62
+ });
63
+
64
+ export default dataInjectionPlugin;
65
+ ```
66
+
67
+ **在组件中使用注入的数据**
68
+
69
+ ```tsx
70
+ import { useRuntimeContext } from '@modern-js/runtime';
71
+
72
+ export default function MyComponent() {
73
+ const context = useRuntimeContext();
74
+ const { message } = context;
75
+
76
+ return <div>{message}</div>;
77
+ }
78
+ ```
79
+
80
+ **结合 SSR 使用**
81
+
82
+ 在 SSR 场景下,浏览器端可以获取服务端渲染期间通过 `onBeforeRender` 注入的数据。开发者可以根据需求决定是否在浏览器端重新获取数据来覆盖服务端数据。
83
+
84
+ ```ts
85
+ import type { RuntimePluginFuture } from '@modern-js/runtime';
86
+
87
+ const dataInjectionPlugin = (): RuntimePluginFuture => ({
88
+ name: 'data-injection-plugin',
89
+ setup: api => {
90
+ api.onBeforeRender(context => {
91
+ if (process.env.MODERN_TARGET === 'node') {
92
+ // 服务端渲染时设置数据
93
+ context.message = 'Hello World By Server';
94
+ } else {
95
+ // 客户端渲染时检查数据
96
+ if (!context.message) {
97
+ // 如果没有获取到服务端数据,则设置客户端数据
98
+ context.message = 'Hello World By Client';
99
+ }
100
+ }
101
+ });
102
+ },
103
+ });
104
+
105
+ export default dataInjectionPlugin;
106
+ ```
107
+
108
+ ## 兼容性说明
109
+
110
+ 在 Modern.js 的早期版本中,支持通过 `routes/layout.tsx` 中的 `init` 钩子以及 `App.init` 方法来添加渲染预处理逻辑。这些方式目前仍然**被支持**,但我们**强烈推荐**使用 Runtime 插件实现。
111
+
112
+ :::warning
113
+
114
+ 未来版本中,`routes/layout.tsx` 的 `init` 钩子和 `App.init` 方法将逐步**废弃**。建议尽早迁移到 Runtime 插件方案。
115
+ :::
@@ -483,101 +483,6 @@ Modern.js 支持通过 `loading.tsx` 文件来解决这个问题,`routes/` 下
483
483
  :::
484
484
 
485
485
 
486
- ## 运行时配置
487
-
488
- {/* Todo 移到动运行时配置章节 */}
489
-
490
- 在根 `<Layout>` 组件中(`routes/layout.ts`),可以动态地定义运行时配置:
491
-
492
- ```tsx title="src/routes/layout.tsx"
493
- // 定义运行时配置
494
- import type { AppConfig } from '@modern-js/runtime';
495
-
496
- export const config = (): AppConfig => {
497
- return {
498
- router: {
499
- createRoutes() {
500
- return [
501
- {
502
- path: 'modern',
503
- element: <div>modern</div>,
504
- },
505
- ];
506
- },
507
- },
508
- };
509
- };
510
- ```
511
-
512
- ## 渲染前的钩子
513
-
514
- {/* Todo 移到动运行时配置章节 */}
515
-
516
- 在有些场景下,需要在应用渲染前做一些操作,可以在 `routes/layout.tsx` 中定义 `init` 钩子,`init` 在客户端和服务端均会执行,基本使用示例如下:
517
-
518
- ```ts title="src/routes/layout.tsx"
519
- import type { RuntimeContext } from '@modern-js/runtime';
520
-
521
- export const init = (context: RuntimeContext) => {
522
- // do something
523
- };
524
- ```
525
-
526
- 通过 `init` 钩子可以挂载一些全局的数据,在应用的其他地方可以访问 `runtimeContext` 变量:
527
-
528
- :::note
529
- 该功能在应用需要页面前置的数据、自定义数据注入或是框架迁移(如 Next.js)时会非常有用。
530
-
531
- :::
532
-
533
- ```ts title="src/routes/layout.tsx"
534
- import { RuntimeContext } from '@modern-js/runtime';
535
-
536
- type InitialData = {
537
- message: string;
538
- }
539
-
540
- export const init = (context: RuntimeContext): InitialData => {
541
- return {
542
- message: 'Hello World',
543
- };
544
- };
545
- ```
546
-
547
- ```tsx title="src/routes/page.tsx"
548
- import { useRuntimeContext } from '@modern-js/runtime';
549
-
550
- export default () => {
551
- const context = useRuntimeContext();
552
- const { message } = context.initialData as InitialData;
553
-
554
- return <div>{message}</div>;
555
- };
556
- ```
557
-
558
- 配合 SSR 功能时,浏览器端可以获取到 SSR 时 `init` 返回的数据,开发者可以自行判断是否要在浏览器端重新获取数据来覆盖 SSR 数据,例如:
559
-
560
- ```tsx title="src/routes/layout.tsx"
561
- import type { RuntimeContext } from '@modern-js/runtime';
562
-
563
- export const init = (runtimeContext: RuntimeContext) => {
564
- if (process.env.MODERN_TARGET === 'node') {
565
- return {
566
- message: 'Hello World By Server',
567
- };
568
- } else {
569
- const { context } = runtimeContext;
570
- const data = context.getInitData();
571
- // 如果没有获取到期望的数据
572
- if (!data.message) {
573
- return {
574
- message: 'Hello World By Client',
575
- };
576
- }
577
- }
578
- };
579
- ```
580
-
581
486
  import Motivation from '@site-docs/components/convention-routing-motivation';
582
487
 
583
488
  <Motivation />
@@ -14,9 +14,9 @@ title: 体验微前端
14
14
 
15
15
  目前项目的路由模式分为以下三种
16
16
 
17
- - 约定式路由 (设置 `router: true` 并使用文件路由)
18
- - 自控式路由 (设置 `router: true` 并自己创建 `BrowserRouter` 等)
19
- - 其他 (设置 `router: false` 项目内自己安装和使用 `react-router-dom`)
17
+ - [约定式路由](/guides/concept/entries.html#约定式路由)
18
+ - [自控式路由](/guides/concept/entries.html#自控式路由)
19
+ - 其他 (项目自行安装和使用 `react-router-dom`)
20
20
 
21
21
  在本教程中我们会为主应用创建两个子应用 Table 和 Dashboard (Table 为约定式路由,Dashboard 为自控式路由)
22
22
 
@@ -206,7 +206,6 @@ export default defineConfig({
206
206
  },
207
207
  runtime: {
208
208
  router: true,
209
- state: true,
210
209
  },
211
210
  deploy: {
212
211
  microFrontend: true,
@@ -259,7 +258,6 @@ export default defineConfig({
259
258
  },
260
259
  runtime: {
261
260
  router: true,
262
- state: true,
263
261
  },
264
262
  deploy: {
265
263
  microFrontend: true,
@@ -35,8 +35,10 @@ import MicroRuntimeConfig from '@site-docs/components/micro-runtime-config';
35
35
 
36
36
  通过该函数可以拉取远程的子应用列表,并将其注册至运行时框架:
37
37
 
38
- ```ts title="App.tsx"
39
- defineConfig(App, {
38
+ ```ts title="src/modern.runtime.ts"
39
+ import { defineRuntimeConfig } from '@modern-js/runtime';
40
+
41
+ export default defineRuntimeConfig({
40
42
  masterApp: {
41
43
  manifest: {
42
44
  getAppList: async () => {