@modern-js/main-doc 0.0.0-next-20221229063546 → 0.0.0-next-20221229142742

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 (27) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +3 -3
  3. package/en/docusaurus-plugin-content-docs/current/configure/app/deploy/microFrontend.md +54 -0
  4. package/en/docusaurus-plugin-content-docs/current/configure/app/output/ssg.md +226 -0
  5. package/en/docusaurus-plugin-content-docs/current/guides/advanced-features/ssg.md +2 -2
  6. package/en/docusaurus-plugin-content-docs/current/guides/basic-features/data-fetch.md +1 -1
  7. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/extend.md +162 -0
  8. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/hook-list.md +803 -0
  9. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/hook.md +169 -0
  10. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/implement.md +247 -0
  11. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/introduction.md +49 -0
  12. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/plugin-api.md +116 -0
  13. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/relationship.md +118 -0
  14. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/config/common.md +1 -1
  15. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/config/module.md +3 -1
  16. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/config/mwa.md +1 -9
  17. package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/project.md +2 -2
  18. package/en/docusaurus-plugin-content-docs/current/tutorials/foundations/introduction.md +1 -1
  19. package/package.json +3 -3
  20. package/zh/configure/app/output/ssg.md +1 -5
  21. package/zh/guides/basic-features/data-fetch.md +1 -1
  22. package/zh/guides/basic-features/env-vars.md +1 -1
  23. package/zh/guides/topic-detail/generator/config/module.md +3 -1
  24. package/zh/guides/topic-detail/generator/config/mwa.md +1 -9
  25. package/zh/tutorials/foundations/introduction.md +1 -1
  26. package/en/docusaurus-plugin-content-docs/current/tutorials/foundations/basic.md +0 -8
  27. package/zh/tutorials/foundations/basic.md +0 -8
@@ -1,4 +1,4 @@
1
1
 
2
- > @modern-js/main-doc@2.0.0-beta.7 build /github/workspace/packages/toolkit/main-doc
2
+ > @modern-js/main-doc@2.0.0-beta.7 build /tmp/repo/modern.js/packages/toolkit/main-doc
3
3
  > npx ts-node ./scripts/sync.ts
4
4
 
package/CHANGELOG.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # @modern-js/main-doc
2
2
 
3
- ## 0.0.0-next-20221229063546
3
+ ## 0.0.0-next-20221229142742
4
4
 
5
5
  ### Patch Changes
6
6
 
