@modern-js/main-doc 0.0.0-next-1686593242894 → 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 CHANGED
@@ -1,37 +1,41 @@
1
1
  # @modern-js/main-doc
2
2
 
3
- ## 0.0.0-next-1686593242894
3
+ ## 0.0.0-next-1686651393235
4
4
 
5
5
  ### Patch Changes
6
6
 
7
- - 8a932767f: docs(main): update basic features alias doc
7
+ - 8a932767f0: docs(main): update basic features alias doc
8
8
 
9
9
  docs(main): 更新基础功能 -- 别名文档
10
10
 
11
- - 7e6fb5fc1: chore: publishConfig add provenance config
11
+ - 7e6fb5fc16: chore: publishConfig add provenance config
12
12
 
13
13
  chore: publishConfig 增加 provenance 配置
14
14
 
15
- - 372b950c1: docs(main): update plugin config doc
15
+ - 372b950c12: docs(main): update plugin config doc
16
16
 
17
17
  docs(main): 更新插件配置文档
18
18
 
19
- - f28b4855c: docs(main): update faq doc
19
+ - f28b4855c2: docs(main): update faq doc
20
20
 
21
21
  docs(main): 更新 faq 文档
22
22
 
23
- - f264c4a7f: docs(main): update html template doc
23
+ - f264c4a7f8: docs(main): update html template doc
24
24
 
25
25
  docs(main): 更新 HTML 模板文档
26
26
 
27
- - ef865f285: docs(main): update config server doc
27
+ - 5cba7c46b7: docs(main): update bff config doc
28
+
29
+ docs(main): 更新 BFF 配置文档
30
+
31
+ - ef865f285b: docs(main): update config server doc
28
32
 
29
33
  docs(main): 更新 server 配置文档
30
34
 
31
- - 8bb0e146f: chore: review code split docs
35
+ - 8bb0e146f2: chore: review code split docs
32
36
  chore: 审查代码分割文档
33
- - Updated dependencies [7e6fb5fc1]
34
- - @modern-js/builder-doc@0.0.0-next-1686593242894
37
+ - Updated dependencies [7e6fb5fc16]
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 features
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 Server port number with `server.port`:
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).
@@ -0,0 +1,3 @@
1
+ :::caution
2
+ Please enable BFF functionality in the current application project root directory using [new](/apis/app/commands#modern-new) first.
3
+ :::
@@ -7,13 +7,13 @@ title: enableHandleWeb
7
7
  - **Type:** `boolean`
8
8
  - **Default:** `false`
9
9
 
10
- :::caution
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
- By default, the BFF service can only handle requests from the BFF API.
12
+ <EnableBFFCaution />
15
13
 
16
- When this value is set to `true`, page request traffic also goes through the BFF, and the logic built into Modern.js for page rendering defaults to running as the last middleware for the BFF service.
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
- :::caution
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
- By default, the route access BFF prefix's directory is `/api`, with the following directory structure:
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 route for `api/hello.ts` access is `localhost:8080/api/hello`.
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` access route is `localhost:8080/api-demo/hello`.
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
- :::caution Caution
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
- BFF Proxy uses the powerful [http-proxy-middleware](https://github.com/chimurai/http-proxy-middleware), and if you need more advanced usage, you can check its [documentation](https://github.com/chimurai/http-proxy-middleware#options).
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.ts`, you can turn on the proxy:
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 that the starting Modern.js BFF server's service address is `localhost:8080`, all requests whose path starts with `api` will be intercepted, such as requests sent to `localhost:8080/api/v1/topics` will be proxied to `https://cnodejs.org/api/v1/topics`.
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
- You can do path rewriting, such as proxying requests sent to `localhost:8080/api/topics` to `https://cnodejs.org/api/v1/topics`.
31
+ import BFFProxyPathRewrite from "@site-docs/components/bff-proxy-path-rewrite";
35
32
 
36
- ```js title="modern.server-runtime.config.js"
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 described in this section only works on requests entering the BFF/API service; at the same time, this configuration can be used not only in the development environment, but also in the production environment. The corresponding request will also be proxied in the production environment.
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
- ## Common usage
37
+ import BFFProxyPrinciple from "@site-docs/components/bff-proxy-principle";
54
38
 
55
- ### Solve interface cross-domain problems
39
+ <BFFProxyPrinciple />
56
40
 
57
- In the process of project development, because web pages and interface services are not deployed under the same domain name, cross-domain problems are often encountered.
41
+ ## Common Usage
58
42
 
59
- There are many ways to solve cross-domain problems, and here we use `bff.proxy` to easily solve cross-domain problems.
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
- In BFF proxy mode, if you do not need to write the BFF interface, the API directory can be deleted; at this time, BFF proxy will still be enabled.
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 the `modern.server-runtime.config.js`, write the following configuration; we send all web pages to the same domain that request proxies starting with `/api` to another domain's service.
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 .js format:
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 dynamically set it with `process.env.NODE _ENV`:
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, you can add the following scripts to `package.json`:
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 set it in the Modern.js configuration file.
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 method to optimize frontend resource loading. This article will introduce the three types of code splitting supported by Modern.js:
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 automatically generated in package.json.
16
+ After executing the above command, the `"test": "modern test"` command will be added in `package.json` automatically.
17
17
 
18
- register plugin in `modern.config.ts`:
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 Modern.js supported testing APIs can be found [here](/apis/app/runtime/testing/cleanup).
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 tests use [babel-jest](https://www.npmjs.com/package/babel-jest) for source code compilation by default. If you need to use [ts-jest](https://github.com/kulshekhar/ts-jest), you can configure it through [testing.transform](/configure/app/testing/transformer).
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 side-centric development framework, Modern.js has weak customization capabilities on the server side. In some development scenarios, special server level logic needs to be customized, such as user authentication, request preprocessing, and adding page rendering skeletons.
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 do you need **Custom Web Server**.
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
- Because 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, so as to avoid the BFF framework restricting how the page is deployed.
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, or customizing for deployment platforms, etc.
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 level capabilities progressively according to their needs.
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 level at a specific life cycle through the server level 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.
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 to server level logic, and you do not want to create additional BFFs or BFFs and pages without common server level logic scenarios.
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 execute the'pnpm run new 'command in the project root directory to enable the "Custom Web Serve" function:
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
- There are currently two Hooks provided, namely `AfterMatch` and `AfterRender`, which can be used to modify the rendering results. It can be written in `server/index.ts` like this:
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 have the following best practices when using Hook:
64
+ Projects should follow these best practices when using Hook:
65
65
 
66
- 1. Permission verification in afterMatch.
66
+ 1. Authentication in afterMatch.
67
67
  2. Do Rewrite and Redirect in afterMatch.
68
- 3. Do HTML content injection in afterRender.
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 have the following best practices when using Middleware:
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. Marking and crawler optimization can be done in Middleware.
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 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.
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 first enable the "BFF" function through `pnpm new`. Then add [`bff.enableHandleWeb`](/configure/app/bff/enable-handle-web.html) configuration in the configuration file:
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 current path prefix of resource file, which is a **read-only** environment variable.
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`: the default value when exec `modern build` or `modern serve`.
20
- - `test`: the default value when exec `modern test`.
21
- - `development`: the default value when exec `modern dev`, alse the default value of other case.
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 judge the environment and execute the appropriate code.
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 the 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).
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
- Custom environment variables can be specified in both `shell` and `.env` files.
78
+ You can specify custom environment variables in both `shell` and `.env` files.
79
79
 
