@modern-js/main-doc 2.0.0-beta.5 → 2.0.0-beta.7
Sign up to get free protection for your applications and to get access to all the features.
- package/.turbo/turbo-build.log +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/hooks/src/index_.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/hooks/src/pages.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/hooks/src/routes.md +86 -0
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/core/use-module-apps.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/router/router.md +176 -373
- package/en/docusaurus-plugin-content-docs/current/components/enable-bff.md +36 -0
- package/en/docusaurus-plugin-content-docs/current/components/global-proxy-config.md +74 -0
- package/en/docusaurus-plugin-content-docs/current/components/global-proxy.md +28 -0
- package/en/docusaurus-plugin-content-docs/current/components/router-legacy-tip.md +1 -0
- package/en/docusaurus-plugin-content-docs/current/configure/app/auto-load-plugin.md +62 -0
- package/en/docusaurus-plugin-content-docs/current/configure/app/dev/proxy.md +2 -72
- package/en/docusaurus-plugin-content-docs/current/configure/app/runtime/router.md +17 -2
- package/en/docusaurus-plugin-content-docs/current/configure/app/source/entries.md +0 -2
- package/en/docusaurus-plugin-content-docs/current/configure/app/tools/tailwindcss.md +16 -22
- package/en/docusaurus-plugin-content-docs/current/guides/advanced-features/bff/_category_.json +8 -0
- package/en/docusaurus-plugin-content-docs/current/guides/advanced-features/bff/bff-proxy.md +27 -0
- package/en/docusaurus-plugin-content-docs/current/guides/advanced-features/bff/frameworks.md +150 -0
- package/en/docusaurus-plugin-content-docs/current/guides/advanced-features/bff/function.md +222 -0
- package/en/docusaurus-plugin-content-docs/current/guides/advanced-features/bff/index.md +20 -0
- package/en/docusaurus-plugin-content-docs/current/guides/advanced-features/bff/type.md +43 -0
- package/en/docusaurus-plugin-content-docs/current/guides/advanced-features/code-split.md +77 -0
- package/en/docusaurus-plugin-content-docs/current/guides/advanced-features/compatibility.md +76 -0
- package/en/docusaurus-plugin-content-docs/current/guides/advanced-features/eslint.md +145 -0
- package/en/docusaurus-plugin-content-docs/current/guides/advanced-features/index.md +12 -0
- package/en/docusaurus-plugin-content-docs/current/guides/advanced-features/low-level.md +46 -0
- package/en/docusaurus-plugin-content-docs/current/guides/advanced-features/ssg.md +132 -0
- package/en/docusaurus-plugin-content-docs/current/guides/advanced-features/ssr.md +306 -0
- package/en/docusaurus-plugin-content-docs/current/guides/advanced-features/testing.md +46 -0
- package/en/docusaurus-plugin-content-docs/current/guides/advanced-features/web-server.md +57 -0
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/alias.md +67 -0
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/css/tailwindcss.md +30 -35
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/data-fetch.md +400 -0
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/env-vars.md +166 -0
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/html.md +235 -0
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/mock.md +78 -0
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/proxy.md +60 -0
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/routes.md +0 -2
- package/en/docusaurus-plugin-content-docs/current/guides/get-started/quick-start.md +2 -4
- package/package.json +4 -4
- package/zh/apis/app/hooks/src/index_.md +1 -1
- package/zh/apis/app/hooks/src/pages.md +1 -1
- package/zh/apis/app/hooks/src/routes.md +89 -0
- package/zh/apis/app/runtime/core/use-module-apps.md +1 -1
- package/zh/apis/app/runtime/router/router.md +170 -368
- package/zh/components/debug-app.md +1 -2
- package/zh/components/enable-bff.md +36 -0
- package/zh/components/global-proxy-config.md +70 -0
- package/zh/components/micro-master-manifest-config.md +15 -0
- package/zh/components/router-legacy-tip.md +1 -0
- package/zh/configure/app/auto-load-plugin.md +62 -0
- package/zh/configure/app/deploy/microFrontend.md +0 -10
- package/zh/configure/app/dev/proxy.md +2 -70
- package/zh/configure/app/runtime/master-app.md +2 -16
- package/zh/configure/app/runtime/router.md +17 -3
- package/zh/configure/app/source/entries.md +1 -3
- package/zh/configure/app/tools/_category_.json +1 -1
- package/zh/configure/app/tools/tailwindcss.md +16 -23
- package/zh/guides/advanced-features/bff/frameworks.md +2 -0
- package/zh/guides/advanced-features/bff/function.md +44 -24
- package/zh/guides/advanced-features/code-split.md +28 -20
- package/zh/guides/advanced-features/compatibility.md +24 -14
- package/zh/guides/advanced-features/ssg.md +5 -47
- package/zh/guides/advanced-features/ssr.md +1 -1
- package/zh/guides/advanced-features/testing.md +2 -2
- package/zh/guides/basic-features/alias.md +5 -5
- package/zh/guides/basic-features/css/tailwindcss.md +31 -35
- package/zh/guides/basic-features/data-fetch.md +7 -6
- package/zh/guides/basic-features/env-vars.md +2 -2
- package/zh/guides/basic-features/html.md +62 -137
- package/zh/guides/basic-features/mock.md +8 -9
- package/zh/guides/basic-features/proxy.md +2 -2
- package/zh/guides/basic-features/routes.md +37 -6
- package/zh/guides/get-started/quick-start.md +1 -2
- package/zh/guides/topic-detail/framework-plugin/implement.md +54 -6
- package/zh/guides/topic-detail/micro-frontend/c02-development.md +2 -2
- package/zh/guides/topic-detail/micro-frontend/c03-main-app.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/dev/with-master-app.md +0 -31
- package/zh/configure/app/dev/with-master-app.md +0 -32
@@ -0,0 +1,70 @@
|
|
1
|
+
* 类型: `string | Object`
|
2
|
+
* 默认值: `null`
|
3
|
+
|
4
|
+
配置该选项后,开发环境时会启动全局代理,类似 [Fiddler](https://www.telerik.com/fiddler), [Charles](https://www.charlesproxy.com/) 等 web 代理调试工具,可以用来查看、修改 HTTP/HTTPS 请求、响应、也可以用作代理服务器。
|
5
|
+
|
6
|
+
|
7
|
+
:::tip 提示
|
8
|
+
使用该选项需要提前安装 `@modern-js/plugin-proxy`。
|
9
|
+
:::
|
10
|
+
|
11
|
+
值为 `Object` 时,对象的 `key` 对应匹配的 `pattern`,对象的 `value` 对应匹配的 `target`。
|
12
|
+
|
13
|
+
例如:
|
14
|
+
|
15
|
+
```typescript title="modern.config.ts"
|
16
|
+
export default defineConfig({
|
17
|
+
dev: {
|
18
|
+
proxy: {
|
19
|
+
'https://www.baidu.com': 'https://google.com.hk',
|
20
|
+
//可以通过 file 协议直接返回静态文件。
|
21
|
+
'https://example.com/api': 'file://./data.json',
|
22
|
+
}
|
23
|
+
}
|
24
|
+
});
|
25
|
+
```
|
26
|
+
|
27
|
+
值为 `string` 时, 可以用来指定单独的代理文件,例如:
|
28
|
+
|
29
|
+
|
30
|
+
```typescript title="modern.config.ts"
|
31
|
+
export default defineConfig({
|
32
|
+
dev: {
|
33
|
+
proxy: './proxy.js',
|
34
|
+
},
|
35
|
+
});
|
36
|
+
```
|
37
|
+
|
38
|
+
```js title="proxy.js"
|
39
|
+
module.exports = {
|
40
|
+
name: 'my-app',
|
41
|
+
rules: `
|
42
|
+
^example.com:8080/api/*** http://localhost:3001/api/$
|
43
|
+
`,
|
44
|
+
};
|
45
|
+
```
|
46
|
+
|
47
|
+
:::info 注
|
48
|
+
Modern.js 全局代理实现底层基于 [whistle](https://wproxy.org/whistle/), 更多匹配模式请参考: [匹配模式](https://wproxy.org/whistle/pattern.html)
|
49
|
+
:::
|
50
|
+
|
51
|
+
执行 `dev`, 提示如下时,即代理服务器启动成功:
|
52
|
+
|
53
|
+
```bash
|
54
|
+
App running at:
|
55
|
+
|
56
|
+
Local: http://localhost:8080/
|
57
|
+
Network: http://192.168.0.1:8080/
|
58
|
+
|
59
|
+
ℹ info Starting the proxy server.....
|
60
|
+
✔ success Proxy Server start on localhost:8899
|
61
|
+
```
|
62
|
+
|
63
|
+
访问 `localhost:8899`, 可以在 UI 界面上查看 Network 以及配置代理规则:
|
64
|
+
|
65
|
+
![proxy ui 界面](https://lf3-static.bytednsdoc.com/obj/eden-cn/aphqeh7uhohpquloj/modern-js/docs/dev-proxy.png)
|
66
|
+
|
67
|
+
:::caution 注意
|
68
|
+
https 代理自动安装证书需要获取 root 权限, 请根据提示输入密码即可。**密码仅在信任证书时使用,不会泄漏或者用于其他环节**。
|
69
|
+
:::
|
70
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
|
@@ -0,0 +1,62 @@
|
|
1
|
+
---
|
2
|
+
title: autoLoadPlugins (自动注册插件)
|
3
|
+
sidebar_position: 11
|
4
|
+
---
|
5
|
+
|
6
|
+
- 类型:`boolean`
|
7
|
+
- 默认值:`false`
|
8
|
+
|
9
|
+
用于配置 Modern.js 是否开启自动注册插件。
|
10
|
+
|
11
|
+
### 手动注册插件
|
12
|
+
|
13
|
+
默认情况下,安装插件后需要你在 `modern.config.ts` 文件中手动注册插件。
|
14
|
+
|
15
|
+
```ts title="modern.config.ts"
|
16
|
+
import AppToolsPlugin, { defineConfig } from '@modern-js/app-tools';
|
17
|
+
import I18nPlugin from '@modern-js/plugin-i18n';
|
18
|
+
|
19
|
+
default export defineConfig({
|
20
|
+
plugins: [AppToolsPlugin(), I18nPlugin()]
|
21
|
+
})
|
22
|
+
|
23
|
+
```
|
24
|
+
|
25
|
+
### 自动注册插件
|
26
|
+
|
27
|
+
除了手段注册,Modern.js 还提供自动注册插件的方式: 将 `autoLoadPlugin` 配置项置为 `true`。
|
28
|
+
|
29
|
+
```ts title="modern.config.ts"
|
30
|
+
import { defineConfig } from '@modern-js/app-tools';
|
31
|
+
|
32
|
+
default export defineConfig({
|
33
|
+
autoLoadPlugins: true
|
34
|
+
})
|
35
|
+
```
|
36
|
+
|
37
|
+
Modern.js 将通过以下几个步骤帮你自动注册插件
|
38
|
+
|
39
|
+
1. Modern.js 在内部维护一份官方插件列表。
|
40
|
+
|
41
|
+
```js
|
42
|
+
const InternalPlugins = ['@modern-js/app-tools', '@modern-js/plugin-i18n', ...];
|
43
|
+
```
|
44
|
+
|
45
|
+
2. Modern.js 将读取你的 `package.json` 文件,收集依赖信息。
|
46
|
+
|
47
|
+
```json title="package.json"
|
48
|
+
"dependencies": {
|
49
|
+
"@modern-js/plugin-i18n": "x.x.x"
|
50
|
+
...
|
51
|
+
},
|
52
|
+
"devDependencies": {
|
53
|
+
"@modern-js/app-tools": "x.x.x"
|
54
|
+
...
|
55
|
+
}
|
56
|
+
```
|
57
|
+
|
58
|
+
3. Modern.js 观察到你安装了 `@modern-js/plugin-i18n` 和 `@modern-js/app-tools` 等依赖后,将会引入插件自动注册。
|
59
|
+
|
60
|
+
可以注意到这种方式相对黑盒,你甚至对加载插件的过程是无感知的。我们希望更多的细节暴露给开发者,能让开发者去控制这一过程。
|
61
|
+
|
62
|
+
**因此我们更加推荐你手动注册插件。**
|
@@ -52,13 +52,3 @@ export default defineConfig({
|
|
52
52
|
* 默认值:`false`
|
53
53
|
|
54
54
|
是否 `external` 基础库,当设置为 `true` 时,当前子应用将会 `external`:`react`、`react-dom`,`EdenX` 主应用会自动 `setExternal` 这两个基础库,如果其他类型的框架请通过 `Garfish.setExternal` 增加 `react`、`react-dom` 依赖
|
55
|
-
|
56
|
-
|
57
|
-
### moduleApp
|
58
|
-
|
59
|
-
* 类型:`string`
|
60
|
-
|
61
|
-
* 默认值:`undefined`
|
62
|
-
|
63
|
-
该值可以配置子应用的名称,目前主要结合 [`dev.withMasterApp`](/docs/configure/app/dev/with-master-app) 进行测试。
|
64
|
-
|
@@ -5,74 +5,6 @@ sidebar_label: proxy
|
|
5
5
|
# dev.proxy
|
6
6
|
|
7
7
|
|
8
|
+
import GlobalProxyConfig from '@site-docs/components/global-proxy-config.md'
|
8
9
|
|
9
|
-
|
10
|
-
* 默认值: `null`
|
11
|
-
|
12
|
-
配置该选项后,开发环境时会启动全局代理,类似 [Fiddler](https://www.telerik.com/fiddler), [Charles](https://www.charlesproxy.com/) 等 web 代理调试工具,可以用来查看、修改 HTTP/HTTPS 请求、响应、也可以用作代理服务器。
|
13
|
-
|
14
|
-
|
15
|
-
:::tip 提示
|
16
|
-
使用该选项需要提前安装 `@modern-js/plugin-proxy`。
|
17
|
-
:::
|
18
|
-
|
19
|
-
值为 `Object` 时,对象的 `key` 对应匹配的 `pattern`,对象的 `value` 对应匹配的 `target`。
|
20
|
-
|
21
|
-
例如:
|
22
|
-
|
23
|
-
```typescript title="modern.config.ts"
|
24
|
-
export default defineConfig({
|
25
|
-
dev: {
|
26
|
-
proxy: {
|
27
|
-
'https://www.baidu.com': 'https://google.com.hk',
|
28
|
-
//可以通过 file 协议直接返回静态文件。
|
29
|
-
'https://example.com/api': 'file://./data.json',
|
30
|
-
}
|
31
|
-
}
|
32
|
-
});
|
33
|
-
```
|
34
|
-
|
35
|
-
值为 `string` 时, 可以用来指定单独的代理文件,例如:
|
36
|
-
|
37
|
-
|
38
|
-
```typescript title="modern.config.ts"
|
39
|
-
export default defineConfig({
|
40
|
-
dev: {
|
41
|
-
proxy: './proxy.js',
|
42
|
-
},
|
43
|
-
});
|
44
|
-
```
|
45
|
-
|
46
|
-
```js title="proxy.js"
|
47
|
-
module.exports = {
|
48
|
-
name: 'my-app',
|
49
|
-
rules: `
|
50
|
-
^example.com:8080/api/*** http://localhost:3001/api/$
|
51
|
-
`,
|
52
|
-
};
|
53
|
-
```
|
54
|
-
|
55
|
-
:::info 注
|
56
|
-
Modern.js 全局代理实现底层基于 [whistle](https://wproxy.org/whistle/), 更多匹配模式请参考: [匹配模式](https://wproxy.org/whistle/pattern.html)
|
57
|
-
:::
|
58
|
-
|
59
|
-
执行 `dev`, 提示如下时,即代理服务器启动成功:
|
60
|
-
|
61
|
-
```bash
|
62
|
-
App running at:
|
63
|
-
|
64
|
-
Local: http://localhost:8080/
|
65
|
-
Network: http://192.168.0.1:8080/
|
66
|
-
|
67
|
-
ℹ info Starting the proxy server.....
|
68
|
-
✔ success Proxy Server start on localhost:8899
|
69
|
-
```
|
70
|
-
|
71
|
-
访问 `localhost:8899`, 可以在 UI 界面上查看 Network 以及配置代理规则:
|
72
|
-
|
73
|
-
![proxy ui 界面](https://lf3-static.bytednsdoc.com/obj/eden-cn/aphqeh7uhohpquloj/modern-js/docs/dev-proxy.png)
|
74
|
-
|
75
|
-
:::caution 注意
|
76
|
-
https 代理自动安装证书需要获取 root 权限, 请根据提示输入密码即可。**密码仅在信任证书时使用,不会泄漏或者用于其他环节**。
|
77
|
-
:::
|
78
|
-
|
10
|
+
<GlobalProxyConfig />
|
@@ -13,24 +13,10 @@ sidebar_label: masterApp
|
|
13
13
|
## 示例
|
14
14
|
|
15
15
|
import EnableMicroFrontend from '@site-docs/components/enable-micro-frontend.md';
|
16
|
+
import MasterManifestAppConfig from '@site-docs/components/micro-master-manifest-config.md';
|
16
17
|
|
17
18
|
<EnableMicroFrontend />
|
18
|
-
|
19
|
-
## `manifest`
|
20
|
-
|
21
|
-
```ts
|
22
|
-
interface Manifest {
|
23
|
-
getAppList?: ()=> Array<AppInfo>
|
24
|
-
}
|
25
|
-
```
|
26
|
-
|
27
|
-
#### `getAppList?`
|
28
|
-
|
29
|
-
通过 `getAppList` 配置,可以自定义如何获取远程列表数据
|
30
|
-
|
31
|
-
```ts
|
32
|
-
type GetAppList = ()=> Promise<Array<AppInfo>>;
|
33
|
-
```
|
19
|
+
<MasterManifestAppConfig />
|
34
20
|
|
35
21
|
|
36
22
|
### apps
|
@@ -2,19 +2,33 @@
|
|
2
2
|
sidebar_label: router
|
3
3
|
---
|
4
4
|
|
5
|
+
import RouterLegacyTip from '@site-docs/components/router-legacy-tip.md'
|
6
|
+
|
7
|
+
<RouterLegacyTip />
|
8
|
+
|
5
9
|
# runtime.router
|
6
10
|
|
7
11
|
* 类型: `boolean | Object`
|
8
12
|
* 默认值: `false`。
|
9
13
|
|
10
|
-
开启 `router` 之后,支持使用 Modern.js 默认提供的约定式路由进行路由管理。
|
14
|
+
开启 `router` 之后,支持使用 Modern.js 默认提供的约定式路由进行路由管理。Modern.js 的路由模块基于 [React Router 6](https://reactrouter.com/) 实现。
|
11
15
|
|
12
16
|
具体配置如下:
|
13
17
|
|
18
|
+
### basename
|
19
|
+
|
20
|
+
* 类型: `string`
|
21
|
+
* 默认值: ``
|
22
|
+
|
23
|
+
设置客户端路由的 `basename`,通常用于应用需要部署在域名非根路径下的场景。
|
24
|
+
|
14
25
|
### supportHtml5History
|
15
26
|
|
16
|
-
* 类型: `
|
27
|
+
* 类型: `boolean`
|
17
28
|
* 默认值: `true`
|
18
29
|
|
19
|
-
值为 `true
|
30
|
+
值为 `true`,使用 `BrowserRouter`;否则使用 `HashRouter`。推荐使用 `BrowserRouter`。
|
20
31
|
|
32
|
+
:::warning
|
33
|
+
当开启 SSR 时,不支持设置 `supportHtml5History`。
|
34
|
+
:::
|
@@ -61,7 +61,6 @@ export default defineConfig({
|
|
61
61
|
|
62
62
|
* `entry`:`string`,入口文件路径。
|
63
63
|
* `disableMount`:`boolean = false`,关闭 Modern.js 生成入口代码的行为。
|
64
|
-
* `enableFileSystemRoutes`:`boolean = false`,是否 [使用约定式路由](/docs/apis/app/hooks/src/pages)。
|
65
64
|
|
66
65
|
```ts title="modern.config.ts"
|
67
66
|
import { defineConfig } from '@modern-js/app-tools';
|
@@ -76,8 +75,7 @@ export default defineConfig({
|
|
76
75
|
// 启用约定式路由
|
77
76
|
entry_spa: {
|
78
77
|
// 约定式路由的入口路径必须设置为目录
|
79
|
-
entry: './src/about'
|
80
|
-
enableFileSystemRoutes: true,
|
78
|
+
entry: './src/about'
|
81
79
|
},
|
82
80
|
},
|
83
81
|
},
|
@@ -4,39 +4,32 @@ title: tools.tailwindcss
|
|
4
4
|
sidebar_label: tailwindcss
|
5
5
|
---
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
* 默认值:见下方配置详情。
|
7
|
+
- 类型: `Object | Function`
|
8
|
+
- 默认值:见下方配置详情。
|
10
9
|
|
11
10
|
<details>
|
12
11
|
<summary>TailwindCSS 配置详情</summary>
|
13
12
|
|
14
13
|
```js
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
removeDeprecatedGapUtilities: false,
|
29
|
-
purgeLayersByDefault: true,
|
30
|
-
defaultLineHeights: false,
|
31
|
-
standardFontWeights: false,
|
32
|
-
},
|
33
|
-
theme: source.designSystem // 使用source.designSystem配置作为Tailwind CSS Theme配置
|
34
|
-
}
|
14
|
+
const tailwind = {
|
15
|
+
content: [
|
16
|
+
'./config/html/**/*.html',
|
17
|
+
'./config/html/**/*.ejs',
|
18
|
+
'./config/html/**/*.hbs',
|
19
|
+
'./src/**/*.js',
|
20
|
+
'./src/**/*.jsx',
|
21
|
+
'./src/**/*.ts',
|
22
|
+
'./src/**/*.tsx',
|
23
|
+
'./storybook/**/*',
|
24
|
+
],
|
25
|
+
theme: source.designSystem, // 使用source.designSystem配置作为Tailwind CSS Theme配置
|
26
|
+
};
|
35
27
|
```
|
36
28
|
|
37
29
|
:::tip 提示
|
38
30
|
更多关于:<a href="https://tailwindcss.com/docs/configuration" target="_blank">TailwindCSS 配置</a>。
|
39
31
|
:::
|
32
|
+
|
40
33
|
</details>
|
41
34
|
|
42
35
|
对应 [TailwindCSS](https://tailwindcss.com/docs/configuration) 的配置,值为 `Object` 类型时,与默认配置通过 `Object.assign` 合并。
|
@@ -3,6 +3,8 @@ sidebar_position: 3
|
|
3
3
|
title: 运行时框架
|
4
4
|
---
|
5
5
|
|
6
|
+
Modern.js 的 BFF 支持不同的运行时框架,当前 Modern.js 的 BFF 支持两种运行时框架 [Express.js](https://expressjs.com/) 和 [Koa.js](https://koajs.com/)。
|
7
|
+
|
6
8
|
## 函数写法
|
7
9
|
|
8
10
|
在函数写法下,各类运行时框架仅中间件写法存在差异,其他实现基本相同。这里以 Express 为例,介绍如何在 `api/_app.ts` 中,手写一个中间件,添加权限校验:
|
@@ -1,18 +1,24 @@
|
|
1
1
|
---
|
2
2
|
sidebar_position: 1
|
3
|
-
title:
|
3
|
+
title: 基础用法
|
4
4
|
---
|
5
5
|
|
6
|
-
Modern.js
|
6
|
+
通过 Modern.js 开发的应用,可以在 `api/` 目录下定义接口函数,前端可以调用这些接口函数,即可发起请求,无需写前后端胶水层代码,同时保证前后端类型安全。
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
## 启用 BFF
|
9
|
+
|
10
|
+
import EnableBFF from '@site-docs/components/enable-bff.md'
|
11
|
+
|
12
|
+
<EnableBFF/>
|
11
13
|
|
12
14
|
## BFF 函数
|
13
15
|
|
14
16
|
允许通过一体化调用的函数,称为 **BFF 函数**。这里写一个最简单的 BFF 函数,创建 `api/hello.ts` 文件:
|
15
17
|
|
18
|
+
:::caution
|
19
|
+
如果是框架模式(有 `api/lambda` 目录),需要创建 `api/lambda/hello.ts`
|
20
|
+
:::
|
21
|
+
|
16
22
|
```ts title="api/hello.ts"
|
17
23
|
export const get = async () => 'Hello Modern.js';
|
18
24
|
```
|
@@ -41,7 +47,7 @@ Modern.js 生成器已经在 `tsconfig.json` 中配置 `@api` 别名,因此可
|
|
41
47
|
|
42
48
|
执行 `pnpm run dev` 打开 `http://localhost:8080/` 可以看到页面已经展示了 BFF 函数返回的内容,在 Network 中可以看到页面向 `http://localhost:8080/api/hello` 发送了请求:
|
43
49
|
|
44
|
-
![Network](https://
|
50
|
+
![Network](https://p6-piu.byteimg.com/tos-cn-i-8jisjyls3a/fd41750f8d414179a9b4ecb519919b36~tplv-8jisjyls3a-3:0:0:q75.png)
|
45
51
|
|
46
52
|
## 函数路由
|
47
53
|
|
@@ -53,32 +59,30 @@ Modern.js 中,BFF 函数对应的路由系统是基于文件系统实现的,
|
|
53
59
|
函数写法和框架写法会在下一节详细介绍。
|
54
60
|
:::
|
55
61
|
|
56
|
-
|
62
|
+
所有 BFF 函数生成的路由都带有统一的前缀,默认值为 `/api`。可以通过 [bff.prefix](/docs/configure/app/bff/prefix) 设置公共路由的前缀。
|
57
63
|
|
58
|
-
|
59
|
-
可以通过 [bff.prefix](/docs/configure/app/bff/prefix) 设置公共路由的前缀。
|
60
|
-
:::
|
64
|
+
下面介绍几种路由的约定。
|
61
65
|
|
62
66
|
### 默认路由
|
63
67
|
|
64
68
|
以 `index.[jt]s` 命名的文件会被映射到上一层目录。
|
65
69
|
|
66
|
-
* `api/index.ts` ->
|
67
|
-
* `api/user/index.ts` ->
|
70
|
+
* `api/index.ts` -> `{prefix}/`
|
71
|
+
* `api/user/index.ts` -> `{prefix}/user`
|
68
72
|
|
69
|
-
###
|
73
|
+
### 多层路由
|
70
74
|
|
71
75
|
支持解析嵌套的文件,如果创建嵌套文件夹结构,文件仍会以相同方式自动解析路由。
|
72
76
|
|
73
|
-
* `api/hello.ts` ->
|
74
|
-
* `api/user/list.ts` ->
|
77
|
+
* `api/hello.ts` -> `{prefix}/hello`
|
78
|
+
* `api/user/list.ts` -> `{prefix}/user/list`
|
75
79
|
|
76
80
|
### 动态路由
|
77
81
|
|
78
|
-
同样的,创建命名带有 `[xxx]`
|
82
|
+
同样的,创建命名带有 `[xxx]` 的文件夹或者文件,支持动态的命名路由参数。动态路由的函数参数规则可以看 [dynamac-path](/docs/guides/advanced-features/bff/function#dynamic-path)
|
79
83
|
|
80
|
-
* `api/user/[username]/info.ts` ->
|
81
|
-
* `api/user/username/[action].ts` ->
|
84
|
+
* `api/user/[username]/info.ts` -> `{prefix}/user/:username/info`
|
85
|
+
* `api/user/username/[action].ts` -> `{prefix}/user/username/:action`
|
82
86
|
|
83
87
|
### 白名单
|
84
88
|
|
@@ -102,7 +106,7 @@ Modern.js 的 BFF 函数需要遵循 RESTful API 标准来定义, 遵循 HTTP Me
|
|
102
106
|
|
103
107
|
### 函数具名导出
|
104
108
|
|
105
|
-
Modern.js BFF 函数的导出名决定了函数对应接口的 Method
|
109
|
+
Modern.js BFF 函数的导出名决定了函数对应接口的 Method,如 `get`,`post` 等。
|
106
110
|
|
107
111
|
例如,按照以下例子,可导出一个 GET 接口。
|
108
112
|
|
@@ -126,9 +130,9 @@ export const post = async () => {
|
|
126
130
|
};
|
127
131
|
```
|
128
132
|
|
129
|
-
*
|
133
|
+
* 对应 HTTP Method,Modern.js 也支持了 9 种定义,即:`GET`、`POST`、`PUT`、`DELETE`、`CONNECT`、`TRACE`、`PATCH`、`OPTION`、`HEAD`,即可以用这些 Method 作为函数导出的名字。
|
130
134
|
|
131
|
-
*
|
135
|
+
* 名字是大小不敏感的,如果是 `GET`,写成 `get`、`Get`、`GEt`、`GET`,都可以准确识别。而默认导出,即 `export default xxx` 则会被映射为 `Get`。
|
132
136
|
|
133
137
|
* 可以在一个文件中定义多个不同 Method 的函数,但如果定义多个相同 Method 的函数,则只有第一个会生效。
|
134
138
|
|
@@ -140,9 +144,7 @@ export const post = async () => {
|
|
140
144
|
|
141
145
|
如上所述,为了满足 RESTful API 的设计标准,因此 Modern.js 中 BFF 函数需要遵循一定的入参规则。
|
142
146
|
|
143
|
-
|
144
|
-
|
145
|
-
普通函数参数分为两块,分别是请求路径中的动态部分和请求选项 `RequestOption`。
|
147
|
+
函数参数分为两块,分别是请求路径中的动态部分和请求选项 `RequestOption`。
|
146
148
|
|
147
149
|
#### Dynamic Path
|
148
150
|
|
@@ -190,6 +192,24 @@ export async function post(
|
|
190
192
|
}
|
191
193
|
```
|
192
194
|
|
195
|
+
这里你也可以使用自定义类型:
|
196
|
+
```ts title="api/lambda/hello.ts"
|
197
|
+
import type { RequestOption } from '@modern-js/runtime/server'
|
198
|
+
|
199
|
+
type IQuery = {
|
200
|
+
// some types
|
201
|
+
}
|
202
|
+
type IData = {
|
203
|
+
// some types
|
204
|
+
}
|
205
|
+
|
206
|
+
export async function post(
|
207
|
+
{ query, data }: { query:IQuery, data:IData }
|
208
|
+
) {
|
209
|
+
// do somethings
|
210
|
+
}
|
211
|
+
```
|
212
|
+
|
193
213
|
当函数文件使用动态路由规则时,动态路由会在 `RequestOption` 对象参数前。
|
194
214
|
|
195
215
|
```ts title="api/[sku]/[id]/item.ts"
|
@@ -5,6 +5,10 @@ sidebar_position: 6
|
|
5
5
|
|
6
6
|
代码分割是优化前端资源加载的一种常用手段,本文将介绍 Modern.js 支持的三种代码分割方式:
|
7
7
|
|
8
|
+
:::info
|
9
|
+
使用 Modern.js [约定式路由](/docs/guides/basic-features/routes#约定式路由)时,默认会根据路由组件做代码分割,包裹 `Suspense` 组件,无需自行进行代码分割。
|
10
|
+
:::
|
11
|
+
|
8
12
|
- `import`
|
9
13
|
- `React.lazy`
|
10
14
|
- `loadable`
|
@@ -21,30 +25,14 @@ import("./math").then(math => {
|
|
21
25
|
|
22
26
|
`./math` 路径对应的 JS 模块会被打包到单独的 JS 文件中。
|
23
27
|
|
24
|
-
##
|
25
|
-
|
26
|
-
使用 `loadable` API,示例如下:
|
27
|
-
|
28
|
-
```ts
|
29
|
-
import loadable from '@modern-js/runtime/loadable'
|
30
|
-
|
31
|
-
const OtherComponent = loadable(() => import('./OtherComponent'));
|
32
|
-
|
33
|
-
function MyComponent() {
|
34
|
-
return <OtherComponent />
|
35
|
-
}
|
36
|
-
```
|
28
|
+
## React.lazy
|
37
29
|
|
38
|
-
|
30
|
+
React 官方提供的组件代码分割的方式。
|
39
31
|
|
40
|
-
:::
|
41
|
-
|
32
|
+
:::caution
|
33
|
+
React 17 及以下版本不支持 SSR,建议 React17 的 SSR 应用使用 loadable。
|
42
34
|
:::
|
43
35
|
|
44
|
-
## React.lazy
|
45
|
-
|
46
|
-
React 官方提供的组件代码分割的方式,**缺点是不支持 SSR**。
|
47
|
-
|
48
36
|
```ts
|
49
37
|
import React, { Suspense } from 'react';
|
50
38
|
|
@@ -66,3 +54,23 @@ function MyComponent() {
|
|
66
54
|
```
|
67
55
|
|
68
56
|
`React.lazy` 更多用法请见 [React lazy](https://zh-hans.reactjs.org/docs/code-splitting.html#reactlazy)。
|
57
|
+
|
58
|
+
## loadable
|
59
|
+
|
60
|
+
使用 `loadable` API,示例如下:
|
61
|
+
|
62
|
+
```ts
|
63
|
+
import loadable from '@modern-js/runtime/loadable'
|
64
|
+
|
65
|
+
const OtherComponent = loadable(() => import('./OtherComponent'));
|
66
|
+
|
67
|
+
function MyComponent() {
|
68
|
+
return <OtherComponent />
|
69
|
+
}
|
70
|
+
```
|
71
|
+
|
72
|
+
`loadable` 更多用法请见 [loadable API](/docs/apis/app/runtime/utility/loadable)。
|
73
|
+
|
74
|
+
:::info 注
|
75
|
+
`loadable` 开箱即用的支持 SSR,但不支持和 Suspense 一起使用,如果是 CSR 项目可以使用 [loadable.lazy](https://loadable-components.com/docs/suspense/)
|
76
|
+
:::
|
@@ -3,13 +3,32 @@ title: 客户端兼容性
|
|
3
3
|
sidebar_position: 5
|
4
4
|
---
|
5
5
|
|
6
|
-
##
|
6
|
+
## Browserslist 配置
|
7
|
+
|
8
|
+
Modern.js 支持在项目根目录 `package.json` 文件中的 `browserslist` 字段(或单独的 `.browserslistrc` 文件)指定项目覆盖的目标浏览器范围。该值会被 [`@babel/preset-env`](https://babeljs.io/docs/en/babel-preset-env) 和 [`autoprefixer`](https://github.com/postcss/autoprefixer) 用来确定需要转换的 JavaScript 语法特性和需要添加的 CSS 浏览器前缀。
|
9
|
+
|
10
|
+
Modern.js 中默认值如下:
|
11
|
+
|
12
|
+
```js
|
13
|
+
['> 0.01%', 'not dead', 'not op_mini all']
|
14
|
+
```
|
15
|
+
|
16
|
+
可以在[这里](https://github.com/browserslist/browserslist)了解如何自定义浏览器范围。
|
17
|
+
|
18
|
+
查看 Modern.js Builder 文档了解更多 [Browserlist](https://modernjs.dev/builder/zh/guide/advanced/browserslist.html) 相关内容。
|
19
|
+
|
20
|
+
:::note
|
21
|
+
Modern.js 支持配置 [output.overrideBrowserlist](/docs/configure/app/output/override-browserslist) 覆盖默认 browserlist 值。
|
22
|
+
:::
|
23
|
+
|
24
|
+
|
25
|
+
## Polyfill
|
7
26
|
|
8
27
|
### 编译时 Polyfill
|
9
28
|
|
10
29
|
Modern.js 在编译时默认通过 [core-js](https://github.com/zloirock/core-js) 引入对应的 Polyfill 代码。
|
11
30
|
|
12
|
-
默认情况下会根据项目
|
31
|
+
默认情况下会根据项目 Browserslist 的设置情况引入所需的 Polyfill 代码, 这样基本不用再担心项目源码和第三方依赖的 Polyfill 问题了,但是因为包含了一些没有用到的 Polyfill 代码,所以最终的包大小可能会有所增加。
|
13
32
|
|
14
33
|
:::info 注
|
15
34
|
对于明确第三方依赖不需要 Polyfill 的场景,可以设置 [`output.polyfill`](/docs/configure/app/output/polyfill) 为 `usage`, 这样 Babel 编译时只会根据代码中使用到的语法引入 Polyfill 代码。
|
@@ -48,17 +67,8 @@ export default defineConfig({
|
|
48
67
|
|
49
68
|
在 Chrome 51 下访问页面可以看到 `http://localhost:8080/__polyfill__` 返回内容如下:
|
50
69
|
|
51
|
-
|
52
70
|
![ua-polyfill](https://lf3-static.bytednsdoc.com/obj/eden-cn/aphqeh7uhohpquloj/modern-js/docs/ua-polyfill.png)
|
53
71
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
Modern.js 中默认值如下:
|
59
|
-
|
60
|
-
```js
|
61
|
-
['> 0.01%', 'not dead', 'not op_mini all']
|
62
|
-
```
|
63
|
-
|
64
|
-
可以在[这里](https://github.com/browserslist/browserslist)了解如何自定义浏览器范围。
|
72
|
+
:::caution 注意
|
73
|
+
该功能只有在使用 Modern.js 内置的 Web Server 时才会生效。
|
74
|
+
:::
|