@modern-js/main-doc 2.22.1 → 2.23.1
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +45 -0
- package/docs/en/apis/app/commands.mdx +5 -5
- package/docs/en/apis/app/hooks/config/storybook.mdx +24 -0
- package/docs/en/apis/app/hooks/src/stories.mdx +1 -1
- package/docs/en/apis/app/runtime/core/bootstrap.mdx +5 -5
- package/docs/en/apis/app/runtime/core/use-loader.mdx +2 -2
- package/docs/en/apis/app/runtime/core/use-module-apps.mdx +2 -2
- package/docs/en/apis/app/runtime/core/use-runtime-context.mdx +1 -1
- package/docs/en/apis/app/runtime/testing/render.mdx +1 -1
- package/docs/en/components/bff-proxy-path-rewrite.mdx +16 -0
- package/docs/en/components/bff-proxy-principle.mdx +1 -0
- package/docs/en/components/enable-bff-caution.mdx +3 -0
- package/docs/en/components/global-proxy-config.mdx +33 -13
- package/docs/en/configure/app/bff/enable-handle-web.mdx +5 -5
- package/docs/en/configure/app/bff/prefix.mdx +6 -6
- package/docs/en/configure/app/bff/proxy.mdx +19 -30
- package/docs/en/configure/app/builder-plugins.mdx +1 -1
- package/docs/en/configure/app/plugins.mdx +19 -19
- package/docs/en/configure/app/runtime/intro.mdx +28 -4
- package/docs/en/configure/app/runtime/master-app.mdx +1 -1
- package/docs/en/configure/app/runtime/router.mdx +2 -2
- package/docs/en/configure/app/server/base-url.mdx +4 -4
- package/docs/en/configure/app/server/enable-framework-ext.mdx +4 -4
- package/docs/en/configure/app/server/port.mdx +6 -6
- package/docs/en/configure/app/server/public-routes.mdx +5 -5
- package/docs/en/configure/app/server/routes.mdx +13 -13
- package/docs/en/configure/app/server/ssr-by-entries.mdx +4 -4
- package/docs/en/configure/app/server/ssr.mdx +5 -5
- package/docs/en/configure/app/testing/transformer.mdx +4 -8
- package/docs/en/configure/app/usage.mdx +5 -5
- package/docs/en/guides/advanced-features/code-split.mdx +15 -14
- package/docs/en/guides/advanced-features/compatibility.mdx +3 -24
- package/docs/en/guides/advanced-features/rspack-start.mdx +0 -1
- package/docs/en/guides/advanced-features/ssr.mdx +2 -2
- package/docs/en/guides/advanced-features/testing.mdx +8 -7
- package/docs/en/guides/advanced-features/using-storybook.mdx +44 -0
- package/docs/en/guides/advanced-features/web-server.mdx +16 -16
- package/docs/en/guides/basic-features/alias.mdx +11 -9
- package/docs/en/guides/basic-features/env-vars.mdx +14 -14
- package/docs/en/guides/basic-features/html.mdx +41 -43
- package/docs/en/guides/basic-features/mock.mdx +10 -10
- package/docs/en/guides/topic-detail/framework-plugin/implement.mdx +4 -4
- package/docs/en/guides/topic-detail/framework-plugin/introduction.mdx +1 -1
- package/docs/en/guides/troubleshooting/cli.mdx +13 -13
- package/docs/en/guides/troubleshooting/dependencies.mdx +25 -25
- package/docs/en/tutorials/first-app/c03-css.mdx +1 -1
- package/docs/zh/apis/app/commands.mdx +4 -4
- package/docs/zh/apis/app/hooks/config/storybook.mdx +24 -0
- package/docs/zh/apis/app/hooks/src/stories.mdx +1 -1
- package/docs/zh/apis/app/runtime/testing/render.mdx +1 -1
- package/docs/zh/components/bff-proxy-path-rewrite.mdx +16 -0
- package/docs/zh/components/bff-proxy-principle.mdx +1 -0
- package/docs/zh/components/enable-bff-caution.mdx +4 -0
- package/docs/zh/components/global-proxy-config.mdx +35 -11
- package/docs/zh/configure/app/bff/enable-handle-web.mdx +3 -3
- package/docs/zh/configure/app/bff/prefix.mdx +2 -3
- package/docs/zh/configure/app/bff/proxy.mdx +11 -21
- package/docs/zh/configure/app/runtime/intro.mdx +1 -1
- package/docs/zh/configure/app/server/base-url.mdx +1 -1
- package/docs/zh/configure/app/server/public-routes.mdx +2 -2
- package/docs/zh/configure/app/server/routes.mdx +2 -2
- package/docs/zh/configure/app/server/ssr-by-entries.mdx +3 -3
- package/docs/zh/configure/app/server/ssr.mdx +1 -1
- package/docs/zh/configure/app/testing/transformer.mdx +6 -3
- package/docs/zh/configure/app/usage.mdx +9 -9
- package/docs/zh/guides/advanced-features/code-split.mdx +13 -12
- package/docs/zh/guides/advanced-features/rspack-start.mdx +0 -1
- package/docs/zh/guides/advanced-features/ssr.mdx +2 -2
- package/docs/zh/guides/advanced-features/testing.mdx +4 -4
- package/docs/zh/guides/advanced-features/using-storybook.mdx +44 -0
- package/docs/zh/guides/basic-features/alias.mdx +5 -3
- package/docs/zh/guides/basic-features/html.mdx +9 -9
- package/docs/zh/guides/basic-features/mock.mdx +2 -2
- package/docs/zh/guides/topic-detail/_category_.json +1 -1
- package/docs/zh/guides/troubleshooting/cli.mdx +1 -1
- package/docs/zh/guides/troubleshooting/dependencies.mdx +1 -1
- package/package.json +7 -6
@@ -5,7 +5,7 @@ title: Add Style
|
|
5
5
|
|
6
6
|
import { Tabs, Tab as TabItem } from "@theme";
|
7
7
|
|
8
|
-
In the previous chapter, we learned how to use components from the
|
8
|
+
In the previous chapter, we learned how to use components from the third-party library.
|
9
9
|
|
10
10
|
In this chapter, we will learn how to implement UI components.
|
11
11
|
|
@@ -106,12 +106,13 @@ File sizes after production build:
|
|
106
106
|
|
107
107
|
`modern new` 命令用于在已有项目中添加项目元素。
|
108
108
|
|
109
|
-
|
109
|
+
比如添加应用入口、启用一些可选功能如 Tailwind CSS、微前端开发模式等。
|
110
110
|
|
111
111
|
```bash
|
112
112
|
Usage: modern new [options]
|
113
113
|
|
114
114
|
Options:
|
115
|
+
--lang <lang> 设置 new 命令执行语言(zh 或者 en)
|
115
116
|
-d, --debug 开启 Debug 模式,打印调试日志信息 (default: false)
|
116
117
|
-c, --config <config> 生成器运行默认配置(JSON 字符串)
|
117
118
|
--dist-tag <tag> 生成器使用特殊的 npm Tag 版本
|
@@ -162,7 +163,6 @@ Usage: modern serve [options]
|
|
162
163
|
Options:
|
163
164
|
-c --config <config> 指定配置文件路径,可以为相对路径或绝对路径
|
164
165
|
-h, --help 显示命令帮助
|
165
|
-
--web-only 仅启动 Web 服务
|
166
166
|
--api-only 仅启动 API 接口服务
|
167
167
|
```
|
168
168
|
|
@@ -180,7 +180,7 @@ export default defineConfig({
|
|
180
180
|
|
181
181
|
在项目根目录下执行命令 `npx modern upgrade`,会默认将当前执行命令项目的 `package.json` 中的 Modern.js 相关依赖更新至最新版本。
|
182
182
|
|
183
|
-
```
|
183
|
+
```bash
|
184
184
|
Usage: modern upgrade [options]
|
185
185
|
|
186
186
|
Options:
|
@@ -194,7 +194,7 @@ Options:
|
|
194
194
|
|
195
195
|
`modern inspect` 命令用于查看项目的 [Modern.js Builder 配置](https://modernjs.dev/builder/guide/basic/builder-config.html) 以及 webpack 配置。
|
196
196
|
|
197
|
-
```
|
197
|
+
```bash
|
198
198
|
Usage: modern inspect [options]
|
199
199
|
|
200
200
|
Options:
|
@@ -2,6 +2,7 @@
|
|
2
2
|
title: storybook/
|
3
3
|
sidebar_position: 7
|
4
4
|
---
|
5
|
+
|
5
6
|
# storybook/
|
6
7
|
|
7
8
|
Modern.js 支持使用 Storybook 进行调试,当需要对 Storybook 进行配置时,需要在项目 config/storybook 目录进行配置。
|
@@ -12,3 +13,26 @@ Storybook 配置请查看:[Storybook 配置](https://storybook.js.org/docs/rea
|
|
12
13
|
使用 Storybook 进行调试需要提前在项目下执行 new 命令启用「Visual Testing (Storybook)」模式功能。
|
13
14
|
|
14
15
|
:::
|
16
|
+
|
17
|
+
### 示例
|
18
|
+
|
19
|
+
对于 Storybook Manager app 部分的 webpack 配置,可以通过增加 `./config/storybook/main.js` 文件进行配置。
|
20
|
+
|
21
|
+
```js
|
22
|
+
// ./config/storybook/main.js
|
23
|
+
|
24
|
+
module.exports = {
|
25
|
+
// it controls the Storybook manager app
|
26
|
+
managerWebpack: async (config, options) => {
|
27
|
+
// update config here
|
28
|
+
return config;
|
29
|
+
},
|
30
|
+
};
|
31
|
+
```
|
32
|
+
|
33
|
+
### 限制
|
34
|
+
|
35
|
+
在使用 config/storybook 目录进行配置时,存在以下限制:
|
36
|
+
|
37
|
+
- 不能修改 Story 文件存放的位置,即无法在 `main.js` 文件里修改 `stories` 配置。
|
38
|
+
- 不支持在 `main.js` 中修改 Webpack 和 Babel 相关配置,相关需求可通过 [`tools.webpack`](/configure/app/tools/webpack.html) / [`tools.babel`](/configure/app/tools/babel.html) 修改。
|
@@ -41,7 +41,7 @@ function render(ui: React.ReactElement<any>, options: Options): RenderResult;
|
|
41
41
|
- `options`:render 可选配置。
|
42
42
|
- `container`:表示组件所要挂载到的 DOM 节点,默认是会创建一个 `div` 元素,并自动添加到 `document.body` 上。这个 `div` 元素就是组件要挂载的节点。默认值是 `document.body.append(document.createElement('div'))`。
|
43
43
|
- `baseElement`:用于指定 `queries` 中使用到的 `basename`。如果指定了 `container`, 则默认值为 `container` 的值,否则就是 `document.body`。
|
44
|
-
- `hydrate`:如果设置为 `true`,则会使用 [ReactDOM.hydrate](https://
|
44
|
+
- `hydrate`:如果设置为 `true`,则会使用 [ReactDOM.hydrate](https://zh-hans.react.dev/reference/react-dom/hydrate) 渲染组件。默认值为 `false`。
|
45
45
|
- `wrapper`:是一个 react 组件,可用于自定义渲染逻辑。
|
46
46
|
- `queries`:自定义一些自己的 `queries`。
|
47
47
|
|
@@ -0,0 +1,16 @@
|
|
1
|
+
这里还可以进行路径重写,如将发送到 `localhost:8080/api/topics` 的请求代理到 `https://cnodejs.org/api/v1/topics`。
|
2
|
+
|
3
|
+
```ts title="modern.server-runtime.config.ts"
|
4
|
+
import { defineConfig } from '@modern-js/app-tools/server';
|
5
|
+
export default defineConfig({
|
6
|
+
bff: {
|
7
|
+
proxy: {
|
8
|
+
'/api': {
|
9
|
+
target: 'https://cnodejs.org',
|
10
|
+
pathRewrite: { '/api/topics': '/api/v1/topics' },
|
11
|
+
changeOrigin: true,
|
12
|
+
},
|
13
|
+
},
|
14
|
+
},
|
15
|
+
});
|
16
|
+
```
|
@@ -0,0 +1 @@
|
|
1
|
+
BFF Proxy 使用了强大的 [http-proxy-middleware](https://github.com/chimurai/http-proxy-middleware),如果需要更多高级的用法, 可以查看它的[文档](https://github.com/chimurai/http-proxy-middleware#options)。
|
@@ -1,14 +1,37 @@
|
|
1
1
|
- **类型:** `string | Object`
|
2
2
|
- **默认值:** `null`
|
3
3
|
|
4
|
-
|
4
|
+
该选项用于在开发环境下启用基于 [whistle](https://wproxy.org/whistle/) 的全局代理,可以用来查看、修改 HTTP/HTTPS 请求、响应、也可以用作代理服务器。
|
5
5
|
|
6
|
-
|
7
|
-
使用该选项需要提前安装 `@modern-js/plugin-proxy`。
|
6
|
+
### 注册插件
|
8
7
|
|
9
|
-
|
8
|
+
使用该选项前,你需要提前安装和注册 `@modern-js/plugin-proxy` 插件:
|
9
|
+
|
10
|
+
```bash
|
11
|
+
# npm
|
12
|
+
npm add @modern-js/plugin-proxy -D
|
13
|
+
|
14
|
+
# yarn
|
15
|
+
yarn add @modern-js/plugin-proxy -D
|
16
|
+
|
17
|
+
# pnpm
|
18
|
+
pnpm add @modern-js/plugin-proxy -D
|
19
|
+
```
|
20
|
+
|
21
|
+
安装完成后,在 `modern.config.ts` 文件中注册插件:
|
22
|
+
|
23
|
+
```ts title="modern.config.ts"
|
24
|
+
import appTools, { defineConfig } from '@modern-js/app-tools';
|
25
|
+
import proxyPlugin from '@modern-js/plugin-proxy';
|
26
|
+
|
27
|
+
export default defineConfig({
|
28
|
+
plugins: [appTools(), proxyPlugin()],
|
29
|
+
});
|
30
|
+
```
|
10
31
|
|
11
|
-
|
32
|
+
### Object 类型
|
33
|
+
|
34
|
+
`dev.proxy` 的值为 `Object` 时,对象的 `key` 对应匹配的 `pattern`,对象的 `value` 对应匹配的 `target`。
|
12
35
|
|
13
36
|
例如:
|
14
37
|
|
@@ -24,7 +47,11 @@ export default defineConfig({
|
|
24
47
|
});
|
25
48
|
```
|
26
49
|
|
27
|
-
|
50
|
+
请参考 [whistle - 匹配模式](https://wproxy.org/whistle/pattern.html) 来了解详细用法。
|
51
|
+
|
52
|
+
### String 类型
|
53
|
+
|
54
|
+
`dev.proxy` 的值为 `string` 时, 可以用来指定单独的代理文件,例如:
|
28
55
|
|
29
56
|
```ts title="modern.config.ts"
|
30
57
|
export default defineConfig({
|
@@ -43,10 +70,7 @@ module.exports = {
|
|
43
70
|
};
|
44
71
|
```
|
45
72
|
|
46
|
-
|
47
|
-
Modern.js 全局代理实现底层基于 [whistle](https://wproxy.org/whistle/), 更多匹配模式请参考: [匹配模式](https://wproxy.org/whistle/pattern.html)
|
48
|
-
|
49
|
-
:::
|
73
|
+
### 启动代理
|
50
74
|
|
51
75
|
执行 `dev`, 提示如下时,即代理服务器启动成功:
|
52
76
|
|
@@ -64,7 +88,7 @@ Modern.js 全局代理实现底层基于 [whistle](https://wproxy.org/whistle/),
|
|
64
88
|
|
65
89
|

|
66
90
|
|
67
|
-
:::
|
91
|
+
:::info
|
68
92
|
https 代理自动安装证书需要获取 root 权限, 请根据提示输入密码即可。**密码仅在信任证书时使用,不会泄漏或者用于其他环节**。
|
69
93
|
|
70
94
|
:::
|
@@ -7,9 +7,9 @@ title: enableHandleWeb
|
|
7
7
|
- **类型:** `boolean`
|
8
8
|
- **默认值:** `false`
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
import EnableBFFCaution from "@site-docs/components/enable-bff-caution";
|
11
|
+
|
12
|
+
<EnableBFFCaution />
|
13
13
|
|
14
14
|
默认情况下,BFF 服务只能处理 BFF API 的请求。
|
15
15
|
|
@@ -7,10 +7,9 @@ sidebar_label: prefix
|
|
7
7
|
- **类型:** `string`
|
8
8
|
- **默认值:** `/api`
|
9
9
|
|
10
|
-
|
11
|
-
请先在当前应用项目根目录使用【[new](/apis/app/commands#modern-new)】 启用 BFF 功能。
|
10
|
+
import EnableBFFCaution from "@site-docs/components/enable-bff-caution";
|
12
11
|
|
13
|
-
|
12
|
+
<EnableBFFCaution />
|
14
13
|
|
15
14
|
默认情况下,BFF API 目录下的路由访问前缀是 `/api`, 如下目录结构:
|
16
15
|
|
@@ -7,13 +7,11 @@ sidebar_label: proxy
|
|
7
7
|
- **类型:** `Record<string, string>`
|
8
8
|
- **默认值:** `{}`
|
9
9
|
|
10
|
-
|
11
|
-
请先在当前应用项目根目录使用【[new](/apis/app/commands#modern-new)】 启用 BFF 功能。
|
10
|
+
import EnableBFFCaution from "@site-docs/components/enable-bff-caution";
|
12
11
|
|
13
|
-
|
12
|
+
<EnableBFFCaution />
|
14
13
|
|
15
|
-
|
16
|
-
BFF Proxy 使用了强大的 [http-proxy-middleware](https://github.com/chimurai/http-proxy-middleware),如果需要更多高级的用法, 可以查看它的[文档](https://github.com/chimurai/http-proxy-middleware#options)。
|
14
|
+
通过简单配置,Modern.js 可以将发送给 BFF server 的请求,自动代理到指定的服务上。
|
17
15
|
|
18
16
|
在 `modern.server-runtime.config.js` 中加入以下配置;即可开启代理:
|
19
17
|
|
@@ -28,27 +26,18 @@ export default defineConfig({
|
|
28
26
|
});
|
29
27
|
```
|
30
28
|
|
31
|
-
|
29
|
+
假设 Modern.js BFF server 的地址是 `localhost:8080`,所有以 `api` 开头的请求都会被代理到 `https://cnodejs.org`,如 `localhost:8080/api/v1/topics` 的请求会被代理到 `https://cnodejs.org/api/v1/topics`。
|
32
30
|
|
33
|
-
|
31
|
+
import BFFProxyPathRewrite from "@site-docs/components/bff-proxy-path-rewrite";
|
34
32
|
|
35
|
-
|
36
|
-
import { defineConfig } from '@modern-js/app-tools/server';
|
37
|
-
export default defineConfig({
|
38
|
-
bff: {
|
39
|
-
proxy: {
|
40
|
-
'/api': {
|
41
|
-
target: 'https://cnodejs.org',
|
42
|
-
pathRewrite: { '/api/topics': '/api/v1/topics' },
|
43
|
-
changeOrigin: true,
|
44
|
-
},
|
45
|
-
},
|
46
|
-
},
|
47
|
-
});
|
48
|
-
```
|
33
|
+
<BFFProxyPathRewrite />
|
49
34
|
|
50
35
|
与 [dev.proxy](/configure/app/dev/proxy) 不同,本节所介绍的代理只作用于进入 BFF/API 服务的请求;同时,这一配置不但可以在开发环境中使用,在生产环境中也会代理相应的请求。
|
51
36
|
|
37
|
+
import BFFProxyPrinciple from "@site-docs/components/bff-proxy-principle";
|
38
|
+
|
39
|
+
<BFFProxyPrinciple />
|
40
|
+
|
52
41
|
## 常见用法
|
53
42
|
|
54
43
|
### 解决接口跨域问题
|
@@ -72,3 +61,4 @@ export default defineServerConfig({
|
|
72
61
|
},
|
73
62
|
};
|
74
63
|
```
|
64
|
+
|
@@ -87,7 +87,7 @@ Runtime 插件运行时配置和直接在 `modern.config.ts` 中的配置默认
|
|
87
87
|
:::
|
88
88
|
|
89
89
|
:::warning
|
90
|
-
defineConfig 中只能定义 Runtime 插件的具体配置内容,确认是否开启插件还需要通过 `
|
90
|
+
defineConfig 中只能定义 Runtime 插件的具体配置内容,确认是否开启插件还需要通过 `modern.config.ts` 中的配置决定。
|
91
91
|
|
92
92
|
:::
|
93
93
|
|
@@ -5,11 +5,11 @@ sidebar_label: publicRoutes
|
|
5
5
|
# server.publicRoutes
|
6
6
|
|
7
7
|
- **类型:** `Object`
|
8
|
-
- **默认值:**
|
8
|
+
- **默认值:** 根据文件约定自动生成的服务端路由规则,每个文件生成一条路由规则。
|
9
9
|
|
10
10
|
该配置选项只作用于服务端路由,可以自定义 `config/public/` 下资源的访问路由。
|
11
11
|
|
12
|
-
对象的 `key`
|
12
|
+
对象的 `key` 为 `config/public/` 的相对文件路径(不使用 `./`), 值可以是 `string`。
|
13
13
|
|
14
14
|
```ts title="modern.config.ts"
|
15
15
|
export default defineConfig({
|
@@ -5,9 +5,9 @@ sidebar_label: routes
|
|
5
5
|
# server.routes
|
6
6
|
|
7
7
|
- **类型:** `Object`
|
8
|
-
- **默认值:**
|
8
|
+
- **默认值:** 根据文件约定自动生成的服务端路由规则,每个入口生成一条路由规则,入口名称等于路由路径。
|
9
9
|
|
10
|
-
|
10
|
+
该配置选项只作用于服务端路由,可以自定义应用入口的访问路由。
|
11
11
|
|
12
12
|
## 自定义访问路由
|
13
13
|
|
@@ -7,7 +7,7 @@ sidebar_label: ssrByEntries
|
|
7
7
|
- **类型:** `Object`
|
8
8
|
- **默认值:** `undefined`
|
9
9
|
|
10
|
-
按入口设置
|
10
|
+
按入口设置 SSR 选项,选项内的属性同 [ssr](./ssr.mdx),指定值会和 SSR 属性内容做替换合并操作,例如:
|
11
11
|
|
12
12
|
:::info
|
13
13
|
「 入口名 」默认为目录名,少数情况下通过 `source.entries` 自定义入口时,入口名为 `source.entries` 对象的 `key`。
|
@@ -19,11 +19,11 @@ export default defineConfig({
|
|
19
19
|
server: {
|
20
20
|
ssr: true,
|
21
21
|
ssrByEntries: {
|
22
|
-
// page-a 不启用
|
22
|
+
// page-a 不启用 SSR
|
23
23
|
'page-a': false,
|
24
24
|
},
|
25
25
|
},
|
26
26
|
});
|
27
27
|
```
|
28
28
|
|
29
|
-
如上配置,项目整体启用了
|
29
|
+
如上配置,项目整体启用了 SSR, 但是针对入口 `page-a` 关闭了 SSR 渲染能力。
|
@@ -25,7 +25,7 @@ export default defineConfig({
|
|
25
25
|
|
26
26
|
当值类型为 `Object` 时,可以配置如下属性:
|
27
27
|
|
28
|
-
- `mode`:`string = 'string'`,默认为使用 `renderToString` 渲染。配置为
|
28
|
+
- `mode`:`string = 'string'`,默认为使用 `renderToString` 渲染。配置为 `stream` 开启流式渲染。
|
29
29
|
- `forceCSR`:`boolean = false`,默认关闭强制 CSR 渲染。配置为 `true` 后,在页面访问时添加 `?csr=true` 即可强制 CSR。
|
30
30
|
- `inlineScript`:`boolean = true`,默认情况下,SSR 的数据会以内联脚本的方式注入到 HTML 中,并且直接赋值给全局变量。配置为 `false` 后,会下发 JSON,而不是赋值给全局变量。
|
31
31
|
|
@@ -8,9 +8,12 @@ sidebar_position: 1
|
|
8
8
|
- **类型:** `'babel-jest' | 'ts-jest'`
|
9
9
|
- **默认值:** `babel-jest`
|
10
10
|
|
11
|
-
|
11
|
+
:::caution Caution
|
12
|
+
请先在当前应用项目根目录使用 [new](/apis/app/commands#modern-new) 启用测试功能。
|
13
|
+
:::
|
12
14
|
|
13
|
-
|
14
|
-
`babel-jest` 也可以编译 TS 文件,但不会类型报错,如果你需要跑测试的时候对 TS 类型进行校验,那么请使用 `ts-jest`。
|
15
|
+
配置执行测试时的编译工具,默认使用 `babel-jest`。可配置为 [babel-jest](https://www.npmjs.com/package/babel-jest) 或 [ts-jest](https://github.com/kulshekhar/ts-jest)。
|
15
16
|
|
17
|
+
:::info 补充信息
|
18
|
+
`babel-jest` 可以编译 TS 文件,但没有类型校验。如果你希望运行测试用例的时,能对 TS 类型进行校验,可以使用 `ts-jest`。
|
16
19
|
:::
|
@@ -4,7 +4,7 @@ sidebar_position: 0
|
|
4
4
|
|
5
5
|
# 配置使用
|
6
6
|
|
7
|
-
Modern.js
|
7
|
+
Modern.js 中有两种配置,分别是编译时配置和服务端运行时配置。
|
8
8
|
|
9
9
|
编译时配置可以在两个位置进行配置:
|
10
10
|
|
@@ -12,10 +12,10 @@ Modern.js 中有两种配置,一个是编译时配置,一个是服务端运
|
|
12
12
|
- `package.json` 文件
|
13
13
|
|
14
14
|
:::info
|
15
|
-
Modern.js 不支持同时在 package.json 中和 modern.config.ts
|
15
|
+
Modern.js 不支持同时在 `package.json` 中和 `modern.config.ts` 中配置同一个配置项,推荐在 `modern.config.ts` 中进行配置。如果 Modern.js 检测到重复配置导致的冲突,将会抛出警告。
|
16
16
|
:::
|
17
17
|
|
18
|
-
服务端运行时配置可以在根路径下的 `modern.server-runtime.config.(ts|js|mjs)`
|
18
|
+
服务端运行时配置可以在根路径下的 `modern.server-runtime.config.(ts|js|mjs)` 中进行配置。
|
19
19
|
|
20
20
|
## 在配置文件中配置
|
21
21
|
|
@@ -27,7 +27,7 @@ Modern.js 的配置文件定义在项目的根目录下,支持 `.ts`, `.js`
|
|
27
27
|
|
28
28
|
### modern.config.ts(推荐)
|
29
29
|
|
30
|
-
我们推荐使用
|
30
|
+
我们推荐使用 `.ts` 格式的配置文件,它提供了友好的 TypeScript 类型提示,从而帮助你避免配置中的错误。
|
31
31
|
|
32
32
|
从 `@modern-js/app-tools` 中导入 `defineConfig` 工具函数, 它会帮助你进行配置的类型推导和类型补全:
|
33
33
|
|
@@ -54,7 +54,7 @@ export default defineConfig({
|
|
54
54
|
|
55
55
|
### modern.config.js
|
56
56
|
|
57
|
-
如果你在开发一个非 TypeScript 项目,可以使用
|
57
|
+
如果你在开发一个非 TypeScript 项目,可以使用 `.js` 格式的配置文件:
|
58
58
|
|
59
59
|
```js title="modern.config.js"
|
60
60
|
export default {
|
@@ -66,7 +66,7 @@ export default {
|
|
66
66
|
};
|
67
67
|
```
|
68
68
|
|
69
|
-
你也可以通过 `process.env.NODE_ENV
|
69
|
+
你也可以通过 `process.env.NODE_ENV`,根据环境做不同配置:
|
70
70
|
|
71
71
|
```js title="modern.config.js"
|
72
72
|
export default {
|
@@ -98,7 +98,7 @@ export default defineConfig(({ env, command }) => ({
|
|
98
98
|
- 当运行 `modern dev` 或 `modern start` 时,`env` 的值为 `development`。
|
99
99
|
- 当运行 `modern build` 或 `modern serve` 时,`env` 的值为 `production`。
|
100
100
|
- 当运行 `modern test` 时,`env` 的值为 `test`。
|
101
|
-
- `command
|
101
|
+
- `command`:对应当前运行的命令,如 `dev`、`start`、`build`、`serve`。
|
102
102
|
|
103
103
|
### 导出异步函数
|
104
104
|
|
@@ -122,7 +122,7 @@ export default defineConfig(async ({ env, command }) => {
|
|
122
122
|
|
123
123
|
Modern.js 命令行支持通过 `--config` 选项来指定配置文件的名称。
|
124
124
|
|
125
|
-
例如,你需要在执行 build 命令时使用 `modern.prod.config.ts` 文件,可以在 `package.json` 中添加如下配置:
|
125
|
+
例如,你需要在执行 `build` 命令时使用 `modern.prod.config.ts` 文件,可以在 `package.json` 中添加如下配置:
|
126
126
|
|
127
127
|
```json title="package.json"
|
128
128
|
{
|
@@ -159,7 +159,7 @@ $ modern build -c modern.prod.config.js
|
|
159
159
|
|
160
160
|
### 注意事项
|
161
161
|
|
162
|
-
- 不建议同时使用 `package.json` 和 `modern.config.js` 进行配置。如果同时使用了两者并出现配置冲突,Modern.js
|
162
|
+
- 不建议同时使用 `package.json` 和 `modern.config.js` 进行配置。如果同时使用了两者并出现配置冲突,Modern.js 会在命令行进行提示错误。
|
163
163
|
- `@modern-js/runtime` 导出了同名的 [defineConfig](/apis/app/runtime/app/define-config) API,请注意区分。
|
164
164
|
|
165
165
|
## 本地调试配置
|
@@ -7,8 +7,7 @@ sidebar_position: 6
|
|
7
7
|
代码分割是优化前端资源加载的一种常用手段,本文将介绍 Modern.js 支持的三种代码分割方式:
|
8
8
|
|
9
9
|
:::info
|
10
|
-
使用 Modern.js [约定式路由](/guides/basic-features/routes#约定式路由)
|
11
|
-
|
10
|
+
使用 Modern.js [约定式路由](/guides/basic-features/routes#约定式路由)时,默认会根据路由组件做代码分割,无需自行进行代码分割。
|
12
11
|
:::
|
13
12
|
|
14
13
|
- `import`
|
@@ -17,7 +16,7 @@ sidebar_position: 6
|
|
17
16
|
|
18
17
|
## import
|
19
18
|
|
20
|
-
使用动态 `import()`
|
19
|
+
使用动态 `import()` 语法,传入的 JS 模块将会被被打包到单独的 JS 文件中。例如:
|
21
20
|
|
22
21
|
```ts
|
23
22
|
import('./math').then(math => {
|
@@ -25,15 +24,14 @@ import('./math').then(math => {
|
|
25
24
|
});
|
26
25
|
```
|
27
26
|
|
28
|
-
`./math` 路径对应的 JS 模块会被打包到单独的 JS 文件中。
|
29
|
-
|
30
27
|
## React.lazy
|
31
28
|
|
32
29
|
React 官方提供的组件代码分割的方式。
|
33
30
|
|
34
31
|
:::caution
|
35
|
-
React
|
32
|
+
`React.lazy` 通常配合 `<Suspense>` 使用,因此仅在 CSR 或 React 18 Streaming SSR 中可用。
|
36
33
|
|
34
|
+
对于使用传统 SSR(字符串渲染)的项目,不支持 `React.lazy`,请使用 Loadable API。
|
37
35
|
:::
|
38
36
|
|
39
37
|
```ts
|
@@ -56,11 +54,11 @@ function MyComponent() {
|
|
56
54
|
}
|
57
55
|
```
|
58
56
|
|
59
|
-
`React.lazy` 更多用法请见 [React
|
57
|
+
`React.lazy` 更多用法请见 [React 官网文档](https://zh-hans.react.dev/reference/react/lazy)。
|
60
58
|
|
61
|
-
##
|
59
|
+
## Loadable
|
62
60
|
|
63
|
-
|
61
|
+
在 Modern.js 中,可以从 `@modern-js/runtime/loadable` 中导出使用 Loadable API,示例如下:
|
64
62
|
|
65
63
|
```ts
|
66
64
|
import loadable from '@modern-js/runtime/loadable';
|
@@ -72,9 +70,12 @@ function MyComponent() {
|
|
72
70
|
}
|
73
71
|
```
|
74
72
|
|
75
|
-
`loadable
|
73
|
+
Modern.js 开箱即用的支持在 SSR 中使用 `loadable`,无需再添加 Babel 插件,或是在 SSR 时向 HTML 中注入脚本。
|
76
74
|
|
77
|
-
|
78
|
-
`loadable` 开箱即用的支持 SSR,但不支持和 Suspense 一起使用,如果是 CSR 项目可以使用 [loadable.lazy](https://loadable-components.com/docs/suspense/)
|
75
|
+
但需要注意的是,任何 Loadable API 在 SSR 中都不支持配合 `<Suspense>` 使用。
|
79
76
|
|
77
|
+
:::info
|
78
|
+
如果希望在 React 17 及以下的 CSR 项目中使用 `<Suspense>`,可以用 [loadable.lazy](https://loadable-components.com/docs/suspense/) 代替 `React.lazy`。
|
80
79
|
:::
|
80
|
+
|
81
|
+
更多用法请见 [Loadable API](/apis/app/runtime/utility/loadable)。
|
@@ -74,7 +74,7 @@ Warning: Expected server HTML to contain a matching <div> in <div>.
|
|
74
74
|
这是 React 在客户端执行 hydrate 逻辑时,发现渲染结果与 SSR 渲染结果不一致造成的。虽然页面表现正常,但在复杂应用中,很有可能因此出现 DOM 层级混乱、样式混乱等问题。
|
75
75
|
|
76
76
|
:::info
|
77
|
-
关于 hydrate (注水)逻辑请参考[这里](https://
|
77
|
+
关于 hydrate (注水)逻辑请参考[这里](https://zh-hans.react.dev/reference/react-dom/hydrate)。
|
78
78
|
|
79
79
|
:::
|
80
80
|
|
@@ -447,7 +447,7 @@ export default Page;
|
|
447
447
|
```
|
448
448
|
|
449
449
|
`Await` 需要包裹在 `Suspense` 组件内部,`Await` 的 `resolve` 传入的是 Data Loader 异步获取的数据,当数据获取完成后,
|
450
|
-
通过 [Render Props](https://
|
450
|
+
通过 [Render Props](https://zh-hans.react.dev/reference/react/cloneElement#passing-data-with-a-render-prop) 模式,渲染获取到的数据。在数据的获取阶段,将展示
|
451
451
|
`Suspense` 组件 `fallback` 属性设置的内容。
|
452
452
|
|
453
453
|
:::warning 注意
|
@@ -8,14 +8,14 @@ Modern.js 默认集成了 [Jest](https://jestjs.io/) 的测试能力。
|
|
8
8
|
|
9
9
|
我们首先需要执行 `pnpm run new` 启用「单元测试 / 集成测试」功能:
|
10
10
|
|
11
|
-
```
|
11
|
+
```bash
|
12
12
|
? 请选择你想要的操作 启用可选功能
|
13
13
|
? 请选择功能名称 启用「单元测试 / 集成测试」功能
|
14
14
|
```
|
15
15
|
|
16
|
-
执行上述命令后,`package.json`
|
16
|
+
执行上述命令后,`package.json` 中将会自动添加 `"test": "modern test"` 命令。
|
17
17
|
|
18
|
-
在 `modern.config.ts` 中注册
|
18
|
+
在 `modern.config.ts` 中注册 `@modern-js/plugin-testing` 插件后即可使用测试功能:
|
19
19
|
|
20
20
|
```ts title="modern.config.ts"
|
21
21
|
import testPlugin from '@modern-js/plugin-testing';
|
@@ -36,7 +36,7 @@ Modern.js 默认识别的测试文件路径为:`<rootDir>/src/**/*.test.[jt]s?
|
|
36
36
|
|
37
37
|
Modern.js test 支持使用 [testing-library](https://testing-library.com/docs/) 相关包 API,可直接通过 `@modern-js/runtime/testing` 进行导入:
|
38
38
|
|
39
|
-
```
|
39
|
+
```ts
|
40
40
|
import { render, screen } from '@modern-js/runtime/testing';
|
41
41
|
```
|
42
42
|
|
@@ -0,0 +1,44 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 20
|
3
|
+
---
|
4
|
+
|
5
|
+
# 使用 Storybook
|
6
|
+
|
7
|
+
[Storybook](https://storybook.js.org/) 是一个专门用于组件调试的工具,它围绕着组件开发提供了:
|
8
|
+
|
9
|
+
- 丰富多样的调试能力
|
10
|
+
- 可与一些测试工具结合使用
|
11
|
+
- 可重复使用的文档内容
|
12
|
+
- 可分享能力
|
13
|
+
- 工作流程自动化
|
14
|
+
|
15
|
+
## 开启 Storybook 调试
|
16
|
+
|
17
|
+
EdenX 默认集成了 Storybook 的调试能力。
|
18
|
+
|
19
|
+
当我们想要对组件进行调试的时候,可以通过 `pnpm run new` 启用 Storybook 调试功能。
|
20
|
+
|
21
|
+
```bash
|
22
|
+
$ npx modern new
|
23
|
+
? 请选择你想要的操作 启用可选功能
|
24
|
+
? 请选择功能名称 启用「Visual Testing (Storybook)」模式
|
25
|
+
```
|
26
|
+
|
27
|
+
执行完成后,你只需在 `modern.config.ts` 文件中注册 Modern.js 的 Storybook 插件,即可启用 Storybook 调试能力。
|
28
|
+
|
29
|
+
```ts title="modern.config.ts"
|
30
|
+
import appTools, { defineConfig } from '@modern-js/app-tools';
|
31
|
+
import storybookPlugin from '@modern-js/plugin-storybook';
|
32
|
+
|
33
|
+
export default defineConfig({
|
34
|
+
plugins: [appTools(), storybookPlugin()],
|
35
|
+
});
|
36
|
+
```
|
37
|
+
|
38
|
+
## 调试代码
|
39
|
+
|
40
|
+
Modern.js 默认识别项目源码目录 src/ 下 [\*.stories.(js|jsx|ts|tsx|mdx)](/apis/app/hooks/src/stories.html) 格式的文件作为 Storybook 的调试文件。
|
41
|
+
|
42
|
+
## 配置 Storybook
|
43
|
+
|
44
|
+
在 Modern.js 中,可以在项目的 [`config/storybook`](/apis/app/hooks/config/storybook.html) 目录下增加 Storybook 配置文件。
|