7
- - Updated dependencies [dda38c9c3e]
8
- - @modern-js/builder-doc@0.0.0-next-20221229063546
7
+ - Updated dependencies [dda38c9c3]
8
+ - @modern-js/builder-doc@0.0.0-next-20221229142742
@@ -0,0 +1,54 @@
1
+ ---
2
+ sidebar_label: microFrontend
3
+ ---
4
+
5
+ # deploy.microFrontend
6
+
7
+ * 类型:`object`
8
+ * 默认值:`{enableHtmlEntry: true, externalBasicLibrary: false}`
9
+
10
+ ```ts
11
+ interface MicroFrontend {
12
+ enableHtmlEntry?: boolean;
13
+ externalBasicLibrary?: boolean;
14
+ moduleApp?: string;
15
+ }
16
+ ```
17
+
18
+ Developers can use the `deploy.microFrontend` to configure micro-frontend sub-application information.
19
+
20
+ :::caution
21
+ Enable the "Micro Frontend" features through `pnpm run new` first.
22
+ :::
23
+
24
+ ## Example
25
+
26
+ ```ts
27
+ export default defineConfig({
28
+ deploy: {
29
+ microFrontend: {
30
+ enableHtmlEntry: true
31
+ }
32
+ }
33
+ });
34
+ ```
35
+
36
+ ## Configuration
37
+
38
+ ### enableHtmlEntry
39
+
40
+ * Type: `boolean`
41
+
42
+ * Default: `true`
43
+
44
+ * Whether to enable the html entry, the default is `true`, the sub-application is built into the `HTML` mode, Garfish supports the `html` entry, you can turn on the open option, experience the corresponding features, and directly point the sub-application entry to the HTML entry when it is the HTML entry. Just point to the html of the sub-application
45
+ * set it to `false` to indicate that the sub-application is built as `js`. After the sub-application is built as `js`, it cannot run independently. When it is a `JS` entry, point the entry file of the sub-application to the `JS` of the sub-application.
46
+
47
+
48
+ ### externalBasicLibrary
49
+
50
+ * Type: `boolean`
51
+
52
+ * Default: `false`
53
+
54
+ Whether the `external` base library, when set to `true`, the current child application will be `external`: `react`, `react-dom`, Modern.js main application will automatically `setExternal` these two base libraries, if other types of frameworks Please add `react`, `react-dom` dependencies through `Garfish.setExternal`.
@@ -0,0 +1,226 @@
1
+ ---
2
+ sidebar_label: ssg
3
+ ---
4
+ # output.ssg
5
+
6
+ * 类型: `boolean` | `object` | `function`
7
+ * 默认值: `undefined`
8
+
9
+ Enable the SSG for **Self-controlled Routing** or **Conventional Routing**.
10
+
11
+ :::info
12
+ For more routes detail, see [routes](/docs/guides/basic-features/routes)。
13
+ :::
14
+
15
+ ## Example
16
+
17
+ ### Single Entry
18
+
19
+ When the configuration is set to `true`, the SSG of all entries will be enabled by default.
20
+
21
+ For **self-controlled routing**, the root route of the entry will be rendered. For **convention routing**, every route in the entry will be rendered.
22
+
23
+ For example, the `src/` directory has the following file structure that satisfies **conventional routing**:
24
+
25
+ ```bash
26
+ .
27
+ ├── src
28
+ │ └── routes
29
+ │ ├── layout.tsx
30
+ │ ├── page.tsx
31
+ │ └── user
32
+ │ ├── layout.tsx
33
+ │ ├── page.tsx
34
+ │ └── profile
35
+ │ └── page.tsx
36
+ ```
37
+
38
+ Make the following config in `modern.config.[tj]s`:
39
+
40
+ ```ts
41
+ export default defineConfig({
42
+ output: {
43
+ ssg: true,
44
+ },
45
+ });
46
+ ```
47
+
48
+ After executing `pnpm build` to build the application. The `dist/` directory will generate three HTML for each of the three routes (only one HTML if SSG not enabled), and all HTML has been rendered.
49
+
50
+ For example the following **self-controlled routing**:
51
+
52
+ ```tsx title="App.tsx"
53
+ import { useRuntimeContext } from '@modern-js/runtime';
54
+ import { Routes, Route, BrowserRouter } from '@modern-js/runtime/router';
55
+ import { StaticRouter } from '@modern-js/runtime/router/server';
56
+
57
+ const Router = typeof window === 'undefined' ? StaticRouter : BrowserRouter;
58
+
59
+ export default () => {
60
+ const { context } = useRuntimeContext();
61
+ return (
62
+ <Router location={context.request.pathname}>
63
+ <Routes>
64
+ <Route index element={<div>index</div>} />
65
+ <Route path="about" element={<div>about</div>} />
66
+ </Routes>
67
+ </Router>
68
+ );
69
+ };
70
+ ```
71
+
72
+ Also using the above configuration, after executing `pnpm run build`, only the entry route `/` will generate the rendered HTML.
73
+
74
+ ### Multi Entries
75
+
76
+ `output.ssg` can also be configured according to the entries, and the rules that the configuration takes effect are also determined by the entries routing method.
77
+
78
+ 例如以下目录结构:
79
+
80
+ ```bash
81
+
82
+ ├── src
83
+ │   ├── entryA
84
+ │ │ └── routes
85
+ │ │ ├── layout.tsx
86
+ │ │ ├── page.tsx
87
+ │ │ └── user
88
+ │ │ ├── layout.tsx
89
+ │ │ ├── page.tsx
90
+ │ │ └── profile
91
+ │ │ └── page.tsx
92
+ │   └── entryB
93
+ │   └── App.tsx
94
+ ```
95
+
96
+ By default, all entryA entrances are rendered at build time after setting `output.ssg` to `true`. You can configure `false` to cancel the default behavior of the specified entries. For example, to cancel the rendering of the `entryA` at build time:
97
+
98
+ ```js
99
+ export default defineConfig({
100
+ output: {
101
+ ssg: {
102
+ entryA: true,
103
+ entryB: false,
104
+ },
105
+ },
106
+ });
107
+ ```
108
+
109
+ ### Configure Route
110
+
111
+ As mentioned above, **Self-Controlled Routing** only enables SSG configuration for entries route by default.
112
+
113
+ Set specific routes in `output.ssg` can tell Modern.js to enable the SSG of these client side routes. For example, the content of the above `src/App.tsx` file is:
114
+
115
+ ```tsx title="src/App.tsx"
116
+ import { useRuntimeContext } from '@modern-js/runtime';
117
+ import { Routes, Route, BrowserRouter } from '@modern-js/runtime/router';
118
+ import { StaticRouter } from '@modern-js/runtime/router/server';
119
+
120
+ const Router = typeof window === 'undefined' ? StaticRouter : BrowserRouter;
121
+
122
+ export default () => {
123
+ const { context } = useRuntimeContext();
124
+ return (
125
+ <Router location={context.request.pathname}>
126
+ <Routes>
127
+ <Route index element={<div>index</div>} />
128
+ <Route path="about" element={<div>about</div>} />
129
+ </Routes>
130
+ </Router>
131
+ );
132
+ };
133
+ ```
134
+
135
+ When set like this in `modern.config.[jt]s`, the `/about` route will also enable SSG:
136
+
137
+ ```js
138
+ export default defineConfig({
139
+ output: {
140
+ ssg: {
141
+ routes: ['/', '/about'],
142
+ },
143
+ },
144
+ });
145
+ ```
146
+
147
+ Modern.js will automatically concat the complete URL according to the entry and hand it over to the SSG plugin to complete the rendering.
148
+
149
+ Request headers can also be configured for specific entries or routes, for example:
150
+
151
+ ```js
152
+ export default defineConfig({
153
+ output: {
154
+ ssg: {
155
+ headers: {},
156
+ routes: [
157
+ '/',
158
+ {
159
+ url: '/about',
160
+ headers: {},
161
+ },
162
+ ],
163
+ },
164
+ },
165
+ });
166
+ ```
167
+
168
+ :::info
169
+ The `headers` set in the route override the `headers` set in the entry.
170
+ :::
171
+
172
+ ### Prevent Default
173
+
174
+ By default, **Conventional Routing** all turn on SSG. Modern.js provides another field to prevent the default SSG behavior.
175
+
176
+ For example, the following directory structure ,`/`、`/user` and `/user/profle` all have SSG enabled:
177
+
178
+ ```bash
179
+ .
180
+ ├── src
181
+ │ └── routes
182
+ │ ├── layout.tsx
183
+ │ ├── page.tsx
184
+ │ └── user
185
+ │ ├── layout.tsx
186
+ │ ├── page.tsx
187
+ │ └── profile
188
+ │ └── page.tsx
189
+ ```
190
+
191
+ You can set this to disable the default behavior of a client-side route:
192
+
193
+ ```js
194
+ export default defineConfig({
195
+ output: {
196
+ preventDefault: ['/user'],
197
+ },
198
+ });
199
+ ```
200
+
201
+ ### Dynamic Params
202
+
203
+ Some routes may be dynamic, such as the `/user/:id` in a self-controlled route or the route generated by the `user/[id]/page.tsx` file in a conventional route.
204
+
205
+ configure specific parameters in `output.ssg` to render the route of the specified parameters, for example:
206
+
207
+ ```js
208
+ export default defineConfig({
209
+ output: {
210
+ ssg: {
211
+ routes: [
212
+ {
213
+ url: '/user/:id',
214
+ params: [
215
+ {
216
+ id: 'modernjs',
217
+ },
218
+ ],
219
+ },
220
+ ],
221
+ },
222
+ },
223
+ });
224
+ ```
225
+
226
+ The features of dynamic routing and SSG is useful when generating static pages in real time based on CMS system.
@@ -27,7 +27,7 @@ export default defineConfig({
27
27
  });
