Skip to content

Commit 28d028d

Browse files
authored
feat(cli): support dev --port/host and preview --port/host (#745)
1 parent 8d09c89 commit 28d028d

File tree

8 files changed

+49
-0
lines changed

8 files changed

+49
-0
lines changed

.changeset/empty-lions-float.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@rsbuild/core': patch
3+
---
4+
5+
feat(cli): cli support `dev --port/--host` and `preview --port/--host`

packages/core/src/cli/commands.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import type { RsbuildMode } from '..';
88
export type CommonOptions = {
99
config?: string;
1010
open?: boolean;
11+
host?: string;
12+
port?: number;
1113
};
1214

1315
export type BuildOptions = CommonOptions & {
@@ -47,6 +49,16 @@ export async function init({
4749
config.dev.startUrl = true;
4850
}
4951

52+
if (commonOpts.host) {
53+
config.server ||= {};
54+
config.server.host = commonOpts.host;
55+
}
56+
57+
if (commonOpts.port) {
58+
config.server ||= {};
59+
config.server.port = commonOpts.port;
60+
}
61+
5062
return await createRsbuild({
5163
rsbuildConfig: config,
5264
provider: config.provider,
@@ -66,6 +78,14 @@ export function runCli() {
6678
program
6779
.command('dev')
6880
.option('--open', 'open the page in browser on startup')
81+
.option(
82+
'--port <port>',
83+
'specify a port number for Rsbuild Server to listen',
84+
)
85+
.option(
86+
'--host <host>',
87+
'specify the host that the Rsbuild Server listens to',
88+
)
6989
.option(
7090
'-c --config <config>',
7191
'specify the configuration file, can be a relative or absolute path',
@@ -106,6 +126,14 @@ export function runCli() {
106126
program
107127
.command('preview')
108128
.option('--open', 'open the page in browser on startup')
129+
.option(
130+
'--port <port>',
131+
'specify a port number for Rsbuild Server to listen',
132+
)
133+
.option(
134+
'--host <host>',
135+
'specify the host that the Rsbuild Server listens to',
136+
)
109137
.option(
110138
'-c --config <config>',
111139
'specify the configuration file, can be a relative or absolute path',

packages/document/docs/en/guide/basic/cli.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Usage: rsbuild dev [options]
1313

1414
Options:
1515
--open open the page in browser on startup
16+
--port <port> specify a port number for Rsbuild Server to listen
17+
--host <host> specify the host that the Rsbuild Server listens to
1618
-c --config <config> specify the configuration file, can be a relative or absolute path
1719
-h, --help display help for command
1820
```
@@ -39,6 +41,8 @@ Usage: rsbuild preview [options]
3941

4042
Options:
4143
--open open the page in browser on startup
44+
--port <port> specify a port number for Rsbuild Server to listen
45+
--host <host> specify the host that the Rsbuild Server listens to
4246
-c --config <config> specify the configuration file, can be a relative or absolute path
4347
-h, --help display help for command
4448
```

packages/document/docs/en/shared/config/server/host.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Specify the host that the Rsbuild Server listens to.
55

66
By default, the Rsbuild Server will listen to `0.0.0.0`, which means listening to all network interfaces, including `localhost` and public network addresses.
77

8+
You can use `server.host` or the `--host` CLI param to set the host (The priority of `--host` option is higher than `server.host`).
9+
810
If you want the Rsbuild Server to listen only on `localhost`, you can set it to:
911

1012
```ts

packages/document/docs/en/shared/config/server/port.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Specify a port number for Rsbuild Server to listen.
55

66
By default, Rsbuild Server listens on port `8080` and automatically increments the port number when the port is occupied.
77

8+
You can use `server.port` or the `--port` CLI param to set the port number (The priority of `--port` option is higher than `server.port`).
9+
810
### Example
911

1012
Set the port to `3000`:

packages/document/docs/zh/guide/basic/cli.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Usage: rsbuild dev [options]
1313

1414
Options:
1515
--open 启动时是否在浏览器中打开页面
16+
--port <port> 设置 Rsbuild Server 监听的端口号
17+
--host <host> 指定 Rsbuild Server 启动时监听的 host
1618
-c --config <config> 指定配置文件路径,可以为相对路径或绝对路径
1719
-h, --help 显示命令帮助
1820
```
@@ -39,6 +41,8 @@ Usage: rsbuild preview [options]
3941

4042
Options:
4143
--open 启动时是否在浏览器中打开页面
44+
--port <port> 设置 Rsbuild Server 监听的端口号
45+
--host <host> 指定 Rsbuild Server 启动时监听的 host
4246
-c --config <config> 指定配置文件路径,可以为相对路径或绝对路径
4347
-h, --help 显示命令帮助
4448
```

packages/document/docs/zh/shared/config/server/host.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
默认情况下,Rsbuild Server 会监听 `0.0.0.0`,这代表监听所有的网络接口,包括 `localhost` 和公网地址。
77

8+
你可以使用 `server.host` 配置或 `--host` 参数来指定 host (`--host` 参数的优先级高于 `server.host` 配置)。
9+
810
如果你希望 Rsbuild Server 只监听 `localhost`,可以设置为:
911

1012
```ts

packages/document/docs/zh/shared/config/server/port.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
默认情况下,Rsbuild Server 会监听 8080 端口,并在端口被占用时自动递增端口号。
77

8+
你可以使用 `server.port` 配置或 `--port` 参数来设置端口号 (`--port` 参数的优先级高于 `server.port` 配置)。
9+
810
### 示例
911

1012
将端口设置为 `3000`

0 commit comments

Comments
 (0)