80
- ### Specify via `shell`
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
- ### Specify via `.env` file
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 }`: Setting environment variables for a specific environment overrides the same in `.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 config [`source.globalVars`](/configure/app/source/global-vars), for example:
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 with the value of `VERSION` in the environment variables.
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 like distinguish development environment and production environment in code.
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 provides the ability to quickly generate Mock data, allowing the front-end to develop independently without being blocked by the back-end interface.
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 File
9
+ ## Enabling Mock
10
10
 
11
- By convention, when there is `index.[jt]s` in the `config/mock/` directory, the Mock Data will be automatically enabled, as follows:
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
- 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`, `Function`:
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
- when access `http://localhost:8080/api/getInfo`, the api will return json `{ "data": [1, 2, 3, 4] }`.
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, for example:
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 Lib
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 Return
66
+ ## Delayed Response
67
67
 
68
- - It can be achieved using the function of the browser "weak connection simulation".
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, through which developers can control whether to execute Mock.
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
- 比如添加应用入口、启用启用一些可选功能如 Tailwind CSS、微前端开发模式等。
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)。
@@ -0,0 +1,4 @@
1
+ :::caution 注意
2
+ 请先在当前应用项目根目录使用【[new](/apis/app/commands#modern-new)】 启用 BFF 功能。
3
+
4
+ :::
@@ -7,9 +7,9 @@ title: enableHandleWeb
7
7
  - **类型:** `boolean`
8
8
  - **默认值:** `false`
9
9
 
10
- :::caution 注意
11
- 请先在当前应用项目根目录使用【[new](/apis/app/commands#modern-new)】 启用 BFF 功能。
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
- :::caution 注意
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
- :::info 补充信息
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
- 通过简单配置,无需编写代码,Modern.js 会自动转发请求。发送给 Modern.js BFF server 的请求,会代理到指定的服务上。
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
- 假设启动的 Modern.js BFF server 的服务地址是 `localhost:8080`,所有 path 以 `api` 开头的请求都会被拦截,如发送到 `localhost:8080/api/v1/topics` 的请求会被代理到 `https://cnodejs.org/api/v1/topics`。
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
- 你可以做路径的重写,如将发送到 `localhost:8080/api/topics` 的请求代理到 `https://cnodejs.org/api/v1/topics`。
31
+ import BFFProxyPathRewrite from "@site-docs/components/bff-proxy-path-rewrite";
34
32
 
35
- ```ts title="modern.server-runtime.config.ts"
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 中配置同一个配置项,推荐优先在 modern.config.ts 中进行配置。如果 Modern.js 检测到重复配置导致的冲突,将会抛出警告。
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
- 我们推荐使用 .ts 格式的配置文件,它提供了友好的 TypeScript 类型提示,从而帮助你避免配置中的错误。
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 项目,可以使用 .js 格式的配置文件:
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`: 对应当前运行的命令,如 `dev`、`start`、`build`、`serve`。
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` 中将会自动生成 `"test": "modern test"` 命令。
16
+ 执行上述命令后,`package.json` 中将会自动添加 `"test": "modern test"` 命令。
17
17
 
18
- 在 `modern.config.ts` 中注册 Test 插件:
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-1686593242894",
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-1686593242894"
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-1686593242894",
38
- "@modern-js/doc-tools": "0.0.0-next-1686593242894",
39
- "@modern-js/doc-plugin-auto-sidebar": "0.0.0-next-1686593242894"
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",