@modern-js/main-doc 0.0.0-nightly-20230917160555 → 0.0.0-nightly-20230918160602
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.
- package/docs/en/apis/app/commands.mdx +5 -5
- package/docs/en/guides/basic-features/css.mdx +34 -14
- package/docs/en/guides/basic-features/html.mdx +2 -2
- package/docs/en/guides/topic-detail/micro-frontend/c01-introduction.mdx +1 -0
- package/docs/en/guides/topic-detail/micro-frontend/c02-development.mdx +24 -17
- package/docs/en/guides/topic-detail/micro-frontend/c03-main-app.mdx +51 -37
- package/docs/en/guides/topic-detail/micro-frontend/c04-communicate.mdx +1 -0
- package/docs/en/guides/topic-detail/micro-frontend/c05-mixed-stack.mdx +1 -1
- package/docs/zh/apis/app/commands.mdx +3 -3
- package/docs/zh/apis/app/hooks/api/api.mdx +2 -1
- package/docs/zh/apis/app/hooks/server/index_.mdx +2 -1
- package/docs/zh/guides/basic-features/css.mdx +34 -14
- package/docs/zh/guides/basic-features/html.mdx +4 -5
- package/docs/zh/guides/topic-detail/framework-plugin/hook-list.mdx +1 -1
- package/docs/zh/guides/topic-detail/micro-frontend/c02-development.mdx +45 -21
- package/docs/zh/guides/topic-detail/micro-frontend/c03-main-app.mdx +51 -33
- package/docs/zh/guides/topic-detail/micro-frontend/c04-communicate.mdx +1 -0
- package/docs/zh/guides/topic-detail/micro-frontend/c05-mixed-stack.mdx +1 -0
- package/package.json +5 -5
@@ -42,7 +42,7 @@ In multi-page (MPA) projects, the `--entry` option can be added to specify one o
|
|
42
42
|
|
43
43
|
For example, execute `modern dev --entry`, the entry selector will be displayed in the command line interface:
|
44
44
|
|
45
|
-
```
|
45
|
+
```text
|
46
46
|
$ modern dev --entry
|
47
47
|
|
48
48
|
? Please select the entry that needs to be built
|
@@ -86,7 +86,7 @@ Options:
|
|
86
86
|
|
87
87
|
execute `npx modern build --analyze` command, can produce an HTML file that analyzes the volume of the bundle while packaging the production code:
|
88
88
|
|
89
|
-
```
|
89
|
+
```text
|
90
90
|
Bundle Analyzer saved report to /example/dist/report.html
|
91
91
|
File sizes after production build:
|
92
92
|
|
@@ -125,7 +125,7 @@ Options:
|
|
125
125
|
|
126
126
|
In the project, execute the `new` command to add entries as follows:
|
127
127
|
|
128
|
-
```
|
128
|
+
```text
|
129
129
|
$ npx modern new
|
130
130
|
? Please select the operation you want: Create Element
|
131
131
|
? Please select the type of element to create: New "entry"
|
@@ -136,7 +136,7 @@ $ npx modern new
|
|
136
136
|
|
137
137
|
In the project, execute the `new` command to enable features as follows:
|
138
138
|
|
139
|
-
```
|
139
|
+
```text
|
140
140
|
$ npx modern new
|
141
141
|
? Please select the operation you want: Enable Features
|
142
142
|
? Please select the feature name: (Use arrow keys)
|
@@ -156,7 +156,7 @@ pnpm does not support the use of JSON strings as parameter values currently. Use
|
|
156
156
|
|
157
157
|
## modern serve
|
158
158
|
|
159
|
-
|
159
|
+
The `modern serve` command is used to start a Modern.js project in the production environment. It can also be used to preview the artifacts built for the production environment locally. Please note that you need to execute the [`build`](/apis/app/commands#modern-build) command beforehand to generate the corresponding artifacts.
|
160
160
|
|
161
161
|
```bash
|
162
162
|
Usage: modern serve [options]
|
@@ -61,16 +61,31 @@ If you need to use other CSS-in-JS libraries such as [styled-jsx](https://www.np
|
|
61
61
|
|
62
62
|
## Using Tailwind CSS
|
63
63
|
|
64
|
-
[Tailwind CSS](https://tailwindcss.com/) is a CSS framework and design system based on Utility Class, which can quickly add common styles to components, and support flexible extension of theme styles.
|
64
|
+
[Tailwind CSS](https://tailwindcss.com/) is a CSS framework and design system based on Utility Class, which can quickly add common styles to components, and support flexible extension of theme styles.
|
65
65
|
|
66
|
-
|
66
|
+
To use [Tailwind CSS](https://tailwindcss.com/) in Modern.js, you can follow the steps below:
|
67
|
+
|
68
|
+
1. Run `pnpm run new` in the root directory of your project and make the following selections:
|
67
69
|
|
68
70
|
```text
|
69
71
|
? Please select the operation you want: Enable features
|
70
72
|
? Please select the feature name: Enable Tailwind CSS
|
71
73
|
```
|
72
74
|
|
73
|
-
|
75
|
+
After successful initialization, you will see the following additions to the `package.json` file:
|
76
|
+
|
77
|
+
```json title="./package.json"
|
78
|
+
{
|
79
|
+
"dependencies": {
|
80
|
+
"tailwindcss": "^3.0.0"
|
81
|
+
},
|
82
|
+
"devDependencies": {
|
83
|
+
"@modern-js/plugin-tailwindcss": "^2.0.0"
|
84
|
+
}
|
85
|
+
}
|
86
|
+
```
|
87
|
+
|
88
|
+
2. Register the Tailwind plugin in `modern.config.ts`:
|
74
89
|
|
75
90
|
```ts title="modern.config.ts"
|
76
91
|
import { tailwindcssPlugin } from '@modern-js/plugin-tailwindcss';
|
@@ -80,29 +95,34 @@ export default defineConfig({
|
|
80
95
|
});
|
81
96
|
```
|
82
97
|
|
83
|
-
|
98
|
+
3. Create a `index.css` file and add the following code:
|
99
|
+
|
100
|
+
```css title="index.css"
|
101
|
+
@tailwind base;
|
102
|
+
@tailwind components;
|
103
|
+
@tailwind utilities;
|
104
|
+
```
|
105
|
+
|
106
|
+
:::info
|
107
|
+
Depending on your needs, you can selectively import the CSS styles provided by Tailwind CSS. Please refer to the [`@tailwind` documentation](https://tailwindcss.com/docs/functions-and-directives#tailwind) for detailed usage of the `@tailwind` directive.
|
108
|
+
:::
|
109
|
+
|
110
|
+
4. Import the `index.css` file, for example, add the following code in the root component `src/App.jsx`:
|
84
111
|
|
85
112
|
```js
|
86
|
-
import '
|
87
|
-
import 'tailwindcss/components.css';
|
88
|
-
import 'tailwindcss/utilities.css';
|
113
|
+
import './index.css';
|
89
114
|
```
|
90
115
|
|
91
|
-
|
116
|
+
5. Now you can use the Utility Classes provided by Tailwind CSS in your components:
|
92
117
|
|
93
118
|
```tsx
|
94
|
-
const
|
119
|
+
const Hello = () => (
|
95
120
|
<div className="h-12 w-48">
|
96
121
|
<p className="text-xl font-medium text-black">hello world</p>
|
97
122
|
</div>
|
98
123
|
);
|
99
124
|
```
|
100
125
|
|
101
|
-
:::info Additional Information
|
102
|
-
Depending on your needs, you can selectively import the CSS files provided by Tailwind CSS. Since using `@tailwind` is equivalent to directly importing CSS files, you can refer to the comments in the [`@tailwind` usage](https://tailwindcss.com/docs/functions-and-directives#tailwind) documentation for the purpose of the CSS files provided by Tailwind CSS.
|
103
|
-
|
104
|
-
:::
|
105
|
-
|
106
126
|
### Configuring Tailwind CSS
|
107
127
|
|
108
128
|
In Modern.js, you have two ways to configure Tailwind CSS:
|
@@ -165,9 +165,9 @@ export default function Document(): React.ReactElement {
|
|
165
165
|
|
166
166
|
## HTML Syntax
|
167
167
|
|
168
|
-
Modern.js also supports HTML
|
168
|
+
Modern.js also supports generating HTML files using HTML (EJS) syntax.
|
169
169
|
|
170
|
-
|
170
|
+
By default, Modern.js projects come with a built-in HTML template for generating HTML code. If you need to customize the HTML template, you can use two methods: **Custom HTML Fragments** and **Fully Custom HTML Templates**.
|
171
171
|
|
172
172
|
### Custom HTML Fragments
|
173
173
|
|
@@ -13,6 +13,7 @@ Through this chapter you can learn:
|
|
13
13
|
## Create an app
|
14
14
|
|
15
15
|
Currently supports two routing modes
|
16
|
+
|
16
17
|
- Self-controlled routing
|
17
18
|
- Conventional routing
|
18
19
|
|
@@ -20,7 +21,6 @@ First, clarify the routing mode of the main application [create a file-based rou
|
|
20
21
|
|
21
22
|
In this experience we will create two sub-applications Table and Dashboard for the main application (Table is reduced routing, Dashboard is self-controlled routing)
|
22
23
|
|
23
|
-
|
24
24
|
### File-based Routing Main App
|
25
25
|
|
26
26
|
Initialize the project with a command line:
|
@@ -39,11 +39,12 @@ After the project is created, we can enable the `micro frontend` through `pnpm r
|
|
39
39
|
|
40
40
|
Next, let's register the micro frontend plugin and add the main micro frontend app and add the list of sub-apps:
|
41
41
|
|
42
|
-
import EnableMicroFrontend from
|
42
|
+
import EnableMicroFrontend from '@site-docs-en/components/enable-micro-frontend';
|
43
43
|
|
44
44
|
<EnableMicroFrontend />
|
45
45
|
|
46
46
|
Then we create two new directories in the routes folder
|
47
|
+
|
47
48
|
- table (for loading conventional routing sub-applications)
|
48
49
|
- dashboard (for loading self-controlled routing sub-applications)
|
49
50
|
|
@@ -61,8 +62,8 @@ const Index = () => {
|
|
61
62
|
<div>
|
62
63
|
<Table />
|
63
64
|
</div>
|
64
|
-
)
|
65
|
-
}
|
65
|
+
);
|
66
|
+
};
|
66
67
|
|
67
68
|
export default Index;
|
68
69
|
```
|
@@ -79,11 +80,12 @@ const Index = () => {
|
|
79
80
|
<div>
|
80
81
|
<Dashboard />
|
81
82
|
</div>
|
82
|
-
)
|
83
|
-
}
|
83
|
+
);
|
84
|
+
};
|
84
85
|
|
85
86
|
export default Index;
|
86
87
|
```
|
88
|
+
|
87
89
|
#### route link
|
88
90
|
|
89
91
|
At this time, the main application configuration has been completed, and the sub-application can be loaded through the route, and the `layout.tsx` of the main application can be modified to jump to the route:
|
@@ -93,9 +95,15 @@ import { Outlet, Link } from '@modern-js/runtime/router';
|
|
93
95
|
|
94
96
|
const Layout = () => (
|
95
97
|
<div>
|
96
|
-
<div
|
97
|
-
|
98
|
-
|
98
|
+
<div>
|
99
|
+
<Link to={'/table'}>Load file-base routing sub-app</Link>
|
100
|
+
</div>
|
101
|
+
<div>
|
102
|
+
<Link to={'/dashboard'}>Load self-controlled routing sub-app</Link>
|
103
|
+
</div>
|
104
|
+
<div>
|
105
|
+
<Link to={'/'}>unmount sub-app</Link>
|
106
|
+
</div>
|
99
107
|
<Outlet />
|
100
108
|
</div>
|
101
109
|
);
|
@@ -127,7 +135,7 @@ Since it is a self-controlled route, we delete the `routes/` folder and add the
|
|
127
135
|
|
128
136
|
#### Load sub-app
|
129
137
|
|
130
|
-
import CustomRouterMicroFrontend from
|
138
|
+
import CustomRouterMicroFrontend from '@site-docs-en/components/custom-router-micro-frontend';
|
131
139
|
|
132
140
|
<CustomRouterMicroFrontend />
|
133
141
|
|
@@ -155,7 +163,7 @@ import { garfishPlugin } from '@modern-js/plugin-garfish';
|
|
155
163
|
|
156
164
|
export default defineConfig({
|
157
165
|
dev: {
|
158
|
-
port: 8081
|
166
|
+
port: 8081,
|
159
167
|
},
|
160
168
|
runtime: {
|
161
169
|
router: true,
|
@@ -172,10 +180,8 @@ add `src/routes/page.tsx`:
|
|
172
180
|
|
173
181
|
```js title="src/routes/page.tsx"
|
174
182
|
const Index = () => {
|
175
|
-
return
|
176
|
-
|
177
|
-
)
|
178
|
-
}
|
183
|
+
return <div className="container-box">subApp</div>;
|
184
|
+
};
|
179
185
|
|
180
186
|
export default Index;
|
181
187
|
```
|
@@ -204,7 +210,7 @@ import { garfishPlugin } from '@modern-js/plugin-garfish';
|
|
204
210
|
|
205
211
|
export default defineConfig({
|
206
212
|
dev: {
|
207
|
-
port: 8082
|
213
|
+
port: 8082,
|
208
214
|
},
|
209
215
|
runtime: {
|
210
216
|
router: true,
|
@@ -224,7 +230,7 @@ And add code in `src/App.tsx`, note that you need to parse from `props` and pass
|
|
224
230
|
```js title="src/App.tsx"
|
225
231
|
import { BrowserRouter, Route, Routes } from '@modern-js/runtime/router';
|
226
232
|
|
227
|
-
export default (props: {basename: string}) => {
|
233
|
+
export default (props: { basename: string }) => {
|
228
234
|
const { basename } = props;
|
229
235
|
|
230
236
|
return (
|
@@ -241,6 +247,7 @@ export default (props: {basename: string}) => {
|
|
241
247
|
## Debug
|
242
248
|
|
243
249
|
Start the application by executing the `pnpm run dev` command in the directory in sequence:
|
250
|
+
|
244
251
|
- masterApp `http://localhost:8080`
|
245
252
|
- table subapplication (conventional routing) `http://localhost:8081`
|
246
253
|
- dashboard subapplication (self-controlled routing) `http://localhost:8082`
|
@@ -2,13 +2,14 @@
|
|
2
2
|
sidebar_position: 3
|
3
3
|
title: Develop Main App
|
4
4
|
---
|
5
|
+
|
5
6
|
# Develop Main App
|
6
7
|
|
7
8
|
In the previous [Experience micro frontend](/guides/topic-detail/micro-frontend/c02-development), an example was used to demonstrate how to create and configure micro frontend sub-applications. Through this chapter, you can further understand how to develop the main application, and its common configuration.
|
8
9
|
|
9
|
-
After creating an
|
10
|
+
After creating an Modern.js project through the `@modern-js/create` command, you can execute `pnpm run new` in the project (the `modern new` command is actually executed). After selecting the 「micro frontend」 mode, the micro frontend will be installed. Dependencies, just register the plugin manually.
|
10
11
|
|
11
|
-
import EnableMicroFrontend from
|
12
|
+
import EnableMicroFrontend from '@site-docs-en/components/enable-micro-frontend';
|
12
13
|
|
13
14
|
<EnableMicroFrontend />
|
14
15
|
|
@@ -26,7 +27,7 @@ It can be configured at runtime via the API [defineConfig](/apis/app/runtime/app
|
|
26
27
|
When the parameter is a function, it cannot be serialized to the output code, so please configure it through defineConfig when it comes to configuration such as functions
|
27
28
|
:::
|
28
29
|
|
29
|
-
import MicroRuntimeConfig from
|
30
|
+
import MicroRuntimeConfig from '@site-docs-en/components/micro-runtime-config';
|
30
31
|
|
31
32
|
<MicroRuntimeConfig />
|
32
33
|
|
@@ -40,15 +41,18 @@ defineConfig(App, {
|
|
40
41
|
manifest: {
|
41
42
|
getAppList: async () => {
|
42
43
|
// get from remote api
|
43
|
-
return [
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
44
|
+
return [
|
45
|
+
{
|
46
|
+
name: 'Table',
|
47
|
+
entry: 'http://localhost:8001',
|
48
|
+
// activeWhen: '/table'
|
49
|
+
},
|
50
|
+
{
|
51
|
+
name: 'Dashboard',
|
52
|
+
entry: 'http://localhost:8002',
|
53
|
+
// activeWhen: '/dashboard'
|
54
|
+
},
|
55
|
+
];
|
52
56
|
},
|
53
57
|
},
|
54
58
|
},
|
@@ -77,16 +81,30 @@ Suppose our subapp list is configured as follows:
|
|
77
81
|
```js title="App.tsx"
|
78
82
|
import { useModuleApps } from '@modern-js/plugin-garfish/runtime';
|
79
83
|
|
80
|
-
import {
|
84
|
+
import {
|
85
|
+
RouterProvider,
|
86
|
+
Route,
|
87
|
+
createBrowserRouter,
|
88
|
+
createRoutesFromElements,
|
89
|
+
BrowserRouter,
|
90
|
+
Link,
|
91
|
+
Outlet,
|
92
|
+
} from '@modern-js/runtime/router';
|
81
93
|
|
82
94
|
const AppLayout = () => (
|
83
95
|
<>
|
84
|
-
<div
|
85
|
-
|
86
|
-
|
96
|
+
<div>
|
97
|
+
<Link to={'/table'}>load file-based sub-app</Link>
|
98
|
+
</div>
|
99
|
+
<div>
|
100
|
+
<Link to={'/dashboard'}>load self-controlled sub-app</Link>
|
101
|
+
</div>
|
102
|
+
<div>
|
103
|
+
<Link to={'/'}>unmount sub-app</Link>
|
104
|
+
</div>
|
87
105
|
<Outlet />
|
88
106
|
</>
|
89
|
-
)
|
107
|
+
);
|
90
108
|
|
91
109
|
export default () => {
|
92
110
|
const { apps, MApp } = useModuleApps();
|
@@ -103,25 +121,25 @@ export default () => {
|
|
103
121
|
key={app.name}
|
104
122
|
path={`${app.name.toLowerCase()}/*`}
|
105
123
|
element={
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
124
|
+
<Component
|
125
|
+
loadable={{
|
126
|
+
loading: ({ pastDelay, error }: any) => {
|
127
|
+
if (error) {
|
128
|
+
return <div>error: {error?.message}</div>;
|
129
|
+
} else if (pastDelay) {
|
130
|
+
return <div>loading</div>;
|
131
|
+
} else {
|
132
|
+
return null;
|
133
|
+
}
|
134
|
+
},
|
135
|
+
}}
|
136
|
+
/>
|
119
137
|
}
|
120
138
|
/>
|
121
|
-
)
|
139
|
+
);
|
122
140
|
})}
|
123
|
-
</Route
|
124
|
-
)
|
141
|
+
</Route>,
|
142
|
+
),
|
125
143
|
);
|
126
144
|
|
127
145
|
return (
|
@@ -164,13 +182,10 @@ function App() {
|
|
164
182
|
|
165
183
|
After starting the application in this way, accessing the '/table' route will render the'Table 'sub-application, and accessing the'/dashboard 'route will render the'Dashboard' sub-application.
|
166
184
|
|
167
|
-
|
168
185
|
### Mix Mode
|
169
186
|
|
170
|
-
|
171
187
|
Of course, **sub-application components** and **centralized routing** can be mixed.
|
172
188
|
|
173
|
-
|
174
189
|
- One molecular application is activated as a **sub-application component**, and the other part is activated as a **centralized routing**.
|
175
190
|
- A molecular application can be activated either as a **centralized routing** or as a **sub-application component**.
|
176
191
|
|
@@ -178,7 +193,6 @@ Of course, **sub-application components** and **centralized routing** can be mix
|
|
178
193
|
|
179
194
|
By configuring the `loadable` configuration, loading components can be added for 「centralized routing」 and 「sub-applicati」, and errors, timeouts, flashes, etc. can be considered, so as to provide users with a better user experience. The design and implementation of this function refer to [react-loadable](https://github.com/jamiebuilds/react-loadable), and the basic functions are similar.
|
180
195
|
|
181
|
-
|
182
196
|
```tsx
|
183
197
|
function Loading() {
|
184
198
|
return <div>Loading...</div>;
|
@@ -2,6 +2,7 @@
|
|
2
2
|
sidebar_position: 5
|
3
3
|
title: Mixed Stack
|
4
4
|
---
|
5
|
+
|
5
6
|
# Mixed Stack
|
6
7
|
|
7
8
|
The Modern.js micro frontend scheme is based on the [Garfish](https://garfishjs.org/) package and provides some more out-of-the-box usage.
|
@@ -16,7 +17,6 @@ When your main application and sub-application are not all Modern.js application
|
|
16
17
|
**Modern.js** subapps compile to generate a standard [Garfish subapp export](https://www.garfishjs.org/guide/start#2%E5%AF%BC%E5%87%BA-provider-%E5%87%BD%E6%95%B0).
|
17
18
|
So you can directly access the standard micro frontend main application.
|
18
19
|
|
19
|
-
|
20
20
|
:::info
|
21
21
|
The child application is **Modern.js**, when the main application uses the native Garfish micro frontend, the **child application debugging mode** is not available.
|
22
22
|
:::
|
@@ -123,7 +123,7 @@ Options:
|
|
123
123
|
|
124
124
|
### 添加入口
|
125
125
|
|
126
|
-
|
126
|
+
在 Modern.js 工程中,执行 `new` 命令添加入口的步骤如下:
|
127
127
|
|
128
128
|
```bash
|
129
129
|
$ npx modern new
|
@@ -134,7 +134,7 @@ $ npx modern new
|
|
134
134
|
|
135
135
|
### 启用可选功能
|
136
136
|
|
137
|
-
|
137
|
+
在 Modern.js 工程中,执行 `new` 命令启用可选能力的步骤如下:
|
138
138
|
|
139
139
|
```bash
|
140
140
|
$ npx modern new
|
@@ -156,7 +156,7 @@ pnpm 暂不支持使用 JSON 字符串作为参数值,可使用 `npm new` 开
|
|
156
156
|
|
157
157
|
## modern serve
|
158
158
|
|
159
|
-
`modern serve`
|
159
|
+
`modern serve` 命令用于在生产环境下启动 Modern.js 工程, 也可以用于在本地预览生产环境构建的产物。注意你需要提前执行 [`build`](/apis/app/commands#modern-build) 命令构建出对应产物。
|
160
160
|
|
161
161
|
```bash
|
162
162
|
Usage: modern serve [options]
|
@@ -2,6 +2,7 @@
|
|
2
2
|
title: '**/*.[tj]s'
|
3
3
|
sidebar_position: 1
|
4
4
|
---
|
5
|
+
|
5
6
|
# **/*.[tj]s
|
6
7
|
|
7
8
|
在 [BFF 函数写法](/guides/advanced-features/bff/type.html#函数写法)下,声明 API 路由的文件。除了[某些约定文件](/apis/app/hooks/api/api#白名单)外,`api` 目录下的文件会被注册为接口的路由。
|
@@ -66,7 +67,7 @@ export const get = async () => {
|
|
66
67
|
|
67
68
|
这样导出函数,则会得到一个 `GET` 接口。
|
68
69
|
|
69
|
-
|
70
|
+
Modern.js 工程中支持了 9 个 Method 定义,即:`GET`、`POST`、`PUT`、`DELETE`、`CONNECT`、`TRACE`、`PATCH`、`OPTION`、`HEAD`,即可以用这些 Method 作为函数导出的名字。
|
70
71
|
|
71
72
|
名字是大小不敏感的,就是说,如果是 `GET`,写成 `get`、`Get`、`GEt`、`GET`,都可以准确识别。而默认导出,即 `export default xxx` 则会被映射为 `Get`。
|
72
73
|
|
@@ -2,8 +2,9 @@
|
|
2
2
|
title: index.[tj]s
|
3
3
|
sidebar_position: 1
|
4
4
|
---
|
5
|
+
|
5
6
|
# index.[tj]s
|
6
7
|
|
7
|
-
扩展 Modern.js Web Server
|
8
|
+
扩展 Modern.js Web Server 的文件,在此文件中可以给 Modern.js 工程启动的 Web Server 添加 [Hook](/apis/app/runtime/web-server/hook) 或 [Middleware](/apis/app/runtime/web-server/middleware)。
|
8
9
|
|
9
10
|
可以对请求和响应进行拦截处理,鉴权与角色、请求预处理、异常兜底等。也可在内置处理逻辑(包括路由匹配、资源寻址、头部注入、页面渲染,静态 Web 托管)插入特定的业务逻辑。
|
@@ -61,16 +61,31 @@ Modern.js 内部集成了 Babel 的 [babel-plugin-styled-components](https://git
|
|
61
61
|
|
62
62
|
## 使用 Tailwind CSS
|
63
63
|
|
64
|
-
[Tailwind CSS](https://tailwindcss.com/) 是一个以 Utility Class 为基础的 CSS
|
64
|
+
[Tailwind CSS](https://tailwindcss.com/) 是一个以 Utility Class 为基础的 CSS 框架和设计系统,可以快速地为组件添加常用样式,同时支持主题样式的灵活扩展。
|
65
65
|
|
66
|
-
|
66
|
+
在 Modern.js 中使用 [Tailwind CSS](https://tailwindcss.com/),你只需要按照以下步骤操作:
|
67
|
+
|
68
|
+
1. 在项目根目录下执行 `pnpm run new`,按照如下进行选择:
|
67
69
|
|
68
70
|
```text
|
69
71
|
? 请选择你想要的操作 启用可选功能
|
70
72
|
? 请选择功能名称 启用 「Tailwind CSS」 支持
|
71
73
|
```
|
72
74
|
|
73
|
-
|
75
|
+
成功开启后,会看到 `package.json` 中新增了依赖:
|
76
|
+
|
77
|
+
```json title="./package.json"
|
78
|
+
{
|
79
|
+
"dependencies": {
|
80
|
+
"tailwindcss": "^3.0.0"
|
81
|
+
},
|
82
|
+
"devDependencies": {
|
83
|
+
"@modern-js/plugin-tailwindcss": "^2.0.0"
|
84
|
+
}
|
85
|
+
}
|
86
|
+
```
|
87
|
+
|
88
|
+
2. 在 `modern.config.ts` 中注册 Tailwind 插件:
|
74
89
|
|
75
90
|
```ts title="modern.config.ts"
|
76
91
|
import { tailwindcssPlugin } from '@modern-js/plugin-tailwindcss';
|
@@ -80,29 +95,34 @@ export default defineConfig({
|
|
80
95
|
});
|
81
96
|
```
|
82
97
|
|
83
|
-
|
98
|
+
3. 创建 `index.css` 文件,添加以下代码:
|
99
|
+
|
100
|
+
```css title="index.css"
|
101
|
+
@tailwind base;
|
102
|
+
@tailwind components;
|
103
|
+
@tailwind utilities;
|
104
|
+
```
|
105
|
+
|
106
|
+
:::info
|
107
|
+
根据需求不同,你可以选择性地导入 Tailwind CSS 提供的 CSS 样式。请参考 [`@tailwind` 文档](https://tailwindcss.com/docs/functions-and-directives#tailwind) 来了解 `@tailwind` 指令的详细用法。
|
108
|
+
:::
|
109
|
+
|
110
|
+
4. 引用 `index.css` 文件,比如在入口的根组件 `src/App.jsx` 添加如下代码:
|
84
111
|
|
85
112
|
```js
|
86
|
-
import '
|
87
|
-
import 'tailwindcss/components.css';
|
88
|
-
import 'tailwindcss/utilities.css';
|
113
|
+
import './index.css';
|
89
114
|
```
|
90
115
|
|
91
|
-
然后即可在各个组件中使用 Tailwind CSS 提供的 Utility Class 了:
|
116
|
+
5. 然后即可在各个组件中使用 Tailwind CSS 提供的 Utility Class 了:
|
92
117
|
|
93
118
|
```tsx
|
94
|
-
const
|
119
|
+
const Hello = () => (
|
95
120
|
<div className="h-12 w-48">
|
96
121
|
<p className="text-xl font-medium text-black">hello world</p>
|
97
122
|
</div>
|
98
123
|
);
|
99
124
|
```
|
100
125
|
|
101
|
-
:::info 补充信息
|
102
|
-
根据需求不同,你可以选择性的导入 Tailwind CSS 提供的 CSS 文件。由于使用 `@tailwind` 与直接导入 CSS 文件的作用等价,因此关于 Tailwind CSS 提供的 CSS 文件的用途,可以参考 [`@tailwind` 的使用](https://tailwindcss.com/docs/functions-and-directives#tailwind) 文档中注释里的内容。
|
103
|
-
|
104
|
-
:::
|
105
|
-
|
106
126
|
### 配置 Tailwind CSS
|
107
127
|
|
108
128
|
在 Modern.js 中,你有两种方式来配置 Tailwind CSS:
|
@@ -135,7 +135,7 @@ export default function Document(): React.ReactElement {
|
|
135
135
|
<!--<?- html ?>-->
|
136
136
|
<h1 style="color:red">以下为构建时传过来的参数:</h1>
|
137
137
|
<h2>entryName: sub</h2>
|
138
|
-
<h2>title
|
138
|
+
<h2>title:</h2>
|
139
139
|
<h2>rootId: root</h2>
|
140
140
|
</div>
|
141
141
|
<h1>bottom</h1>
|
@@ -166,12 +166,11 @@ export default function Document(): React.ReactElement {
|
|
166
166
|
}
|
167
167
|
```
|
168
168
|
|
169
|
+
## HTML 语法
|
169
170
|
|
170
|
-
|
171
|
+
Modern.js 也支持通过 HTML(EJS) 语法来生成 HTML 文件。
|
171
172
|
|
172
|
-
Modern.js
|
173
|
-
|
174
|
-
基于 HTML 语法的模板,Modern.js 提供了**自定义 HTML 片段**和**完全自定义 HTML 模板**两种方式来自定义模板。
|
173
|
+
默认情况下,Modern.js 的工程中会内置一份 HTML 模板,用于生成 HTML 代码。如果你需要自定义 HTML 模板,可以使用**自定义 HTML 片段**和**完全自定义 HTML 模板**两种方式。
|
175
174
|
|
176
175
|
### 自定义 HTML 片段
|
177
176
|
|
@@ -2,6 +2,7 @@
|
|
2
2
|
sidebar_position: 2
|
3
3
|
title: 体验微前端
|
4
4
|
---
|
5
|
+
|
5
6
|
# 体验微前端
|
6
7
|
|
7
8
|
通过本章你可以了解到:
|
@@ -10,7 +11,9 @@ title: 体验微前端
|
|
10
11
|
- 微前端项目开发的基本流程。
|
11
12
|
|
12
13
|
## 创建应用
|
14
|
+
|
13
15
|
目前支持两种路由模式
|
16
|
+
|
14
17
|
- 自控式路由
|
15
18
|
- 约定式路由
|
16
19
|
|
@@ -27,7 +30,7 @@ mkdir masterApp && cd masterApp
|
|
27
30
|
npx @modern-js/create@latest
|
28
31
|
```
|
29
32
|
|
30
|
-
import DefaultMWAGenerate from
|
33
|
+
import DefaultMWAGenerate from '@site-docs/components/default-mwa-generate';
|
31
34
|
|
32
35
|
<DefaultMWAGenerate />
|
33
36
|
|
@@ -40,17 +43,19 @@ import DefaultMWAGenerate from "@site-docs/components/default-mwa-generate";
|
|
40
43
|
|
41
44
|
接下来,让我们注册微前端插件并添加开启微前端主应用,并增加子应用列表:
|
42
45
|
|
43
|
-
import EnableMicroFrontend from
|
46
|
+
import EnableMicroFrontend from '@site-docs/components/enable-micro-frontend';
|
44
47
|
|
45
48
|
<EnableMicroFrontend />
|
46
49
|
|
47
50
|
然后我们在 routes 文件夹下新建两个目录
|
51
|
+
|
48
52
|
- table (用于加载约定式路由子应用)
|
49
53
|
- dashboard (用于加载自控式路由子应用)
|
50
54
|
|
51
55
|
在这两个目录下我们需要新建一个 `$.tsx` 文件作为主应用约定式路由的入口($ 代表模糊匹配,即 `/table` 和 `/table/test` 都会匹配到这个 `$.tsx` 作为该路由的入口文件,这会保证在微前端场景下正确加载子应用路由)
|
52
56
|
|
53
57
|
#### 加载约定式路由子应用
|
58
|
+
|
54
59
|
```js title="src/routes/table/$.tsx"
|
55
60
|
import { useModuleApps } from '@modern-js/plugin-garfish/runtime';
|
56
61
|
|
@@ -61,13 +66,14 @@ const Index = () => {
|
|
61
66
|
<div>
|
62
67
|
<Table />
|
63
68
|
</div>
|
64
|
-
)
|
65
|
-
}
|
69
|
+
);
|
70
|
+
};
|
66
71
|
|
67
72
|
export default Index;
|
68
73
|
```
|
69
74
|
|
70
75
|
#### 加载自控式路由子应用
|
76
|
+
|
71
77
|
```js title="src/routes/dashboard/$.tsx"
|
72
78
|
import { useModuleApps } from '@modern-js/plugin-garfish/runtime';
|
73
79
|
|
@@ -78,21 +84,30 @@ const Index = () => {
|
|
78
84
|
<div>
|
79
85
|
<Dashboard />
|
80
86
|
</div>
|
81
|
-
)
|
82
|
-
}
|
87
|
+
);
|
88
|
+
};
|
83
89
|
|
84
90
|
export default Index;
|
85
91
|
```
|
92
|
+
|
86
93
|
#### 路由跳转
|
94
|
+
|
87
95
|
此时主应用配置已经完成,通过路由即可加载子应用,修改主应用的 `layout.tsx` 来跳转路由
|
96
|
+
|
88
97
|
```js title="src/route/layout.tsx"
|
89
98
|
import { Outlet, Link } from '@modern-js/runtime/router';
|
90
99
|
|
91
100
|
const Layout = () => (
|
92
101
|
<div>
|
93
|
-
<div
|
94
|
-
|
95
|
-
|
102
|
+
<div>
|
103
|
+
<Link to={'/table'}>加载约定式路由子应用</Link>
|
104
|
+
</div>
|
105
|
+
<div>
|
106
|
+
<Link to={'/dashboard'}>加载自控式路由子应用</Link>
|
107
|
+
</div>
|
108
|
+
<div>
|
109
|
+
<Link to={'/'}>卸载子应用</Link>
|
110
|
+
</div>
|
96
111
|
<Outlet />
|
97
112
|
</div>
|
98
113
|
);
|
@@ -123,8 +138,10 @@ npx @modern-js/create@latest
|
|
123
138
|
<EnableMicroFrontend />
|
124
139
|
|
125
140
|
由于是自控式路由,我们删除掉 `routes` 文件夹并在 `src` 目录下增加 `App.tsx` 文件,此处如果使用的 `非 MApp` 组件,需要通过 `React Router v6` 的 `createBrowserRouter` API 来创建路由
|
141
|
+
|
126
142
|
#### 加载子应用
|
127
|
-
|
143
|
+
|
144
|
+
import CustomRouterMicroFrontend from '@site-docs/components/custom-router-micro-frontend';
|
128
145
|
|
129
146
|
<CustomRouterMicroFrontend />
|
130
147
|
|
@@ -139,7 +156,7 @@ npx @modern-js/create@latest
|
|
139
156
|
|
140
157
|
按照如下选择,生成项目:
|
141
158
|
|
142
|
-
<DefaultMWAGenerate/>
|
159
|
+
<DefaultMWAGenerate />
|
143
160
|
|
144
161
|
我们执行 `pnpm run new` 来开启 `微前端` 功能:
|
145
162
|
|
@@ -156,7 +173,7 @@ import { garfishPlugin } from '@modern-js/plugin-garfish';
|
|
156
173
|
|
157
174
|
export default defineConfig({
|
158
175
|
dev: {
|
159
|
-
port: 8081
|
176
|
+
port: 8081,
|
160
177
|
},
|
161
178
|
runtime: {
|
162
179
|
router: true,
|
@@ -170,12 +187,13 @@ export default defineConfig({
|
|
170
187
|
```
|
171
188
|
|
172
189
|
添加 `src/routes/page.tsx` 代码
|
190
|
+
|
173
191
|
```js title="src/routes/page.tsx"
|
174
192
|
const Index = () => {
|
175
193
|
return (
|
176
194
|
<div className="container-box">subApp: 约定式路由的子应用的根路由</div>
|
177
|
-
)
|
178
|
-
}
|
195
|
+
);
|
196
|
+
};
|
179
197
|
|
180
198
|
export default Index;
|
181
199
|
```
|
@@ -191,7 +209,7 @@ npx @modern-js/create@latest
|
|
191
209
|
|
192
210
|
按照如下选择,生成项目:
|
193
211
|
|
194
|
-
<DefaultMWAGenerate/>
|
212
|
+
<DefaultMWAGenerate />
|
195
213
|
|
196
214
|
我们执行 `pnpm run new` 来开启 `微前端`:
|
197
215
|
|
@@ -208,7 +226,7 @@ import { garfishPlugin } from '@modern-js/plugin-garfish';
|
|
208
226
|
|
209
227
|
export default defineConfig({
|
210
228
|
dev: {
|
211
|
-
port: 8082
|
229
|
+
port: 8082,
|
212
230
|
},
|
213
231
|
runtime: {
|
214
232
|
router: true,
|
@@ -220,6 +238,7 @@ export default defineConfig({
|
|
220
238
|
plugins: [appTools(), garfishPlugin()],
|
221
239
|
});
|
222
240
|
```
|
241
|
+
|
223
242
|
自控式路由需要删除掉 `routes` 文件夹并在 `src` 目录下新建 `App.tsx`
|
224
243
|
|
225
244
|
并在 `src/App.tsx` 添加代码,注意需要从 `props` 中解析并传递 `basename` 给 `BrowserRouter`
|
@@ -227,7 +246,7 @@ export default defineConfig({
|
|
227
246
|
```js title="src/App.tsx"
|
228
247
|
import { BrowserRouter, Route, Routes } from '@modern-js/runtime/router';
|
229
248
|
|
230
|
-
export default (props: {basename: string}) => {
|
249
|
+
export default (props: { basename: string }) => {
|
231
250
|
const { basename } = props;
|
232
251
|
|
233
252
|
return (
|
@@ -242,7 +261,9 @@ export default (props: {basename: string}) => {
|
|
242
261
|
```
|
243
262
|
|
244
263
|
## 调试
|
264
|
+
|
245
265
|
按顺序在目录执行 `pnpm run dev` 命令启动应用:
|
266
|
+
|
246
267
|
- masterApp 主应用 `http://localhost:8080`
|
247
268
|
- table 子应用(约定式路由) `http://localhost:8081`
|
248
269
|
- dashboard 子应用(自控式路由) `http://localhost:8082`
|
@@ -254,6 +275,7 @@ export default (props: {basename: string}) => {
|
|
254
275
|
## Modern.js 微前端和直接使用 Garfish 的区别
|
255
276
|
|
256
277
|
使用纯 `Garfish` API 开发微前端应用时
|
278
|
+
|
257
279
|
- 主应用:
|
258
280
|
- 安装 Garfish 依赖并使用 `Garfish.run` 注册子应用 [参考](https://www.garfishjs.org/api/run)
|
259
281
|
- 提供一个常驻的 `DOM` 节点供子应用挂载 [参考](https://www.garfishjs.org/api/registerApp#domgetter)
|
@@ -264,6 +286,7 @@ export default (props: {basename: string}) => {
|
|
264
286
|
区别于直接使用 `Garfish` 运行时 API 开发微前端项目,`Modern.js` 的微前端方案更加开箱即用。
|
265
287
|
使用 `pnpm new` 启用微前端模式后会自动在 `Modern.js` 应用中集成 `Garfish` 插件,在 `Garfish`
|
266
288
|
插件的加持下,你只需要
|
289
|
+
|
267
290
|
- 主应用:
|
268
291
|
- 配置 `runtime.masterApp.apps` 参数注册子应用
|
269
292
|
- 使用 `useModuleApps` API 获取子应用实例并在组件中完成渲染
|
@@ -271,10 +294,11 @@ export default (props: {basename: string}) => {
|
|
271
294
|
- 配置 `deploy.microFrontend`
|
272
295
|
|
273
296
|
所以插件中为你做了如下事情
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
297
|
+
|
298
|
+
- 帮助你通过 `Garfish` 运行时 API 自动注册子应用(主应用)
|
299
|
+
- `useModulesApps` 函数的返回值提供了一个常驻的 `DOM` 节点供子应用挂载(主应用)
|
300
|
+
- 帮助你正确导出了 `provider`(子应用)
|
301
|
+
- 帮助你正确设置了 `basename` 给 `Modern.js` 运行时提供 `Router` 实例,如果是`自控式路由`或`手动引入的 react-router-dom` 那么需要从 `App.tsx` 的 `props` 中获取 `basename` 手动传递给引入的 `Router 实例`(子应用)
|
278
302
|
|
279
303
|
## 常见问题
|
280
304
|
|
@@ -2,13 +2,14 @@
|
|
2
2
|
sidebar_position: 3
|
3
3
|
title: 开发主应用
|
4
4
|
---
|
5
|
+
|
5
6
|
# 开发主应用
|
6
7
|
|
7
8
|
在上一章 [体验微前端](/guides/topic-detail/micro-frontend/c02-development) 通过一个示例演示了如何创建和配置微前端子应用,通过本章你可以进一步了解如何开发主应用,以及它的常见配置。
|
8
9
|
|
9
|
-
在通过 `@modern-js/create`
|
10
|
+
在通过 `@modern-js/create` 命令创建 Modern.js 工程后,可以在项目中执行 `pnpm run new`(实际执行了 `modern new` 命令),在选择了「微前端」模式后,会安装微前端依赖依赖,只需手动注册插件即可。
|
10
11
|
|
11
|
-
import EnableMicroFrontend from
|
12
|
+
import EnableMicroFrontend from '@site-docs/components/enable-micro-frontend';
|
12
13
|
|
13
14
|
<EnableMicroFrontend />
|
14
15
|
|
@@ -26,7 +27,7 @@ import EnableMicroFrontend from "@site-docs/components/enable-micro-frontend";
|
|
26
27
|
|
27
28
|
:::
|
28
29
|
|
29
|
-
import MicroRuntimeConfig from
|
30
|
+
import MicroRuntimeConfig from '@site-docs/components/micro-runtime-config';
|
30
31
|
|
31
32
|
<MicroRuntimeConfig />
|
32
33
|
|
@@ -40,15 +41,18 @@ defineConfig(App, {
|
|
40
41
|
manifest: {
|
41
42
|
getAppList: async () => {
|
42
43
|
// 可以从其他远程接口获取
|
43
|
-
return [
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
44
|
+
return [
|
45
|
+
{
|
46
|
+
name: 'Table',
|
47
|
+
entry: 'http://localhost:8001',
|
48
|
+
// activeWhen: '/table'
|
49
|
+
},
|
50
|
+
{
|
51
|
+
name: 'Dashboard',
|
52
|
+
entry: 'http://localhost:8002',
|
53
|
+
// activeWhen: '/dashboard'
|
54
|
+
},
|
55
|
+
];
|
52
56
|
},
|
53
57
|
},
|
54
58
|
},
|
@@ -77,16 +81,30 @@ defineConfig(App, {
|
|
77
81
|
```js title="App.tsx"
|
78
82
|
import { useModuleApps } from '@modern-js/plugin-garfish/runtime';
|
79
83
|
|
80
|
-
import {
|
84
|
+
import {
|
85
|
+
RouterProvider,
|
86
|
+
Route,
|
87
|
+
createBrowserRouter,
|
88
|
+
createRoutesFromElements,
|
89
|
+
BrowserRouter,
|
90
|
+
Link,
|
91
|
+
Outlet,
|
92
|
+
} from '@modern-js/runtime/router';
|
81
93
|
|
82
94
|
const AppLayout = () => (
|
83
95
|
<>
|
84
|
-
<div
|
85
|
-
|
86
|
-
|
96
|
+
<div>
|
97
|
+
<Link to={'/table'}>加载约定式路由子应用</Link>
|
98
|
+
</div>
|
99
|
+
<div>
|
100
|
+
<Link to={'/dashboard'}>加载自控式路由子应用</Link>
|
101
|
+
</div>
|
102
|
+
<div>
|
103
|
+
<Link to={'/'}>卸载子应用</Link>
|
104
|
+
</div>
|
87
105
|
<Outlet />
|
88
106
|
</>
|
89
|
-
)
|
107
|
+
);
|
90
108
|
|
91
109
|
export default () => {
|
92
110
|
const { apps, MApp } = useModuleApps();
|
@@ -103,25 +121,25 @@ export default () => {
|
|
103
121
|
key={app.name}
|
104
122
|
path={`${app.name.toLowerCase()}/*`}
|
105
123
|
element={
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
124
|
+
<Component
|
125
|
+
loadable={{
|
126
|
+
loading: ({ pastDelay, error }: any) => {
|
127
|
+
if (error) {
|
128
|
+
return <div>error: {error?.message}</div>;
|
129
|
+
} else if (pastDelay) {
|
130
|
+
return <div>loading</div>;
|
131
|
+
} else {
|
132
|
+
return null;
|
133
|
+
}
|
134
|
+
},
|
135
|
+
}}
|
136
|
+
/>
|
119
137
|
}
|
120
138
|
/>
|
121
|
-
)
|
139
|
+
);
|
122
140
|
})}
|
123
|
-
</Route
|
124
|
-
)
|
141
|
+
</Route>,
|
142
|
+
),
|
125
143
|
);
|
126
144
|
|
127
145
|
return (
|
package/package.json
CHANGED
@@ -15,17 +15,17 @@
|
|
15
15
|
"modern",
|
16
16
|
"modern.js"
|
17
17
|
],
|
18
|
-
"version": "0.0.0-nightly-
|
18
|
+
"version": "0.0.0-nightly-20230918160602",
|
19
19
|
"publishConfig": {
|
20
20
|
"registry": "https://registry.npmjs.org/",
|
21
21
|
"access": "public",
|
22
22
|
"provenance": true
|
23
23
|
},
|
24
24
|
"dependencies": {
|
25
|
-
"@modern-js/sandpack-react": "0.0.0-nightly-
|
25
|
+
"@modern-js/sandpack-react": "0.0.0-nightly-20230918160602"
|
26
26
|
},
|
27
27
|
"peerDependencies": {
|
28
|
-
"@modern-js/builder-doc": "0.0.0-nightly-
|
28
|
+
"@modern-js/builder-doc": "0.0.0-nightly-20230918160602"
|
29
29
|
},
|
30
30
|
"devDependencies": {
|
31
31
|
"classnames": "^2",
|
@@ -39,8 +39,8 @@
|
|
39
39
|
"@rspress/shared": "0.0.13",
|
40
40
|
"@types/node": "^16",
|
41
41
|
"@types/fs-extra": "^9",
|
42
|
-
"@modern-js/doc
|
43
|
-
"@modern-js/
|
42
|
+
"@modern-js/builder-doc": "0.0.0-nightly-20230918160602",
|
43
|
+
"@modern-js/doc-plugin-auto-sidebar": "0.0.0-nightly-20230918160602"
|
44
44
|
},
|
45
45
|
"scripts": {
|
46
46
|
"dev": "rspress dev",
|