Skip to content

Commit a51d2a8

Browse files
authored
refactor: streamline port handling in output and middleware logic (#5039)
1 parent 3135d26 commit a51d2a8

File tree

5 files changed

+16
-15
lines changed

5 files changed

+16
-15
lines changed

packages/core/src/plugins/output.ts

Lines changed: 2 additions & 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,8 @@ function getPublicPath({
5352
}
5453
}
5554

55+
const defaultPort = server.port ?? DEFAULT_PORT;
56+
const port = isProd ? defaultPort : (context.devServer?.port ?? defaultPort);
5657
return formatPublicPath(replacePortPlaceholder(publicPath, port));
5758
}
5859

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}.`)}`

packages/core/tests/output.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createStubRsbuild } from '@scripts/test-helper';
2+
import { createRsbuild } from '../src';
23
import { pluginOutput } from '../src/plugins/output';
34

45
describe('plugin-output', () => {
@@ -179,8 +180,8 @@ describe('plugin-output', () => {
179180
});
180181

181182
it('should replace `<port>` placeholder with server.port', async () => {
182-
const rsbuild = await createStubRsbuild({
183-
plugins: [pluginOutput()],
183+
vi.stubEnv('NODE_ENV', 'development');
184+
const rsbuild = await createRsbuild({
184185
rsbuildConfig: {
185186
server: { port: 4000 },
186187
dev: {
@@ -190,12 +191,12 @@ describe('plugin-output', () => {
190191
});
191192
const [config] = await rsbuild.initConfigs();
192193
expect(config?.output?.publicPath).toEqual('http://example-4000.com:4000/');
194+
vi.unstubAllEnvs();
193195
});
194196

195197
it('should replace `<port>` placeholder of `output.assetPrefix` with default port', async () => {
196198
vi.stubEnv('NODE_ENV', 'production');
197-
const rsbuild = await createStubRsbuild({
198-
plugins: [pluginOutput()],
199+
const rsbuild = await createRsbuild({
199200
rsbuildConfig: {
200201
output: {
201202
assetPrefix: 'http://example.com:<port>/',
@@ -205,5 +206,6 @@ describe('plugin-output', () => {
205206

206207
const [config] = await rsbuild.initConfigs();
207208
expect(config?.output?.publicPath).toEqual('http://example.com:3000/');
209+
vi.unstubAllEnvs();
208210
});
209211
});

scripts/dictionary.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ unocss
134134
unpatch
135135
unplugin
136136
unshift
137+
unstub
137138
upath
138139
vitest
139140
vnode

0 commit comments

Comments
 (0)