Skip to content

Commit 37546e1

Browse files
authored
docs: update context.devServer with examples (#5267)
1 parent e2d40a3 commit 37546e1

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

packages/core/src/types/context.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,29 @@ export type RsbuildContext = {
1515
distPath: string;
1616
/** Absolute path of cache files. */
1717
cachePath: string;
18-
/** Info of dev server */
18+
/**
19+
* Dev server information when running in dev mode.
20+
* Available after the dev server has been created.
21+
*
22+
* @example
23+
* ```ts
24+
* import { createRsbuild } from '@rsbuild/core';
25+
*
26+
* async function main() {
27+
* const rsbuild = createRsbuild({
28+
* // ...
29+
* });
30+
* await rsbuild.startDevServer();
31+
* console.log(rsbuild.context.devServer); // { hostname: 'localhost', port: 3000, https: false }
32+
* }
33+
* ```
34+
*/
1935
devServer?: {
36+
/** The hostname the server is running on. */
2037
hostname: string;
38+
/** The port number the server is listening on. */
2139
port: number;
40+
/** Whether the server is using HTTPS protocol. */
2241
https: boolean;
2342
};
2443
/**

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,33 @@ Here are some tools based on Rsbuild that have already set the `callerName` valu
106106

107107
### context.devServer
108108

109-
Dev server information, including the current dev server hostname and port number.
109+
Dev server information when running in dev mode. Available after the dev server has been created.
110110

111111
- **Type:**
112112

113113
```ts
114114
type DevServer = {
115115
hostname: string;
116116
port: number;
117+
https: boolean;
117118
};
118119
```
119120

121+
- **Example:**
122+
123+
```ts
124+
import { createRsbuild } from '@rsbuild/core';
125+
126+
async function main() {
127+
const rsbuild = createRsbuild({
128+
// ...
129+
});
130+
await rsbuild.startDevServer();
131+
132+
console.log(rsbuild.context.devServer); // { hostname: 'localhost', port: 3000, https: false }
133+
}
134+
```
135+
120136
### context.action
121137

122138
The current action type.

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,33 @@ export const myPlugin = {
106106

107107
### context.devServer
108108

109-
Dev server 相关信息,包含了当前 dev server 的 hostname 和端口号
109+
在开发模式下运行时的 dev server 信息。仅在 dev server 创建后可访问
110110

111111
- **类型:**
112112

113113
```ts
114114
type DevServer = {
115115
hostname: string;
116116
port: number;
117+
https: boolean;
117118
};
118119
```
119120

121+
- **示例:**
122+
123+
```ts
124+
import { createRsbuild } from '@rsbuild/core';
125+
126+
async function main() {
127+
const rsbuild = createRsbuild({
128+
// ...
129+
});
130+
await rsbuild.startDevServer();
131+
132+
console.log(rsbuild.context.devServer); // { hostname: 'localhost', port: 3000, https: false }
133+
}
134+
```
135+
120136
### context.action
121137

122138
当前的动作类型。

0 commit comments

Comments
 (0)