@modern-js/main-doc 2.27.0 → 2.29.0
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +12 -0
- package/docs/en/configure/app/performance/dns-prefetch.mdx +13 -0
- package/docs/en/configure/app/performance/preconnect.mdx +13 -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/en/guides/troubleshooting/dependencies.mdx +13 -0
- package/docs/zh/configure/app/performance/dns-prefetch.mdx +13 -0
- package/docs/zh/configure/app/performance/preconnect.mdx +13 -0
- 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/docs/zh/guides/troubleshooting/dependencies.mdx +13 -0
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
@@ -0,0 +1,13 @@
|
|
1
|
+
---
|
2
|
+
sidebar_label: dnsPrefetch
|
3
|
+
---
|
4
|
+
|
5
|
+
# performance.dnsPrefetch
|
6
|
+
|
7
|
+
:::tip
|
8
|
+
This config is provided by Modern.js Builder, more detail can see [performance.dnsPrefetch](https://modernjs.dev/builder/en/api/config-performance.html#performancednsprefetch).
|
9
|
+
:::
|
10
|
+
|
11
|
+
import Main from '@modern-js/builder-doc/docs/en/config/performance/dnsPrefetch.md';
|
12
|
+
|
13
|
+
<Main />
|
@@ -0,0 +1,13 @@
|
|
1
|
+
---
|
2
|
+
sidebar_label: preconnect
|
3
|
+
---
|
4
|
+
|
5
|
+
# performance.preconnect
|
6
|
+
|
7
|
+
:::tip
|
8
|
+
This config is provided by Modern.js Builder, more detail can see [performance.preconnect](https://modernjs.dev/builder/en/api/config-performance.html#performancepreconnect).
|
9
|
+
:::
|
10
|
+
|
11
|
+
import Main from '@modern-js/builder-doc/docs/en/config/performance/preconnect.md';
|
12
|
+
|
13
|
+
<Main />
|
@@ -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';
|
@@ -108,3 +108,16 @@ In most cases, peer dependencies warnings will not affect the project operation
|
|
108
108
|
- If you are still using React 16, you will not be able to use Modern.js's runtime or server-side capabilities. You can consider using the build mode of Modern.js, that is, only using Modern.js's build capabilities. In this case, React 16 can still be used.
|
109
109
|
|
110
110
|
In future major versions of Modern.js, we will gradually remove support for React 16 and React 17. Therefore, please upgrade to React 18 or higher as soon as possible.
|
111
|
+
|
112
|
+
---
|
113
|
+
|
114
|
+
### Type error in Modern.js configuration file?
|
115
|
+
|
116
|
+
```bash
|
117
|
+
Type 'CliPlugin<{}, {}, {}, {}>' is not assignable to type 'CliPlugin<any, {}, {}, {}>'.
|
118
|
+
Types of property 'setup' are incompatible.
|
119
|
+
```
|
120
|
+
|
121
|
+
When you use the Modern.js framework, the above error occurs in the configuration file, it may be due to the inconsistent versions of Modern.js related packages。You can upgrade and unify the version of modern.js related packages through `npx modern upgrade` command.
|
122
|
+
|
123
|
+
In the monorepo, the above error may also occur due to inconsistent versions of the Modern.js framework used by different sub-projects.
|
@@ -0,0 +1,13 @@
|
|
1
|
+
---
|
2
|
+
sidebar_label: dnsPrefetch
|
3
|
+
---
|
4
|
+
|
5
|
+
# performance.dnsPrefetch
|
6
|
+
|
7
|
+
:::tip
|
8
|
+
该配置由 Modern.js Builder 提供,更多信息可参考 [performance.dnsPrefetch](https://modernjs.dev/builder/api/config-performance.html#performancednsprefetch)。
|
9
|
+
:::
|
10
|
+
|
11
|
+
import Main from '@modern-js/builder-doc/docs/zh/config/performance/dnsPrefetch.md';
|
12
|
+
|
13
|
+
<Main />
|
@@ -0,0 +1,13 @@
|
|
1
|
+
---
|
2
|
+
sidebar_label: preconnect
|
3
|
+
---
|
4
|
+
|
5
|
+
# performance.preconnect
|
6
|
+
|
7
|
+
:::tip
|
8
|
+
该配置由 Modern.js Builder 提供,更多信息可参考 [performance.preconnect](https://modernjs.dev/builder/api/config-performance.html#performancepreconnect)。
|
9
|
+
:::
|
10
|
+
|
11
|
+
import Main from '@modern-js/builder-doc/docs/zh/config/performance/preconnect.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
|
## 介绍
|
@@ -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
|
|
@@ -108,3 +108,16 @@ Type '{}' is not assignable to type 'ReactNode'.
|
|
108
108
|
- 如果你仍然在使用 React 16,那么将无法使用 Modern.js 的运行时或服务端能力。你可以考虑使用 Modern.js 的构建模式,即只使用 Modern.js 的构建能力,这种情况可以继续使用 React 16。
|
109
109
|
|
110
110
|
在 Modern.js 未来的 major 版本中,我们会逐步移除对 React 16 和 React 17 的支持。因此,请尽快升级到 React 18 以上版本。
|
111
|
+
|
112
|
+
---
|
113
|
+
|
114
|
+
### Modern.js 配置出现类型错误?
|
115
|
+
|
116
|
+
```bash
|
117
|
+
Type 'CliPlugin<{}, {}, {}, {}>' is not assignable to type 'CliPlugin<any, {}, {}, {}>'.
|
118
|
+
Types of property 'setup' are incompatible.
|
119
|
+
```
|
120
|
+
|
121
|
+
当你在使用 Modern.js 框架时配置文件出现以上报错,可能是由于 Modern.js 相关包的版本号未统一导致。可以通过 `npx modern upgrade` 命令对 modern.js 依赖进行升级和版本统一。
|
122
|
+
|
123
|
+
在 monorepo 中由于不同子项目所用的 Modern.js 框架版本不一致也可能出现以上问题。
|
package/package.json
CHANGED
@@ -15,14 +15,14 @@
|
|
15
15
|
"modern",
|
16
16
|
"modern.js"
|
17
17
|
],
|
18
|
-
"version": "2.
|
18
|
+
"version": "2.29.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.29.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-tools": "2.
|
39
|
-
"@modern-js/doc-plugin-auto-sidebar": "2.
|
37
|
+
"@modern-js/builder-doc": "2.29.0",
|
38
|
+
"@modern-js/doc-tools": "2.29.0",
|
39
|
+
"@modern-js/doc-plugin-auto-sidebar": "2.29.0"
|
40
40
|
},
|
41
41
|
"scripts": {
|
42
42
|
"dev": "modern dev",
|