Skip to content

Commit 222a37d

Browse files
committed
feat: export server.printUrls method
1 parent 40818cc commit 222a37d

File tree

3 files changed

+83
-61
lines changed

3 files changed

+83
-61
lines changed

packages/core/src/server/devServer.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type HTTPServer = Server | Http2SecureServer;
3737
export type RsbuildDevServer = {
3838
/**
3939
* Listen the Rsbuild server.
40+
* Do not call this method if you are using a custom server.
4041
*/
4142
listen: () => Promise<{
4243
port: number;
@@ -45,14 +46,10 @@ export type RsbuildDevServer = {
4546
close: () => Promise<void>;
4647
};
4748
}>;
48-
49-
/** The following APIs can be used when using a custom server */
50-
5149
/**
52-
* The Rsbuild server environment API
50+
* Environment API of Rsbuild server.
5351
*/
5452
environments: EnvironmentAPI;
55-
5653
/**
5754
* The resolved port.
5855
* By default, Rsbuild server listens on port `3000` and automatically increments the port number if the port is occupied.
@@ -70,13 +67,17 @@ export type RsbuildDevServer = {
7067
afterListen: () => Promise<void>;
7168
/**
7269
* Activate socket connection.
73-
* This is used if you are using a custom server.
70+
* This ensures that HMR works properly.
7471
*/
7572
connectWebSocket: (options: { server: HTTPServer }) => void;
7673
/**
7774
* Close the Rsbuild server.
7875
*/
7976
close: () => Promise<void>;
77+
/**
78+
* Print the server URLs.
79+
*/
80+
printUrls: () => void;
8081
};
8182

8283
const formatDevConfig = (config: NormalizedDevConfig, port: number) => {
@@ -182,25 +183,21 @@ export async function createDevServer<
182183
environments: options.context.environments,
183184
});
184185

185-
if (runCompile) {
186-
options.context.hooks.onBeforeCreateCompiler.tap(() => {
187-
// print server url should between listen and beforeCompile
188-
printServerURLs({
189-
urls,
190-
port,
191-
routes,
192-
protocol,
193-
printUrls: config.server.printUrls,
194-
});
195-
});
196-
} else {
186+
const printUrls = () => {
197187
printServerURLs({
198188
urls,
199189
port,
200190
routes,
201191
protocol,
202192
printUrls: config.server.printUrls,
203193
});
194+
};
195+
196+
if (runCompile) {
197+
// print server url should between listen and beforeCompile
198+
options.context.hooks.onBeforeCreateCompiler.tap(printUrls);
199+
} else {
200+
printUrls();
204201
}
205202

206203
const compileMiddlewareAPI = runCompile ? await startCompile() : undefined;
@@ -339,6 +336,7 @@ export async function createDevServer<
339336
await devMiddlewares.close();
340337
await fileWatcher?.close();
341338
},
339+
printUrls,
342340
};
343341

