@modern-js/main-doc 2.0.0-beta.3 → 2.0.0-beta.5
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/commands/dev.md +8 -3
- package/en/docusaurus-plugin-content-docs/current/apis/app/commands/inspect.md +31 -10
- package/en/docusaurus-plugin-content-docs/current/apis/app/commands/serve.md +32 -0
- package/en/docusaurus-plugin-content-docs/current/apis/app/hooks/src/server.md +31 -0
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/core/bootstrap.md +4 -3
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/core/create-app.md +2 -3
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/core/use-module-apps.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/components/init-app.md +42 -0
- package/en/docusaurus-plugin-content-docs/current/configure/app/builder-plugins.md +70 -0
- package/en/docusaurus-plugin-content-docs/current/configure/app/dev/with-master-app.md +0 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/plugins.md +11 -5
- package/en/docusaurus-plugin-content-docs/current/configure/app/server/routes.md +2 -4
- package/en/docusaurus-plugin-content-docs/current/configure/app/source/disable-entry-dirs.md +38 -0
- package/en/docusaurus-plugin-content-docs/current/configure/app/source/entries.md +66 -2
- package/en/docusaurus-plugin-content-docs/current/configure/app/tools/esbuild.md +16 -39
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/css/css-in-js.md +38 -0
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/css/css-modules.md +86 -0
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/css/less-sass.md +17 -0
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/css/postcss.md +81 -0
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/css/tailwindcss.md +95 -0
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/data-fetch.md +66 -0
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/routes.md +270 -0
- package/en/docusaurus-plugin-content-docs/current/guides/concept/entries.md +116 -0
- package/en/docusaurus-plugin-content-docs/current/guides/get-started/quick-start.md +162 -0
- package/en/docusaurus-plugin-content-docs/current/guides/get-started/upgrade.md +78 -0
- package/{zh/tutorials/first-app → en/docusaurus-plugin-content-docs/current/guides}/overview.md +4 -4
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/_category_.json +4 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/lifecycle.md +15 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/foundations/introduction.md +1 -1
- package/en/docusaurus-plugin-content-docs/current.json +11 -11
- package/package.json +4 -4
- package/zh/apis/app/commands/dev.md +9 -4
- package/zh/apis/app/commands/inspect.md +32 -11
- package/zh/apis/app/commands/new.md +1 -1
- package/zh/apis/app/commands/{start.md → serve.md} +3 -3
- package/zh/apis/app/hooks/src/index_.md +5 -4
- package/zh/apis/app/hooks/src/server.md +31 -0
- package/zh/apis/app/runtime/core/bootstrap.md +3 -4
- package/zh/apis/app/runtime/core/create-app.md +1 -18
- package/zh/apis/app/runtime/core/use-module-apps.md +64 -33
- package/zh/apis/app/runtime/web-server/hook.md +1 -1
- package/zh/apis/app/runtime/web-server/middleware.md +1 -0
- package/zh/components/debug-app.md +18 -0
- package/zh/components/default-mwa-generate.md +5 -0
- package/zh/components/deploy.md +1 -0
- package/zh/components/enable-micro-frontend.md +13 -0
- package/zh/components/global-proxy.md +28 -0
- package/zh/components/init-app.md +44 -0
- package/zh/components/micro-runtime-config.md +18 -0
- package/zh/components/prerequisites.md +19 -0
- package/zh/components/release-note.md +1 -0
- package/zh/configure/app/builder-plugins.md +72 -0
- package/zh/configure/app/deploy/_category_.json +4 -0
- package/zh/configure/app/deploy/microFrontend.md +64 -0
- package/zh/configure/app/dev/with-master-app.md +0 -2
- package/zh/configure/app/plugins.md +10 -4
- package/zh/configure/app/runtime/master-app.md +33 -36
- package/zh/configure/app/server/routes.md +2 -4
- package/zh/configure/app/source/disable-entry-dirs.md +37 -0
- package/zh/configure/app/source/entries-dir.md +0 -3
- package/zh/configure/app/source/entries.md +66 -3
- package/zh/configure/app/tools/esbuild.md +16 -39
- package/zh/guides/advanced-features/bff/bff-proxy.md +1 -1
- package/zh/guides/advanced-features/compatibility.md +14 -39
- package/zh/guides/advanced-features/eslint.md +21 -21
- package/zh/guides/advanced-features/ssg.md +20 -9
- package/zh/guides/advanced-features/ssr.md +95 -52
- package/zh/guides/advanced-features/testing.md +44 -1
- package/zh/guides/advanced-features/web-server.md +14 -3
- package/zh/guides/basic-features/css/tailwindcss.md +13 -6
- package/zh/guides/basic-features/data-fetch.md +398 -5
- package/zh/guides/basic-features/html.md +182 -0
- package/zh/guides/basic-features/mock.md +3 -9
- package/zh/guides/basic-features/proxy.md +2 -27
- package/zh/guides/basic-features/routes.md +35 -3
- package/zh/guides/concept/entries.md +108 -19
- package/zh/guides/get-started/quick-start.md +14 -83
- package/zh/guides/get-started/upgrade.md +11 -9
- package/zh/guides/{concept → topic-detail/framework-plugin}/lifecycle.md +0 -0
- package/zh/guides/topic-detail/micro-frontend/c01-introduction.md +29 -0
- package/zh/guides/topic-detail/micro-frontend/c02-development.md +191 -0
- package/zh/guides/topic-detail/micro-frontend/c03-main-app.md +246 -0
- package/zh/guides/topic-detail/micro-frontend/c04-communicate.md +54 -0
- package/zh/guides/topic-detail/micro-frontend/{mixed-stack.md → c05-mixed-stack.md} +3 -3
- package/zh/guides/topic-detail/model/quick-start.md +1 -1
- package/zh/guides/topic-detail/model/test-model.md +2 -2
- package/zh/guides/topic-detail/monorepo/create-sub-project.md +2 -2
- package/zh/guides/topic-detail/monorepo/intro.md +1 -1
- package/zh/guides/troubleshooting/dependencies.md +0 -69
- package/zh/tutorials/first-app/_category_.json +1 -1
- package/zh/tutorials/first-app/c01-start.md +99 -0
- package/zh/tutorials/first-app/{c05-component/5.1-use-ui-library.md → c02-component.md} +13 -15
- package/zh/tutorials/first-app/c03-css.md +324 -0
- package/zh/tutorials/first-app/{c08-client-side-routing/8.1-code-based-routing.md → c04-routes.md} +52 -39
- package/zh/tutorials/first-app/c05-loader.md +82 -0
- package/zh/tutorials/first-app/c06-model.md +256 -0
- package/zh/tutorials/first-app/c07-container.md +283 -0
- package/zh/tutorials/first-app/c08-entries.md +137 -0
- package/zh/tutorials/foundations/introduction.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/commands/start.md +0 -32
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/enable-modern-mode.md +0 -34
- package/zh/apis/generator/overview.md +0 -32
- package/zh/configure/app/output/enable-modern-mode.md +0 -34
- package/zh/guides/advanced-features/custom-app.md +0 -72
- package/zh/guides/topic-detail/micro-frontend/communicate.md +0 -39
- package/zh/guides/topic-detail/micro-frontend/debugging.md +0 -168
- package/zh/guides/topic-detail/micro-frontend/introduction.md +0 -13
- package/zh/guides/topic-detail/micro-frontend/route-mode.md +0 -110
- package/zh/guides/topic-detail/monorepo/deploy.md +0 -43
- package/zh/tutorials/first-app/c01-getting-started/1.1-prerequisites.md +0 -25
- package/zh/tutorials/first-app/c01-getting-started/1.2-minimal-mwa.md +0 -118
- package/zh/tutorials/first-app/c01-getting-started/1.3-dev-command.md +0 -29
- package/zh/tutorials/first-app/c01-getting-started/1.4-enable-ssr.md +0 -47
- package/zh/tutorials/first-app/c01-getting-started/1.5-start-command.md +0 -18
- package/zh/tutorials/first-app/c01-getting-started/1.6-create-repo.md +0 -31
- package/zh/tutorials/first-app/c01-getting-started/_category_.json +0 -3
- package/zh/tutorials/first-app/c02-generator-and-studio/2.1-generator.md +0 -79
- package/zh/tutorials/first-app/c02-generator-and-studio/2.2-boilerplates.md +0 -34
- package/zh/tutorials/first-app/c02-generator-and-studio/2.3-configuration.md +0 -19
- package/zh/tutorials/first-app/c02-generator-and-studio/_category_.json +0 -3
- package/zh/tutorials/first-app/c03-ide/3.1-setting-up.md +0 -55
- package/zh/tutorials/first-app/c03-ide/3.2-hints-in-ide.md +0 -60
- package/zh/tutorials/first-app/c03-ide/3.3-autofix-in-ide.md +0 -11
- package/zh/tutorials/first-app/c03-ide/3.4-autofix-in-cli.md +0 -63
- package/zh/tutorials/first-app/c03-ide/_category_.json +0 -3
- package/zh/tutorials/first-app/c04-es6-plus-and-ts/4.1-use-es6-plus.md +0 -54
- package/zh/tutorials/first-app/c04-es6-plus-and-ts/4.2-use-typescript.md +0 -135
- package/zh/tutorials/first-app/c04-es6-plus-and-ts/4.3-compatibility.md +0 -67
- package/zh/tutorials/first-app/c04-es6-plus-and-ts/_category_.json +0 -3
- package/zh/tutorials/first-app/c05-component/5.2-use-standalone-component.md +0 -72
- package/zh/tutorials/first-app/c05-component/_category_.json +0 -3
- package/zh/tutorials/first-app/c06-css-and-component/6.1-css-in-js.md +0 -110
- package/zh/tutorials/first-app/c06-css-and-component/6.2-utility-class.md +0 -143
- package/zh/tutorials/first-app/c06-css-and-component/6.3-postcss.md +0 -84
- package/zh/tutorials/first-app/c06-css-and-component/6.4-design-system.md +0 -83
- package/zh/tutorials/first-app/c06-css-and-component/6.5-storybook.md +0 -77
- package/zh/tutorials/first-app/c06-css-and-component/6.6-testing.md +0 -104
- package/zh/tutorials/first-app/c06-css-and-component/_category_.json +0 -3
- package/zh/tutorials/first-app/c07-app-entry/7.1-intro.md +0 -69
- package/zh/tutorials/first-app/c07-app-entry/7.2-add-entry-in-cli.md +0 -100
- package/zh/tutorials/first-app/c07-app-entry/7.3-manage-entries-by-hand.md +0 -69
- package/zh/tutorials/first-app/c07-app-entry/_category_.json +0 -3
- package/zh/tutorials/first-app/c08-client-side-routing/_category_.json +0 -3
- package/zh/tutorials/first-app/c09-bff/9.1-serverless.md +0 -30
- package/zh/tutorials/first-app/c09-bff/9.2-enable-bff.md +0 -95
- package/zh/tutorials/first-app/c09-bff/9.3-fetch-bff.md +0 -131
- package/zh/tutorials/first-app/c09-bff/_category_.json +0 -3
- package/zh/tutorials/first-app/c10-model/10.1-application-architecture.md +0 -21
- package/zh/tutorials/first-app/c10-model/10.2-add-model.md +0 -185
- package/zh/tutorials/first-app/c10-model/10.3-use-model.md +0 -55
- package/zh/tutorials/first-app/c10-model/10.4-testing.md +0 -69
- package/zh/tutorials/first-app/c10-model/_category_.json +0 -3
- package/zh/tutorials/first-app/c11-container/11.1-use-model-with-app-state.md +0 -240
- package/zh/tutorials/first-app/c11-container/11.2-add-container.md +0 -109
- package/zh/tutorials/first-app/c11-container/11.3-use-loader.md +0 -63
- package/zh/tutorials/first-app/c11-container/11.4-testing.md +0 -56
- package/zh/tutorials/first-app/c11-container/_category_.json +0 -3
@@ -7,7 +7,7 @@ sidebar_position: 3
|
|
7
7
|
|
8
8
|
启用 SSR 非常简单,只需要设置 [`server.ssr`](/docs/configure/app/server/ssr) 为 `true` 即可:
|
9
9
|
|
10
|
-
```json
|
10
|
+
```json
|
11
11
|
{
|
12
12
|
"server": {
|
13
13
|
"ssr": true
|
@@ -17,12 +17,23 @@ sidebar_position: 3
|
|
17
17
|
|
18
18
|
## SSR 时的数据获取
|
19
19
|
|
20
|
-
Modern.js 中提供了
|
20
|
+
Modern.js 中提供了 Data Loader,方便开发者在 SSR、CSR 下同构的获取数据。每个路由模块,如 `layout.tsx` 和 `page.tsx` 都可以定义自己的 Data Loader:
|
21
21
|
|
22
|
-
```ts
|
23
|
-
const
|
24
|
-
return
|
25
|
-
|
22
|
+
```ts title="src/routes/page.tsx"
|
23
|
+
export const loader = () => {
|
24
|
+
return {
|
25
|
+
message: 'Hello World',
|
26
|
+
};
|
27
|
+
};
|
28
|
+
```
|
29
|
+
|
30
|
+
在组件中可以通过 Hooks API 的方式获取 `loader` 函数返回的数据:
|
31
|
+
|
32
|
+
```tsx
|
33
|
+
export default () => {
|
34
|
+
const data = useLoaderData();
|
35
|
+
return <div>{data.message}</div>;
|
36
|
+
}
|
26
37
|
```
|
27
38
|
|
28
39
|
Modern.js 打破传统的 SSR 开发模式,提供了用户无感的 SSR 开发体验。并且提供了优雅的降级处理,一旦 SSR 请求失败,会自动降级在浏览器端重新发起请求。
|
@@ -30,7 +41,7 @@ Modern.js 打破传统的 SSR 开发模式,提供了用户无感的 SSR 开发
|
|
30
41
|
不过,开发者仍然需要关注数据的兜底处理,例如 `null` 值或不符合预期的数据返回。避免在 SSR 时产生 React 渲染错误或是返回凌乱的渲染结果。
|
31
42
|
|
32
43
|
:::info 补充信息
|
33
|
-
|
44
|
+
使用 Data Loader 时,数据获取发生在渲染前,Modern.js 也仍然支持在组件渲染时获取数据。更多相关内容可以查看[数据获取](/docs/guides/basic-features/data-fetch)。
|
34
45
|
:::
|
35
46
|
|
36
47
|
## 保持渲染一致
|
@@ -55,10 +66,10 @@ Modern.js 打破传统的 SSR 开发模式,提供了用户无感的 SSR 开发
|
|
55
66
|
Warning: Expected server HTML to contain a matching <div> in <div>.
|
56
67
|
```
|
57
68
|
|
58
|
-
这是 React
|
69
|
+
这是 React 在客户端执行 hydrate 逻辑时,发现渲染结果与 SSR 渲染结果不一致造成的。虽然页面表现正常,但在复杂应用中,很有可能因此出现 DOM 层级混乱、样式混乱等问题。
|
59
70
|
|
60
71
|
:::info 注
|
61
|
-
|
72
|
+
关于 hydrate (注水)逻辑请参考[这里](https://reactjs.org/docs/react-dom.html#hydrate)。
|
62
73
|
:::
|
63
74
|
|
64
75
|
应用需要保持 SSR 与 CSR 渲染结果的一致性,如果存在不一致的情况,说明这部分内容无需在 SSR 中进行渲染。Modern.js 为这类在 SSR 中不需要渲染的内容提供 [`<NoSSR>` 工具组件](/docs/apis/app/runtime/core/use-runtime-context):
|
@@ -129,29 +140,25 @@ SPR 利用预渲染与缓存技术,为 SSR 页面提供静态 Web 的响应性
|
|
129
140
|
|
130
141
|
在 Modern.js 中使用 SPR 非常简单,只需要在组件中新增 `PreRender` 组件,该组件所在的页面就会自动开启 SPR。
|
131
142
|
|
132
|
-
这里模拟一个使用 `
|
143
|
+
这里模拟一个使用 `useLoaderData` API 的组件,Data Loader 中的请求需要消耗 2s 时间。
|
133
144
|
|
134
145
|
```jsx
|
135
|
-
import {
|
146
|
+
import { useLoaderData } from '@modern-js/runtime/router';
|
147
|
+
|
148
|
+
export const loader = async () => {
|
149
|
+
await new Promise((resolve, reject) => {
|
150
|
+
setTimeout(() => {
|
151
|
+
resolve(null);
|
152
|
+
}, 2000);
|
153
|
+
});
|
154
|
+
|
155
|
+
return {
|
156
|
+
message: 'Hello Modern.js',
|
157
|
+
};
|
158
|
+
};
|
136
159
|
|
137
160
|
export default () => {
|
138
|
-
const
|
139
|
-
async () => {
|
140
|
-
await new Promise((resolve, reject) => {
|
141
|
-
setTimeout(() => {
|
142
|
-
resolve(null);
|
143
|
-
}, 2000);
|
144
|
-
});
|
145
|
-
|
146
|
-
return {
|
147
|
-
message: 'Hello Modern.js',
|
148
|
-
};
|
149
|
-
},
|
150
|
-
{
|
151
|
-
params: 'foo',
|
152
|
-
},
|
153
|
-
);
|
154
|
-
|
161
|
+
const data = useLoaderData();
|
155
162
|
return <div>{data?.message}</div>;
|
156
163
|
};
|
157
164
|
```
|
@@ -170,7 +177,7 @@ import { PreRender } from '@modern-js/runtime/ssr';
|
|
170
177
|
<PreRender interval={5} />
|
171
178
|
```
|
172
179
|
|
173
|
-
修改后,执行 `pnpm run build && pnpm run
|
180
|
+
修改后,执行 `pnpm run build && pnpm run serve` 启动应用,并打开页面。
|
174
181
|
|
175
182
|
首次打开时,和之前的渲染并没有什么不同,同样存在 2s 延迟。点击刷新,页面瞬间打开,但此时,页面数据并没有因为刷新发生变化,这是因为缓存还没有过期。
|
176
183
|
|
@@ -187,17 +194,39 @@ import { PreRender } from '@modern-js/runtime/ssr';
|
|
187
194
|
|
188
195
|
开启 SSR 时,Modern.js 会用相同的入口,构建出 SSR Bundle 和 CSR Bundle 两份产物。因此,在 SSR Bundle 中存在 Web API,或是在 CSR Bundle 中存在 Node API 时,都可能导致运行出错。
|
189
196
|
|
197
|
+
在组件中引入 Web API,通常情况下是要做一些全局监听,或是获取浏览器相关的数据,例如:
|
198
|
+
|
199
|
+
```tsx
|
200
|
+
document.addEventListener('load', () => {
|
201
|
+
console.log('document load');
|
202
|
+
});
|
203
|
+
const App = () => {
|
204
|
+
return <div>Hello World</div>
|
205
|
+
}
|
206
|
+
export default App;
|
207
|
+
```
|
208
|
+
|
209
|
+
在组件文件中引入 Node API,通常情况下是因为使用了 Data Loader,例如:
|
210
|
+
|
211
|
+
```ts
|
212
|
+
import fse from 'fs-extra';
|
213
|
+
export const loader = () => {
|
214
|
+
const file = fse.readFileSync('./myfile');
|
215
|
+
return {
|
216
|
+
...
|
217
|
+
};
|
218
|
+
};
|
219
|
+
```
|
220
|
+
|
190
221
|
### 环境变量区分
|
191
222
|
|
192
|
-
|
223
|
+
对于第一种情况,我们可以直接使用 Modern.js 内置的环境变量 `MODERN_TARGET` 进行判断,在构建时删除无用代码:
|
193
224
|
|
194
225
|
```ts
|
195
|
-
|
196
|
-
|
197
|
-
console.log('
|
198
|
-
}
|
199
|
-
console.log('client render');
|
200
|
-
}
|
226
|
+
if (process.env.MODERN_TARGET === 'browser') {
|
227
|
+
document.addEventListener('load', () => {
|
228
|
+
console.log('document load');
|
229
|
+
});
|
201
230
|
}
|
202
231
|
```
|
203
232
|
|
@@ -207,38 +236,34 @@ export default () => {
|
|
207
236
|
|
208
237
|
### 文件后缀区分
|
209
238
|
|
210
|
-
|
239
|
+
但例如第二种情况,Treeshaking 的方式并不能保证代码被完全分离。Modern.js 也支持通过 `.node.` 后缀的文件来区分 SSR Bundle 和 CSR Bundle 产物的打包文件。
|
211
240
|
|
212
|
-
|
213
|
-
|
214
|
-
```ts
|
215
|
-
// client-sdk
|
216
|
-
export const href = location.href;
|
217
|
-
```
|
218
|
-
|
219
|
-
这时候直接引用到组件中,会造成 SSR 报错。可以创建同名的 `.ts` 和 `.node.ts` 文件做一层代理:
|
241
|
+
例如在代码中引入了 `fs-extra`,这时候直接引用到组件中,会造成 CSR 加载报错。可以创建同名的 `.ts` 和 `.node.ts` 文件做一层代理:
|
220
242
|
|
221
243
|
```ts title="compat.ts"
|
222
|
-
export {
|
244
|
+
export { readFileSync } from 'fs-extra';
|
223
245
|
```
|
224
246
|
|
225
247
|
```ts title="compat.node.ts"
|
226
|
-
export const
|
248
|
+
export const readFileSync: any = () => {};
|
227
249
|
```
|
228
250
|
|
229
251
|
在文件中直接引入 `./compat`,此时 SSR 环境下会优先使用 `.node.ts` 后缀的文件,CSR 环境下会使用 `.ts` 后缀的文件。
|
230
252
|
|
231
253
|
```ts title="App.tsx"
|
232
|
-
import {
|
254
|
+
import { readFileSync } from './compat'
|
233
255
|
|
234
|
-
export
|
235
|
-
|
236
|
-
|
256
|
+
export const loader = () => {
|
257
|
+
const file = readFileSync('./myfile');
|
258
|
+
return {
|
259
|
+
...
|
260
|
+
};
|
261
|
+
};
|
237
262
|
```
|
238
263
|
|
239
264
|
### 独立文件
|
240
265
|
|
241
|
-
上述两种方式,都会为开发者带来一些心智负担。Modern.js
|
266
|
+
上述两种方式,都会为开发者带来一些心智负担。Modern.js 基于[嵌套路由](/docs/guides/basic-features/routes)开发设计了[更简单的方案](/docs/guides/basic-features/data-fetch)来分离 CSR 和 SSR 的代码。
|
242
267
|
|
243
268
|
## 接口请求
|
244
269
|
|
@@ -247,3 +272,21 @@ export default () => {
|
|
247
272
|
需要注意的是,此时获取到的是 HTML 请求的请求头,不一定适用于接口请求,因此**千万不能**透传所有请求头。并且,一些后端接口,或是通用网关,会根据请求头中的信息做校验,全量透传容易出现各种难以排查的问题,推荐**按需透传**。
|
248
273
|
|
249
274
|
如果实在需要透传所有请求头,请务必过滤 `host` 字段。
|
275
|
+
|
276
|
+
## 流式渲染
|
277
|
+
|
278
|
+
Modern.js 支持了 React 18 的流式渲染,可以通过如下配置修改默认的渲染模式:
|
279
|
+
|
280
|
+
```json
|
281
|
+
{
|
282
|
+
"server": {
|
283
|
+
"ssr": {
|
284
|
+
"mode": "stream"
|
285
|
+
}
|
286
|
+
}
|
287
|
+
}
|
288
|
+
```
|
289
|
+
|
290
|
+
:::note
|
291
|
+
目前 Modern.js 内置的数据获取方式还未支持流式渲染,如迫切需要开发者可以按照 React Stream SSR 的 Demo 自建。
|
292
|
+
:::
|
@@ -1,4 +1,47 @@
|
|
1
1
|
---
|
2
|
-
title: 测试
|
3
2
|
sidebar_position: 10
|
4
3
|
---
|
4
|
+
|
5
|
+
# 测试
|
6
|
+
|
7
|
+
Modern.js 默认继承了 [Jest](https://jestjs.io/) 的测试能力。
|
8
|
+
|
9
|
+
我们首先需要执行 `pnpm run new` 启用「单元测试 / 集成测试」功能:
|
10
|
+
|
11
|
+
```
|
12
|
+
? 请选择你想要的操作: 启用可选功能
|
13
|
+
? 启用可选功能: 启用「单元测试 / 集成测试」功能
|
14
|
+
```
|
15
|
+
|
16
|
+
执行上述命令后,`package.json` 中将会自动生成 `"test": "modern test"` 命令。
|
17
|
+
|
18
|
+
在 `modern.config.ts` 中注册 Test 插件:
|
19
|
+
|
20
|
+
```ts title="modern.config.ts"
|
21
|
+
import TestPlugin from '@modern-js/plugin-testing';
|
22
|
+
// https://modernjs.dev/docs/apis/app/config
|
23
|
+
export default defineConfig({
|
24
|
+
...,
|
25
|
+
plugins: [..., TestPlugin()],
|
26
|
+
});
|
27
|
+
```
|
28
|
+
|
29
|
+
## 测试文件
|
30
|
+
|
31
|
+
Modern.js 默认识别的测试文件路径为: `<rootDir>/src/**/*.test.[jt]s?(x)` 和 `<rootDir>/tests/**/*.test.[jt]s?(x)`。
|
32
|
+
|
33
|
+
如果你需要自定义 test 目录,可通过 [tools.jest](/docs/configure/app/tools/jest) 进行配置。
|
34
|
+
|
35
|
+
## 使用姿势
|
36
|
+
|
37
|
+
Modern.js test 支持使用 [testing-library](https://testing-library.com/docs/) 相关包 API,可直接通过 `@modern-js/runtime/testing` 进行导入:
|
38
|
+
|
39
|
+
```
|
40
|
+
import { render, screen } from '@modern-js/runtime/testing';
|
41
|
+
```
|
42
|
+
|
43
|
+
其他 Modern.js 支持的 testing API 可参考[这里](/docs/apis/app/runtime/testing/cleanup)。
|
44
|
+
|
45
|
+
## transform
|
46
|
+
|
47
|
+
Modern.js 测试默认使用 [babel-jest](https://www.npmjs.com/package/babel-jest) 进行源码编译,如果你需要使用 [ts-jest](https://github.com/kulshekhar/ts-jest),可以通过 [testing.transform](/docs/configure/app/testing/transformer) 进行配置。
|
@@ -12,11 +12,22 @@ Modern.js 作为以客户端为中心的开发框架,对服务端的定制能
|
|
12
12
|
在项目根目录执行 `pnpm run new` 命令,按照如下选择,开启「自定义 Web Serve」功能:
|
13
13
|
|
14
14
|
```bash
|
15
|
-
?
|
16
|
-
?
|
15
|
+
? 请选择你想要的操作 创建工程元素
|
16
|
+
? 创建工程元素 新建「自定义 Web Server」源码目录
|
17
17
|
```
|
18
18
|
|
19
|
-
|
19
|
+
执行命令后,在 `modern.config.ts` 中注册 Server 插件:
|
20
|
+
|
21
|
+
```ts title="modern.config.ts"
|
22
|
+
import ServerPlugin from '@modern-js/plugin-server';
|
23
|
+
// https://modernjs.dev/docs/apis/app/config
|
24
|
+
export default defineConfig({
|
25
|
+
...,
|
26
|
+
plugins: [..., ServerPlugin()],
|
27
|
+
});
|
28
|
+
```
|
29
|
+
|
30
|
+
项目目录下会新建 `server/index.ts` 文件,自定义逻辑在这个文件中编写。
|
20
31
|
|
21
32
|
## 使用 API 扩展 Web Server
|
22
33
|
|
@@ -9,8 +9,19 @@ sidebar_position: 2
|
|
9
9
|
按照如下进行选择:
|
10
10
|
|
11
11
|
```bash
|
12
|
-
?
|
13
|
-
?
|
12
|
+
? 请选择你想要的操作 启用可选功能
|
13
|
+
? 启用可选功能 启用 Tailwind CSS 支持
|
14
|
+
```
|
15
|
+
|
16
|
+
在 `modern.config.ts` 中注册 Tailwind 插件:
|
17
|
+
|
18
|
+
```ts title="modern.config.ts"
|
19
|
+
import TailwindCSSPlugin from '@modern-js/plugin-tailwindcss';
|
20
|
+
// https://modernjs.dev/docs/apis/app/config
|
21
|
+
export default defineConfig({
|
22
|
+
...,
|
23
|
+
plugins: [..., TailwindCSSPlugin()],
|
24
|
+
});
|
14
25
|
```
|
15
26
|
|
16
27
|
使用时在入口的根组件(如 `src/App.jsx`)添加如下代码:
|
@@ -68,11 +79,7 @@ export default defineConfig({
|
|
68
79
|
在开启「Tailwind CSS 支持」的功能后,首先需要安装 [`Twin`](https://github.com/ben-rogerson/twin.macro) 依赖:
|
69
80
|
|
70
81
|
``` bash
|
71
|
-
# package manager is `pnpm`
|
72
82
|
pnpm add twin.macro -D
|
73
|
-
|
74
|
-
# package manager is `yarn`
|
75
|
-
yarn add twin.macro -D
|
76
83
|
```
|
77
84
|
|
78
85
|
当项目安装 `twin.macro` 依赖后,Modern.js 会检测到该依赖并对内置的 `babel-plugin-macro` 增加 `twin.macro` 相关的配置。因此在安装完依赖后,无需手动配置。下面是一个简单使用 `twin.macro` 的示例:
|