@modern-js/main-doc 3.6.0 → 3.8.0
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/docs/en/apis/app/hooks/config/mock.mdx +4 -1
- package/docs/en/configure/app/dev/mock-dir.mdx +35 -0
- package/docs/en/configure/app/source/react-compiler.mdx +72 -0
- package/docs/en/configure/app/tools/ts-checker.mdx +7 -23
- package/docs/en/guides/advanced-features/page-performance/react-compiler.mdx +42 -27
- package/docs/en/guides/basic-features/debug/mock.mdx +11 -0
- package/docs/en/guides/get-started/ai-coding-agents.mdx +19 -4
- package/docs/en/guides/get-started/upgrade.mdx +2 -2
- package/docs/zh/apis/app/hooks/config/mock.mdx +3 -1
- package/docs/zh/configure/app/dev/mock-dir.mdx +33 -0
- package/docs/zh/configure/app/source/react-compiler.mdx +72 -0
- package/docs/zh/configure/app/tools/ts-checker.mdx +7 -23
- package/docs/zh/guides/advanced-features/page-performance/react-compiler.mdx +42 -27
- package/docs/zh/guides/basic-features/debug/mock.mdx +10 -0
- package/docs/zh/guides/get-started/ai-coding-agents.mdx +19 -4
- package/docs/zh/guides/get-started/upgrade.mdx +2 -2
- package/package.json +17 -2
|
@@ -4,4 +4,7 @@ sidebar_position: 5
|
|
|
4
4
|
---
|
|
5
5
|
# mock/
|
|
6
6
|
|
|
7
|
-
When there is a `config/mock/index.
|
|
7
|
+
When there is a `config/mock/index.ts` or `config/mock/index.js` file in the
|
|
8
|
+
project directory, Modern.js will automatically enable the Mock service during
|
|
9
|
+
development. Use [`dev.mockDir`](/configure/app/dev/mock-dir) to customize
|
|
10
|
+
this directory.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: mockDir
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# dev.mockDir
|
|
6
|
+
|
|
7
|
+
- **Type:** `string`
|
|
8
|
+
- **Default:** `'./config/mock'`
|
|
9
|
+
|
|
10
|
+
Sets the directory containing the Mock API entry file. Relative paths are
|
|
11
|
+
resolved from the application directory, and absolute paths are also supported.
|
|
12
|
+
Modern.js loads `index.ts` or `index.js` from this directory during development.
|
|
13
|
+
|
|
14
|
+
For example, move the Mock API entry to `mocks/index.ts`:
|
|
15
|
+
|
|
16
|
+
```js title="modern.config.ts"
|
|
17
|
+
import { defineConfig } from '@modern-js/app-tools';
|
|
18
|
+
|
|
19
|
+
export default defineConfig({
|
|
20
|
+
dev: {
|
|
21
|
+
mockDir: './mocks',
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
This is also useful in a monorepo when multiple applications share one Mock
|
|
27
|
+
directory:
|
|
28
|
+
|
|
29
|
+
```js title="modern.config.ts"
|
|
30
|
+
export default defineConfig({
|
|
31
|
+
dev: {
|
|
32
|
+
mockDir: '../../shared/mocks',
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
```
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: reactCompiler
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# source.reactCompiler
|
|
6
|
+
|
|
7
|
+
- **Type:** `boolean | ReactCompilerOptions`
|
|
8
|
+
- **Default:** `undefined` (disabled)
|
|
9
|
+
|
|
10
|
+
Whether to enable [React Compiler](https://react.dev/learn/react-compiler). React Compiler is a build-time tool that optimizes re-rendering performance of React applications through automatic memoization.
|
|
11
|
+
|
|
12
|
+
Modern.js implements this capability based on the Rust-based React Compiler built into Rspack's `builtin:swc-loader` (equivalent to setting SWC's `jsc.transform.reactCompiler`), reusing Rspack's built-in SWC transform chain without introducing Babel.
|
|
13
|
+
|
|
14
|
+
:::tip
|
|
15
|
+
This option is disabled by default. It must be enabled explicitly for any React version, including React 19.
|
|
16
|
+
:::
|
|
17
|
+
|
|
18
|
+
## Example
|
|
19
|
+
|
|
20
|
+
### Enable React Compiler (React 19)
|
|
21
|
+
|
|
22
|
+
```ts title="modern.config.ts"
|
|
23
|
+
import { defineConfig } from '@modern-js/app-tools';
|
|
24
|
+
|
|
25
|
+
export default defineConfig({
|
|
26
|
+
source: {
|
|
27
|
+
reactCompiler: true,
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Using with React 18
|
|
33
|
+
|
|
34
|
+
The compiled output targets React 19 by default. To use it in React 18 projects, you need to:
|
|
35
|
+
|
|
36
|
+
1. Install [react-compiler-runtime](https://www.npmjs.com/package/react-compiler-runtime) as a **runtime dependency** (the compiled output references it at runtime):
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
npm add react-compiler-runtime
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
2. Specify the React version via `target`:
|
|
43
|
+
|
|
44
|
+
```ts title="modern.config.ts"
|
|
45
|
+
import { defineConfig } from '@modern-js/app-tools';
|
|
46
|
+
|
|
47
|
+
export default defineConfig({
|
|
48
|
+
source: {
|
|
49
|
+
reactCompiler: {
|
|
50
|
+
target: '18',
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Customize compilation behavior
|
|
57
|
+
|
|
58
|
+
When passing an object, the options are the same as Rspack's `jsc.transform.reactCompiler`. For example, use `compilationMode: 'annotation'` to only compile functions annotated with the `"use memo"` directive:
|
|
59
|
+
|
|
60
|
+
```ts title="modern.config.ts"
|
|
61
|
+
import { defineConfig } from '@modern-js/app-tools';
|
|
62
|
+
|
|
63
|
+
export default defineConfig({
|
|
64
|
+
source: {
|
|
65
|
+
reactCompiler: {
|
|
66
|
+
compilationMode: 'annotation',
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
For the complete list of options, see [Rsbuild - reactCompiler](https://rsbuild.rs/plugins/list/plugin-react#reactcompiler) and the [React Compiler configuration docs](https://react.dev/reference/react-compiler/configuration).
|
|
@@ -81,32 +81,16 @@ export default {
|
|
|
81
81
|
|
|
82
82
|
> Please refer to [@rsbuild/plugin-type-check](https://github.com/rstackjs/rsbuild-plugin-type-check) for more details.
|
|
83
83
|
|
|
84
|
-
## TypeScript
|
|
84
|
+
## TypeScript 7+ Support
|
|
85
85
|
|
|
86
|
-
`tools.tsChecker` supports
|
|
86
|
+
`tools.tsChecker` supports the native checker included in TypeScript 7+. This capability is provided by [`ts-checker-rspack-plugin`](https://github.com/rstackjs/ts-checker-rspack-plugin), which is integrated by [`@rsbuild/plugin-type-check`](https://github.com/rstackjs/rsbuild-plugin-type-check), and can reduce type-checking time on large projects.
|
|
87
87
|
|
|
88
|
-
Install TypeScript 7.0
|
|
88
|
+
Install TypeScript >= 7.0.0 to enable `tsgo` automatically:
|
|
89
89
|
|
|
90
|
-
<PackageManagerTabs command="install typescript@
|
|
90
|
+
<PackageManagerTabs command="install typescript@latest -D" />
|
|
91
91
|
|
|
92
|
-
|
|
92
|
+
When `tsgo` is enabled and `typescript.typescriptPath` is set manually, it must point to an absolute TypeScript 7+ `typescript/package.json` path or the legacy `@typescript/native-preview/package.json` path.
|
|
93
93
|
|
|
94
|
-
|
|
94
|
+
> The `@typescript/native-preview` path is kept only for compatibility. New setups should use TypeScript 7+ from the standard `typescript` package.
|
|
95
95
|
|
|
96
|
-
|
|
97
|
-
export default {
|
|
98
|
-
tools: {
|
|
99
|
-
tsChecker: {
|
|
100
|
-
typescript: {
|
|
101
|
-
tsgo: true,
|
|
102
|
-
},
|
|
103
|
-
},
|
|
104
|
-
},
|
|
105
|
-
};
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
When `tsgo` is enabled and `typescript.typescriptPath` is set manually, it must point to an absolute `typescript/package.json` path from TypeScript 7+ or `@typescript/native-preview/package.json`.
|
|
109
|
-
|
|
110
|
-
> The `@typescript/native-preview` usage is deprecated and kept only for compatibility. We recommend installing `typescript@rc` to use `tsgo`.
|
|
111
|
-
|
|
112
|
-
For supported options and limitations, please refer to [ts-checker-rspack-plugin - TypeScript Go support](https://github.com/rstackjs/ts-checker-rspack-plugin#typescript-go-support).
|
|
96
|
+
For supported options and limitations, please refer to [ts-checker-rspack-plugin - TypeScript 7+ support](https://github.com/rstackjs/ts-checker-rspack-plugin#typescript-7-support).
|
|
@@ -1,54 +1,69 @@
|
|
|
1
1
|
# React Compiler
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
React Compiler is a build-time compiler from the React team that reduces unnecessary re-renders through automatic memoization, without manually writing `useMemo`, `useCallback`, or `React.memo`.
|
|
4
4
|
|
|
5
|
-
Before starting to use
|
|
5
|
+
Before starting to use React Compiler, it is recommended to read the [React Compiler documentation](https://react.dev/learn/react-compiler) to understand its features, current status, and usage.
|
|
6
6
|
|
|
7
7
|
## How to Use
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
Modern.js provides built-in support for React Compiler via the [source.reactCompiler](/configure/app/source/react-compiler) option, powered by the Rust-based React Compiler implementation in Rspack's `builtin:swc-loader`. It reuses Rspack's built-in SWC transform chain without introducing Babel (`babel-plugin-react-compiler`).
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
This capability is **disabled by default** and must be enabled explicitly for any React version.
|
|
12
12
|
|
|
13
|
-
### React
|
|
13
|
+
### React 19
|
|
14
14
|
|
|
15
|
-
If you are using React
|
|
15
|
+
If you are using React 19, simply enable `source.reactCompiler` — no additional dependencies are required:
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
```ts title="modern.config.ts"
|
|
18
|
+
import { appTools, defineConfig } from '@modern-js/app-tools';
|
|
18
19
|
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
export default defineConfig({
|
|
21
|
+
source: {
|
|
22
|
+
reactCompiler: true,
|
|
23
|
+
},
|
|
24
|
+
plugins: [appTools()],
|
|
25
|
+
});
|
|
21
26
|
```
|
|
22
27
|
|
|
23
|
-
|
|
28
|
+
### React 18
|
|
29
|
+
|
|
30
|
+
If you are using React 18, configure it as follows:
|
|
31
|
+
|
|
32
|
+
1. Install `react-compiler-runtime` as a **runtime dependency** to allow the compiled code to run on versions before React 19:
|
|
24
33
|
|
|
25
34
|
```bash
|
|
26
|
-
npm
|
|
35
|
+
npm add react-compiler-runtime
|
|
27
36
|
```
|
|
28
37
|
|
|
29
|
-
|
|
38
|
+
2. Specify the React version via `target` in the configuration:
|
|
30
39
|
|
|
31
40
|
```ts title="modern.config.ts"
|
|
32
41
|
import { appTools, defineConfig } from '@modern-js/app-tools';
|
|
33
|
-
import { pluginBabel } from '@rsbuild/plugin-babel';
|
|
34
42
|
|
|
35
43
|
export default defineConfig({
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
'babel-plugin-react-compiler',
|
|
42
|
-
{
|
|
43
|
-
target: '18', // 或 '17',根据你使用的 React 版本
|
|
44
|
-
},
|
|
45
|
-
],
|
|
46
|
-
]);
|
|
47
|
-
},
|
|
48
|
-
});
|
|
49
|
-
];
|
|
44
|
+
source: {
|
|
45
|
+
reactCompiler: {
|
|
46
|
+
target: '18',
|
|
47
|
+
},
|
|
48
|
+
},
|
|
50
49
|
plugins: [appTools()],
|
|
51
50
|
});
|
|
52
51
|
```
|
|
53
52
|
|
|
53
|
+
## Customize Compilation Behavior
|
|
54
|
+
|
|
55
|
+
`source.reactCompiler` accepts an object to configure the compilation behavior, with the same options as Rspack's `jsc.transform.reactCompiler`. For example, use `compilationMode: 'annotation'` to only compile functions annotated with the `"use memo"` directive, enabling React Compiler incrementally:
|
|
56
|
+
|
|
57
|
+
```ts title="modern.config.ts"
|
|
58
|
+
export default defineConfig({
|
|
59
|
+
source: {
|
|
60
|
+
reactCompiler: {
|
|
61
|
+
compilationMode: 'annotation',
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
For the complete list of options, see [Rsbuild - reactCompiler](https://rsbuild.rs/plugins/list/plugin-react#reactcompiler) and the [React Compiler configuration docs](https://react.dev/reference/react-compiler/configuration).
|
|
68
|
+
|
|
54
69
|
> For detailed code, you can refer to the [Modern.js & React Compiler example project](https://github.com/web-infra-dev/modern.js/tree/main/examples/react-compiler)
|
|
@@ -20,6 +20,17 @@ By convention, when there is an `index.ts` in the `config/mock/` directory, mock
|
|
|
20
20
|
└── modern.config.ts
|
|
21
21
|
```
|
|
22
22
|
|
|
23
|
+
You can use [`dev.mockDir`](/configure/app/dev/mock-dir) to place the
|
|
24
|
+
Mock entry in another directory:
|
|
25
|
+
|
|
26
|
+
```js title="modern.config.ts"
|
|
27
|
+
export default {
|
|
28
|
+
dev: {
|
|
29
|
+
mockDir: './mocks',
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
```
|
|
33
|
+
|
|
23
34
|
## Writing Mock Files
|
|
24
35
|
|
|
25
36
|
The `config/mock/index.ts` file only needs to export an object containing all Mock APIs. The properties of the object are composed of the request configuration `method` and `url`, and the corresponding property values can be `Object`, `Array`, or `Function`:
|
|
@@ -5,16 +5,31 @@ sidebar_position: 6
|
|
|
5
5
|
|
|
6
6
|
# Modern.js For AI
|
|
7
7
|
|
|
8
|
-
Modern.js provides a toolkit for AI agents that
|
|
8
|
+
Modern.js provides a toolkit for AI agents that makes your project agent-ready out of the box, helping you use AI to develop, upgrade, and migrate Modern.js applications more accurately and efficiently.
|
|
9
|
+
|
|
10
|
+
## Bundled docs
|
|
11
|
+
|
|
12
|
+
Modern.js ships the English docs that match your installed version together with `@modern-js/app-tools`. After you install or upgrade, they live at `node_modules/@modern-js/app-tools/docs/`, available offline with no extra setup.
|
|
13
|
+
|
|
14
|
+
AI agents use them to get APIs, config, and conventions that exactly match your framework version, rather than relying on potentially outdated training data.
|
|
15
|
+
|
|
16
|
+
## AGENTS.md
|
|
17
|
+
|
|
18
|
+
`AGENTS.md` gives AI coding agents project-level guidance — telling them to read the bundled docs before making changes and to follow Modern.js conventions. A `CLAUDE.md` is generated alongside it, reusing the same guidance for Claude Code via the `@AGENTS.md` import.
|
|
19
|
+
|
|
20
|
+
- **New projects**: `npx @modern-js/create` generates both files by default; pass `--no-agents-md` to skip them.
|
|
21
|
+
- **Existing projects**: run `npx @modern-js/create --agents-md-only` at the project root to create or update them. Re-run it any time; your own additions are preserved.
|
|
22
|
+
|
|
23
|
+
Modern.js only maintains the content inside the `<!-- BEGIN:modernjs-agent-rules -->` markers in `AGENTS.md`; anything you write outside them is left untouched.
|
|
9
24
|
|
|
10
25
|
## llms.txt
|
|
11
26
|
|
|
12
|
-
|
|
27
|
+
llms.txt is an online documentation index following the [llms.txt specification](https://llmstxt.org/), auto-generated by [`@rspress/plugin-llms`](https://rspress.rs/plugin/official-plugins/llms) for AI tools to retrieve the full docs online:
|
|
13
28
|
|
|
14
29
|
- Index: [`https://modernjs.dev/llms.txt`](https://modernjs.dev/llms.txt)
|
|
15
|
-
- Full text: `https://modernjs.dev/llms-full.txt` (large — fetch on demand)
|
|
30
|
+
- Full text: [`https://modernjs.dev/llms-full.txt`](https://modernjs.dev/llms-full.txt) (large — fetch on demand)
|
|
16
31
|
|
|
17
|
-
|
|
32
|
+
Use it when you need content beyond the bundled docs — just let your agent retrieve it on demand, no need to copy docs into your project. Typical cases: looking up another framework version, a section not included in the bundle, or getting Modern.js information outside of a project (for example before a project exists, or when researching on its own).
|
|
18
33
|
|
|
19
34
|
## Skills
|
|
20
35
|
|
|
@@ -24,7 +24,7 @@ All Modern.js official packages are released with a **uniform version number**,
|
|
|
24
24
|
- Visit [npm](https://www.npmjs.com/package/@modern-js/app-tools) to check the latest version of `@modern-js/app-tools`
|
|
25
25
|
- Check [GitHub Releases](https://github.com/web-infra-dev/modern.js/releases)
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
<ReleaseNote />
|
|
28
28
|
|
|
29
29
|
2. **Update package.json**
|
|
30
30
|
|
|
@@ -46,7 +46,7 @@ All Modern.js official packages are released with a **uniform version number**,
|
|
|
46
46
|
|
|
47
47
|
After updating `package.json`, reinstall dependencies:
|
|
48
48
|
|
|
49
|
-
|
|
49
|
+
<PackageManagerTabs command="install" />
|
|
50
50
|
|
|
51
51
|
:::tip
|
|
52
52
|
When upgrading, you need to upgrade all packages provided by Modern.js uniformly, rather than upgrading individual dependencies. Ensure that all `@modern-js/**` packages have the same version number.
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: mockDir
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# dev.mockDir
|
|
6
|
+
|
|
7
|
+
- **类型:** `string`
|
|
8
|
+
- **默认值:** `'./config/mock'`
|
|
9
|
+
|
|
10
|
+
设置 Mock API 入口文件所在的目录。相对路径基于应用目录解析,同时也支持绝对路径。
|
|
11
|
+
开发环境下,Modern.js 会加载该目录中的 `index.ts` 或 `index.js`。
|
|
12
|
+
|
|
13
|
+
例如,将 Mock API 入口移动到 `mocks/index.ts`:
|
|
14
|
+
|
|
15
|
+
```js title="modern.config.ts"
|
|
16
|
+
import { defineConfig } from '@modern-js/app-tools';
|
|
17
|
+
|
|
18
|
+
export default defineConfig({
|
|
19
|
+
dev: {
|
|
20
|
+
mockDir: './mocks',
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
在 Monorepo 中,也可以让多个应用指向一个共享的 Mock 目录:
|
|
26
|
+
|
|
27
|
+
```js title="modern.config.ts"
|
|
28
|
+
export default defineConfig({
|
|
29
|
+
dev: {
|
|
30
|
+
mockDir: '../../shared/mocks',
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
```
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: reactCompiler
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# source.reactCompiler
|
|
6
|
+
|
|
7
|
+
- **类型:** `boolean | ReactCompilerOptions`
|
|
8
|
+
- **默认值:** `undefined`(不开启)
|
|
9
|
+
|
|
10
|
+
是否启用 [React Compiler](https://zh-hans.react.dev/learn/react-compiler)。React Compiler 是一个构建期工具,通过自动记忆化(memoization)优化 React 应用的重渲染性能。
|
|
11
|
+
|
|
12
|
+
Modern.js 基于 Rspack `builtin:swc-loader` 内置的 Rust 版 React Compiler 实现该能力(等价于设置 SWC 的 `jsc.transform.reactCompiler`),复用 Rspack 内置的 SWC 转换链,无需额外引入 Babel。
|
|
13
|
+
|
|
14
|
+
:::tip
|
|
15
|
+
该配置默认关闭,任何 React 版本下都需要显式开启,包括 React 19。
|
|
16
|
+
:::
|
|
17
|
+
|
|
18
|
+
## 示例
|
|
19
|
+
|
|
20
|
+
### 开启 React Compiler(React 19)
|
|
21
|
+
|
|
22
|
+
```ts title="modern.config.ts"
|
|
23
|
+
import { defineConfig } from '@modern-js/app-tools';
|
|
24
|
+
|
|
25
|
+
export default defineConfig({
|
|
26
|
+
source: {
|
|
27
|
+
reactCompiler: true,
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### 在 React 18 中使用
|
|
33
|
+
|
|
34
|
+
React Compiler 编译产物默认面向 React 19。在 React 18 项目中使用时,需要:
|
|
35
|
+
|
|
36
|
+
1. 将 [react-compiler-runtime](https://www.npmjs.com/package/react-compiler-runtime) 安装为**运行时依赖**(编译产物会在运行时引用它):
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
npm add react-compiler-runtime
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
2. 通过 `target` 指定 React 版本:
|
|
43
|
+
|
|
44
|
+
```ts title="modern.config.ts"
|
|
45
|
+
import { defineConfig } from '@modern-js/app-tools';
|
|
46
|
+
|
|
47
|
+
export default defineConfig({
|
|
48
|
+
source: {
|
|
49
|
+
reactCompiler: {
|
|
50
|
+
target: '18',
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### 自定义编译行为
|
|
57
|
+
|
|
58
|
+
传入对象时,选项与 Rspack `jsc.transform.reactCompiler` 一致,例如通过 `compilationMode: 'annotation'` 仅编译带有 `"use memo"` 指令的函数:
|
|
59
|
+
|
|
60
|
+
```ts title="modern.config.ts"
|
|
61
|
+
import { defineConfig } from '@modern-js/app-tools';
|
|
62
|
+
|
|
63
|
+
export default defineConfig({
|
|
64
|
+
source: {
|
|
65
|
+
reactCompiler: {
|
|
66
|
+
compilationMode: 'annotation',
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
完整选项请参考 [Rsbuild - reactCompiler](https://rsbuild.rs/plugins/list/plugin-react#reactcompiler) 与 [React Compiler 配置文档](https://zh-hans.react.dev/reference/react-compiler/configuration)。
|
|
@@ -81,32 +81,16 @@ export default {
|
|
|
81
81
|
|
|
82
82
|
> 请参考 [@rsbuild/plugin-type-check](https://github.com/rstackjs/rsbuild-plugin-type-check) 了解更多用法。
|
|
83
83
|
|
|
84
|
-
## TypeScript
|
|
84
|
+
## TypeScript 7+ 支持
|
|
85
85
|
|
|
86
|
-
`tools.tsChecker`
|
|
86
|
+
`tools.tsChecker` 支持使用 TypeScript 7+ 的原生检查器进行类型检查。该能力由 [`@rsbuild/plugin-type-check`](https://github.com/rstackjs/rsbuild-plugin-type-check) 底层集成的 [`ts-checker-rspack-plugin`](https://github.com/rstackjs/ts-checker-rspack-plugin) 提供,可以减少大型项目的类型检查耗时。
|
|
87
87
|
|
|
88
|
-
安装 TypeScript 7.0
|
|
88
|
+
安装 TypeScript >= 7.0.0 后会自动启用 `tsgo`:
|
|
89
89
|
|
|
90
|
-
<PackageManagerTabs command="install typescript@
|
|
90
|
+
<PackageManagerTabs command="install typescript@latest -D" />
|
|
91
91
|
|
|
92
|
-
|
|
92
|
+
开启 `tsgo` 后,如果手动设置 `typescript.typescriptPath`,它必须指向 TypeScript 7+ 的 `typescript/package.json` 或旧版 `@typescript/native-preview/package.json` 的绝对路径。
|
|
93
93
|
|
|
94
|
-
|
|
94
|
+
> `@typescript/native-preview` 路径仅作为兼容保留。新项目应使用标准 `typescript` 包提供的 TypeScript 7+。
|
|
95
95
|
|
|
96
|
-
|
|
97
|
-
export default {
|
|
98
|
-
tools: {
|
|
99
|
-
tsChecker: {
|
|
100
|
-
typescript: {
|
|
101
|
-
tsgo: true,
|
|
102
|
-
},
|
|
103
|
-
},
|
|
104
|
-
},
|
|
105
|
-
};
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
开启 `tsgo` 后,如果手动设置 `typescript.typescriptPath`,它必须指向 TypeScript 7+ 的 `typescript/package.json` 或 `@typescript/native-preview/package.json` 的绝对路径。
|
|
109
|
-
|
|
110
|
-
> `@typescript/native-preview` 的用法已废弃,仅作为兼容保留。推荐安装 `typescript@rc` 使用 `tsgo`。
|
|
111
|
-
|
|
112
|
-
关于 `tsgo` 模式下生效的配置项和相关限制,请参考 [ts-checker-rspack-plugin - TypeScript Go support](https://github.com/rstackjs/ts-checker-rspack-plugin#typescript-go-support)。
|
|
96
|
+
关于 `tsgo` 模式下生效的配置项和相关限制,请参考 [ts-checker-rspack-plugin - TypeScript 7+ support](https://github.com/rstackjs/ts-checker-rspack-plugin#typescript-7-support)。
|
|
@@ -1,54 +1,69 @@
|
|
|
1
1
|
# React Compiler
|
|
2
2
|
|
|
3
|
-
React Compiler 是 React
|
|
3
|
+
React Compiler 是 React 官方提供的构建期编译器,通过自动记忆化(memoization)减少不必要的重渲染,无需手动编写 `useMemo`、`useCallback` 和 `React.memo`。
|
|
4
4
|
|
|
5
|
-
在开始使用 React Compiler 之前,建议阅读 [React Compiler 文档](https://zh-hans.react.dev/learn/react-compiler)
|
|
5
|
+
在开始使用 React Compiler 之前,建议阅读 [React Compiler 文档](https://zh-hans.react.dev/learn/react-compiler),以了解它的功能、当前状态和使用方法。
|
|
6
6
|
|
|
7
7
|
## 如何使用
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
Modern.js 通过 [source.reactCompiler](/configure/app/source/react-compiler) 配置内置支持 React Compiler,底层使用 Rspack `builtin:swc-loader` 中 Rust 实现的 React Compiler,复用 Rspack 内置的 SWC 转换链,无需额外引入 Babel(`babel-plugin-react-compiler`)。
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
该能力**默认关闭**,任何 React 版本下都需要显式开启。
|
|
12
12
|
|
|
13
|
-
### React
|
|
13
|
+
### React 19
|
|
14
14
|
|
|
15
|
-
如果你使用的是 React
|
|
15
|
+
如果你使用的是 React 19,开启 `source.reactCompiler` 即可,无需安装任何额外依赖:
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
```ts title="modern.config.ts"
|
|
18
|
+
import { appTools, defineConfig } from '@modern-js/app-tools';
|
|
18
19
|
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
export default defineConfig({
|
|
21
|
+
source: {
|
|
22
|
+
reactCompiler: true,
|
|
23
|
+
},
|
|
24
|
+
plugins: [appTools()],
|
|
25
|
+
});
|
|
21
26
|
```
|
|
22
27
|
|
|
23
|
-
|
|
28
|
+
### React 18
|
|
29
|
+
|
|
30
|
+
如果你使用的是 React 18,需要按照以下步骤配置:
|
|
31
|
+
|
|
32
|
+
1. 将 `react-compiler-runtime` 安装为**运行时依赖**,以允许编译后的代码在 React 19 之前的版本上运行:
|
|
24
33
|
|
|
25
34
|
```bash
|
|
26
|
-
npm
|
|
35
|
+
npm add react-compiler-runtime
|
|
27
36
|
```
|
|
28
37
|
|
|
29
|
-
|
|
38
|
+
2. 在配置中通过 `target` 指定 React 版本:
|
|
30
39
|
|
|
31
40
|
```ts title="modern.config.ts"
|
|
32
41
|
import { appTools, defineConfig } from '@modern-js/app-tools';
|
|
33
|
-
import { pluginBabel } from '@rsbuild/plugin-babel';
|
|
34
42
|
|
|
35
43
|
export default defineConfig({
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
'babel-plugin-react-compiler',
|
|
42
|
-
{
|
|
43
|
-
target: '18', // 或 '17',根据你使用的 React 版本
|
|
44
|
-
},
|
|
45
|
-
],
|
|
46
|
-
]);
|
|
47
|
-
},
|
|
48
|
-
});
|
|
49
|
-
];
|
|
44
|
+
source: {
|
|
45
|
+
reactCompiler: {
|
|
46
|
+
target: '18',
|
|
47
|
+
},
|
|
48
|
+
},
|
|
50
49
|
plugins: [appTools()],
|
|
51
50
|
});
|
|
52
51
|
```
|
|
53
52
|
|
|
53
|
+
## 自定义编译行为
|
|
54
|
+
|
|
55
|
+
`source.reactCompiler` 支持传入对象来配置编译行为,选项与 Rspack 的 `jsc.transform.reactCompiler` 一致。例如,通过 `compilationMode: 'annotation'` 可以只编译带有 `"use memo"` 指令的函数,渐进式地启用 React Compiler:
|
|
56
|
+
|
|
57
|
+
```ts title="modern.config.ts"
|
|
58
|
+
export default defineConfig({
|
|
59
|
+
source: {
|
|
60
|
+
reactCompiler: {
|
|
61
|
+
compilationMode: 'annotation',
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
完整选项请参考 [Rsbuild - reactCompiler](https://rsbuild.rs/plugins/list/plugin-react#reactcompiler) 与 [React Compiler 配置文档](https://zh-hans.react.dev/reference/react-compiler/configuration)。
|
|
68
|
+
|
|
54
69
|
> 详细代码可以参考:[Modern.js & React Compiler 示例项目](https://github.com/web-infra-dev/modern.js/tree/main/examples/react-compiler)
|
|
@@ -20,6 +20,16 @@ Modern.js 提供了快速生成 Mock 数据的功能,能够让前端独立自
|
|
|
20
20
|
└── modern.config.ts
|
|
21
21
|
```
|
|
22
22
|
|
|
23
|
+
你可以通过 [`dev.mockDir`](/configure/app/dev/mock-dir) 将 Mock 入口放到其他目录:
|
|
24
|
+
|
|
25
|
+
```js title="modern.config.ts"
|
|
26
|
+
export default {
|
|
27
|
+
dev: {
|
|
28
|
+
mockDir: './mocks',
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
```
|
|
32
|
+
|
|
23
33
|
## 编写 Mock 文件
|
|
24
34
|
|
|
25
35
|
`config/mock/index.ts` 文件只需要导出一个包含所有 Mock API 的对象,对象的属性由请求配置 `method` 和 `url` 组成,对应的属性值可以为 `Object`、`Array`、`Function`:
|
|
@@ -5,16 +5,31 @@ sidebar_position: 6
|
|
|
5
5
|
|
|
6
6
|
# Modern.js For AI
|
|
7
7
|
|
|
8
|
-
Modern.js 为 AI Agent
|
|
8
|
+
Modern.js 为 AI Agent 提供了一套工具套件,让项目开箱即为 agent-ready,帮助你更准确、高效地用 AI 完成 Modern.js 应用的开发、升级与迁移。
|
|
9
|
+
|
|
10
|
+
## 随包文档
|
|
11
|
+
|
|
12
|
+
Modern.js 把与你安装版本一致的英文文档随 `@modern-js/app-tools` 一起分发。安装或升级后,文档即位于 `node_modules/@modern-js/app-tools/docs/`,离线可用、无需额外配置。
|
|
13
|
+
|
|
14
|
+
AI Agent 由此获取与当前框架版本严格匹配的 API、配置与约定,而不是依赖可能过时的训练数据。
|
|
15
|
+
|
|
16
|
+
## AGENTS.md
|
|
17
|
+
|
|
18
|
+
`AGENTS.md` 是给 AI 编码助手的项目指引,告诉它在动手前先阅读随包文档、并遵循 Modern.js 的约定;同时会生成 `CLAUDE.md`,通过 `@AGENTS.md` 让 Claude Code 复用同一份指引。
|
|
19
|
+
|
|
20
|
+
- **新建项目**:`npx @modern-js/create` 默认生成这两个文件,加 `--no-agents-md` 可跳过。
|
|
21
|
+
- **已有项目**:在项目根运行 `npx @modern-js/create --agents-md-only` 生成或更新,可随时重复运行,你添加的自定义内容会被保留。
|
|
22
|
+
|
|
23
|
+
Modern.js 只维护 `AGENTS.md` 中 `<!-- BEGIN:modernjs-agent-rules -->` 标记内的内容,标记之外你写的内容不会被改动。
|
|
9
24
|
|
|
10
25
|
## llms.txt
|
|
11
26
|
|
|
12
|
-
|
|
27
|
+
llms.txt 是遵循 [llms.txt 规范](https://llmstxt.org/) 的在线文档索引,由 [`@rspress/plugin-llms`](https://rspress.rs/plugin/official-plugins/llms) 自动生成,供 AI 工具在线检索完整文档:
|
|
13
28
|
|
|
14
29
|
- 索引:[`https://modernjs.dev/llms.txt`](https://modernjs.dev/llms.txt)
|
|
15
|
-
-
|
|
30
|
+
- 全文:[`https://modernjs.dev/llms-full.txt`](https://modernjs.dev/llms-full.txt)(体积较大,按需取片段)
|
|
16
31
|
|
|
17
|
-
|
|
32
|
+
当需要查阅随包文档之外的内容时,让 Agent 在线检索它即可,不必把文档复制进项目。典型场景包括:查阅其他框架版本、随包未包含的章节,或在 Modern.js 项目之外获取信息(例如尚未创建项目、或独立查阅资料时)。
|
|
18
33
|
|
|
19
34
|
## Skills
|
|
20
35
|
|
|
@@ -24,7 +24,7 @@ Modern.js 所有的官方包使用**统一版本号**进行发布,因此升级
|
|
|
24
24
|
- 访问 [npm](https://www.npmjs.com/package/@modern-js/app-tools) 查看 `@modern-js/app-tools` 的最新版本
|
|
25
25
|
- 查看 [GitHub Releases](https://github.com/web-infra-dev/modern.js/releases)
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
<ReleaseNote />
|
|
28
28
|
|
|
29
29
|
2. **更新 package.json**
|
|
30
30
|
|
|
@@ -46,7 +46,7 @@ Modern.js 所有的官方包使用**统一版本号**进行发布,因此升级
|
|
|
46
46
|
|
|
47
47
|
更新完 `package.json` 后,重新安装依赖:
|
|
48
48
|
|
|
49
|
-
|
|
49
|
+
<PackageManagerTabs command="install" />
|
|
50
50
|
|
|
51
51
|
:::tip
|
|
52
52
|
当升级时,需要对 Modern.js 官方提供的所有包做统一升级,而不是升级单个依赖。确保所有 `@modern-js/**` 包的版本号保持一致。
|
package/package.json
CHANGED
|
@@ -16,14 +16,14 @@
|
|
|
16
16
|
"modern",
|
|
17
17
|
"modern.js"
|
|
18
18
|
],
|
|
19
|
-
"version": "3.
|
|
19
|
+
"version": "3.8.0",
|
|
20
20
|
"publishConfig": {
|
|
21
21
|
"registry": "https://registry.npmjs.org/",
|
|
22
22
|
"access": "public"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
25
|
"mermaid": "^11.15.0",
|
|
26
|
-
"@modern-js/sandpack-react": "3.
|
|
26
|
+
"@modern-js/sandpack-react": "3.8.0"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"rsbuild-plugin-open-graph": "1.1.3",
|
|
@@ -42,6 +42,21 @@
|
|
|
42
42
|
"ts-node": "^10.9.2",
|
|
43
43
|
"typescript": "^5"
|
|
44
44
|
},
|
|
45
|
+
"nx": {
|
|
46
|
+
"targets": {
|
|
47
|
+
"build": {
|
|
48
|
+
"inputs": [
|
|
49
|
+
"build",
|
|
50
|
+
"^build",
|
|
51
|
+
"{projectRoot}/docs/**/*",
|
|
52
|
+
"{projectRoot}/rspress.config.ts"
|
|
53
|
+
],
|
|
54
|
+
"outputs": [
|
|
55
|
+
"{projectRoot}/doc_build"
|
|
56
|
+
]
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
},
|
|
45
60
|
"scripts": {
|
|
46
61
|
"dev": "rspress dev",
|
|
47
62
|
"dev:no_lazy": "LAZY=false rspress dev",
|