28
28
  ```
29
29
 
30
- SSG in **Conventional Routing** and **Self-control Routing** has different usage.
30
+ SSG in **Conventional Routing** and **Self-controlled Routing** has different usage.
31
31
 
32
32
  ### Conventional Routing
33
33
 
@@ -84,7 +84,7 @@ Using **Conventional Routing**, each route will generate a HTML file. Looking at
84
84
 
85
85
  After executing `pnpm run serve` to start the project, visit the page in the Network, view the document returned by the request. The document contains the complete page content rendered by the component.
86
86
 
87
- ### Self-control Routing
87
+ ### Self-controlled Routing
88
88
 
89
89
  **Self-controlled routing** is a custom routing through component code, which requires the application to run to obtain accurate routing information. Therefore, the SSG function cannot be used out of the box. At this time, the user needs to inform the Modern.js framework in advance which routes need to enable the SSG.
90
90
 
@@ -427,7 +427,7 @@ export default () => {
427
427
  // No real request is sent here, just a hard coding data is returned.
428
428
  // In a real project, the data obtained from the remote end should be returned.
429
429
  return {
430
- name: 'modern.js',
430
+ name: 'Modern.js',
431
431
  };
432
432
  });
433
433
 
@@ -0,0 +1,162 @@
1
+ ---
2
+ title: 扩展插件 Hook
3
+ sidebar_position: 5
4
+ ---
5
+
6
+ 本小节介绍如何通过动态注册 [Hook 模型](/docs/guides/topic-detail/framework-plugin/hook) 的方式来扩展插件 Hook。
7
+
8
+ ## 示例
9
+
10
+ 这里我们用一个简单的例子演示一下。假设我们要添加一些管理控制台输出信息的 Hook。
11
+
12
+ 首先我们初始化一个空的项目文件,并添加基础依赖:
13
+
14
+ ```console
15
+ $ npx @modern-js/create modern-js-demo
16
+ ```
17
+
18
+ ### 创建 Hook
19
+
20
+ 我们先创建一个 Hook 模型:
21
+
22
+ ```ts title=config/plugin/MyPlugin.ts
23
+ import { createWaterfall } from '@modern-js/plugin';
24
+
25
+ const message = createWaterfall<string[]>();
26
+ ```
27
+
28
+ 然后注册它:
29
+
30
+ ```ts title=config/plugin/MyPlugin.ts
31
+ import type { CliPlugin } from '@modern-js/core';
32
+
33
+ export default (): CliPlugin => ({
34
+ name: 'my-plugin',
35
+
36
+ registerHook: {
37
+ message,
38
+ },
39
+ });
40
+ ```
41
+
42
+ 添加 Hook 类型:
43
+
44
+ ```ts title=config/plugin/MyPlugin.ts
45
+ declare module '@modern-js/core' {
46
+ export interface Hooks {
47
+ message: typeof message;
48
+ }
49
+ }
50
+ ```
51
+
52
+ ### 使用 Hook
53
+
54
+ 创建插件,通过 `commands` Hook 函数,添加命令处理逻辑:
55
+
56
+ ```ts title=config/plugin/MyPlugin.ts
57
+ import type { CliPlugin } from '@modern-js/core';
58
+
59
+ export default (): CliPlugin => ({
60
+ name: 'my-plugin',
61
+
62
+ setup(api) {
63
+ return {
64
+ commands({ program }) {
65
+ program.command('message').action(async () => {
66
+ const hookRunners = api.useHookRunners();
67
+ const messages = hookRunners.message([]);
68
+ console.log(messages.join('\n'));
69
+ });
70
+ },
71
+ };
72
+ },
73
+ });
74
+ ```
75
+
76
+ 最后 `config/plugin/MyPlugin.ts` 的状态是:
77
+
78
+ ```ts title=config/plugin/MyPlugin.ts
79
+ import { createWaterfall } from '@modern-js/plugin';
80
+ import type { CliPlugin } from '@modern-js/core';
81
+
82
+ const message = createWaterfall<string[]>();
83
+
84
+ export default (): CliPlugin => ({
85
+ name: 'my-plugin',
86
+
87
+ registerHook: {
88
+ message,
89
+ },
90
+
91
+ setup(api) {
92
+ return {
93
+ commands({ program }) {
94
+ program.command('message').action(async () => {
95
+ const hookRunners = api.useHookRunners();
96
+ const messages = hookRunners.message([]);
97
+ console.log(messages.join('\n'));
98
+ });
99
+ },
100
+ };
101
+ },
102
+ });
103
+
104
+ declare module '@modern-js/core' {
105
+ export interface Hooks {
106
+ message: typeof message;
107
+ }
108
+ }
109
+ ```
110
+
111
+ 然后在 `modern.config.ts` 中添加这个插件:
112
+
113
+ ```ts title="modern.config.ts"
114
+ import { defineConfig } from '@modern-js/app-tools';
115
+ import MyPlugin from './config/plugin/MyPlugin';
116
+
117
+ export default defineConfig({
118
+ plugins: [MyPlugin()],
119
+ });
120
+ ```
121
+
122
+ 这时运行 `npx modern message` 就会执行相关逻辑,但由于没有收集到任何信息,所以控制台输出为空。
123
+
124
+ 那这里我们添加一个:
125
+
126
+ ```ts title=config/plugin/OtherPlugin.ts
127
+ import type { CliPlugin } from '@modern-js/core';
128
+
129
+ export default (): CliPlugin => ({
130
+ name: 'my-plugin',
131
+
132
+ setup(api) {
133
+ return {
134
+ message(list) {
135
+ return [...list, '[foo] line 0', '[foo] line 1'];
136
+ },
137
+ };
138
+ },
139
+ });
140
+ ```
141
+
142
+ 将它添加到配置中:
143
+
144
+ ```ts title="modern.config.ts"
145
+ import { defineConfig } from '@modern-js/app-tools';
146
+ import MyPlugin from './config/plugin/MyPlugin';
147
+ import OtherPlugin from './config/plugin/OtherPlugin';
148
+
149
+ export default defineConfig({
150
+ plugins: [MyPlugin(), OtherPlugin()],
151
+ });
152
+ ```
153
+
154
+ 这时运行 `npx modern message` 就可以在控制台看到信息了:
155
+
156
+ ```console
157
+ $ modern message
158
+ [foo] line 0
159
+ [foo] line 1
160
+ ```
161
+
162
+ 以上面这种方式就可以扩展出拥有各种能力的插件 Hook。