@modern-js/main-doc 2.27.0 → 2.28.0
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +6 -0
- package/docs/en/configure/app/tools/swc.mdx +16 -1
- package/docs/en/guides/advanced-features/rspack-start.mdx +4 -0
- package/docs/en/guides/advanced-features/ssr.mdx +6 -2
- package/docs/en/guides/basic-features/routes.mdx +35 -1
- package/docs/zh/configure/app/tools/swc.mdx +16 -1
- package/docs/zh/guides/advanced-features/rspack-start.mdx +4 -0
- package/docs/zh/guides/advanced-features/ssr.mdx +2 -2
- package/docs/zh/guides/basic-features/routes.mdx +42 -4
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
@@ -4,7 +4,7 @@ sidebar_label: swc
|
|
4
4
|
|
5
5
|
# tools.swc
|
6
6
|
|
7
|
-
- **Type:** `Object`
|
7
|
+
- **Type:** `Object | Function`
|
8
8
|
- **Default:** `undefined`
|
9
9
|
|
10
10
|
## Introduction
|
@@ -44,4 +44,19 @@ export default defineConfig({
|
|
44
44
|
});
|
45
45
|
```
|
46
46
|
|
47
|
+
Or using function to have more customize config, or to modify the default config.
|
48
|
+
|
49
|
+
```js title="modern.config.ts"
|
50
|
+
import { defineConfig } from '@modern-js/app-tools';
|
51
|
+
|
52
|
+
export default defineConfig({
|
53
|
+
tools: {
|
54
|
+
swc(config, { setConfig }) {
|
55
|
+
// Used for transpiling decorator of mobx correctly
|
56
|
+
setConfig(config, 'jsc.transform.useDefineForClassFields', true);
|
57
|
+
},
|
58
|
+
},
|
59
|
+
});
|
60
|
+
```
|
61
|
+
|
47
62
|
For config details, please refer to [Modern.js Builder - SWC Plugin Configuration](https://modernjs.dev/builder/en/plugins/plugin-swc.html#config).
|
@@ -54,6 +54,10 @@ import { appTools, defineConfig } from '@modern-js/app-tools';
|
|
54
54
|
});
|
55
55
|
```
|
56
56
|
|
57
|
+
import RspackPrecautions from '@modern-js/builder-doc/docs/en/shared/rspackPrecautions.md';
|
58
|
+
|
59
|
+
<RspackPrecautions />
|
60
|
+
|
57
61
|
## Migrating configuration
|
58
62
|
|
59
63
|
After enabling Rspack building capability, further configuration migration is needed by referring to the [Configuration Differences](https://modernjs.dev/builder/en/guide/advanced/rspack-start.html#migrating-from-webpack-to-rspack).
|
@@ -159,10 +159,10 @@ Using SPR in Modern.js is very simple. Just add the PreRender component to your
|
|
159
159
|
|
160
160
|
Here is a simulated component that uses the useLoaderData API. The request in the Data Loader takes 2 seconds to consume.
|
161
161
|
|
162
|
-
```
|
162
|
+
```tsx title="page.loader.ts"
|
163
163
|
import { useLoaderData } from '@modern-js/runtime/router';
|
164
164
|
|
165
|
-
export
|
165
|
+
export default async () => {
|
166
166
|
await new Promise((resolve, reject) => {
|
167
167
|
setTimeout(() => {
|
168
168
|
resolve(null);
|
@@ -173,6 +173,10 @@ export const loader = async () => {
|
|
173
173
|
message: 'Hello Modern.js',
|
174
174
|
};
|
175
175
|
};
|
176
|
+
```
|
177
|
+
|
178
|
+
```tsx title="page.tsx"
|
179
|
+
import { useLoaderData } from '@modern-js/runtime/router';
|
176
180
|
|
177
181
|
export default () => {
|
178
182
|
const data = useLoaderData();
|
@@ -147,6 +147,29 @@ In summary, if there is a `layout.tsx` file under the sub-route's file directory
|
|
147
147
|
|
148
148
|
All routes should end with the `<Page>` component. If the developer introduces the `<Outlet>` component in the `page.tsx` file, there will be no effect.
|
149
149
|
|
150
|
+
#### Config
|
151
|
+
|
152
|
+
Each `Layout` or `Page` file can define its own `config` file, such as `page.config.ts`. In this file, we have an conventinal on a named export called `handle`, which you can define any properties:
|
153
|
+
|
154
|
+
```ts title="routes/blog/page.config.ts"
|
155
|
+
export const handle = {
|
156
|
+
breadcrumbName: 'profile'
|
157
|
+
}
|
158
|
+
```
|
159
|
+
|
160
|
+
These properties as defined are available via the [`useMatches`](https://reactrouter.com/en/main/hooks/use-matches) hook:
|
161
|
+
|
162
|
+
```ts title="routes/layout.ts"
|
163
|
+
export default () => {
|
164
|
+
const matches = useMatches;
|
165
|
+
const breadcrumbs = matches.map(matchedRoute => matchedRoute?.handle?.breadcrumbName);
|
166
|
+
return (
|
167
|
+
<Breadcrumb names={breadcrumbs}>
|
168
|
+
</Breadcrumb>
|
169
|
+
)
|
170
|
+
}
|
171
|
+
```
|
172
|
+
|
150
173
|
### Dynamic Routing
|
151
174
|
|
152
175
|
Routes generated from file directories named with `[]` will be handled as dynamic routes. For example, the following file directory:
|
@@ -204,7 +227,18 @@ For example, the following directory structure:
|
|
204
227
|
└── page.tsx
|
205
228
|
```
|
206
229
|
|
207
|
-
When accessing any path that does not match, the `routes/$.tsx` component will be rendered
|
230
|
+
When accessing any path that does not match(For example `/blog/a`), the `routes/$.tsx` component will be rendered, because there is `layout.tsx` here, the rendered UI is as follows.
|
231
|
+
```tsx
|
232
|
+
<RootLayout>
|
233
|
+
<BlogLayout>
|
234
|
+
<$></$>
|
235
|
+
</BlogLayout>
|
236
|
+
</RootLayout>
|
237
|
+
```
|
238
|
+
|
239
|
+
If you want access to `/blog` to also match the `blog/$.tsx` file, you need to delete the `blog/layout.tsx` file in the same directory and make sure that there are no other subroutes under `blog`.
|
240
|
+
|
241
|
+
As same, you can use [useParams](/apis/app/runtime/router/router#useparams) in `$.tsx` to capture the remaining parts of the URL.
|
208
242
|
|
209
243
|
```ts title="$.tsx"
|
210
244
|
import { useParams } from '@modern-js/runtime/router';
|
@@ -4,7 +4,7 @@ sidebar_label: swc
|
|
4
4
|
|
5
5
|
# tools.swc
|
6
6
|
|
7
|
-
- **类型:** `Object`
|
7
|
+
- **类型:** `Object | Function`
|
8
8
|
- **默认值:** `undefined`
|
9
9
|
|
10
10
|
## 介绍
|
@@ -44,4 +44,19 @@ export default defineConfig({
|
|
44
44
|
});
|
45
45
|
```
|
46
46
|
|
47
|
+
当然也可以使用函数进行更灵活的配置,或者修改某些默认配置。
|
48
|
+
|
49
|
+
```js title="modern.config.ts"
|
50
|
+
import { defineConfig } from '@modern-js/app-tools';
|
51
|
+
|
52
|
+
export default defineConfig({
|
53
|
+
tools: {
|
54
|
+
swc(config, { setConfig }) {
|
55
|
+
// 用于 mobx 中 decorator 编译
|
56
|
+
setConfig(config, 'jsc.transform.useDefineForClassFields', true);
|
57
|
+
},
|
58
|
+
},
|
59
|
+
});
|
60
|
+
```
|
61
|
+
|
47
62
|
完整配置项请参考 [Modern.js Builder - SWC 插件配置](https://modernjs.dev/builder/plugins/plugin-swc.html#%E9%85%8D%E7%BD%AE)。
|
@@ -55,6 +55,10 @@ import { appTools, defineConfig } from '@modern-js/app-tools';
|
|
55
55
|
});
|
56
56
|
```
|
57
57
|
|
58
|
+
import RspackPrecautions from '@modern-js/builder-doc/docs/zh/shared/rspackPrecautions.md';
|
59
|
+
|
60
|
+
<RspackPrecautions />
|
61
|
+
|
58
62
|
## 配置迁移
|
59
63
|
|
60
64
|
开启 Rspack 构建能力后,还需要参考 [配置差异](https://modernjs.dev/builder/guide/advanced/rspack-start.html#从-webpack-迁移到-rspack) 进行进一步的配置迁移。
|
@@ -149,8 +149,6 @@ SPR 利用预渲染与缓存技术,为 SSR 页面提供静态 Web 的响应性
|
|
149
149
|
这里模拟一个使用 `useLoaderData` API 的组件,Data Loader 中的请求需要消耗 2s 时间。
|
150
150
|
|
151
151
|
```tsx title="page.loader.ts"
|
152
|
-
import { useLoaderData } from '@modern-js/runtime/router';
|
153
|
-
|
154
152
|
export default async () => {
|
155
153
|
await new Promise((resolve, reject) => {
|
156
154
|
setTimeout(() => {
|
@@ -165,6 +163,8 @@ export default async () => {
|
|
165
163
|
```
|
166
164
|
|
167
165
|
```tsx title="page.tsx"
|
166
|
+
import { useLoaderData } from '@modern-js/runtime/router';
|
167
|
+
|
168
168
|
export default () => {
|
169
169
|
const data = useLoaderData();
|
170
170
|
return <div>{data?.message}</div>;
|
@@ -150,6 +150,31 @@ export default () => {
|
|
150
150
|
|
151
151
|
所有的路由,理论上都应该由 `<Page>` 组件结束。在 `page.tsx` 文件内,如果开发者引入 `<Outlet>` 组件,不会有任何效果。
|
152
152
|
|
153
|
+
#### Config
|
154
|
+
|
155
|
+
每个 `Layout` 或 `Page` 文件都可以定义一个自己的 `config` 文件,如 `page.config.ts`,该文件中我们约定了一个具名导出 `handle`,
|
156
|
+
这个字段中你可以定义任意属性:
|
157
|
+
|
158
|
+
```ts title="routes/page.config.ts"
|
159
|
+
export const handle = {
|
160
|
+
breadcrumbName: 'profile'
|
161
|
+
}
|
162
|
+
```
|
163
|
+
|
164
|
+
定义的这些属性可以通过 [`useMatches`](https://reactrouter.com/en/main/hooks/use-matches) hook 获取:
|
165
|
+
|
166
|
+
```ts title="routes/layout.ts"
|
167
|
+
export default () => {
|
168
|
+
const matches = useMatches;
|
169
|
+
const breadcrumbs = matches.map(matchedRoute => matchedRoute?.handle?.breadcrumbName);
|
170
|
+
return (
|
171
|
+
<Breadcrumb names={breadcrumbs}>
|
172
|
+
</Breadcrumb>
|
173
|
+
)
|
174
|
+
}
|
175
|
+
```
|
176
|
+
|
177
|
+
|
153
178
|
### 动态路由
|
154
179
|
|
155
180
|
通过 `[]` 命名的文件目录,生成的路由会作为动态路由。例如以下文件目录:
|
@@ -201,13 +226,25 @@ export default () => {
|
|
201
226
|
|
202
227
|
```bash
|
203
228
|
└── routes
|
204
|
-
├── $.tsx
|
205
229
|
├── blog
|
206
|
-
│
|
230
|
+
│ ├── $.tsx
|
231
|
+
│ └── layout.tsx
|
232
|
+
└── layout.tsx
|
207
233
|
└── page.tsx
|
208
234
|
```
|
209
235
|
|
210
|
-
|
236
|
+
当访问任何匹配不到的路径时(如 `/blog/a`),都会渲染 `routes/$.tsx` 组件,因为这里有 `layout.tsx`,渲染的 UI 如下:
|
237
|
+
```tsx
|
238
|
+
<RootLayout>
|
239
|
+
<BlogLayout>
|
240
|
+
<$></$>
|
241
|
+
</BlogLayout>
|
242
|
+
</RootLayout>
|
243
|
+
```
|
244
|
+
|
245
|
+
如果希望访问 `/blog` 时,也匹配到 `blog/$.tsx` 文件,需要删除同目录下的 `blog/layout.tsx` 文件,同时保证 `blog` 下面没有其他子路由。
|
246
|
+
|
247
|
+
同样,`$.tsx` 中可以使用 [useParams](/apis/app/runtime/router/router#useparams) 捕获 url 的剩余部分。
|
211
248
|
|
212
249
|
```ts title="$.tsx"
|
213
250
|
import { useParams } from '@modern-js/runtime/router';
|
@@ -216,7 +253,8 @@ const params = useParams();
|
|
216
253
|
params['*']; // => 'aaa/bbb'
|
217
254
|
```
|
218
255
|
|
219
|
-
`$.tsx` 可以加入到 `routes` 目录下的任意目录中,一个常见的使用示例是添加 `routes/$.tsx` 文件去定制任意层级的 404
|
256
|
+
`$.tsx` 可以加入到 `routes` 目录下的任意目录中,一个常见的使用示例是添加 `routes/$.tsx` 文件去定制任意层级的 404 内容。
|
257
|
+
|
220
258
|
|
221
259
|
### 无路径布局
|
222
260
|
|
package/package.json
CHANGED
@@ -15,14 +15,14 @@
|
|
15
15
|
"modern",
|
16
16
|
"modern.js"
|
17
17
|
],
|
18
|
-
"version": "2.
|
18
|
+
"version": "2.28.0",
|
19
19
|
"publishConfig": {
|
20
20
|
"registry": "https://registry.npmjs.org/",
|
21
21
|
"access": "public",
|
22
22
|
"provenance": true
|
23
23
|
},
|
24
24
|
"peerDependencies": {
|
25
|
-
"@modern-js/builder-doc": "^2.
|
25
|
+
"@modern-js/builder-doc": "^2.28.0"
|
26
26
|
},
|
27
27
|
"devDependencies": {
|
28
28
|
"classnames": "^2",
|
@@ -34,9 +34,9 @@
|
|
34
34
|
"fs-extra": "^10",
|
35
35
|
"@types/node": "^16",
|
36
36
|
"@types/fs-extra": "^9",
|
37
|
-
"@modern-js/builder-doc": "2.
|
38
|
-
"@modern-js/doc-
|
39
|
-
"@modern-js/doc-
|
37
|
+
"@modern-js/builder-doc": "2.28.0",
|
38
|
+
"@modern-js/doc-plugin-auto-sidebar": "2.28.0",
|
39
|
+
"@modern-js/doc-tools": "2.28.0"
|
40
40
|
},
|
41
41
|
"scripts": {
|
42
42
|
"dev": "modern dev",
|