@modern-js/main-doc 0.0.0-next-1686625388611 → 0.0.0-next-1686651393235
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 +6 -2
- package/docs/en/apis/app/commands.mdx +5 -5
- 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/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/usage.mdx +5 -5
- package/docs/en/guides/advanced-features/code-split.mdx +1 -1
- package/docs/en/guides/advanced-features/testing.mdx +8 -7
- package/docs/en/guides/advanced-features/web-server.mdx +16 -16
- package/docs/en/guides/basic-features/env-vars.mdx +14 -14
- package/docs/en/guides/basic-features/mock.mdx +10 -10
- package/docs/zh/apis/app/commands.mdx +4 -4
- 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/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/usage.mdx +9 -9
- package/docs/zh/guides/advanced-features/testing.mdx +4 -4
- package/docs/zh/guides/basic-features/mock.mdx +2 -2
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @modern-js/main-doc
|
|
2
2
|
|
|
3
|
-
## 0.0.0-next-
|
|
3
|
+
## 0.0.0-next-1686651393235
|
|
4
4
|
|
|
5
5
|
### Patch Changes
|
|
6
6
|
|
|
@@ -24,6 +24,10 @@
|
|
|
24
24
|
|
|
25
25
|
docs(main): 更新 HTML 模板文档
|
|
26
26
|
|
|
27
|
+
- 5cba7c46b7: docs(main): update bff config doc
|
|
28
|
+
|
|
29
|
+
docs(main): 更新 BFF 配置文档
|
|
30
|
+
|
|
27
31
|
- ef865f285b: docs(main): update config server doc
|
|
28
32
|
|
|
29
33
|
docs(main): 更新 server 配置文档
|
|
@@ -31,7 +35,7 @@
|
|
|
31
35
|
- 8bb0e146f2: chore: review code split docs
|
|
32
36
|
chore: 审查代码分割文档
|
|
33
37
|
- Updated dependencies [7e6fb5fc16]
|
|
34
|
-
- @modern-js/builder-doc@0.0.0-next-
|
|
38
|
+
- @modern-js/builder-doc@0.0.0-next-1686651393235
|
|
35
39
|
|
|
36
40
|
## 2.22.1
|
|
37
41
|
|
|
@@ -112,6 +112,7 @@ For example, add application entry, enable some optional features such as Tailwi
|
|
|
112
112
|
Usage: modern new [options]
|
|
113
113
|
|
|
114
114
|
Options:
|
|
115
|
+
--lang <lang> set the language (zh or en) for the new command.
|
|
115
116
|
-d, --debug using debug mode to log something (default: false)
|
|
116
117
|
-c, --config <config> set default generator config(json string)
|
|
117
118
|
--dist-tag <tag> use specified tag version for its generator
|
|
@@ -136,7 +137,7 @@ In the project, execute the `new` command to enable features as follows:
|
|
|
136
137
|
|
|
137
138
|
```bash
|
|
138
139
|
$ npx modern new
|
|
139
|
-
? Please select the operation you want: Enable
|
|
140
|
+
? Please select the operation you want: Enable Features
|
|
140
141
|
? Please select the feature name: (Use arrow keys)
|
|
141
142
|
❯ Enable Tailwind CSS
|
|
142
143
|
Enable BFF
|
|
@@ -162,11 +163,10 @@ Usage: modern serve [options]
|
|
|
162
163
|
Options:
|
|
163
164
|
-c --config <config> configuration file path, which can be a relative path or an absolute path
|
|
164
165
|
-h, --help show command help
|
|
165
|
-
--web-only only run web service
|
|
166
166
|
--api-only only run API service
|
|
167
167
|
```
|
|
168
168
|
|
|
169
|
-
By default, the project will run in `localhost:8080`, you can modify the
|
|
169
|
+
By default, the project will run in `localhost:8080`, you can modify the server port number with `server.port`:
|
|
170
170
|
|
|
171
171
|
```js
|
|
172
172
|
export default defineConfig({
|
|
@@ -180,7 +180,7 @@ export default defineConfig({
|
|
|
180
180
|
|
|
181
181
|
Execute the command `npx modern upgrade` in the project, by default, dependencies in the `package.json` are updated to the latest version.
|
|
182
182
|
|
|
183
|
-
```
|
|
183
|
+
```bash
|
|
184
184
|
Usage: modern upgrade [options]
|
|
185
185
|
|
|
186
186
|
Options:
|
|
@@ -194,7 +194,7 @@ Options:
|
|
|
194
194
|
|
|
195
195
|
The `modern inspect` command is used to view the [Modern.js Builder config](https://modernjs.dev/builder/en/guide/basic/builder-config.html) and webpack config of the project.
|
|
196
196
|
|
|
197
|
-
```
|
|
197
|
+
```bash
|
|
198
198
|
Usage: modern inspect [options]
|
|
199
199
|
|
|
200
200
|
Options:
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
Path rewriting can also be performed here, such as proxying the request sent to `localhost:8080/api/topics` to `https://cnodejs.org/api/v1/topics`.
|
|
2
|
+
|
|
3
|
+
```js title="modern.server-runtime.config.js"
|
|
4
|
+
import { defineConfig } from '@modern-js/app-tools/server';
|
|
5
|
+
export default defineConfig({
|
|
6
|
+
bff: {
|
|
7
|
+
proxy: {
|
|
8
|
+
'/api': {
|
|
9
|
+
target: 'https://cnodejs.org',
|
|
10
|
+
pathRewrite: { '/api/topics': '/api/v1/topics' },
|
|
11
|
+
changeOrigin: true,
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
BFF Proxy uses the powerful [http-proxy-middleware](https://github.com/chimurai/http-proxy-middleware). For more advanced usage, please refer to its [documentation](https://github.com/chimurai/http-proxy-middleware#options).
|
|
@@ -7,13 +7,13 @@ title: enableHandleWeb
|
|
|
7
7
|
- **Type:** `boolean`
|
|
8
8
|
- **Default:** `false`
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
First you need to enable the "BFF" function using [new](/apis/app/commands#modern-new) command.
|
|
12
|
-
:::
|
|
10
|
+
import EnableBFFCaution from "@site-docs-en/components/enable-bff-caution";
|
|
13
11
|
|
|
14
|
-
|
|
12
|
+
<EnableBFFCaution />
|
|
15
13
|
|
|
16
|
-
|
|
14
|
+
By default, the BFF service can only handle requests for BFF APIs.
|
|
15
|
+
|
|
16
|
+
When this value is set to `true`, page request will also pass through BFF, and the default logic for page rendering built in Modern.js will run as the last middleware of the BFF service.
|
|
17
17
|
|
|
18
18
|
```ts title="modern.config.ts"
|
|
19
19
|
export default defineConfig({
|
|
@@ -7,18 +7,18 @@ sidebar_label: prefix
|
|
|
7
7
|
- **Type:** `string`
|
|
8
8
|
- **Default:** `/api`
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
First you need to enable the "BFF" function using [new](/apis/app/commands#modern-new) command.
|
|
12
|
-
:::
|
|
10
|
+
import EnableBFFCaution from "@site-docs-en/components/enable-bff-caution";
|
|
13
11
|
|
|
14
|
-
|
|
12
|
+
<EnableBFFCaution />
|
|
13
|
+
|
|
14
|
+
By default, the prefix for accessing routes in the BFF API directory is `/api`, as shown in the following directory structure:
|
|
15
15
|
|
|
16
16
|
```bash
|
|
17
17
|
api
|
|
18
18
|
└── hello.ts
|
|
19
19
|
```
|
|
20
20
|
|
|
21
|
-
The corresponding
|
|
21
|
+
The route corresponding to `api/hello.ts` when accessed is `localhost:8080/api/hello`.
|
|
22
22
|
|
|
23
23
|
This configuration option can modify the default route prefix:
|
|
24
24
|
|
|
@@ -30,4 +30,4 @@ export default defineConfig({
|
|
|
30
30
|
});
|
|
31
31
|
```
|
|
32
32
|
|
|
33
|
-
The corresponding `api/hello.ts`
|
|
33
|
+
The corresponding route for `api/hello.ts` when accessed is `localhost:8080/api-demo/hello`.
|
|
@@ -7,16 +7,13 @@ sidebar_label: proxy
|
|
|
7
7
|
- **Type:** `Record<string, string>`
|
|
8
8
|
- **Default:** `{}`
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
First you need to enable the "BFF" function using [new](/apis/app/commands#modern-new) command.
|
|
10
|
+
import EnableBFFCaution from "@site-docs-en/components/enable-bff-caution";
|
|
12
11
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
With simple configuration, no code is required, Modern.js automatically forwards requests. Requests sent to Modern.js BFF server are proxied to the specified service.
|
|
12
|
+
<EnableBFFCaution />
|
|
16
13
|
|
|
17
|
-
|
|
14
|
+
With simple configuration, Modern.js can automatically proxy requests sent to the BFF server to the specified service.
|
|
18
15
|
|
|
19
|
-
Add the following configuration to `modern.server-runtime.config.
|
|
16
|
+
Add the following configuration to `modern.server-runtime.config.js` to enable proxy:
|
|
20
17
|
|
|
21
18
|
```ts title="modern.server-runtime.config.ts"
|
|
22
19
|
import { defineConfig } from '@modern-js/app-tools/server';
|
|
@@ -29,41 +26,31 @@ export default defineConfig({
|
|
|
29
26
|
});
|
|
30
27
|
```
|
|
31
28
|
|
|
32
|
-
Assuming
|
|
29
|
+
Assuming the address of Modern.js BFF server is `localhost:8080`, all requests starting with `api` will be proxied to `https://cnodejs.org`, for example, the request to `localhost:8080/api/v1/topics` will be proxied to `https://cnodejs.org/api/v1/topics`.
|
|
33
30
|
|
|
34
|
-
|
|
31
|
+
import BFFProxyPathRewrite from "@site-docs/components/bff-proxy-path-rewrite";
|
|
35
32
|
|
|
36
|
-
|
|
37
|
-
import { defineConfig } from '@modern-js/app-tools/server';
|
|
38
|
-
export default defineConfig({
|
|
39
|
-
bff: {
|
|
40
|
-
proxy: {
|
|
41
|
-
'/api': {
|
|
42
|
-
target: 'https://cnodejs.org',
|
|
43
|
-
pathRewrite: { '/api/topics': '/api/v1/topics' },
|
|
44
|
-
changeOrigin: true,
|
|
45
|
-
},
|
|
46
|
-
},
|
|
47
|
-
},
|
|
48
|
-
});
|
|
49
|
-
```
|
|
33
|
+
<BFFProxyPathRewrite />
|
|
50
34
|
|
|
51
|
-
Unlike [dev.proxy](/configure/app/dev/proxy), the proxy
|
|
35
|
+
Unlike [dev.proxy](/configure/app/dev/proxy), the proxy here only applies to requests entering the BFF/API service; at the same time, this configuration can not only be used in the development environment, but also proxies corresponding requests in the production environment.
|
|
52
36
|
|
|
53
|
-
|
|
37
|
+
import BFFProxyPrinciple from "@site-docs/components/bff-proxy-principle";
|
|
54
38
|
|
|
55
|
-
|
|
39
|
+
<BFFProxyPrinciple />
|
|
56
40
|
|
|
57
|
-
|
|
41
|
+
## Common Usage
|
|
58
42
|
|
|
59
|
-
|
|
43
|
+
### Solving Cross-Domain Issues for APIs
|
|
44
|
+
|
|
45
|
+
During project development, cross-domain issues are often encountered because web pages and API services are not deployed under the same domain name.
|
|
46
|
+
There are many ways to solve cross-domain issues, and here we can easily solve them using `bff.proxy`.
|
|
60
47
|
|
|
61
48
|
:::info
|
|
62
|
-
|
|
49
|
+
Under BFF proxy mode, if you don't need to write BFF code, the API directory can be deleted; BFF proxy will still be enabled.
|
|
63
50
|
|
|
64
51
|
:::
|
|
65
52
|
|
|
66
|
-
As shown below, in
|
|
53
|
+
As shown below, the following configuration in `modern.server-runtime.config.ts` will proxy all web page requests starting with `/api` to a service on another domain with the same domain.
|
|
67
54
|
|
|
68
55
|
```ts title="modern.server-runtime.config.ts"
|
|
69
56
|
export default defineServerConfig({
|
|
@@ -74,3 +61,5 @@ export default defineServerConfig({
|
|
|
74
61
|
},
|
|
75
62
|
};
|
|
76
63
|
```
|
|
64
|
+
|
|
65
|
+
|
|
@@ -12,7 +12,7 @@ The compile configuration can be configured in two places:
|
|
|
12
12
|
- `package.json` file
|
|
13
13
|
|
|
14
14
|
:::info
|
|
15
|
-
Configurations in both package.json and modern.config.ts file are not supported for the same configuration. Configuration in modern.config.ts is recommended.
|
|
15
|
+
Configurations in both `package.json` and `modern.config.ts` file are not supported for the same configuration. Configuration in `modern.config.ts` is recommended.
|
|
16
16
|
:::
|
|
17
17
|
|
|
18
18
|
Server runtime configuration can be configured in the `modern.server-runtime.config.(ts|js|mjs)` file in the root path.
|
|
@@ -54,7 +54,7 @@ When using Rspack as the bundler, due to some differences in configuration types
|
|
|
54
54
|
|
|
55
55
|
### modern.config.js
|
|
56
56
|
|
|
57
|
-
If you are developing a non-TypeScript project, you can use the configuration file in
|
|
57
|
+
If you are developing a non-TypeScript project, you can use the configuration file in `.js` format:
|
|
58
58
|
|
|
59
59
|
```js title="modern.config.js"
|
|
60
60
|
export default {
|
|
@@ -66,7 +66,7 @@ export default {
|
|
|
66
66
|
};
|
|
67
67
|
```
|
|
68
68
|
|
|
69
|
-
You can also
|
|
69
|
+
You can also configure depending on your environment with `process.env.NODE_ENV`:
|
|
70
70
|
|
|
71
71
|
```js title="modern.config.js"
|
|
72
72
|
export default {
|
|
@@ -122,7 +122,7 @@ export default defineConfig(async ({ env, command }) => {
|
|
|
122
122
|
|
|
123
123
|
You can specify the name of the configuration file using the `--config` option.
|
|
124
124
|
|
|
125
|
-
For example, if you need to use the `modern.prod.config.js` file when running build
|
|
125
|
+
For example, if you need to use the `modern.prod.config.js` file when running `build`, you can add the following scripts to `package.json`:
|
|
126
126
|
|
|
127
127
|
```json title="package.json"
|
|
128
128
|
{
|
|
@@ -155,7 +155,7 @@ In addition to configuration files, configuration options can also be set the `m
|
|
|
155
155
|
}
|
|
156
156
|
```
|
|
157
157
|
|
|
158
|
-
Due to the limitation of the JSON file format, only simple types such as numbers, strings, boolean values, arrays, etc. can be defined in `package.json`. When we need to set the value of the function type, it is recommended to
|
|
158
|
+
Due to the limitation of the JSON file format, only simple types such as numbers, strings, boolean values, arrays, etc. can be defined in `package.json`. When we need to set the value of the function type, it is recommended to do so in the Modern.js configuration file.
|
|
159
159
|
|
|
160
160
|
### Note
|
|
161
161
|
|
|
@@ -4,7 +4,7 @@ sidebar_position: 6
|
|
|
4
4
|
---
|
|
5
5
|
# Code Splitting
|
|
6
6
|
|
|
7
|
-
Code splitting is a common
|
|
7
|
+
Code splitting is a common way to optimize frontend resource loading. This article will introduce the three types of code splitting supported by Modern.js:
|
|
8
8
|
|
|
9
9
|
:::info
|
|
10
10
|
When using Modern.js [Conventional routing](/guides/basic-features/routes#conventional-routing). By default, code splitting is done according to the routing component, so you don't need to do it yourself.
|
|
@@ -6,16 +6,16 @@ title: Testing
|
|
|
6
6
|
|
|
7
7
|
Modern.js integrates the testing capabilities of [Jest](https://jestjs.io/) by default.
|
|
8
8
|
|
|
9
|
-
First need to execute `pnpm run new` enable [unit test/integration test] features:
|
|
9
|
+
First need to execute `pnpm run new` to enable [unit test/integration test] features:
|
|
10
10
|
|
|
11
|
-
```
|
|
11
|
+
```bash
|
|
12
12
|
? Please select the operation you want: Enable features
|
|
13
13
|
? Please select the feature name: Enable Unit Test / Integration Test
|
|
14
14
|
```
|
|
15
15
|
|
|
16
|
-
After executing the above command, the `"test": "modern test"` command will be
|
|
16
|
+
After executing the above command, the `"test": "modern test"` command will be added in `package.json` automatically.
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
After registering the `@modern-js/plugin-testing` plugin in `modern.config.ts`, you can use the testing features:
|
|
19
19
|
|
|
20
20
|
```ts title="modern.config.ts"
|
|
21
21
|
import testPlugin from '@modern-js/plugin-testing';
|
|
@@ -35,12 +35,13 @@ If you need to customize the test directory, you can configure it with [tools.je
|
|
|
35
35
|
|
|
36
36
|
Modern.js test support [testing-library](https://testing-library.com/docs/). API can be imported from `@modern-js/runtime/testing`.
|
|
37
37
|
|
|
38
|
-
```
|
|
38
|
+
```ts
|
|
39
39
|
import { render, screen } from '@modern-js/runtime/testing';
|
|
40
40
|
```
|
|
41
41
|
|
|
42
|
-
Other
|
|
42
|
+
Other testing APIs supported by Modern.js can be referred to [here](/apis/app/runtime/testing/cleanup).
|
|
43
43
|
|
|
44
44
|
## transform
|
|
45
45
|
|
|
46
|
-
Modern.js
|
|
46
|
+
By default, Modern.js testing uses [babel-jest](https://www.npmjs.com/package/babel-jest) for source code compilation. If you need to use [ts-jest](https://github.com/kulshekhar/ts-jest), you can configure it through [testing.transform](/configure/app/testing/transformer).
|
|
47
|
+
|
|
@@ -4,15 +4,15 @@ sidebar_position: 3
|
|
|
4
4
|
---
|
|
5
5
|
# Custom Web Server
|
|
6
6
|
|
|
7
|
-
As a client
|
|
7
|
+
As a client-centric development framework, Modern.js has limited customization capabilities on the server side. However, in some development scenarios, special server-level logic needs to be customized, such as user authentication, request preprocessing, and adding page rendering skeletons.
|
|
8
8
|
|
|
9
|
-
Some developers may be wondering, Modern.js already provides [BFF](/guides/advanced-features/bff/function.html), why
|
|
9
|
+
Some developers may be wondering, Modern.js already provides [BFF](/guides/advanced-features/bff/function.html), why you need **Custom Web Server**.
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
The reason is that by default, page routing does not go through BFF, it has no way to provide server-side custom logic for page access. The reason for this design is that we do not want the service that controls the page to be bound to the BFF service, this is to avoid the BFF framework restricting how the page is deployed.
|
|
12
12
|
|
|
13
|
-
For example, hosting pages separately from BFF, deploying page services to non-Node environments,
|
|
13
|
+
For example, hosting pages separately from BFF, deploying page services to non-Node environments, customizing for deployment platforms, etc.
|
|
14
14
|
|
|
15
|
-
For the above reasons, Modern.js provides three ways that projects can customize server
|
|
15
|
+
For the above reasons, Modern.js provides three ways that projects can customize server-level capabilities progressively according to their needs.
|
|
16
16
|
|
|
17
17
|
:::warning
|
|
18
18
|
The three extension methods cannot work at the same time, and developers need to choose the appropriate method according to the scenario.
|
|
@@ -20,11 +20,11 @@ The three extension methods cannot work at the same time, and developers need to
|
|
|
20
20
|
|
|
21
21
|
## Extending Web Server with API
|
|
22
22
|
|
|
23
|
-
The first way is to customize the server
|
|
23
|
+
The first way is to customize the server-side at a specific lifecycle through the server-side runtime API provided by Modern.js. The purpose of providing this way is that in some cases, developers do not need to control the full Web Server, but only need to add server-level logic.
|
|
24
24
|
|
|
25
|
-
Because the full web server cannot be controlled this way, and the extension logic **only takes effect when the page is requested**. Therefore, it is relatively simple to apply
|
|
25
|
+
Because the full web server cannot be controlled this way, and the extension logic **only takes effect when the page is requested**. Therefore, it is relatively simple to apply server-level logic, and you do not want to create additional BFFs or BFFs and pages without common server-level logic scenarios.
|
|
26
26
|
|
|
27
|
-
You can
|
|
27
|
+
You can run the'pnpm run new 'command in the project root directory to enable the "Custom Web Serve" function:
|
|
28
28
|
|
|
29
29
|
```bash
|
|
30
30
|
? Please select the operation you want: Create Element
|
|
@@ -47,7 +47,7 @@ After the function is turned on, the `server/index.ts` file will be automaticall
|
|
|
47
47
|
|
|
48
48
|
The Hook provided by Modern.js is used to control the built-in logic in the Web Server, and all page requests go through the Hook.
|
|
49
49
|
|
|
50
|
-
|
|
50
|
+
Currently, two Hooks are provided: `AfterMatch` and `AfterRender`, which can be used to modify the rendering results. It can be written in `server/index.ts` as follows:
|
|
51
51
|
|
|
52
52
|
```ts
|
|
53
53
|
import type { AfterMatchHook, AfterRenderHook } from '@modern-js/runtime/server';
|
|
@@ -61,11 +61,11 @@ export const afterRender: AfterRenderHook = (ctx, next) => {
|
|
|
61
61
|
}
|
|
62
62
|
```
|
|
63
63
|
|
|
64
|
-
Projects should
|
|
64
|
+
Projects should follow these best practices when using Hook:
|
|
65
65
|
|
|
66
|
-
1.
|
|
66
|
+
1. Authentication in afterMatch.
|
|
67
67
|
2. Do Rewrite and Redirect in afterMatch.
|
|
68
|
-
3.
|
|
68
|
+
3. Inject HTML content in afterRender.
|
|
69
69
|
|
|
70
70
|
:::note
|
|
71
71
|
For more detail, see [Hook](/apis/app/runtime/web-server/hook).
|
|
@@ -91,10 +91,10 @@ export const middleware: Middlewre = (context, next) => {
|
|
|
91
91
|
For more detail, see [Middleware] (/apis/app/runtime/web-server/middleware).
|
|
92
92
|
:::
|
|
93
93
|
|
|
94
|
-
Projects should
|
|
94
|
+
Projects should follow these best practices when using Middleware:
|
|
95
95
|
|
|
96
96
|
1. In Middleware, you can directly operate origin request and response objects, do event tracking, and inject Node services (databases, Redis, etc.) that may be used for SSR rendering.
|
|
97
|
-
2.
|
|
97
|
+
2. Operations such as marking and crawler optimization can be done in Middleware.
|
|
98
98
|
3. In Middleware, you can ignore the default rendering and customize the rendering process.
|
|
99
99
|
|
|
100
100
|
**In general, in CSR projects, using Hook can basically meet all the needs of simple scenarios. In SSR projects, Middleware can be used for more complex Node extensions.**
|
|
@@ -103,9 +103,9 @@ Projects should have the following best practices when using Middleware:
|
|
|
103
103
|
|
|
104
104
|
The second way is to use BFF to Managed page rendering. In this way, all requests will first hit the BFF service.
|
|
105
105
|
|
|
106
|
-
This method can uniformly control the server
|
|
106
|
+
This method can uniformly control the server-level logic of all requests through BFF. Therefore, it is suitable for scenarios where the server-level logic is complex, and BFF and pages need common server-level logic. But it still relies on the Web Server of Modern.js as a whole, and cannot run the logic on existing services.
|
|
107
107
|
|
|
108
|
-
To use this method, we need to
|
|
108
|
+
To use this method, we first need to enable the "BFF" function through `pnpm new`. Then add [`bff.enableHandleWeb`](/configure/app/bff/enable-handle-web.html) configuration in the configuration file:
|
|
109
109
|
|
|
110
110
|
```ts
|
|
111
111
|
export default defineConfig({
|
|
@@ -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,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.
|
|
@@ -106,12 +106,13 @@ File sizes after production build:
|
|
|
106
106
|
|
|
107
107
|
`modern new` 命令用于在已有项目中添加项目元素。
|
|
108
108
|
|
|
109
|
-
|
|
109
|
+
比如添加应用入口、启用一些可选功能如 Tailwind CSS、微前端开发模式等。
|
|
110
110
|
|
|
111
111
|
```bash
|
|
112
112
|
Usage: modern new [options]
|
|
113
113
|
|
|
114
114
|
Options:
|
|
115
|
+
--lang <lang> 设置 new 命令执行语言(zh 或者 en)
|
|
115
116
|
-d, --debug 开启 Debug 模式,打印调试日志信息 (default: false)
|
|
116
117
|
-c, --config <config> 生成器运行默认配置(JSON 字符串)
|
|
117
118
|
--dist-tag <tag> 生成器使用特殊的 npm Tag 版本
|
|
@@ -162,7 +163,6 @@ Usage: modern serve [options]
|
|
|
162
163
|
Options:
|
|
163
164
|
-c --config <config> 指定配置文件路径,可以为相对路径或绝对路径
|
|
164
165
|
-h, --help 显示命令帮助
|
|
165
|
-
--web-only 仅启动 Web 服务
|
|
166
166
|
--api-only 仅启动 API 接口服务
|
|
167
167
|
```
|
|
168
168
|
|
|
@@ -180,7 +180,7 @@ export default defineConfig({
|
|
|
180
180
|
|
|
181
181
|
在项目根目录下执行命令 `npx modern upgrade`,会默认将当前执行命令项目的 `package.json` 中的 Modern.js 相关依赖更新至最新版本。
|
|
182
182
|
|
|
183
|
-
```
|
|
183
|
+
```bash
|
|
184
184
|
Usage: modern upgrade [options]
|
|
185
185
|
|
|
186
186
|
Options:
|
|
@@ -194,7 +194,7 @@ Options:
|
|
|
194
194
|
|
|
195
195
|
`modern inspect` 命令用于查看项目的 [Modern.js Builder 配置](https://modernjs.dev/builder/guide/basic/builder-config.html) 以及 webpack 配置。
|
|
196
196
|
|
|
197
|
-
```
|
|
197
|
+
```bash
|
|
198
198
|
Usage: modern inspect [options]
|
|
199
199
|
|
|
200
200
|
Options:
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
这里还可以进行路径重写,如将发送到 `localhost:8080/api/topics` 的请求代理到 `https://cnodejs.org/api/v1/topics`。
|
|
2
|
+
|
|
3
|
+
```ts title="modern.server-runtime.config.ts"
|
|
4
|
+
import { defineConfig } from '@modern-js/app-tools/server';
|
|
5
|
+
export default defineConfig({
|
|
6
|
+
bff: {
|
|
7
|
+
proxy: {
|
|
8
|
+
'/api': {
|
|
9
|
+
target: 'https://cnodejs.org',
|
|
10
|
+
pathRewrite: { '/api/topics': '/api/v1/topics' },
|
|
11
|
+
changeOrigin: true,
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
BFF Proxy 使用了强大的 [http-proxy-middleware](https://github.com/chimurai/http-proxy-middleware),如果需要更多高级的用法, 可以查看它的[文档](https://github.com/chimurai/http-proxy-middleware#options)。
|
|
@@ -7,9 +7,9 @@ title: enableHandleWeb
|
|
|
7
7
|
- **类型:** `boolean`
|
|
8
8
|
- **默认值:** `false`
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
import EnableBFFCaution from "@site-docs/components/enable-bff-caution";
|
|
11
|
+
|
|
12
|
+
<EnableBFFCaution />
|
|
13
13
|
|
|
14
14
|
默认情况下,BFF 服务只能处理 BFF API 的请求。
|
|
15
15
|
|
|
@@ -7,10 +7,9 @@ sidebar_label: prefix
|
|
|
7
7
|
- **类型:** `string`
|
|
8
8
|
- **默认值:** `/api`
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
请先在当前应用项目根目录使用【[new](/apis/app/commands#modern-new)】 启用 BFF 功能。
|
|
10
|
+
import EnableBFFCaution from "@site-docs/components/enable-bff-caution";
|
|
12
11
|
|
|
13
|
-
|
|
12
|
+
<EnableBFFCaution />
|
|
14
13
|
|
|
15
14
|
默认情况下,BFF API 目录下的路由访问前缀是 `/api`, 如下目录结构:
|
|
16
15
|
|
|
@@ -7,13 +7,11 @@ sidebar_label: proxy
|
|
|
7
7
|
- **类型:** `Record<string, string>`
|
|
8
8
|
- **默认值:** `{}`
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
请先在当前应用项目根目录使用【[new](/apis/app/commands#modern-new)】 启用 BFF 功能。
|
|
10
|
+
import EnableBFFCaution from "@site-docs/components/enable-bff-caution";
|
|
12
11
|
|
|
13
|
-
|
|
12
|
+
<EnableBFFCaution />
|
|
14
13
|
|
|
15
|
-
|
|
16
|
-
BFF Proxy 使用了强大的 [http-proxy-middleware](https://github.com/chimurai/http-proxy-middleware),如果需要更多高级的用法, 可以查看它的[文档](https://github.com/chimurai/http-proxy-middleware#options)。
|
|
14
|
+
通过简单配置,Modern.js 可以将发送给 BFF server 的请求,自动代理到指定的服务上。
|
|
17
15
|
|
|
18
16
|
在 `modern.server-runtime.config.js` 中加入以下配置;即可开启代理:
|
|
19
17
|
|
|
@@ -28,27 +26,18 @@ export default defineConfig({
|
|
|
28
26
|
});
|
|
29
27
|
```
|
|
30
28
|
|
|
31
|
-
|
|
29
|
+
假设 Modern.js BFF server 的地址是 `localhost:8080`,所有以 `api` 开头的请求都会被代理到 `https://cnodejs.org`,如 `localhost:8080/api/v1/topics` 的请求会被代理到 `https://cnodejs.org/api/v1/topics`。
|
|
32
30
|
|
|
33
|
-
|
|
31
|
+
import BFFProxyPathRewrite from "@site-docs/components/bff-proxy-path-rewrite";
|
|
34
32
|
|
|
35
|
-
|
|
36
|
-
import { defineConfig } from '@modern-js/app-tools/server';
|
|
37
|
-
export default defineConfig({
|
|
38
|
-
bff: {
|
|
39
|
-
proxy: {
|
|
40
|
-
'/api': {
|
|
41
|
-
target: 'https://cnodejs.org',
|
|
42
|
-
pathRewrite: { '/api/topics': '/api/v1/topics' },
|
|
43
|
-
changeOrigin: true,
|
|
44
|
-
},
|
|
45
|
-
},
|
|
46
|
-
},
|
|
47
|
-
});
|
|
48
|
-
```
|
|
33
|
+
<BFFProxyPathRewrite />
|
|
49
34
|
|
|
50
35
|
与 [dev.proxy](/configure/app/dev/proxy) 不同,本节所介绍的代理只作用于进入 BFF/API 服务的请求;同时,这一配置不但可以在开发环境中使用,在生产环境中也会代理相应的请求。
|
|
51
36
|
|
|
37
|
+
import BFFProxyPrinciple from "@site-docs/components/bff-proxy-principle";
|
|
38
|
+
|
|
39
|
+
<BFFProxyPrinciple />
|
|
40
|
+
|
|
52
41
|
## 常见用法
|
|
53
42
|
|
|
54
43
|
### 解决接口跨域问题
|
|
@@ -72,3 +61,4 @@ export default defineServerConfig({
|
|
|
72
61
|
},
|
|
73
62
|
};
|
|
74
63
|
```
|
|
64
|
+
|
|
@@ -4,7 +4,7 @@ sidebar_position: 0
|
|
|
4
4
|
|
|
5
5
|
# 配置使用
|
|
6
6
|
|
|
7
|
-
Modern.js
|
|
7
|
+
Modern.js 中有两种配置,分别是编译时配置和服务端运行时配置。
|
|
8
8
|
|
|
9
9
|
编译时配置可以在两个位置进行配置:
|
|
10
10
|
|
|
@@ -12,10 +12,10 @@ Modern.js 中有两种配置,一个是编译时配置,一个是服务端运
|
|
|
12
12
|
- `package.json` 文件
|
|
13
13
|
|
|
14
14
|
:::info
|
|
15
|
-
Modern.js 不支持同时在 package.json 中和 modern.config.ts
|
|
15
|
+
Modern.js 不支持同时在 `package.json` 中和 `modern.config.ts` 中配置同一个配置项,推荐在 `modern.config.ts` 中进行配置。如果 Modern.js 检测到重复配置导致的冲突,将会抛出警告。
|
|
16
16
|
:::
|
|
17
17
|
|
|
18
|
-
服务端运行时配置可以在根路径下的 `modern.server-runtime.config.(ts|js|mjs)`
|
|
18
|
+
服务端运行时配置可以在根路径下的 `modern.server-runtime.config.(ts|js|mjs)` 中进行配置。
|
|
19
19
|
|
|
20
20
|
## 在配置文件中配置
|
|
21
21
|
|
|
@@ -27,7 +27,7 @@ Modern.js 的配置文件定义在项目的根目录下,支持 `.ts`, `.js`
|
|
|
27
27
|
|
|
28
28
|
### modern.config.ts(推荐)
|
|
29
29
|
|
|
30
|
-
我们推荐使用
|
|
30
|
+
我们推荐使用 `.ts` 格式的配置文件,它提供了友好的 TypeScript 类型提示,从而帮助你避免配置中的错误。
|
|
31
31
|
|
|
32
32
|
从 `@modern-js/app-tools` 中导入 `defineConfig` 工具函数, 它会帮助你进行配置的类型推导和类型补全:
|
|
33
33
|
|
|
@@ -54,7 +54,7 @@ export default defineConfig({
|
|
|
54
54
|
|
|
55
55
|
### modern.config.js
|
|
56
56
|
|
|
57
|
-
如果你在开发一个非 TypeScript 项目,可以使用
|
|
57
|
+
如果你在开发一个非 TypeScript 项目,可以使用 `.js` 格式的配置文件:
|
|
58
58
|
|
|
59
59
|
```js title="modern.config.js"
|
|
60
60
|
export default {
|
|
@@ -66,7 +66,7 @@ export default {
|
|
|
66
66
|
};
|
|
67
67
|
```
|
|
68
68
|
|
|
69
|
-
你也可以通过 `process.env.NODE_ENV
|
|
69
|
+
你也可以通过 `process.env.NODE_ENV`,根据环境做不同配置:
|
|
70
70
|
|
|
71
71
|
```js title="modern.config.js"
|
|
72
72
|
export default {
|
|
@@ -98,7 +98,7 @@ export default defineConfig(({ env, command }) => ({
|
|
|
98
98
|
- 当运行 `modern dev` 或 `modern start` 时,`env` 的值为 `development`。
|
|
99
99
|
- 当运行 `modern build` 或 `modern serve` 时,`env` 的值为 `production`。
|
|
100
100
|
- 当运行 `modern test` 时,`env` 的值为 `test`。
|
|
101
|
-
- `command
|
|
101
|
+
- `command`:对应当前运行的命令,如 `dev`、`start`、`build`、`serve`。
|
|
102
102
|
|
|
103
103
|
### 导出异步函数
|
|
104
104
|
|
|
@@ -122,7 +122,7 @@ export default defineConfig(async ({ env, command }) => {
|
|
|
122
122
|
|
|
123
123
|
Modern.js 命令行支持通过 `--config` 选项来指定配置文件的名称。
|
|
124
124
|
|
|
125
|
-
例如,你需要在执行 build 命令时使用 `modern.prod.config.ts` 文件,可以在 `package.json` 中添加如下配置:
|
|
125
|
+
例如,你需要在执行 `build` 命令时使用 `modern.prod.config.ts` 文件,可以在 `package.json` 中添加如下配置:
|
|
126
126
|
|
|
127
127
|
```json title="package.json"
|
|
128
128
|
{
|
|
@@ -159,7 +159,7 @@ $ modern build -c modern.prod.config.js
|
|
|
159
159
|
|
|
160
160
|
### 注意事项
|
|
161
161
|
|
|
162
|
-
- 不建议同时使用 `package.json` 和 `modern.config.js` 进行配置。如果同时使用了两者并出现配置冲突,Modern.js
|
|
162
|
+
- 不建议同时使用 `package.json` 和 `modern.config.js` 进行配置。如果同时使用了两者并出现配置冲突,Modern.js 会在命令行进行提示错误。
|
|
163
163
|
- `@modern-js/runtime` 导出了同名的 [defineConfig](/apis/app/runtime/app/define-config) API,请注意区分。
|
|
164
164
|
|
|
165
165
|
## 本地调试配置
|
|
@@ -8,14 +8,14 @@ Modern.js 默认集成了 [Jest](https://jestjs.io/) 的测试能力。
|
|
|
8
8
|
|
|
9
9
|
我们首先需要执行 `pnpm run new` 启用「单元测试 / 集成测试」功能:
|
|
10
10
|
|
|
11
|
-
```
|
|
11
|
+
```bash
|
|
12
12
|
? 请选择你想要的操作 启用可选功能
|
|
13
13
|
? 请选择功能名称 启用「单元测试 / 集成测试」功能
|
|
14
14
|
```
|
|
15
15
|
|
|
16
|
-
执行上述命令后,`package.json`
|
|
16
|
+
执行上述命令后,`package.json` 中将会自动添加 `"test": "modern test"` 命令。
|
|
17
17
|
|
|
18
|
-
在 `modern.config.ts` 中注册
|
|
18
|
+
在 `modern.config.ts` 中注册 `@modern-js/plugin-testing` 插件后即可使用测试功能:
|
|
19
19
|
|
|
20
20
|
```ts title="modern.config.ts"
|
|
21
21
|
import testPlugin from '@modern-js/plugin-testing';
|
|
@@ -36,7 +36,7 @@ Modern.js 默认识别的测试文件路径为:`<rootDir>/src/**/*.test.[jt]s?
|
|
|
36
36
|
|
|
37
37
|
Modern.js test 支持使用 [testing-library](https://testing-library.com/docs/) 相关包 API,可直接通过 `@modern-js/runtime/testing` 进行导入:
|
|
38
38
|
|
|
39
|
-
```
|
|
39
|
+
```ts
|
|
40
40
|
import { render, screen } from '@modern-js/runtime/testing';
|
|
41
41
|
```
|
|
42
42
|
|
|
@@ -6,7 +6,7 @@ sidebar_position: 6
|
|
|
6
6
|
|
|
7
7
|
Modern.js 提供了快速生成 Mock 数据的功能,能够让前端独立自主开发,不被后端接口阻塞。
|
|
8
8
|
|
|
9
|
-
## Mock
|
|
9
|
+
## 启用 Mock
|
|
10
10
|
|
|
11
11
|
约定当 `config/mock` 目录下存在 `index.[jt]s` 时,会自动开启 Mock 功能,如下:
|
|
12
12
|
|
|
@@ -63,7 +63,7 @@ module.exports = {
|
|
|
63
63
|
|
|
64
64
|
:::
|
|
65
65
|
|
|
66
|
-
##
|
|
66
|
+
## 延迟响应
|
|
67
67
|
|
|
68
68
|
- 可以使用浏览器「 弱网模拟 」的功能实现。
|
|
69
69
|
- 可以通过 `setTimeout` 为单个接口设置延迟,例如:
|
package/package.json
CHANGED
|
@@ -15,14 +15,14 @@
|
|
|
15
15
|
"modern",
|
|
16
16
|
"modern.js"
|
|
17
17
|
],
|
|
18
|
-
"version": "0.0.0-next-
|
|
18
|
+
"version": "0.0.0-next-1686651393235",
|
|
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": "0.0.0-next-
|
|
25
|
+
"@modern-js/builder-doc": "0.0.0-next-1686651393235"
|
|
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": "0.0.0-next-
|
|
38
|
-
"@modern-js/doc-
|
|
39
|
-
"@modern-js/doc-
|
|
37
|
+
"@modern-js/builder-doc": "0.0.0-next-1686651393235",
|
|
38
|
+
"@modern-js/doc-tools": "0.0.0-next-1686651393235",
|
|
39
|
+
"@modern-js/doc-plugin-auto-sidebar": "0.0.0-next-1686651393235"
|
|
40
40
|
},
|
|
41
41
|
"scripts": {
|
|
42
42
|
"dev": "modern dev",
|