344342
logger.debug('create dev server done');

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

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ await rsbuild.startDevServer({
242242

243243
Rsbuild comes with a built-in dev server designed to improve the development experience. When you run the `rsbuild dev` command, the server will start, providing features such as page preview, routing, and hot module reloading.
244244

245-
If you want to integrate Rsbuild dev server into an custom server, you can use this method to get the instance methods of Rsbuild dev server and call them on demand.
245+
If you want to integrate Rsbuild dev server into an custom server, you can use this method to get the instance methods of dev server and call them on demand.
246246

247247
- **Type:**
248248

@@ -253,15 +253,13 @@ type EnvironmentAPI = {
253253
* Get stats info about current environment.
254254
*/
255255
getStats: () => Promise<Stats>;
256-
257256
/**
258257
* Load and execute stats bundle in server.
259258
*
260259
* @param entryName - relate to Rsbuild's `source.entry`
261260
* @returns the return value of entry module.
262261
*/
263262
loadBundle: <T = unknown>(entryName: string) => Promise<T>;
264-
265263
/**
266264
* Get the compiled HTML template.
267265
*/
@@ -270,39 +268,49 @@ type EnvironmentAPI = {
270268
};
271269

272270
type RsbuildDevServer = {
273-
/** start the Rsbuild DevServer */
271+
/**
272+
* Listen the Rsbuild server.
273+
* Do not call this method if you are using a custom server.
274+
*/
274275
listen: () => Promise<{
275-
urls: string[];
276276
port: number;
277-
server: Server;
277+
urls: string[];
278+
server: {
279+
close: () => Promise<void>;
280+
};
278281
}>;
279-
280-
/** The following APIs will be used when you use a custom server */
281-
282-
/** The Rsbuild server environment API */
282+
/**
283+
* Environment API of Rsbuild server.
284+
*/
283285
environments: EnvironmentAPI;
284-
285286
/**
286-
* The resolved port
287-
*
288-
* By default, Rsbuild Server listens on port `3000` and automatically increments the port number when the port is occupied.
287+
* The resolved port.
288+
* By default, Rsbuild server listens on port `3000` and automatically increments the port number if the port is occupied.
289289
*/
290290
port: number;
291-
292291
/**
293-
* connect app instance.
294-
*
292+
* The `connect` app instance.
295293
* Can be used to attach custom middlewares to the dev server.
296294
*/
297-
middlewares: Middlewares;
298-
299-
/** Notify Rsbuild that the custom server has started */
295+
middlewares: Connect.Server;
296+
/**
297+
* Notify that the Rsbuild server has been started.
298+
* Rsbuild will trigger `onAfterStartDevServer` hook in this stage.
299+
*/
300300
afterListen: () => Promise<void>;
301-
301+
/**
302+
* Activate socket connection.
303+
* This ensures that HMR works properly.
304+
*/
302305
connectWebSocket: (options: { server: HTTPServer }) => void;
303-
304-
/** close the Rsbuild DevServer */
306+
/**
307+
* Close the Rsbuild server.
308+
*/
305309
close: () => Promise<void>;
310+
/**
311+
* Print the server URLs.
312+
*/
313+
printUrls: () => void;
306314
};
307315

308316
type CreateDevServerOptions = {

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

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ await rsbuild.startDevServer({
264264

265265
Rsbuild 配备了一个内置的开发服务器,当你执行 `rsbuild dev` 时,将启动 Rsbuild dev server,并提供页面预览、路由、模块热更新等功能。
266266

267-
如果你希望将 Rsbuild dev server 集成到自定义的 server 中,可以通过该方法获取 Rsbuild dev server 的实例方法,进行按需调用
267+
如果你希望将 Rsbuild dev server 集成到自定义的 server 中,可以通过该方法获取 dev server 的实例方法,按需进行调用
268268

269269
- **类型:**
270270

@@ -275,15 +275,13 @@ type EnvironmentAPI = {
275275
* 获取当前环境的构建信息
276276
*/
277277
getStats: () => Promise<Stats>;
278-
279278
/**
280279
* 在服务端加载并执行构建产物
281280
*
282281
* @param entryName - 入口名称,和 Rsbuild source.entry 的某一个 key 值对应
283282
* @returns 入口模块的返回值
284283
*/
285284
loadBundle: <T = unknown>(entryName: string) => Promise<T>;
286-
287285
/**
288286
* 获取编译后的 HTML 模版内容
289287
*/
@@ -292,31 +290,49 @@ type EnvironmentAPI = {
292290
};
293291

294292
type RsbuildDevServer = {
295-
/** 启动 Rsbuild DevServer */
293+
/**
294+
* 监听 Rsbuild server
295+
* 当你使用自定义 server 时,不需要调用该方法
296+
*/
296297
listen: () => Promise<{
297-
urls: string[];
298298
port: number;
299-
server: Server;
299+
urls: string[];
300+
server: {
301+
close: () => Promise<void>;
302+
};
300303
}>;
301-
302-
/** 以下 API 在使用自定义 Server 时会用到 */
303-
304-
/** Rsbuild server environment API */
304+
/**
305+
* Rsbuild server 提供的 environment API
306+
*/
305307
environments: EnvironmentAPI;
306-
307-
/** 解析后的端口号 (默认情况下,Rsbuild Server 会监听 `3000` 端口,并在端口被占用时自动递增端口号。) */
308+
/**
309+
* 解析后的端口号
310+
* 默认情况下,Rsbuild Server 会监听 `3000` 端口,如果端口被占用,则自动递增端口号
311+
*/
308312
port: number;
309-
310-
/** Connect 实例,包含 Rsbuild 内置中间件 */
311-
middlewares: Middlewares;
312-
313-
/** 通知 Rsbuild 自定义 Server 已启动 */
313+
/**
314+
* `connect` 实例
315+
* 可用于向 dev server 附加自定义中间件
316+
*/
317+
middlewares: Connect.Server;
318+
/**
319+
* 通知 Rsbuild 自定义 Server 已启动
320+
* Rsbuild 会在此阶段触发 `onAfterStartDevServer` 钩子
321+
*/
314322
afterListen: () => Promise<void>;
315-
323+
/**
324+
* 激活 socket 连接
325+
* 这确保了 HMR 正常工作
326+
*/
316327
connectWebSocket: (options: { server: HTTPServer }) => void;
317-
318-
/** 关闭 Rsbuild DevServer */
328+
/**
329+
* 关闭 Rsbuild server
330+
*/
319331
close: () => Promise<void>;
332+
/**
333+
* 打印 server URLs
334+
*/
335+
printUrls: () => void;
320336
};
321337

322338
type CreateDevServerOptions = {

0 commit comments

Comments
 (0)