Skip to content

Commit 6335ec2

Browse files
authored
docs: examples for Environment Context (#4193)
1 parent 2e388bd commit 6335ec2

File tree

2 files changed

+152
-17
lines changed

2 files changed

+152
-17
lines changed

website/docs/en/api/javascript-api/environment-api.mdx

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# Environment API
22

3-
This section describes some type definitions related to Rsbuild environment API.
3+
Here you can find all the environment related APIs.
4+
5+
> See [Multi-Environment Builds](/guide/advanced/environments) for more details.
46
57
## Environment Context
68

79
Environment context is a read-only object that provides some context information about the current environment.
810

9-
In Rsbuild's hooks, you can get the environment context object through the `environment` or `environments` parameter.
11+
In Rsbuild's [Plugin hooks](/plugins/dev/hooks#plugin-hooks), you can get the environment context object through the `environment` or `environments` parameter.
1012

1113
```ts
1214
type EnvironmentContext = {
@@ -25,16 +27,34 @@ type EnvironmentContext = {
2527
The unique name of the current environment is used to distinguish and locate the environment, corresponds to the key in the [environments](/config/environments) configuration.
2628

2729
- **Type:** `string`
30+
- **Example:**
31+
32+
```js
33+
api.modifyRspackConfig((config, { environment }) => {
34+
if (environment.name === 'node') {
35+
// modify config for node environment
36+
}
37+
return config;
38+
});
39+
```
2840

2941
### browserslist
3042

31-
The range of target browsers that the project is compatible with. See details in [Browserslist](/guide/advanced/browserslist).
43+
The browserslist configuration of the current environment. See [Browserslist](/guide/advanced/browserslist) for more details.
3244

3345
- **Type:** `string[]`
46+
- **Example:**
47+
48+
```js
49+
api.modifyRspackConfig((config, { environment }) => {
50+
console.log(environment.browserslist);
51+
return config;
52+
});
53+
```
3454

3555
### config
3656

37-
The Rsbuild environment configuration after normalization.
57+
The normalized Rsbuild config for the current environment.
3858

3959
- **Type:**
4060

@@ -53,12 +73,31 @@ type NormalizedEnvironmentConfig = DeepReadonly<{
5373
}>;
5474
```
5575

76+
- **Example:**
77+
78+
```js
79+
api.modifyRspackConfig((config, { environment }) => {
80+
// Rspack config
81+
console.log(config);
82+
// Rsbuild config for current environment
83+
console.log(environment.config);
84+
return config;
85+
});
86+
```
87+
5688
### distPath
5789

5890
The absolute path of the output directory, corresponding to the [output.distPath.root](/config/output/dist-path) config in `RsbuildConfig`.
5991

6092
- **Type:** `string`
6193

94+
```js
95+
api.modifyRspackConfig((config, { environment }) => {
96+
console.log(environment.distPath);
97+
return config;
98+
});
99+
```
100+
62101
### entry
63102

64103
The entry object from the [source.entry](/config/source/entry) option.
@@ -69,6 +108,15 @@ The entry object from the [source.entry](/config/source/entry) option.
69108
type RsbuildEntry = Record<string, string | string[] | EntryDescription>;
70109
```
71110

111+
- **Example:**
112+
113+
```js
114+
api.modifyRspackConfig((config, { environment }) => {
115+
console.log(environment.entry);
116+
return config;
117+
});
118+
```
119+
72120
### htmlPaths
73121

74122
The path information for all HTML assets.
@@ -81,6 +129,15 @@ This API will return an object, the key is the entry name and the value is the r
81129
type htmlPaths = Record<string, string>;
82130
```
83131

132+
- **Example:**
133+
134+
```js
135+
api.modifyRspackConfig((config, { environment }) => {
136+
console.log(environment.htmlPaths);
137+
return config;
138+
});
139+
```
140+
84141
### tsconfigPath
85142

86143
The absolute path of the tsconfig.json file, or `undefined` if the tsconfig.json file does not exist in current project.
@@ -91,11 +148,20 @@ The absolute path of the tsconfig.json file, or `undefined` if the tsconfig.json
91148
type TsconfigPath = string | undefined;
92149
```
93150

151+
- **Example:**
152+
153+
```js
154+
api.modifyRspackConfig((config, { environment }) => {
155+
console.log(environment.tsconfigPath);
156+
return config;
157+
});
158+
```
159+
94160
## Environment API
95161

96-
Environment API provides a series of APIs related to the build environment.
162+
Environment API provides some APIs related to the multi-environment build.
97163

98-
You can use environment API in [Rsbuild DevMiddleware](/config/dev/setup-middlewares) or [Custom Server](/api/javascript-api/instance#rsbuildcreatedevserver) to operate the build artifacts in a specific environment.
164+
You can use environment API via [rsbuild.createDevServer()](/api/javascript-api/instance#rsbuildcreatedevserver) or [dev.setupMiddlewares](/config/dev/setup-middlewares), which allows you to get the build outputs information for a specific environment in the server side.
99165

100166
```ts
101167
type EnvironmentAPI = {
@@ -127,7 +193,7 @@ console.log(webStats.toJson({ all: false }));
127193

128194
### loadBundle
129195

130-
Load and execute the build artifacts on the server side. This method returns the exports of the entry module.
196+
Load and execute the bundles on the server side. This method returns the exports of the entry module.
131197

132198
- **Type:**
133199

@@ -142,7 +208,7 @@ type LoadBundle = <T = unknown>(entryName: string) => Promise<T>;
142208
- **Example:**
143209

144210
```ts
145-
// Load the build artifacts of main entry
211+
// Load the bundle of `main` entry
146212
const result = await environments.node.loadBundle('main');
147213
```
148214

website/docs/zh/api/javascript-api/environment-api.mdx

Lines changed: 78 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# Environment API
22

3-
本章节描述了 Rsbuild environment API 相关的一些类型定义。
3+
在这里你可以找到所有与 environment 相关的 API。
4+
5+
> 参考 [多环境构建](/guide/advanced/environments) 了解更多。
46
57
## Environment Context
68

79
Environment context 是一个只读对象,提供一些和当前环境有关的上下文信息。
810

9-
在 Rsbuild 的 hooks 中,你可以通过 `environment``environments` 入参获取 environment context 对象。
11+
在 Rsbuild 的 [Plugin hooks](/plugins/dev/hooks#plugin-hooks) 中,你可以通过 `environment``environments` 入参获取 environment context 对象。
1012

1113
```ts
1214
type EnvironmentContext = {
@@ -25,16 +27,34 @@ type EnvironmentContext = {
2527
当前环境的唯一名称,用于区分和定位环境,对应于 [environments](/config/environments) 配置中的 key。
2628

2729
- **类型:** `string`
30+
- **示例:**
31+
32+
```js
33+
api.modifyRspackConfig((config, { environment }) => {
34+
if (environment.name === 'node') {
35+
// modify config for node environment
36+
}
37+
return config;
38+
});
39+
```
2840

2941
### browserslist
3042

31-
项目兼容的目标浏览器范围。详情可参考 [设置浏览器范围](/guide/advanced/browserslist).
43+
当前环境设置的目标浏览器范围。详见 [设置浏览器范围](/guide/advanced/browserslist)
3244

3345
- **类型:** `string[]`
46+
- **示例:**
47+
48+
```js
49+
api.modifyRspackConfig((config, { environment }) => {
50+
console.log(environment.browserslist);
51+
return config;
52+
});
53+
```
3454

3555
### config
3656

37-
归一化后当前环境的 Rsbuild 配置
57+
当前环境使用的 Rsbuild 配置(经过归一化处理)。
3858

3959
- **类型:**
4060

@@ -53,11 +73,31 @@ type NormalizedEnvironmentConfig = DeepReadonly<{
5373
}>;
5474
```
5575

76+
- **示例:**
77+
78+
```js
79+
api.modifyRspackConfig((config, { environment }) => {
80+
// Rspack
81+
console.log(config);
82+
// Rsbuild config for current environment
83+
console.log(environment.config);
84+
return config;
85+
});
86+
```
87+
5688
### distPath
5789

5890
构建产物输出目录的绝对路径,对应 RsbuildConfig 中的 [output.distPath.root](/config/output/dist-path) 配置项。
5991

6092
- **类型:** `string`
93+
- **示例:**
94+
95+
```js
96+
api.modifyRspackConfig((config, { environment }) => {
97+
console.log(environment.distPath);
98+
return config;
99+
});
100+
```
61101

62102
### entry
63103

@@ -69,31 +109,60 @@ type NormalizedEnvironmentConfig = DeepReadonly<{
69109
type RsbuildEntry = Record<string, string | string[] | EntryDescription>;
70110
```
71111

112+
- **示例:**
113+
114+
```js
115+
api.modifyRspackConfig((config, { environment }) => {
116+
console.log(environment.entry);
117+
return config;
118+
});
119+
```
120+
72121
### htmlPaths
73122

74-
HTML 产物的路径信息。该 API 会返回一个对象,对象的 key 为 entry 名称,value 为 HTML 文件在产物目录下的相对路径。
123+
HTML 产物的路径信息。
124+
125+
该 API 会返回一个对象,对象的 key 为 entry 名称,value 为 HTML 文件在产物目录下的相对路径。
75126

76127
- **类型:**
77128

78129
```ts
79130
type htmlPaths = Record<string, string>;
80131
```
81132

133+
- **示例:**
134+
135+
```js
136+
api.modifyRspackConfig((config, { environment }) => {
137+
console.log(environment.htmlPaths);
138+
return config;
139+
});
140+
```
141+
82142
### tsconfigPath
83143

84-
tsconfig.json 文件的绝对路径,若项目中不存在 tsconfig.json 文件,则为 undefined。
144+
tsconfig.json 文件的绝对路径,若项目中不存在 tsconfig.json 文件,则为 `undefined`
85145

86146
- **类型:**
87147

88148
```ts
89149
type TsconfigPath = string | undefined;
90150
```
91151

152+
- **示例:**
153+
154+
```js
155+
api.modifyRspackConfig((config, { environment }) => {
156+
console.log(environment.tsconfigPath);
157+
return config;
158+
});
159+
```
160+
92161
## Environment API
93162

94-
Environment API 提供了一系列和构建环境相关的 API。
163+
Environment API 包了一些与多环境构建相关的 API。
95164

96-
你可以在 [Rsbuild DevMiddleware](/config/dev/setup-middlewares)[自定义 Server](/api/javascript-api/instance#rsbuildcreatedevserver) 中使用 environment API 对特定环境下的构建产物进行操作
165+
你可以通过 [rsbuild.createDevServer()](/api/javascript-api/instance#rsbuildcreatedevserver) [dev.setupMiddlewares](/config/dev/setup-middlewares) 使用 environment API,这允许你在服务端获取特定环境下的构建产物信息
97166

98167
```ts
99168
type EnvironmentAPI = {
@@ -140,7 +209,7 @@ type LoadBundle = <T = unknown>(entryName: string) => Promise<T>;
140209
- **示例:**
141210

142211
```ts
143-
// 加载 main 入口的构建产物
212+
// 加载 `main` 入口的 bundle
144213
const result = await environments.node.loadBundle('main');
145214
```
146215

0 commit comments

Comments
 (0)