@modern-js/main-doc 2.22.1 → 2.23.1
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +45 -0
- package/docs/en/apis/app/commands.mdx +5 -5
- package/docs/en/apis/app/hooks/config/storybook.mdx +24 -0
- package/docs/en/apis/app/hooks/src/stories.mdx +1 -1
- package/docs/en/apis/app/runtime/core/bootstrap.mdx +5 -5
- package/docs/en/apis/app/runtime/core/use-loader.mdx +2 -2
- package/docs/en/apis/app/runtime/core/use-module-apps.mdx +2 -2
- package/docs/en/apis/app/runtime/core/use-runtime-context.mdx +1 -1
- package/docs/en/apis/app/runtime/testing/render.mdx +1 -1
- package/docs/en/components/bff-proxy-path-rewrite.mdx +16 -0
- package/docs/en/components/bff-proxy-principle.mdx +1 -0
- package/docs/en/components/enable-bff-caution.mdx +3 -0
- package/docs/en/components/global-proxy-config.mdx +33 -13
- package/docs/en/configure/app/bff/enable-handle-web.mdx +5 -5
- package/docs/en/configure/app/bff/prefix.mdx +6 -6
- package/docs/en/configure/app/bff/proxy.mdx +19 -30
- package/docs/en/configure/app/builder-plugins.mdx +1 -1
- package/docs/en/configure/app/plugins.mdx +19 -19
- package/docs/en/configure/app/runtime/intro.mdx +28 -4
- package/docs/en/configure/app/runtime/master-app.mdx +1 -1
- package/docs/en/configure/app/runtime/router.mdx +2 -2
- package/docs/en/configure/app/server/base-url.mdx +4 -4
- package/docs/en/configure/app/server/enable-framework-ext.mdx +4 -4
- package/docs/en/configure/app/server/port.mdx +6 -6
- package/docs/en/configure/app/server/public-routes.mdx +5 -5
- package/docs/en/configure/app/server/routes.mdx +13 -13
- package/docs/en/configure/app/server/ssr-by-entries.mdx +4 -4
- package/docs/en/configure/app/server/ssr.mdx +5 -5
- package/docs/en/configure/app/testing/transformer.mdx +4 -8
- package/docs/en/configure/app/usage.mdx +5 -5
- package/docs/en/guides/advanced-features/code-split.mdx +15 -14
- package/docs/en/guides/advanced-features/compatibility.mdx +3 -24
- package/docs/en/guides/advanced-features/rspack-start.mdx +0 -1
- package/docs/en/guides/advanced-features/ssr.mdx +2 -2
- package/docs/en/guides/advanced-features/testing.mdx +8 -7
- package/docs/en/guides/advanced-features/using-storybook.mdx +44 -0
- package/docs/en/guides/advanced-features/web-server.mdx +16 -16
- package/docs/en/guides/basic-features/alias.mdx +11 -9
- package/docs/en/guides/basic-features/env-vars.mdx +14 -14
- package/docs/en/guides/basic-features/html.mdx +41 -43
- package/docs/en/guides/basic-features/mock.mdx +10 -10
- package/docs/en/guides/topic-detail/framework-plugin/implement.mdx +4 -4
- package/docs/en/guides/topic-detail/framework-plugin/introduction.mdx +1 -1
- package/docs/en/guides/troubleshooting/cli.mdx +13 -13
- package/docs/en/guides/troubleshooting/dependencies.mdx +25 -25
- package/docs/en/tutorials/first-app/c03-css.mdx +1 -1
- package/docs/zh/apis/app/commands.mdx +4 -4
- package/docs/zh/apis/app/hooks/config/storybook.mdx +24 -0
- package/docs/zh/apis/app/hooks/src/stories.mdx +1 -1
- package/docs/zh/apis/app/runtime/testing/render.mdx +1 -1
- package/docs/zh/components/bff-proxy-path-rewrite.mdx +16 -0
- package/docs/zh/components/bff-proxy-principle.mdx +1 -0
- package/docs/zh/components/enable-bff-caution.mdx +4 -0
- package/docs/zh/components/global-proxy-config.mdx +35 -11
- package/docs/zh/configure/app/bff/enable-handle-web.mdx +3 -3
- package/docs/zh/configure/app/bff/prefix.mdx +2 -3
- package/docs/zh/configure/app/bff/proxy.mdx +11 -21
- package/docs/zh/configure/app/runtime/intro.mdx +1 -1
- package/docs/zh/configure/app/server/base-url.mdx +1 -1
- package/docs/zh/configure/app/server/public-routes.mdx +2 -2
- package/docs/zh/configure/app/server/routes.mdx +2 -2
- package/docs/zh/configure/app/server/ssr-by-entries.mdx +3 -3
- package/docs/zh/configure/app/server/ssr.mdx +1 -1
- package/docs/zh/configure/app/testing/transformer.mdx +6 -3
- package/docs/zh/configure/app/usage.mdx +9 -9
- package/docs/zh/guides/advanced-features/code-split.mdx +13 -12
- package/docs/zh/guides/advanced-features/rspack-start.mdx +0 -1
- package/docs/zh/guides/advanced-features/ssr.mdx +2 -2
- package/docs/zh/guides/advanced-features/testing.mdx +4 -4
- package/docs/zh/guides/advanced-features/using-storybook.mdx +44 -0
- package/docs/zh/guides/basic-features/alias.mdx +5 -3
- package/docs/zh/guides/basic-features/html.mdx +9 -9
- package/docs/zh/guides/basic-features/mock.mdx +2 -2
- package/docs/zh/guides/topic-detail/_category_.json +1 -1
- package/docs/zh/guides/troubleshooting/cli.mdx +1 -1
- package/docs/zh/guides/troubleshooting/dependencies.mdx +1 -1
- package/package.json +7 -6
@@ -4,7 +4,7 @@ sidebar_position: 8
|
|
4
4
|
---
|
5
5
|
# Alias
|
6
6
|
|
7
|
-
Modern.js
|
7
|
+
Modern.js allows you to use alias to import modules from custom directories in JS and CSS, and comes with the following built-in alias:
|
8
8
|
|
9
9
|
```js
|
10
10
|
{
|
@@ -14,11 +14,11 @@ Modern.js allow aliases in JS and CSS, and the following aliases are built in:
|
|
14
14
|
```
|
15
15
|
|
16
16
|
:::info
|
17
|
-
When
|
17
|
+
When enabling optional features, the `new` command will also dynamically add built-in alias specific to the features. For example, when enabling BFF, the `@api` alias is added by default.
|
18
18
|
|
19
19
|
:::
|
20
20
|
|
21
|
-
For example,
|
21
|
+
For example, importing modules from the `src/common` directory in the `src/App.tsx` file:
|
22
22
|
|
23
23
|
```bash
|
24
24
|
.
|
@@ -27,17 +27,19 @@ For example, import the modules from the `src/common/` directory in the `src/App
|
|
27
27
|
│ │ └── base.css
|
28
28
|
│ └── utils
|
29
29
|
│ └── index.ts
|
30
|
-
|
30
|
+
└── App.tsx
|
31
31
|
```
|
32
32
|
|
33
|
-
|
33
|
+
The code in `src/App.tsx` is as follows:
|
34
34
|
|
35
35
|
```ts
|
36
36
|
import utils from '@/src/common/utils';
|
37
37
|
import '@/src/common/styles/base.css';
|
38
38
|
```
|
39
39
|
|
40
|
-
Modern.js also provides a way to
|
40
|
+
Modern.js also provides a way to customize alias. For example, adding the `@common` alias is as follows:
|
41
|
+
|
42
|
+
For TypeScript projects, just set `compilerOptions.paths` in the project's `tsconfig.json`:
|
41
43
|
|
42
44
|
```json
|
43
45
|
{
|
@@ -50,9 +52,9 @@ Modern.js also provides a way to config aliases. Adding the `@common` alias as a
|
|
50
52
|
}
|
51
53
|
```
|
52
54
|
|
53
|
-
JavaScript
|
55
|
+
For JavaScript projects, set `source.alias` in `modern.config.js`:
|
54
56
|
|
55
|
-
```ts title="modern.config.
|
57
|
+
```ts title="modern.config.js"
|
56
58
|
export default defineConfig({
|
57
59
|
source: {
|
58
60
|
alias: {
|
@@ -62,4 +64,4 @@ export default defineConfig({
|
|
62
64
|
});
|
63
65
|
```
|
64
66
|
|
65
|
-
For
|
67
|
+
For the specific usage of alias configuration, please refer to the [source.alias documentation](/configure/app/source/alias).
|
@@ -10,15 +10,15 @@ Modern.js provides support for environment variables, including built-in environ
|
|
10
10
|
|
11
11
|
### ASSET_PREFIX
|
12
12
|
|
13
|
-
The
|
13
|
+
The current path prefix of the resource file, which is a read-only environment variable.
|
14
14
|
|
15
15
|
### NODE_ENV
|
16
16
|
|
17
17
|
The current execution environment and is a **read-only** environment variable whose have different values under different execution commands:
|
18
18
|
|
19
|
-
- `production`:
|
20
|
-
- `test`:
|
21
|
-
- `development`:
|
19
|
+
- `production`: Default value when running `modern build` or `modern serve`.
|
20
|
+
- `test`: Default value when running `modern test`.
|
21
|
+
- `development`: Default value when running `modern dev`, also the default value in other cases.
|
22
22
|
|
23
23
|
### MODERN_ENV
|
24
24
|
|
@@ -33,7 +33,7 @@ MODERN_ENV priority is higher than NODE_ENV.
|
|
33
33
|
|
34
34
|
When using `@modern-js/runtime`, Modern.js will automatically inject `MODERN_TARGET` to distinguish between SSR and CSR environments.
|
35
35
|
|
36
|
-
You can use `process.env.MODERN_TARGET` to
|
36
|
+
You can use `process.env.MODERN_TARGET` to determine the environment and execute appropriate code.
|
37
37
|
|
38
38
|
```ts title="App.tsx"
|
39
39
|
function App() {
|
@@ -65,7 +65,7 @@ function App() {
|
|
65
65
|
This can provide different outputs for different environments to ensure that the bundle size is minimized. It can also be convenient to deal with some side effects for different environments.
|
66
66
|
|
67
67
|
:::note Dead Code Elimination
|
68
|
-
In
|
68
|
+
In production environment, minimizers such as Terser and SWC will analyze the code and remove dead code to reduce the bundle size. This process is called "Dead Code Elimination" (DCE).
|
69
69
|
|
70
70
|
For example, the code inside the `if (false)` statement will be removed, while the code inside the `if (true)` will be preserved.
|
71
71
|
|
@@ -75,9 +75,9 @@ If you do not use `process.env.MODERN_TARGET` as described above, the code minim
|
|
75
75
|
|
76
76
|
## Custom Environment Variables
|
77
77
|
|
78
|
-
|
78
|
+
You can specify custom environment variables in both `shell` and `.env` files.
|
79
79
|
|
80
|
-
###
|
80
|
+
### Via `shell`
|
81
81
|
|
82
82
|
Add custom environment variables before the command:
|
83
83
|
|
@@ -85,7 +85,7 @@ Add custom environment variables before the command:
|
|
85
85
|
REACT_APP_FOO=123 BAR=456 pnpm run dev
|
86
86
|
```
|
87
87
|
|
88
|
-
###
|
88
|
+
### Via `.env` file
|
89
89
|
|
90
90
|
Create a `.env` file in the project root and add custom environment variables, which are added to the Node.js process by default, for example:
|
91
91
|
|
@@ -97,7 +97,7 @@ BAR=456
|
|
97
97
|
The `.env` file follows the following loading rules:
|
98
98
|
|
99
99
|
- `.env`: default.
|
100
|
-
- `.env.{ MODERN_ENV | NODE_ENV }`:
|
100
|
+
- `.env.{ MODERN_ENV | NODE_ENV }`: Overrides `.env` for a specific environment.
|
101
101
|
|
102
102
|
When you need to use different config according to the environment, you can define environment variables in the `.env` file corresponding to the environment name, and manually set the execution environment when starting the project.
|
103
103
|
|
@@ -137,7 +137,7 @@ In custom HTML templates, you can also use such environment variables directly.
|
|
137
137
|
|
138
138
|
### Any Other Names
|
139
139
|
|
140
|
-
If you need to use environment variables with any other names in your code, you can
|
140
|
+
If you need to use environment variables with any other names in your code, you can configure them in [`source.globalVars`](/configure/app/source/global-vars). For example:
|
141
141
|
|
142
142
|
```ts title="modern.config.ts"
|
143
143
|
export default defineConfig({
|
@@ -149,7 +149,7 @@ export default defineConfig({
|
|
149
149
|
});
|
150
150
|
```
|
151
151
|
|
152
|
-
At this point, the `process.env.VERSION` in the code will be replaced
|
152
|
+
At this point, the `process.env.VERSION` in the code will be replaced by the value of `VERSION` in the environment variables.
|
153
153
|
|
154
154
|
:::note
|
155
155
|
`source.globalVars` also supports replacing other expressions or strings with specified values, not limited to environment variables.
|
@@ -158,7 +158,7 @@ At this point, the `process.env.VERSION` in the code will be replaced with the v
|
|
158
158
|
|
159
159
|
## Use Global Replacement
|
160
160
|
|
161
|
-
In addition to environment variables, Modern.js also supports replacing variables in code with other values or expressions, which can be used
|
161
|
+
In addition to environment variables, Modern.js also supports replacing variables in code with other values or expressions, which can be used to distinguish between development environment and production environment in code.
|
162
162
|
|
163
163
|
For example, converts the expression `TWO` to `1 + 1`:
|
164
164
|
|
@@ -179,4 +179,4 @@ const foo = TWO;
|
|
179
179
|
const foo = 1 + 1;
|
180
180
|
```
|
181
181
|
|
182
|
-
In most cases, `source.globalVars` is already sufficient to replace variables. But the values passed in by `source.globalVars` will be serialized by JSON by default. So it cannot be replaced like `1 + 1` in the example above, at this time, we need use [`source.define`](/configure/app/source/define).
|
182
|
+
In most cases, `source.globalVars` is already sufficient to replace variables. But the values passed in by `source.globalVars` will be serialized by JSON by default. So it cannot be replaced like `1 + 1` in the example above, at this time, we need to use [`source.define`](/configure/app/source/define).
|
@@ -4,13 +4,13 @@ sidebar_position: 9
|
|
4
4
|
---
|
5
5
|
# HTML Template
|
6
6
|
|
7
|
-
Modern.js provides **JSX syntax** and **HTML(EJS) syntax**
|
7
|
+
Modern.js provides **JSX syntax** and **HTML(EJS) syntax** to customize the HTML template.
|
8
8
|
|
9
|
-
## JSX
|
9
|
+
## JSX Syntax
|
10
10
|
|
11
|
-
Modern.js
|
11
|
+
According to Modern.js conventions, you can create a `Document.[jt]sx` file under `src/` or the entry directory and default export a component". The rendering result of this component can be used as the HTML template of the entry.
|
12
12
|
|
13
|
-
For example the following directory structure:
|
13
|
+
For example, consider the following directory structure:
|
14
14
|
|
15
15
|
```bash
|
16
16
|
.
|
@@ -24,44 +24,42 @@ For example the following directory structure:
|
|
24
24
|
└── modern-app-env.d.ts
|
25
25
|
```
|
26
26
|
|
27
|
-
`entry-a` will
|
27
|
+
`entry-a` will use the `Docoument.[jt]sx` file under the current entry as the template. If there is no `Document.[jt]sx` file under the current entry, like `entry-b`, it will look for the `Document.[jt]sx` file under the root directory.
|
28
28
|
|
29
|
-
If not,
|
29
|
+
If not found, default template will be used.
|
30
30
|
|
31
|
-
### HTML
|
31
|
+
### HTML Components
|
32
32
|
|
33
|
-
Modern.js provides
|
33
|
+
Modern.js provides some components for rendering pages to help developers generate templates. These components can be used from `@modern-js/runtime/document`:
|
34
34
|
|
35
35
|
```tsx
|
36
36
|
import { Html, Body, Root, Head, Scripts } from '@modern-js/runtime/document';
|
37
37
|
```
|
38
38
|
|
39
|
-
|
39
|
+
- `Html`: provide the ability of native HTML Element and and render necessary components that the developer did not add by default. `<Head>` and `<Body>` must exist, and other components can be assembled as needed.
|
40
40
|
|
41
|
-
- `
|
41
|
+
- `Body`: provide the ability of native Body Element and needs to contain the `<Root>` component internally. It also supports other elements as child elements at the same time, such as adding footers.
|
42
42
|
|
43
|
-
- `
|
43
|
+
- `Root`: the root node `<div id='root'></div>` to be rendered. The default id of the root node is `id = 'root'`. You can set `props.rootId` to change the id attribute. Child components can be added and will be rendered in the HTML template. After React rendering is complete, it will be overwritten and is generally used to implement global Loading.
|
44
44
|
|
45
|
-
- `
|
45
|
+
- `Head`: provide the ability of native Head Element and automatically fills in `<meta>` and `<Scripts>` components.
|
46
46
|
|
47
|
-
- `
|
47
|
+
- `Scripts`: provide the ability to adjust the position of script tags injected into the HTML during the build process. By default, it is placed in the `<Head>` component.
|
48
48
|
|
49
|
-
- `
|
49
|
+
- `Comment`: retain user-written comments like `<!-- gateway -->` and outputs them to the rendered HTML.
|
50
50
|
|
51
|
-
|
51
|
+
### Template Parameters
|
52
52
|
|
53
|
-
|
53
|
+
Because it is in JSX format, various variables can be used freely in the component to assign values to various custom components in `Document.[jt]sx`.
|
54
54
|
|
55
|
-
|
55
|
+
Modern.js also provides `DocumentContext` to provide some configuration and environment parameters for easy access. The main parameters are:
|
56
56
|
|
57
|
-
|
57
|
+
- `processEnv`: provides the `process.env` during the build.
|
58
|
+
- `config`: the configuration of the Modern.js project. Currently, only the output configuration is exposed.
|
59
|
+
- `entryName`: the current entry name.
|
60
|
+
- `templateParams`: parameters of HTML template (compatible with traditional templates, so it's not recommended for use).
|
58
61
|
|
59
|
-
|
60
|
-
- `config`: The configuration of the project, only `output` are exposed.
|
61
|
-
- `entryName`: current entry name.
|
62
|
-
- `templateParams`: HTML template params(in order to be compatible with traditional templates, it is not recommended to use).
|
63
|
-
|
64
|
-
### Examples
|
62
|
+
### Example
|
65
63
|
|
66
64
|
```tsx
|
67
65
|
import React, { useContext } from 'react';
|
@@ -102,7 +100,7 @@ export default function Document(): React.ReactElement {
|
|
102
100
|
}
|
103
101
|
```
|
104
102
|
|
105
|
-
The above JSX
|
103
|
+
The above JSX components will generate the following HTML template:
|
106
104
|
|
107
105
|
```html
|
108
106
|
<!DOCTYPE html>
|
@@ -149,22 +147,22 @@ The above JSX component will generate the following HTML template:
|
|
149
147
|
|
150
148
|
```
|
151
149
|
|
152
|
-
##
|
150
|
+
## HTML Syntax
|
153
151
|
|
154
|
-
Modern.js also supports HTML syntax. By default,
|
152
|
+
Modern.js also supports HTML syntax. By default, Modern.js application projects will include a built-in HTML template for generating HTML code.
|
155
153
|
|
156
|
-
Based on HTML syntax
|
154
|
+
Based on the HTML syntax template, Modern.js provides two ways to customize the template: **custom HTML fragments** and **custom the whole HTML**.
|
157
155
|
|
158
156
|
### Custom HTML Fragments
|
159
157
|
|
160
|
-
|
158
|
+
Under the application root directory, create the `config/html/` directory, which supports the creation of four types of HTML fragments:
|
161
159
|
|
162
160
|
- `top.html`
|
163
161
|
- `head.html`
|
164
162
|
- `body.html`
|
165
163
|
- `bottom.html`
|
166
164
|
|
167
|
-
**These fragments will be injected into the default HTML template.**
|
165
|
+
**These fragments will be injected into the default HTML template according to their positions.**
|
168
166
|
|
169
167
|
```html
|
170
168
|
<!DOCTYPE html>
|
@@ -189,44 +187,44 @@ In the application root directory, create the `config/html/` directory, which su
|
|
189
187
|
</html>
|
190
188
|
```
|
191
189
|
|
192
|
-
HTML
|
190
|
+
HTML fragments support the use of [Lodash template](https://lodash.com/docs/4.17.15#template).
|
193
191
|
|
194
|
-
For example, insert
|
192
|
+
For example, to insert an external script in `body.html`:
|
195
193
|
|
196
194
|
```html title="config/html/body.html"
|
197
195
|
<script src="//example.com/assets/a.js"></script>
|
198
196
|
```
|
199
197
|
|
200
198
|
:::info
|
201
|
-
The implementation of
|
199
|
+
The implementation of custom HTML fragments is to merge the fragments with the built-in template. Since the `<title>` already exists in the default template, the title tag in the custom HTML template will not take effect. Please use [html.title](/configure/app/html/title) to modify the page title.
|
202
200
|
|
203
201
|
:::
|
204
202
|
|
205
|
-
###
|
203
|
+
### Custom the entire HTML Template
|
206
204
|
|
207
|
-
In some cases, HTML
|
205
|
+
In some cases, HTML fragments may be is not better meet the customization requirements. Modern.js provides a fully customized way.
|
208
206
|
|
209
|
-
:::caution
|
210
|
-
It is not recommended to override the default HTML template
|
207
|
+
:::caution Note
|
208
|
+
It is generally not recommended to directly override the default HTML template, as some functional options may be lost. If it is truly necessary to customize the entire HTML template, it is recommended to modify based on the built-in template as needed.
|
211
209
|
|
212
210
|
:::
|
213
211
|
|
214
|
-
|
212
|
+
Under the `config/html/` directory, create an `index.html` file that will replace the default HTML template.
|
215
213
|
|
216
214
|
:::info
|
217
|
-
The
|
215
|
+
The default HTML template can be viewed in `node_modules/.modern-js/${entryName}/index.html`.
|
218
216
|
|
219
217
|
:::
|
220
218
|
|
221
|
-
### Template
|
219
|
+
### Template Parameters
|
222
220
|
|
223
|
-
The parameters used in the template can be defined by the [html.templateParameters](/configure/app/html/template-parameters).
|
221
|
+
The parameters used in the template can be defined by the [html.templateParameters](/configure/app/html/template-parameters) configuration.
|
224
222
|
|
225
223
|
### Config By Entry
|
226
224
|
|
227
|
-
The
|
225
|
+
The HTML fragments in the `config/html/` directory take effect for all entries in the application. If you want to customize HTML fragments by entry, you can create a directory named after the **entry name** under the `config/html/` directory, and then customize the HTML fragments in this directory.
|
228
226
|
|
229
|
-
For example, the following HTML
|
227
|
+
For example, the following HTML fragments are only effective for the `entry1` entry:
|
230
228
|
|
231
229
|
```bash
|
232
230
|
.
|
@@ -4,11 +4,11 @@ sidebar_position: 6
|
|
4
4
|
---
|
5
5
|
# Mock
|
6
6
|
|
7
|
-
Modern.js
|
7
|
+
Modern.js allows you to easily generate mock data so that the front-end can develop independently without depending on the back-end API.
|
8
8
|
|
9
|
-
## Mock
|
9
|
+
## Enabling Mock
|
10
10
|
|
11
|
-
By convention, when there is `index.[jt]s` in the `config/mock/` directory,
|
11
|
+
By convention, when there is an `index.[jt]s` in the `config/mock/` directory, mock data will be automatically enabled:
|
12
12
|
|
13
13
|
```bash
|
14
14
|
.
|
@@ -22,7 +22,7 @@ By convention, when there is `index.[jt]s` in the `config/mock/` directory, the
|
|
22
22
|
|
23
23
|
## Writing Mock Files
|
24
24
|
|
25
|
-
|
25
|
+
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`:
|
26
26
|
|
27
27
|
```js
|
28
28
|
export default {
|
@@ -40,11 +40,11 @@ export default {
|
|
40
40
|
};
|
41
41
|
```
|
42
42
|
|
43
|
-
|
43
|
+
When you access `http://localhost:8080/api/getInfo`, the API will return JSON `{ "data": [1, 2, 3, 4] }`.
|
44
44
|
|
45
45
|
## Return Random Data
|
46
46
|
|
47
|
-
Libraries such as [Mock.js](https://github.com/nuysoft/Mock/wiki/Getting-Started) can be used in `config/mock/index.js` to generate random data
|
47
|
+
Libraries such as [Mock.js](https://github.com/nuysoft/Mock/wiki/Getting-Started) can be used in `config/mock/index.js` to generate random data. For example:
|
48
48
|
|
49
49
|
```js
|
50
50
|
const Mock = require('mockjs');
|
@@ -56,16 +56,16 @@ module.exports = {
|
|
56
56
|
};
|
57
57
|
```
|
58
58
|
|
59
|
-
:::info Other Mock
|
59
|
+
:::info Other Mock Libraries
|
60
60
|
|
61
61
|
- [Chancejs](https://github.com/chancejs/chancejs)
|
62
62
|
- [Mock](https://github.com/nuysoft/Mock/wiki/Getting-Started)
|
63
63
|
|
64
64
|
:::
|
65
65
|
|
66
|
-
## Delayed
|
66
|
+
## Delayed Response
|
67
67
|
|
68
|
-
-
|
68
|
+
- You can do this by using the browser's "weak connection simulation" feature.
|
69
69
|
- Delays can be set via `setTimeout`, for example:
|
70
70
|
|
71
71
|
```ts
|
@@ -92,7 +92,7 @@ export const config = {
|
|
92
92
|
}
|
93
93
|
```
|
94
94
|
|
95
|
-
Currently only the `enable` configuration is supported,
|
95
|
+
Currently only the `enable` configuration is supported, which allows developers to control whether to execute mock.
|
96
96
|
|
97
97
|
:::note
|
98
98
|
After modifying `config`, there is no need to restart the service, which will take effect immediately.
|
@@ -1,10 +1,10 @@
|
|
1
1
|
---
|
2
|
-
title:
|
2
|
+
title: Develop Plugins
|
3
3
|
sidebar_position: 3
|
4
4
|
---
|
5
|
-
# How to
|
5
|
+
# How to Develop Plugins
|
6
6
|
|
7
|
-
The previous section introduced the Hook models used by Modern.js plugins, while this section describes how to
|
7
|
+
The previous section introduced the Hook models used by Modern.js plugins, while this section describes how to develop plugins.
|
8
8
|
|
9
9
|
## Implementing a Plugin
|
10
10
|
|
@@ -115,7 +115,7 @@ Please refer to [Extending Hooks](/guides/topic-detail/framework-plugin/extend)
|
|
115
115
|
|
116
116
|
### Plugin Configuration
|
117
117
|
|
118
|
-
**It is recommended to
|
118
|
+
**It is recommended to develop plugins in the form of functions**, so that plugins can receive configuration options through function parameters:
|
119
119
|
|
120
120
|
```ts
|
121
121
|
import type { CliPlugin } from '@modern-js/core';
|
@@ -33,7 +33,7 @@ Among them, the CLI plugin is the main running flow control model in Modern.js,
|
|
33
33
|
|
34
34
|
## What Plugins Can Do
|
35
35
|
|
36
|
-
All of Modern.js's features are implemented through this set of plugins, which means that all of Modern.js's capabilities are open to developers. Developers can
|
36
|
+
All of Modern.js's features are implemented through this set of plugins, which means that all of Modern.js's capabilities are open to developers. Developers can develop plugins to extend more functionality and adapt to complex scenarios, including but not limited to:
|
37
37
|
|
38
38
|
- Registering commands
|
39
39
|
- Modifying Modern.js configuration and validation schema
|
@@ -4,21 +4,21 @@ sidebar_position: 2
|
|
4
4
|
|
5
5
|
# CLI FAQ
|
6
6
|
|
7
|
-
###
|
7
|
+
### Unable to pass command line arguments correctly when using pnpm?
|
8
8
|
|
9
|
-
|
9
|
+
There are some differences between pnpm v6 and pnpm v7 in how they execute commands, and the following should be noted:
|
10
10
|
|
11
11
|
pnpm v7:
|
12
12
|
|
13
|
-
When using pnpm to
|
13
|
+
When using pnpm to call the commands in `package.json`, if you need to pass parameters to pnpm, you need to put the parameters before the command.
|
14
14
|
|
15
|
-
For example,
|
15
|
+
For example, using the pnpm `--filter` parameter to run the prepare command:
|
16
16
|
|
17
17
|
```bash
|
18
18
|
pnpm run --filter "./packages/**" prepare
|
19
19
|
```
|
20
20
|
|
21
|
-
If you need to pass parameters to
|
21
|
+
If you need to pass parameters to the command, you need to put the parameters after the command.
|
22
22
|
|
23
23
|
For example, in the following `package.json` configuration:
|
24
24
|
|
@@ -30,7 +30,7 @@ For example, in the following `package.json` configuration:
|
|
30
30
|
}
|
31
31
|
```
|
32
32
|
|
33
|
-
The way to
|
33
|
+
The way to pass parameters when running the command is:
|
34
34
|
|
35
35
|
```bash
|
36
36
|
pnpm run command --options
|
@@ -48,16 +48,16 @@ In the following `package.json` configuration:
|
|
48
48
|
}
|
49
49
|
```
|
50
50
|
|
51
|
-
If you need to
|
51
|
+
If you need to run `modern command --option`:
|
52
52
|
|
53
|
-
When using pnpm, you need to
|
53
|
+
When using pnpm, you need to run `pnpm run command -- --option`.
|
54
54
|
|
55
|
-
This is because pnpm
|
55
|
+
This is because pnpm's handling of command parameters is different from that of Yarn, but similar to that of npm: when the `--` string is not added, the parameters passed are for pnpm; when the `--` string is used, the parameters passed are for running the script.
|
56
56
|
|
57
|
-
In the above example the
|
57
|
+
In the above example, the `--option` parameter is passed to `modern command`. If you run `pnpm run command --option`, the `--option` parameter will be passed to pnpm.
|
58
58
|
|
59
|
-
|
59
|
+
In summary:
|
60
60
|
|
61
|
-
**When using pnpm v7, if
|
61
|
+
**When using pnpm v7, if passing parameters to pnpm, the parameters need to be placed before the command.**
|
62
62
|
|
63
|
-
**When using pnpm v6, if the
|
63
|
+
**When using pnpm v6, if the parameters are passed to pnpm, `--` is not required; if the parameters are passed to the script, the `--` string needs to be added.**
|
@@ -6,15 +6,15 @@ sidebar_position: 1
|
|
6
6
|
|
7
7
|
### How to check the actual installed version of a dependency in the project?
|
8
8
|
|
9
|
-
You can use the `ls` command
|
9
|
+
You can use the `ls` command provided by the package manager to view the version of the dependency in the project.
|
10
10
|
|
11
|
-
|
11
|
+
Here are some basic examples. For detailed usage, please refer to the documentation of each package manager.
|
12
12
|
|
13
13
|
**npm / yarn**
|
14
14
|
|
15
|
-
For projects using npm or yarn, the `npm ls` command
|
15
|
+
For projects using npm or yarn, you can use the `npm ls` command.
|
16
16
|
|
17
|
-
For example,
|
17
|
+
For example, running `npm ls @modern-js/core` will show the following result:
|
18
18
|
|
19
19
|
```
|
20
20
|
project
|
@@ -24,9 +24,9 @@ project
|
|
24
24
|
|
25
25
|
**pnpm**
|
26
26
|
|
27
|
-
For projects using pnpm, you can use `pnpm ls` command.
|
27
|
+
For projects using pnpm, you can use the `pnpm ls` command.
|
28
28
|
|
29
|
-
For example,
|
29
|
+
For example, running `pnpm ls @modern-js/core --depth Infinity` will show the following result:
|
30
30
|
|
31
31
|
```
|
32
32
|
devDependencies:
|
@@ -36,9 +36,9 @@ devDependencies:
|
|
36
36
|
|
37
37
|
---
|
38
38
|
|
39
|
-
### The engine "node" is incompatible
|
39
|
+
### Getting "The engine "node" is incompatible" error during dependency installation?
|
40
40
|
|
41
|
-
If the following error message
|
41
|
+
If you encounter the following error message during dependency installation, it means that the current environment is using a Node.js version that is too low, and you need to upgrade Node.js to a higher version.
|
42
42
|
|
43
43
|
```bash
|
44
44
|
The engine "node" is incompatible with this module.
|
@@ -46,11 +46,11 @@ The engine "node" is incompatible with this module.
|
|
46
46
|
Expected version ">=14.17.6". Got "12.20.1"
|
47
47
|
```
|
48
48
|
|
49
|
-
When using Modern.js, it is recommended to use [Node.js 14.x](https://nodejs.org/download/release/latest-v14.x/) or [Node.js 16.x](https://nodejs.org/download/release/latest-v16.x/).
|
49
|
+
When using Modern.js, it is recommended to use the latest version of [Node.js 14.x](https://nodejs.org/download/release/latest-v14.x/) or [Node.js 16.x](https://nodejs.org/download/release/latest-v16.x/).
|
50
50
|
|
51
|
-
If the Node.js version
|
51
|
+
If the Node.js version of the current environment is lower than the above requirement, you can use tools such as [nvm](https://github.com/nvm-sh/nvm) or [fnm](https://github.com/Schniz/fnm) to switch versions.
|
52
52
|
|
53
|
-
Here
|
53
|
+
Here is an example of using nvm:
|
54
54
|
|
55
55
|
```
|
56
56
|
# Install Node.js v14
|
@@ -63,13 +63,13 @@ nvm use 14
|
|
63
63
|
nvm default 14
|
64
64
|
```
|
65
65
|
|
66
|
-
|
66
|
+
For local development environments, it is recommended to use [fnm](https://github.com/Schniz/fnm), which has better performance than nvm and has similar usage.
|
67
67
|
|
68
68
|
---
|
69
69
|
|
70
|
-
### ReactNode type error after upgrading dependencies?
|
70
|
+
### Getting a ReactNode type error after upgrading dependencies?
|
71
71
|
|
72
|
-
|
72
|
+
After upgrading the dependencies of the project, if the following type error occurs, it means that the wrong version of `@types/react` is installed in the project.
|
73
73
|
|
74
74
|
```bash
|
75
75
|
The types returned by 'render()' are incompatible between these types.
|
@@ -77,9 +77,9 @@ Type 'React.ReactNode' is not assignable to type 'import("/node_modules/@types/r
|
|
77
77
|
Type '{}' is not assignable to type 'ReactNode'.
|
78
78
|
```
|
79
79
|
|
80
|
-
The reason for this problem is that the ReactNode type
|
80
|
+
The reason for this problem is that the ReactNode type definition in React 18 is different from that in React 16/17. If there are multiple different versions of `@types/react` in the project, a ReactNode type conflict will occur, resulting in the above error.
|
81
81
|
|
82
|
-
The solution is to lock `@types/react` and `@types/react-dom` in the project to a unified version, such as `v17`.
|
82
|
+
The solution is to lock the `@types/react` and `@types/react-dom` in the project to a unified version, such as `v17`.
|
83
83
|
|
84
84
|
```json
|
85
85
|
{
|
@@ -88,23 +88,23 @@ The solution is to lock `@types/react` and `@types/react-dom` in the project to
|
|
88
88
|
}
|
89
89
|
```
|
90
90
|
|
91
|
-
For
|
91
|
+
For methods of locking dependency versions, please refer to [Lock nested dependency](/guides/get-started/upgrade.html#lock-nested-dependency).
|
92
92
|
|
93
93
|
---
|
94
94
|
|
95
|
-
###
|
95
|
+
### Getting peer dependencies warnings in the console after running `pnpm install`?
|
96
96
|
|
97
|
-
The reason for this warning is that the version range of peer dependencies declared by some third-party npm packages
|
97
|
+
The reason for this warning is that the version range of peer dependencies declared by some third-party npm packages is inconsistent with the version range installed in Modern.js.
|
98
98
|
|
99
|
-
In most cases,
|
99
|
+
In most cases, peer dependencies warnings will not affect the project operation and do not need to be processed separately. Please ignore the relevant warnings.
|
100
100
|
|
101
101
|
---
|
102
102
|
|
103
|
-
### What is the minimum
|
103
|
+
### What is the minimum supported version of React for the Modern.js framework?
|
104
104
|
|
105
|
-
The recommended React version for
|
105
|
+
**The recommended React version for the Modern.js framework is >= 18.0.0**, and different functions have different requirements for the React version.
|
106
106
|
|
107
|
-
- If you are using React 17, some framework
|
108
|
-
- If you
|
107
|
+
- If you are using React 17, some framework functions will not be available, such as Steaming SSR, because it relies on new features provided by React 18.
|
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
|
-
In
|
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.
|