Skip to content

Commit 784f8f9

Browse files
committed
refactor: streamline port handling in output and middleware logic
1 parent 3135d26 commit 784f8f9

File tree

3 files changed

+8
-11
lines changed

3 files changed

+8
-11
lines changed

packages/core/src/plugins/output.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ function getPublicPath({
2525
const { dev, output, server } = config;
2626

2727
let publicPath = DEFAULT_ASSET_PREFIX;
28-
const port = context.devServer?.port || server.port || DEFAULT_PORT;
2928

3029
if (isProd) {
3130
if (typeof output.assetPrefix === 'string') {
@@ -53,6 +52,7 @@ function getPublicPath({
5352
}
5453
}
5554

55+
const port = (isProd ? server.port : context.devServer?.port) ?? DEFAULT_PORT;
5656
return formatPublicPath(replacePortPlaceholder(publicPath, port));
5757
}
5858

packages/core/src/server/devMiddlewares.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { isAbsolute, join } from 'node:path';
22
import rspack from '@rspack/core';
33
import { normalizePublicDirs } from '../config';
4-
import { DEFAULT_PORT } from '../constants';
54
import { isMultiCompiler } from '../helpers';
65
import { logger } from '../logger';
76
import type {
@@ -124,13 +123,12 @@ const applyDefaultMiddlewares = async ({
124123

125124
if (
126125
typeof dev.lazyCompilation === 'object' &&
127-
typeof dev.lazyCompilation.serverUrl === 'string'
126+
typeof dev.lazyCompilation.serverUrl === 'string' &&
127+
context.devServer
128128
) {
129-
const port = context.devServer?.port || server.port || DEFAULT_PORT;
130-
131129
dev.lazyCompilation.serverUrl = replacePortPlaceholder(
132130
dev.lazyCompilation.serverUrl,
133-
port,
131+
context.devServer.port,
134132
);
135133
}
136134

packages/core/src/server/helper.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import net from 'node:net';
44
import type { Socket } from 'node:net';
55
import os from 'node:os';
66
import { posix, relative, sep } from 'node:path';
7-
import { DEFAULT_DEV_HOST, DEFAULT_PORT } from '../constants';
7+
import { DEFAULT_DEV_HOST } from '../constants';
88
import {
99
addTrailingSlash,
1010
color,
@@ -310,14 +310,13 @@ export const getServerConfig = async ({
310310
https: boolean;
311311
portTip: string | undefined;
312312
}> => {
313-
const host = config.server.host || DEFAULT_DEV_HOST;
314-
const originalPort = config.server.port || DEFAULT_PORT;
313+
const { host, port: originalPort, strictPort } = config.server;
315314
const port = await getPort({
316315
host,
317316
port: originalPort,
318-
strictPort: config.server.strictPort || false,
317+
strictPort,
319318
});
320-
const https = Boolean(config.server.https) || false;
319+
const https = Boolean(config.server.https);
321320
const portTip =
322321
port !== originalPort
323322
? `port ${originalPort} is in use, ${color.yellow(`using port ${port}.`)}`

0 commit comments

Comments
 (0)