@modern-js/main-doc 0.0.0-next-1678773288418 → 0.0.0-next-1678802796441
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/CHANGELOG.md +3 -3
- package/en/guides/advanced-features/rspack-start.mdx +14 -20
- package/en/guides/advanced-features/ssr.mdx +18 -15
- package/package.json +3 -3
- package/zh/guides/advanced-features/rspack-start.mdx +16 -21
- package/zh/guides/advanced-features/ssr.mdx +19 -15
- package/zh/guides/basic-features/mock.mdx +1 -1
- package/zh/index.md +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
# @modern-js/main-doc
|
|
2
2
|
|
|
3
|
-
## 0.0.0-next-
|
|
3
|
+
## 0.0.0-next-1678802796441
|
|
4
4
|
|
|
5
5
|
### Patch Changes
|
|
6
6
|
|
|
7
|
-
-
|
|
7
|
+
- 7035d5c22: fix: doc info block not work
|
|
8
8
|
|
|
9
9
|
fix: 修复文档站 info 不生效问题
|
|
10
10
|
|
|
11
|
-
- @modern-js/builder-doc@0.0.0-next-
|
|
11
|
+
- @modern-js/builder-doc@0.0.0-next-1678802796441
|
|
12
12
|
|
|
13
13
|
## 2.8.0
|
|
14
14
|
|
|
@@ -1,35 +1,32 @@
|
|
|
1
1
|
---
|
|
2
|
-
title: Rspack Start
|
|
3
2
|
sidebar_position: 1
|
|
4
3
|
---
|
|
5
4
|
|
|
6
|
-
# Rspack
|
|
5
|
+
# Using Rspack
|
|
7
6
|
|
|
8
|
-
|
|
7
|
+
import Rspack from '@modern-js/builder-doc/docs/en/shared/rspack.md';
|
|
9
8
|
|
|
10
|
-
Rspack
|
|
9
|
+
<Rspack />
|
|
11
10
|
|
|
12
|
-
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
**Modern.js provides out-of-the-box Rspack support**, so you can switch between the stable Webpack and the faster Rspack.
|
|
12
|
+
|
|
13
|
+
This document will show you how to enable Rspack build mode in Modern.js.
|
|
15
14
|
|
|
16
15
|
## Initializing an Rspack project
|
|
17
16
|
|
|
18
17
|
The Modern.js generator provides an interactive question-and-answer interface to initialize a project. To create an Rspack project, simply select the **Rspack** build tool by running:
|
|
19
18
|
|
|
20
|
-
import InitRspackApp from
|
|
19
|
+
import InitRspackApp from '@site-docs-en/components/init-rspack-app';
|
|
21
20
|
|
|
22
21
|
<InitRspackApp />
|
|
23
22
|
|
|
24
23
|
After the project is created, you can experience the project by running `pnpm run dev`. For more project information, please refer to [Quick Start](/guides/get-started/quick-start.html).
|
|
25
24
|
|
|
26
25
|
:::tip
|
|
27
|
-
When using Rspack as the bundler, the following
|
|
26
|
+
When using Rspack as the bundler, the following Features are temporarily unavailable as some of the capabilities are still under development and we will provide support in the future.
|
|
28
27
|
|
|
29
|
-
-
|
|
30
|
-
-
|
|
31
|
-
- Micro Frontend.
|
|
32
|
-
- Storybook debugging.
|
|
28
|
+
- Micro Frontend
|
|
29
|
+
- Storybook devtool
|
|
33
30
|
|
|
34
31
|
:::
|
|
35
32
|
|
|
@@ -45,21 +42,18 @@ $ pnpm run new
|
|
|
45
42
|
|
|
46
43
|
After executing the command, enable the Rspack build in `modern.config.ts`:
|
|
47
44
|
|
|
48
|
-
```
|
|
45
|
+
```diff title=modern.config.ts
|
|
49
46
|
import appTools, { defineConfig } from '@modern-js/app-tools';
|
|
50
47
|
|
|
51
|
-
export default defineConfig<'rspack'>({
|
|
52
|
-
runtime: {
|
|
53
|
-
router: true,
|
|
54
|
-
},
|
|
48
|
+
+ export default defineConfig<'rspack'>({
|
|
55
49
|
plugins: [
|
|
56
50
|
appTools({
|
|
57
|
-
|
|
51
|
+
+ bundler: 'experimental-rspack',
|
|
58
52
|
}),
|
|
59
53
|
],
|
|
60
54
|
});
|
|
61
55
|
```
|
|
62
56
|
|
|
63
57
|
:::tip
|
|
64
|
-
|
|
58
|
+
When migrating from webpack to Rspack, there may have some differences in build and configuration capabilities. For more details, please refer to [Configuration Differences](https://modernjs.dev/builder/en/guide/advanced/rspack-start.html#migrating-from-webpack-to-rspack).
|
|
65
59
|
:::
|
|
@@ -8,12 +8,14 @@ In Modern.js, SSR also works out of the box. Developers do not need to write com
|
|
|
8
8
|
|
|
9
9
|
Enabling SSR is very easy, just set ['server.ssr'](/configure/app/server/ssr) to `true`:
|
|
10
10
|
|
|
11
|
-
```
|
|
12
|
-
{
|
|
11
|
+
```ts title="modern.config.ts"
|
|
12
|
+
import { defineConfig } from '@modern-js/app-tools';
|
|
13
|
+
|
|
14
|
+
export default defineConfig({
|
|
13
15
|
"server": {
|
|
14
|
-
"ssr": true
|
|
15
|
-
}
|
|
16
|
-
}
|
|
16
|
+
"ssr": true,
|
|
17
|
+
},
|
|
18
|
+
})
|
|
17
19
|
```
|
|
18
20
|
|
|
19
21
|
## SSR Data Fetch
|
|
@@ -293,14 +295,16 @@ Be sure to filter the `host` field if you really need to pass through all reques
|
|
|
293
295
|
|
|
294
296
|
Modern.js supports streaming rendering in React 18. Opt in it with the following configuration:
|
|
295
297
|
|
|
296
|
-
```
|
|
297
|
-
{
|
|
298
|
+
```ts title="modern.config.ts"
|
|
299
|
+
import { defineConfig } from '@modern-js/app-tools';
|
|
300
|
+
|
|
301
|
+
export default defineConfig({
|
|
298
302
|
"server": {
|
|
299
303
|
"ssr": {
|
|
300
|
-
"mode": "stream"
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
+
"mode": "stream",
|
|
305
|
+
},
|
|
306
|
+
},
|
|
307
|
+
})
|
|
304
308
|
```
|
|
305
309
|
|
|
306
310
|
The streaming SSR of Modern.js is implemented based on React Router, and the main APIs involved are:
|
|
@@ -383,7 +387,7 @@ export default ({ params }: LoaderFunctionArgs) => {
|
|
|
383
387
|
|
|
384
388
|
Use the `Await` component to render the data returned asynchronously from the Data Loader. For example:
|
|
385
389
|
|
|
386
|
-
```
|
|
390
|
+
```tsx title='page.tsx'
|
|
387
391
|
import { Await, useLoaderData } from '@modern-js/runtime/router';
|
|
388
392
|
import { Suspense } from 'react';
|
|
389
393
|
import type { Data } from './page.loader';
|
|
@@ -425,8 +429,7 @@ So, here we import like this: `import type { Data } from './page.loader'`;
|
|
|
425
429
|
|
|
426
430
|
You can also get the asynchronous data returned by Data Loader through `useAsyncValue`. For example:
|
|
427
431
|
|
|
428
|
-
```
|
|
429
|
-
```ts title='page.tsx'
|
|
432
|
+
```tsx title='page.tsx'
|
|
430
433
|
import { useAsyncValue } from '@modern-js/runtime/router';
|
|
431
434
|
|
|
432
435
|
// skip some codes
|
|
@@ -480,7 +483,7 @@ export default () => {
|
|
|
480
483
|
|
|
481
484
|
Then use `useAsyncError` to get the error, and assign the component used to render the error to the `errorElement` property of the `Await` component:
|
|
482
485
|
|
|
483
|
-
```
|
|
486
|
+
```tsx title='page.ts'
|
|
484
487
|
import { Await, useAsyncError, useLoaderData } from '@modern-js/runtime/router';
|
|
485
488
|
import { Suspense } from 'react';
|
|
486
489
|
|
package/package.json
CHANGED
|
@@ -11,13 +11,13 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "0.0.0-next-
|
|
14
|
+
"version": "0.0.0-next-1678802796441",
|
|
15
15
|
"publishConfig": {
|
|
16
16
|
"registry": "https://registry.npmjs.org/",
|
|
17
17
|
"access": "public"
|
|
18
18
|
},
|
|
19
19
|
"peerDependencies": {
|
|
20
|
-
"@modern-js/builder-doc": "0.0.0-next-
|
|
20
|
+
"@modern-js/builder-doc": "0.0.0-next-1678802796441"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"ts-node": "^10",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"fs-extra": "^10",
|
|
26
26
|
"@types/node": "^16",
|
|
27
27
|
"@types/fs-extra": "^9",
|
|
28
|
-
"@modern-js/builder-doc": "0.0.0-next-
|
|
28
|
+
"@modern-js/builder-doc": "0.0.0-next-1678802796441"
|
|
29
29
|
},
|
|
30
30
|
"scripts": {
|
|
31
31
|
"build": "npx ts-node ./scripts/sync.ts"
|
|
@@ -5,29 +5,27 @@ sidebar_position: 1
|
|
|
5
5
|
|
|
6
6
|
# 使用 Rspack
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
import Rspack from '@modern-js/builder-doc/docs/zh/shared/rspack.md';
|
|
9
9
|
|
|
10
|
-
Rspack
|
|
10
|
+
<Rspack />
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
**Modern.js 提供开箱即用的 Rspack 支持**,你可以在成熟的 Webpack 和更快的 Rspack 之间进行切换。
|
|
13
|
+
|
|
14
|
+
这篇文档会向你介绍如何在 Modern.js 中开启 Rspack 构建模式。
|
|
15
15
|
|
|
16
16
|
## 初始化 Rspack 项目
|
|
17
17
|
|
|
18
18
|
Modern.js 生成器会提供一个可交互的问答界面,只需将构建工具选择为 **Rspack**,即可创建一个 Rspack 项目:
|
|
19
19
|
|
|
20
|
-
import InitRspackApp from
|
|
20
|
+
import InitRspackApp from '@site-docs/components/init-rspack-app';
|
|
21
21
|
|
|
22
22
|
<InitRspackApp />
|
|
23
23
|
|
|
24
|
-
项目创建完成后,在项目中执行 `pnpm run dev`
|
|
24
|
+
项目创建完成后,在项目中执行 `pnpm run dev` 即可体验项目,更多信息可参考[快速上手](/guides/get-started/quick-start.html)。
|
|
25
25
|
|
|
26
26
|
:::tip
|
|
27
|
-
|
|
27
|
+
在使用 Rspack 作为打包工具时,由于部分能力尚在开发中,以下 features 暂时无法使用,我们将在未来提供支持:
|
|
28
28
|
|
|
29
|
-
- 服务端渲染(SSR)
|
|
30
|
-
- 静态站点生成(SSG)
|
|
31
29
|
- 微前端(Micro Frontend)
|
|
32
30
|
- Storybook 调试
|
|
33
31
|
|
|
@@ -35,31 +33,28 @@ import InitRspackApp from "@site-docs/components/init-rspack-app"
|
|
|
35
33
|
|
|
36
34
|
## 从 webpack 迁移至 Rspack
|
|
37
35
|
|
|
38
|
-
|
|
36
|
+
在一个已有的 Modern.js 项目中,你可以通过执行 `pnpm run new` 来启用 Rspack 构建:
|
|
39
37
|
|
|
40
38
|
```bash
|
|
41
39
|
$ pnpm run new
|
|
42
|
-
?
|
|
43
|
-
?
|
|
40
|
+
? 请选择你想要的操作:启用可选功能
|
|
41
|
+
? 启用可选功能:启用「Rspack 构建」
|
|
44
42
|
```
|
|
45
43
|
|
|
46
|
-
|
|
44
|
+
执行以上命令后,在 `modern.config.ts` 中添加 Rspack 相关配置即可:
|
|
47
45
|
|
|
48
|
-
```
|
|
46
|
+
```diff title=modern.config.ts
|
|
49
47
|
import appTools, { defineConfig } from '@modern-js/app-tools';
|
|
50
48
|
|
|
51
|
-
export default defineConfig<'rspack'>({
|
|
52
|
-
runtime: {
|
|
53
|
-
router: true,
|
|
54
|
-
},
|
|
49
|
+
+ export default defineConfig<'rspack'>({
|
|
55
50
|
plugins: [
|
|
56
51
|
appTools({
|
|
57
|
-
|
|
52
|
+
+ bundler: 'experimental-rspack',
|
|
58
53
|
}),
|
|
59
54
|
],
|
|
60
55
|
});
|
|
61
56
|
```
|
|
62
57
|
|
|
63
58
|
:::tip
|
|
64
|
-
从 webpack 迁移至 Rspack
|
|
59
|
+
从 webpack 迁移至 Rspack 时,存在一些构建能力和配置上的差异,详情可参考:[配置差异](https://modernjs.dev/builder/guide/advanced/rspack-start.html#从-webpack-迁移到-rspack)
|
|
65
60
|
:::
|
|
@@ -8,12 +8,14 @@ sidebar_position: 3
|
|
|
8
8
|
|
|
9
9
|
启用 SSR 非常简单,只需要设置 [`server.ssr`](/configure/app/server/ssr) 为 `true` 即可:
|
|
10
10
|
|
|
11
|
-
```
|
|
12
|
-
{
|
|
11
|
+
```ts title="modern.config.ts"
|
|
12
|
+
import { defineConfig } from '@modern-js/app-tools';
|
|
13
|
+
|
|
14
|
+
export default defineConfig({
|
|
13
15
|
"server": {
|
|
14
|
-
"ssr": true
|
|
15
|
-
}
|
|
16
|
-
}
|
|
16
|
+
"ssr": true,
|
|
17
|
+
},
|
|
18
|
+
})
|
|
17
19
|
```
|
|
18
20
|
|
|
19
21
|
## SSR 时的数据获取
|
|
@@ -282,14 +284,16 @@ export const loader = () => {
|
|
|
282
284
|
|
|
283
285
|
Modern.js 支持了 React 18 的流式渲染,可以通过如下配置启用:
|
|
284
286
|
|
|
285
|
-
```
|
|
286
|
-
{
|
|
287
|
+
```ts title="modern.config.ts"
|
|
288
|
+
import { defineConfig } from '@modern-js/app-tools';
|
|
289
|
+
|
|
290
|
+
export default defineConfig({
|
|
287
291
|
"server": {
|
|
288
292
|
"ssr": {
|
|
289
|
-
"mode": "stream"
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
+
"mode": "stream",
|
|
294
|
+
},
|
|
295
|
+
},
|
|
296
|
+
})
|
|
293
297
|
```
|
|
294
298
|
|
|
295
299
|
Modern.js 的流式渲染基于 React Router 实现,主要涉及 API 有:
|
|
@@ -372,7 +376,7 @@ export default ({ params }: LoaderFunctionArgs) => {
|
|
|
372
376
|
|
|
373
377
|
通过 `Await` 组件,可以获取到 Data Loader 中异步返回的数据,然后进行渲染。例如:
|
|
374
378
|
|
|
375
|
-
```
|
|
379
|
+
```tsx title='page.tsx'
|
|
376
380
|
import { Await, useLoaderData } from '@modern-js/runtime/router';
|
|
377
381
|
import { Suspense } from 'react';
|
|
378
382
|
import type { Data } from './page.loader';
|
|
@@ -413,8 +417,8 @@ export default Page;
|
|
|
413
417
|
|
|
414
418
|
也可以通过 `useAsyncValue` 获取 Data Loader 返回的异步数据。例如:
|
|
415
419
|
|
|
416
|
-
|
|
417
|
-
```
|
|
420
|
+
|
|
421
|
+
```tsx title='page.tsx'
|
|
418
422
|
import { useAsyncValue } from '@modern-js/runtime/router';
|
|
419
423
|
|
|
420
424
|
// 省略部分代码
|
|
@@ -468,7 +472,7 @@ export default () => {
|
|
|
468
472
|
|
|
469
473
|
然后通过 `useAsyncError` 获取错误,并将用于渲染错误信息的组件赋值给 `Await` 组件的 `errorElement` 属性:
|
|
470
474
|
|
|
471
|
-
```
|
|
475
|
+
```tsx title='page.ts'
|
|
472
476
|
import { Await, useAsyncError, useLoaderData } from '@modern-js/runtime/router';
|
|
473
477
|
import { Suspense } from 'react';
|
|
474
478
|
|
|
@@ -25,7 +25,7 @@ Modern.js 提供了快速生成 Mock 数据的功能,能够让前端独立自
|
|
|
25
25
|
`config/mock/index.ts` 文件只需要导出一个包含所有 Mock API 的对象,对象的属性由请求配置 `method` 和 `url` 组成,对应的属性值可以为 `Object`、`Array`、`Function`:
|
|
26
26
|
|
|
27
27
|
```js
|
|
28
|
-
|
|
28
|
+
export default {
|
|
29
29
|
/* 属性为具体的 method 和 请求 url,值为 object 或 array 作为请求的结果 */
|
|
30
30
|
'GET /api/getInfo': { data: [1, 2, 3, 4] },
|
|
31
31
|
|
package/zh/index.md
CHANGED