@modern-js/main-doc 2.11.0 → 2.13.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/CHANGELOG.md +27 -0
- package/docs/en/apis/app/commands.mdx +1 -1
- package/docs/en/apis/app/hooks/api/framework/lambda.mdx +2 -2
- package/docs/en/apis/app/hooks/api/functions/api.mdx +3 -3
- package/docs/en/apis/app/hooks/api/functions/app.mdx +1 -1
- package/docs/en/apis/app/hooks/api/test.mdx +2 -1
- package/docs/en/apis/app/hooks/config/icon.mdx +46 -6
- package/docs/en/apis/app/hooks/config/mock.mdx +1 -1
- package/docs/en/apis/app/hooks/config/upload.mdx +1 -1
- package/docs/en/apis/app/hooks/server/test.mdx +1 -1
- package/docs/en/apis/app/hooks/src/app.mdx +1 -1
- package/docs/en/apis/app/hooks/src/pages.mdx +1 -1
- package/docs/en/apis/app/runtime/app/define-config.mdx +1 -1
- package/docs/en/apis/app/runtime/core/bootstrap.mdx +2 -2
- package/docs/en/apis/app/runtime/core/use-loader.mdx +6 -2
- package/docs/en/apis/app/runtime/model/auto-actions.mdx +6 -6
- package/docs/en/apis/app/runtime/model/connect.mdx +2 -1
- package/docs/en/apis/app/runtime/model/create-app.mdx +1 -1
- package/docs/en/apis/app/runtime/model/handle-effect.mdx +3 -3
- package/docs/en/apis/app/runtime/model/model_.mdx +2 -2
- package/docs/en/apis/app/runtime/model/use-model.mdx +2 -2
- package/docs/en/apis/app/runtime/model/use-static-model.mdx +2 -2
- package/docs/en/apis/app/runtime/model/use-store.mdx +1 -1
- package/docs/en/apis/app/runtime/router/router.mdx +1 -0
- package/docs/en/apis/app/runtime/ssr/pre-render.mdx +2 -2
- package/docs/en/apis/app/runtime/testing/render.mdx +1 -1
- package/docs/en/components/enable-bff.mdx +2 -2
- package/docs/en/configure/app/html/script-loading.mdx +13 -0
- package/docs/en/configure/app/output/ssg.mdx +1 -1
- package/docs/en/configure/app/plugins.mdx +3 -3
- package/docs/en/configure/app/source/disable-entry-dirs.mdx +1 -1
- package/docs/en/configure/app/tools/bundler-chain.mdx +13 -0
- package/docs/en/configure/app/tools/swc.mdx +4 -3
- package/docs/en/configure/app/usage.mdx +65 -23
- package/docs/en/guides/advanced-features/bff/function.mdx +3 -3
- package/docs/en/guides/advanced-features/code-split.mdx +2 -2
- package/docs/en/guides/advanced-features/eslint.mdx +1 -1
- package/docs/en/guides/advanced-features/ssg.mdx +2 -2
- package/docs/en/guides/advanced-features/ssr.mdx +45 -8
- package/docs/en/guides/basic-features/alias.mdx +1 -1
- package/docs/en/guides/basic-features/data-fetch.mdx +1 -1
- package/docs/en/guides/basic-features/env-vars.mdx +5 -5
- package/docs/en/guides/basic-features/html.mdx +0 -4
- package/docs/en/guides/basic-features/routes.mdx +48 -28
- package/docs/en/guides/concept/entries.mdx +3 -3
- package/docs/en/guides/get-started/introduction.mdx +21 -1
- package/docs/en/guides/get-started/quick-start.mdx +6 -23
- package/docs/en/guides/get-started/upgrade.mdx +7 -7
- package/docs/en/guides/topic-detail/framework-plugin/hook.mdx +1 -1
- package/docs/en/guides/topic-detail/generator/codesmith/api/json.mdx +3 -3
- package/docs/en/guides/topic-detail/generator/config/app.mdx +1 -1
- package/docs/en/guides/topic-detail/generator/config/monorepo.mdx +1 -1
- package/docs/en/guides/topic-detail/generator/plugin/api/file/addHelper.mdx +1 -1
- package/docs/en/guides/topic-detail/model/manage-effects.mdx +2 -1
- package/docs/en/guides/topic-detail/model/model-communicate.mdx +1 -1
- package/docs/en/guides/topic-detail/model/use-model.mdx +2 -1
- package/docs/en/guides/troubleshooting/builder.mdx +31 -0
- package/docs/en/tutorials/foundations/introduction.mdx +0 -2
- package/docs/zh/apis/app/hooks/config/icon.mdx +44 -6
- package/docs/zh/apis/app/hooks/src/pages.mdx +1 -1
- package/docs/zh/apis/app/runtime/core/use-loader.mdx +4 -0
- package/docs/zh/apis/app/runtime/model/handle-effect.mdx +2 -1
- package/docs/zh/apis/app/runtime/model/model_.mdx +4 -2
- package/docs/zh/blog/updates/2022-0708-updates.md +1 -2
- package/docs/zh/components/enable-bff.mdx +2 -2
- package/docs/zh/configure/app/html/script-loading.mdx +13 -0
- package/docs/zh/configure/app/runtime/router.mdx +1 -1
- package/docs/zh/configure/app/source/disable-entry-dirs.mdx +2 -2
- package/docs/zh/configure/app/tools/bundler-chain.mdx +13 -0
- package/docs/zh/configure/app/tools/swc.mdx +4 -3
- package/docs/zh/configure/app/usage.mdx +66 -24
- package/docs/zh/guides/advanced-features/ssr.mdx +74 -36
- package/docs/zh/guides/basic-features/data-fetch.mdx +1 -1
- package/docs/zh/guides/basic-features/html.mdx +0 -4
- package/docs/zh/guides/basic-features/routes.mdx +64 -24
- package/docs/zh/guides/concept/entries.mdx +4 -4
- package/docs/zh/guides/get-started/introduction.mdx +21 -1
- package/docs/zh/guides/get-started/upgrade.mdx +3 -3
- package/docs/zh/guides/topic-detail/changesets/release.mdx +2 -2
- package/docs/zh/guides/topic-detail/model/use-model.mdx +2 -1
- package/docs/zh/guides/troubleshooting/builder.mdx +31 -0
- package/modern.config.ts +3 -2
- package/package.json +5 -5
- package/src/components/SolutionCards/index.module.scss +62 -0
- package/src/components/SolutionCards/index.tsx +26 -0
- package/docs/en/configure/app/source/plugin-import.mdx +0 -13
- package/docs/zh/configure/app/source/plugin-import.mdx +0 -13
|
@@ -25,24 +25,24 @@ You can see that the dependency in the project `package.json` has been changed t
|
|
|
25
25
|
|
|
26
26
|
## Specify version upgrade
|
|
27
27
|
|
|
28
|
-
Modern.js
|
|
28
|
+
All packages of Modern.js are published using the **unified version number**.
|
|
29
29
|
|
|
30
|
-
According to the website
|
|
30
|
+
According to the website release note, developers can also manually upgrade the project to the desired version.
|
|
31
31
|
|
|
32
32
|
:::tip
|
|
33
33
|
When upgrading, you need to upgrade to all packages provided by the Modern.js, rather than upgrading a single dependency.
|
|
34
34
|
|
|
35
35
|
:::
|
|
36
36
|
|
|
37
|
-
##
|
|
37
|
+
## Lock nested dependency
|
|
38
38
|
|
|
39
|
-
When there is a problem with one of the
|
|
39
|
+
When there is a problem with one of the nested dependencies of the project, and the Modern.js cannot be updated immediately, you can use the package manager to lock the child dependency version.
|
|
40
40
|
|
|
41
41
|
### pnpm
|
|
42
42
|
|
|
43
43
|
For projects using pnpm, add the following configuration to the `package.json` in the **project root directory** and re-execute `pnpm install`:
|
|
44
44
|
|
|
45
|
-
```json
|
|
45
|
+
```json title="package.json"
|
|
46
46
|
{
|
|
47
47
|
"pnpm": {
|
|
48
48
|
"overrides": {
|
|
@@ -56,7 +56,7 @@ For projects using pnpm, add the following configuration to the `package.json` i
|
|
|
56
56
|
|
|
57
57
|
For projects using Yarn, add the following configuration to the `package.json` in the **project root directory** and re-execute `yarn install`:
|
|
58
58
|
|
|
59
|
-
```json
|
|
59
|
+
```json title="package.json"
|
|
60
60
|
{
|
|
61
61
|
"resolutions": {
|
|
62
62
|
"package-name": "^1.0.0"
|
|
@@ -68,7 +68,7 @@ For projects using Yarn, add the following configuration to the `package.json` i
|
|
|
68
68
|
|
|
69
69
|
For projects using Npm, add the following configuration to the `package.json` in the **project root directory** and re-execute `npm install`:
|
|
70
70
|
|
|
71
|
-
```json
|
|
71
|
+
```json title="package.json"
|
|
72
72
|
{
|
|
73
73
|
"overrides": {
|
|
74
74
|
"package-name": "^1.0.0"
|
|
@@ -32,7 +32,7 @@ pipeline.run(1); // 4
|
|
|
32
32
|
pipeline.run(5); // 12
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
-
In this example, a `Pipeline<number, number>` is created on line 3. This means that when you run it, you need to pass in a `number`, and you will get a `number` as a result
|
|
35
|
+
In this example, a `Pipeline<number, number>` is created on line 3. This means that when you run it, you need to pass in a `number`, and you will get a `number` as a result, the type is:
|
|
36
36
|
|
|
37
37
|
```ts
|
|
38
38
|
(count: number, next: (nextCount: number) => number) => number;
|
|
@@ -35,7 +35,7 @@ Get the JSON file content.
|
|
|
35
35
|
|
|
36
36
|
Parameter:
|
|
37
37
|
|
|
38
|
-
- resource: `FsResource`. A file resource
|
|
38
|
+
- resource: `FsResource`. A file resource, get by `context.materials.default.get(<filename>)`.
|
|
39
39
|
|
|
40
40
|
### extend
|
|
41
41
|
|
|
@@ -43,7 +43,7 @@ Merge objects into a JSON file.
|
|
|
43
43
|
|
|
44
44
|
Parameter:
|
|
45
45
|
|
|
46
|
-
- resource: `FsResource`. A file resource
|
|
46
|
+
- resource: `FsResource`. A file resource, get by `context.materials.default.get(<filename>)`.
|
|
47
47
|
- obj: `Record<string, any>`. Object to be merged.
|
|
48
48
|
|
|
49
49
|
### update
|
|
@@ -52,5 +52,5 @@ pdate object fields to JSON file.
|
|
|
52
52
|
|
|
53
53
|
Parameter:
|
|
54
54
|
|
|
55
|
-
- resource: `FsResource`. A file resource
|
|
55
|
+
- resource: `FsResource`. A file resource, get by `context.materials.default.get(<filename>)`.
|
|
56
56
|
- operation: `{ query: Record<string, any>; update: Record<string, any> }`. Update operation, use gesture to view [declaration-update](https://www.npmjs.com/package/declaration-update) in detail.
|
|
@@ -12,7 +12,7 @@ The Monorepo project supports the creation of sub-projects by using the new comm
|
|
|
12
12
|
|
|
13
13
|
### solution
|
|
14
14
|
|
|
15
|
-
Subproject type(solution)
|
|
15
|
+
Subproject type(solution), the different subproject type fields are:
|
|
16
16
|
|
|
17
17
|
- Application (mwa)
|
|
18
18
|
- Application (Test) (mwa_test)
|
|
@@ -4,7 +4,7 @@ sidebar_position: 2
|
|
|
4
4
|
|
|
5
5
|
# addHelper
|
|
6
6
|
|
|
7
|
-
For text files, add a customized Help function of Handlebars
|
|
7
|
+
For text files, add a customized Help function of Handlebars, the specific viewable document[Custom Helpers](https://handlebarsjs.com/guide/#custom-helpers).
|
|
8
8
|
|
|
9
9
|
This method is available on the `onForged` time to live API parameter.
|
|
10
10
|
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
sidebar_position: 5
|
|
3
3
|
title: Management Effect
|
|
4
4
|
---
|
|
5
|
+
|
|
5
6
|
# Management Effect
|
|
6
7
|
|
|
7
8
|
The actions in the model must be pure functions and cannot have any side effects during execution. However, in real-world scenarios, we often encounter many side effects, such as: requesting data from an HTTP API to obtain state data, or modifying `localStorage` or sending events while updating the state. In Reduck, side effects are managed through the model's `effects` function.
|
|
@@ -88,7 +89,7 @@ For this type of side effect scenario, `handleEffect` stipulates that the state
|
|
|
88
89
|
{
|
|
89
90
|
result: null,
|
|
90
91
|
error: null,
|
|
91
|
-
pending: false
|
|
92
|
+
pending: false,
|
|
92
93
|
}
|
|
93
94
|
```
|
|
94
95
|
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
sidebar_position: 3
|
|
3
3
|
title: Use Models
|
|
4
4
|
---
|
|
5
|
+
|
|
5
6
|
# Use Models
|
|
6
7
|
|
|
7
8
|
## Using Models in Components
|
|
@@ -146,7 +147,7 @@ will always get the initial value of Input.
|
|
|
146
147
|
|
|
147
148
|
:::
|
|
148
149
|
|
|
149
|
-
`useStaticModel` is also suitable for use with animation libraries such as [react-three-fiber](https://github.com/pmndrs/react-three-fiber), because binding fast-changing states in animation component UI can easily cause [performance issues](https
|
|
150
|
+
`useStaticModel` is also suitable for use with animation libraries such as [react-three-fiber](https://github.com/pmndrs/react-three-fiber), because binding fast-changing states in animation component UI can easily cause [performance issues](https://docs.pmnd.rs/react-three-fiber/advanced/pitfalls#never-bind-fast-state-reactive). In this case, you can choose to use `useStaticModel`, which only subscribes to the State but does not cause the view component to re-render. Here is a simplified example:
|
|
150
151
|
|
|
151
152
|
```ts
|
|
152
153
|
function ThreeComponent() {
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
---
|
|
2
|
+
sidebar_position: 3
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# Build FAQ
|
|
6
|
+
|
|
7
|
+
If you encounter build-related problems, you can refer to the following documents:
|
|
8
|
+
|
|
9
|
+
## General FAQ
|
|
10
|
+
|
|
11
|
+
Please refer to [Modern.js Builder - General FAQ](https://modernjs.dev/builder/en/guide/faq/general.html), which contains some general questions about concepts, such as:
|
|
12
|
+
|
|
13
|
+
- The relationship between Modern.js Builder and Modern.js?
|
|
14
|
+
- Can Builder be used to build libraries or UI components?
|
|
15
|
+
- Will Builder support Vite?
|
|
16
|
+
|
|
17
|
+
## Features FAQ
|
|
18
|
+
|
|
19
|
+
Please refer to [Modern.js Builder - Features FAQ](https://modernjs.dev/builder/en/guide/faq/features.html), which contains an introduction to some common building features, such as:
|
|
20
|
+
|
|
21
|
+
- How to import UI Component library on demand?
|
|
22
|
+
- How to run ESLint during compilation?
|
|
23
|
+
- How to configure CDN path for static assets?
|
|
24
|
+
|
|
25
|
+
## Exceptions FAQ
|
|
26
|
+
|
|
27
|
+
Please refer to [Modern.js Builder - Exceptions FAQ](https://modernjs.dev/builder/en/guide/faq/exceptions.html), which contains some common construction problems, such as:
|
|
28
|
+
|
|
29
|
+
- 'compilation' argument error when webpack compiling?
|
|
30
|
+
- Compile error `You may need additional loader`?
|
|
31
|
+
- Find `exports is not defined` runtime error?
|
|
@@ -1,18 +1,56 @@
|
|
|
1
1
|
---
|
|
2
|
-
title: icon
|
|
2
|
+
title: icon
|
|
3
3
|
sidebar_position: 2
|
|
4
4
|
---
|
|
5
|
-
# icon.png
|
|
6
5
|
|
|
7
|
-
|
|
6
|
+
# Icon
|
|
8
7
|
|
|
9
|
-
|
|
8
|
+
## 设置 favicon
|
|
9
|
+
|
|
10
|
+
当项目根目录的 `config` 目录下存在 `favicon.*` 文件时,Modern.js 会自动将该文件设置到 [html.favicon](/configure/app/html/favicon) 配置项中,用于生成页面的 favicon 图标:
|
|
11
|
+
|
|
12
|
+
```
|
|
13
|
+
./config
|
|
14
|
+
└── favicon.ico
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
构建完成后,可以看到 HTML 中自动生成了以下标签:
|
|
18
|
+
|
|
19
|
+
```html
|
|
20
|
+
<link rel="icon" href="/favicon.ico" />
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### 查找顺序
|
|
24
|
+
|
|
25
|
+
在设置 app icon 时,Modern.js 会按以下顺序寻找文件:
|
|
26
|
+
|
|
27
|
+
- favicon.png
|
|
28
|
+
- favicon.jpg
|
|
29
|
+
- favicon.jpeg
|
|
30
|
+
- favicon.svg
|
|
31
|
+
- favicon.ico
|
|
32
|
+
|
|
33
|
+
## 设置 app icon
|
|
34
|
+
|
|
35
|
+
当项目根目录的 `config` 目录下存在 `icon.*` 文件时,Modern.js 会自动将该文件设置到 [html.appIcon](/configure/app/html/app-icon) 配置项中,用于生成 iOS 系统下的 apple-touch-icon 图标。
|
|
10
36
|
|
|
11
37
|
```
|
|
12
38
|
./config
|
|
13
39
|
└── icon.png
|
|
14
40
|
```
|
|
15
41
|
|
|
16
|
-
|
|
42
|
+
构建完成后,可以看到 HTML 中自动生成了以下标签:
|
|
43
|
+
|
|
44
|
+
```html
|
|
45
|
+
<link rel="apple-touch-icon" sizes="180*180" href="/static/image/icon.png" />
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### 查找顺序
|
|
49
|
+
|
|
50
|
+
在设置 app icon 时,Modern.js 会按以下顺序寻找文件:
|
|
17
51
|
|
|
18
|
-
|
|
52
|
+
- icon.png
|
|
53
|
+
- icon.jpg
|
|
54
|
+
- icon.jpeg
|
|
55
|
+
- icon.svg
|
|
56
|
+
- icon.ico
|
|
@@ -85,7 +85,7 @@ pages 目录下的文件满足以下条件的不会被当做路由文件
|
|
|
85
85
|
import React from 'react';
|
|
86
86
|
import UserLayout from 'xxxx';
|
|
87
87
|
|
|
88
|
-
export
|
|
88
|
+
export const App = ({Component, ...pageProps}:{ Component: React.ComponentType}) => {
|
|
89
89
|
return (
|
|
90
90
|
<UserLayout>
|
|
91
91
|
<Component {...pageProps} />
|
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
sidebar_position: 1
|
|
3
3
|
title: model
|
|
4
4
|
---
|
|
5
|
+
|
|
5
6
|
# model
|
|
6
7
|
|
|
7
|
-
import ReduckTip from
|
|
8
|
+
import ReduckTip from '@site-docs/components/reduck-tip';
|
|
8
9
|
|
|
9
10
|
<ReduckTip />
|
|
10
11
|
|
|
@@ -61,7 +62,8 @@ const fooModel = model('foo').define({
|
|
|
61
62
|
- context: Context,Reduck 上下文对象,可以获取底层的 `store` 对象。`store` 除支持 Redux Store 的所有 [API](https://redux.js.org/api/store) 以外,还挂载了用于消费 Model 的 `use` 的方法,和用于卸载 Model 的 `unmount` 方法。
|
|
62
63
|
- utils: Utils,定义 Model 时,常用的工具函数:`use`、`onMount`。`use` 作用同 `store` 对象上的 `use`,`onMount` 是 Model 挂载后的钩子函数。
|
|
63
64
|
|
|
64
|
-
{/* TODO: @anchao 调整类型
|
|
65
|
+
{/* TODO: @anchao 调整类型 */}
|
|
66
|
+
|
|
65
67
|
```ts
|
|
66
68
|
interface Utils {
|
|
67
69
|
use: UseModel;
|
|
@@ -39,8 +39,7 @@ npx @modern-js/upgrade
|
|
|
39
39
|
|
|
40
40
|
新的模块工程项目,不仅支持对产物做 bundless 构建,也支持 bundle 构建。通过配置 `buildConfig` 下的 [`buildType`](https://modernjs.dev/v1/docs/apis/module/config/output/build-config/build-type) ,即可进行 bundle 构建:
|
|
41
41
|
|
|
42
|
-
```ts
|
|
43
|
-
// modern.config.ts
|
|
42
|
+
```ts title="modern.config.ts"
|
|
44
43
|
import { defineConfig } from '@modern-js/module-tools';
|
|
45
44
|
|
|
46
45
|
export default defineConfig({
|
|
@@ -6,7 +6,7 @@ import { Tabs, Tab as TabItem } from "@theme";
|
|
|
6
6
|
<Tabs>
|
|
7
7
|
<TabItem value="express" label="Express.js" default>
|
|
8
8
|
|
|
9
|
-
```ts title="
|
|
9
|
+
```ts title="modern.config.ts"
|
|
10
10
|
import expressPlugin from '@modern-js/plugin-express';
|
|
11
11
|
import bffPlugin from '@modern-js/plugin-bff';
|
|
12
12
|
|
|
@@ -18,7 +18,7 @@ export default defineConfig({
|
|
|
18
18
|
</TabItem>
|
|
19
19
|
<TabItem value="koa" label="Koa.js">
|
|
20
20
|
|
|
21
|
-
```ts title="
|
|
21
|
+
```ts title="modern.config.ts"
|
|
22
22
|
import koaPlugin from '@modern-js/plugin-koa';
|
|
23
23
|
import bffPlugin from '@modern-js/plugin-bff';
|
|
24
24
|
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
---
|
|
2
|
+
sidebar_label: scriptLoading
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# html.scriptLoading
|
|
6
|
+
|
|
7
|
+
:::tip
|
|
8
|
+
该配置由 Modern.js Builder 提供,更多信息可参考 [html.scriptLoading](https://modernjs.dev/builder/api/config-html.html#htmlscriptloading)。
|
|
9
|
+
:::
|
|
10
|
+
|
|
11
|
+
import Main from '@modern-js/builder-doc/docs/zh/config/html/scriptLoading.md';
|
|
12
|
+
|
|
13
|
+
<Main />
|
|
@@ -7,14 +7,14 @@ sidebar_label: disableEntryDirs
|
|
|
7
7
|
- **类型:** `string[]`
|
|
8
8
|
- **默认值:** `[]`
|
|
9
9
|
|
|
10
|
-
默认会根据 `src`
|
|
10
|
+
Modern.js 默认会根据 `src` 目录识别应用入口,你可以通过该选项禁止某些目录被识别为应用入口。
|
|
11
11
|
|
|
12
12
|
例如,当配置与目录结构如下时:
|
|
13
13
|
|
|
14
14
|
```ts title="modern.config.ts"
|
|
15
15
|
export default defineConfig({
|
|
16
16
|
source: {
|
|
17
|
-
disableEntryDirs: './src/one',
|
|
17
|
+
disableEntryDirs: ['./src/one'],
|
|
18
18
|
},
|
|
19
19
|
});
|
|
20
20
|
```
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
---
|
|
2
|
+
sidebar_label: bundlerChain
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# tools.bundlerChain
|
|
6
|
+
|
|
7
|
+
:::tip
|
|
8
|
+
该配置由 Modern.js Builder 提供,更多信息可参考 [tools.bundlerChain](https://modernjs.dev/builder/api/config-tools.html#toolsbundlerchain)。
|
|
9
|
+
:::
|
|
10
|
+
|
|
11
|
+
import Main from '@modern-js/builder-doc/docs/zh/config/tools/bundlerChain.mdx';
|
|
12
|
+
|
|
13
|
+
<Main />
|
|
@@ -19,13 +19,14 @@ Modern.js 提供了开箱即用的 SWC 插件,可以为你的 Web 应用提供
|
|
|
19
19
|
|
|
20
20
|
## 安装
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
首先,你需要执行 `pnpm run new` 启用 SWC 编译:
|
|
23
23
|
|
|
24
24
|
```bash
|
|
25
|
-
|
|
25
|
+
? 请选择你想要的操作:启用可选功能
|
|
26
|
+
? 启用可选功能:启用「SWC 编译」
|
|
26
27
|
```
|
|
27
28
|
|
|
28
|
-
|
|
29
|
+
执行完成后,你只需在 `modern.config.ts` 文件中注册 SWC 插件,即可启用 SWC 编译和压缩能力。
|
|
29
30
|
|
|
30
31
|
```ts title="modern.config.ts"
|
|
31
32
|
import appTools, { defineConfig } from '@modern-js/app-tools';
|
|
@@ -19,17 +19,33 @@ Modern.js 不支持同时在 package.json 中和 modern.config.ts 中配置同
|
|
|
19
19
|
|
|
20
20
|
## 在配置文件中配置
|
|
21
21
|
|
|
22
|
-
Modern.js 的配置文件定义在项目的根目录下,支持 `.
|
|
22
|
+
Modern.js 的配置文件定义在项目的根目录下,支持 `.ts`, `.js` 和 `.mjs` 格式:
|
|
23
23
|
|
|
24
|
-
- `modern.config.js`
|
|
25
24
|
- `modern.config.ts`
|
|
25
|
+
- `modern.config.js`
|
|
26
26
|
- `modern.config.mjs`
|
|
27
27
|
|
|
28
|
-
### modern.config.
|
|
28
|
+
### modern.config.ts(推荐)
|
|
29
|
+
|
|
30
|
+
我们推荐使用 .ts 格式的配置文件,它提供了友好的 TypeScript 类型提示,从而帮助你避免配置中的错误。
|
|
31
|
+
|
|
32
|
+
从 `@modern-js/app-tools` 中导入 `defineConfig` 工具函数, 它会帮助你进行配置的类型推导和类型补全:
|
|
33
|
+
|
|
34
|
+
```ts title="modern.config.ts"
|
|
35
|
+
import { defineConfig } from '@modern-js/app-tools';
|
|
36
|
+
|
|
37
|
+
export default defineConfig({
|
|
38
|
+
source: {
|
|
39
|
+
alias: {
|
|
40
|
+
'@common': './src/common',
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
```
|
|
29
45
|
|
|
30
|
-
|
|
46
|
+
### modern.config.js
|
|
31
47
|
|
|
32
|
-
|
|
48
|
+
如果你在开发一个非 TypeScript 项目,可以使用 .js 格式的配置文件:
|
|
33
49
|
|
|
34
50
|
```js title="modern.config.js"
|
|
35
51
|
export default {
|
|
@@ -51,24 +67,6 @@ export default {
|
|
|
51
67
|
};
|
|
52
68
|
```
|
|
53
69
|
|
|
54
|
-
### modern.config.ts(推荐)
|
|
55
|
-
|
|
56
|
-
我们推荐使用 .ts 格式的配置文件,它提供了友好的 TypeScript 类型提示,从而帮助你避免配置中的错误。
|
|
57
|
-
|
|
58
|
-
从 `@modern-js/app-tools` 中导入 `defineConfig` 工具函数, 它会帮助你进行配置的类型推导和类型补全:
|
|
59
|
-
|
|
60
|
-
```ts title="modern.config.ts"
|
|
61
|
-
import { defineConfig } from '@modern-js/app-tools';
|
|
62
|
-
|
|
63
|
-
export default defineConfig({
|
|
64
|
-
source: {
|
|
65
|
-
alias: {
|
|
66
|
-
'@common': './src/common',
|
|
67
|
-
},
|
|
68
|
-
},
|
|
69
|
-
});
|
|
70
|
-
```
|
|
71
|
-
|
|
72
70
|
### 导出配置函数
|
|
73
71
|
|
|
74
72
|
Modern.js 支持在配置文件中导出一个函数,你可以在函数中动态计算配置,并返回给 Modern.js。
|
|
@@ -134,7 +132,7 @@ $ modern build -c modern.prod.config.js
|
|
|
134
132
|
|
|
135
133
|
## 在 package.json 中配置(不推荐)
|
|
136
134
|
|
|
137
|
-
|
|
135
|
+
除了配置文件外,你也可以在 `package.json` 中的 `modernConfig` 字段下设置配置项,如:
|
|
138
136
|
|
|
139
137
|
```json title="package.json"
|
|
140
138
|
{
|
|
@@ -200,3 +198,47 @@ modern.config.local.ts
|
|
|
200
198
|
modern.config.local.js
|
|
201
199
|
modern.config.local.mjs
|
|
202
200
|
```
|
|
201
|
+
|
|
202
|
+
## 合并多份配置
|
|
203
|
+
|
|
204
|
+
在某些情况下,你可能需要将多份配置合并为一份配置,此时你可以使用 `mergeConfig` 工具函数来合并多个配置。
|
|
205
|
+
|
|
206
|
+
`mergeConfig` 函数接受一个数组作为参数,数组中的每一项都是一个配置对象,`mergeConfig` 会将数组中的每一项配置对象进行深层合并,自动将多个函数项合并为数组,最终返回一个合并后的配置对象。
|
|
207
|
+
|
|
208
|
+
### 示例
|
|
209
|
+
|
|
210
|
+
```ts title="modern.config.ts"
|
|
211
|
+
import { mergeConfig } from '@modern-js/app-tools';
|
|
212
|
+
|
|
213
|
+
const config1 = {
|
|
214
|
+
dev: {
|
|
215
|
+
port: 3000,
|
|
216
|
+
},
|
|
217
|
+
tools: {
|
|
218
|
+
postcss: () => console.log('config1');
|
|
219
|
+
},
|
|
220
|
+
};
|
|
221
|
+
const config2 = {
|
|
222
|
+
dev: {
|
|
223
|
+
port: 3001,
|
|
224
|
+
},
|
|
225
|
+
tools: {
|
|
226
|
+
postcss: () => console.log('config2');
|
|
227
|
+
},
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
const mergedConfig = mergeConfig([config1, config2]);
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
在以上示例中,合并后的配置对象为:
|
|
234
|
+
|
|
235
|
+
```ts
|
|
236
|
+
const mergedConfig = {
|
|
237
|
+
dev: {
|
|
238
|
+
port: 3001,
|
|
239
|
+
},
|
|
240
|
+
tools: {
|
|
241
|
+
postcss: [() => console.log('config1'), () => console.log('config2')],
|
|
242
|
+
},
|
|
243
|
+
};
|
|
244
|
+
```
|