@modern-js/main-doc 2.26.0 → 2.28.0
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +16 -0
- package/docs/en/configure/app/experiments/source-build.mdx +13 -0
- package/docs/en/configure/app/source/alias-strategy.mdx +13 -0
- package/docs/en/configure/app/tools/swc.mdx +17 -2
- package/docs/en/guides/advanced-features/rspack-start.mdx +5 -1
- package/docs/en/guides/advanced-features/ssr.mdx +6 -2
- package/docs/en/guides/basic-features/routes.mdx +35 -1
- package/docs/en/guides/topic-detail/framework-plugin/hook-list.mdx +202 -168
- package/docs/en/tutorials/first-app/c03-css.mdx +3 -2
- package/docs/zh/configure/app/experiments/source-build.mdx +13 -0
- package/docs/zh/configure/app/source/alias-strategy.mdx +13 -0
- package/docs/zh/configure/app/tools/swc.mdx +17 -2
- package/docs/zh/guides/advanced-features/rspack-start.mdx +5 -1
- package/docs/zh/guides/advanced-features/ssr.mdx +2 -2
- package/docs/zh/guides/basic-features/routes.mdx +42 -4
- package/docs/zh/guides/topic-detail/framework-plugin/hook-list.mdx +158 -124
- package/package.json +5 -5
@@ -1,9 +1,10 @@
|
|
1
1
|
---
|
2
2
|
title: Add Style
|
3
3
|
---
|
4
|
+
|
4
5
|
# Add Style
|
5
6
|
|
6
|
-
import { Tabs, Tab as TabItem } from
|
7
|
+
import { Tabs, Tab as TabItem } from '@theme';
|
7
8
|
|
8
9
|
In the previous chapter, we learned how to use components from the third-party library.
|
9
10
|
|
@@ -15,7 +16,7 @@ First of all, we want to control the display of contact avatars by ourselves, an
|
|
15
16
|
|
16
17
|

|
17
18
|
|
18
|
-
Hypothesis has no ready-made components to implement, so you need to write some CSS yourself. Here we use [styled-components]
|
19
|
+
Hypothesis has no ready-made components to implement, so you need to write some CSS yourself. Here we use [styled-components](https://styled-components.com/) to implement similar requirements. Modern.js out of the box supports styled-components, which requires neither dependency nor configuration.
|
19
20
|
|
20
21
|
Style-components avoids many problems of traditional CSS writing through modularization. For example, writing styles directly on the style attribute of elements, the visual details of UI will also be mixed with the details of UI structure and business logic. Or classname needs to avoid global space renaming, which requires the use of naming conventions.
|
21
22
|
|
@@ -0,0 +1,13 @@
|
|
1
|
+
---
|
2
|
+
sidebar_label: sourceBuild
|
3
|
+
---
|
4
|
+
|
5
|
+
# experiments.sourceBuild
|
6
|
+
|
7
|
+
:::tip
|
8
|
+
该配置由 Modern.js Builder 提供,更多信息可参考 [experiments.sourceBuild](https://modernjs.dev/builder/api/config-experiments.html#experimentssourcebuild)。
|
9
|
+
:::
|
10
|
+
|
11
|
+
import Main from '@modern-js/builder-doc/docs/zh/config/experiments/sourceBuild.md';
|
12
|
+
|
13
|
+
<Main />
|
@@ -0,0 +1,13 @@
|
|
1
|
+
---
|
2
|
+
sidebar_label: aliasStrategy
|
3
|
+
---
|
4
|
+
|
5
|
+
# source.aliasStrategy
|
6
|
+
|
7
|
+
:::tip
|
8
|
+
该配置由 Modern.js Builder 提供,更多信息可参考 [source.aliasStrategy](https://modernjs.dev/builder/api/config-source.html#sourcealiasstrategy)。
|
9
|
+
:::
|
10
|
+
|
11
|
+
import Main from '@modern-js/builder-doc/docs/zh/config/source/aliasStrategy.md';
|
12
|
+
|
13
|
+
<Main />
|
@@ -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
|
## 介绍
|
@@ -21,7 +21,7 @@ import SWC from '@modern-js/builder-doc/docs/zh/shared/swc.md';
|
|
21
21
|
|
22
22
|
## 安装
|
23
23
|
|
24
|
-
import EnableSWC from '@modern-js/builder-doc/docs/zh/shared/
|
24
|
+
import EnableSWC from '@modern-js/builder-doc/docs/zh/shared/enableSwc.md';
|
25
25
|
|
26
26
|
<EnableSWC />
|
27
27
|
|
@@ -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)。
|
@@ -5,7 +5,7 @@ sidebar_position: 1
|
|
5
5
|
|
6
6
|
# 使用 Rspack
|
7
7
|
|
8
|
-
import Rspack from '@modern-js/builder-doc/docs/zh/shared/
|
8
|
+
import Rspack from '@modern-js/builder-doc/docs/zh/shared/rspackTip.mdx';
|
9
9
|
|
10
10
|
<Rspack />
|
11
11
|
|
@@ -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
|
|