Skip to content

Commit 749ee50

Browse files
authored
refactor: migrate types to use rspack/core definitions (#158)
1 parent 9dc1a86 commit 749ee50

5 files changed

Lines changed: 75 additions & 149 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
},
4747
"dependencies": {
4848
"@types/connect-history-api-fallback": "^1.5.4",
49-
"@types/serve-static": "^2.2.0",
5049
"@types/ws": "^8.18.1",
5150
"chokidar": "^5.0.0",
5251
"connect-history-api-fallback": "^2.0.0",
@@ -67,6 +66,7 @@
6766
"@types/mime-types": "3.0.1",
6867
"@types/node": "^24.12.0",
6968
"@types/node-forge": "^1.3.14",
69+
"@types/serve-static": "^2.2.0",
7070
"@types/trusted-types": "^2.0.7",
7171
"@types/ws": "8.18.1",
7272
"cross-env": "^10.1.0",

pnpm-lock.yaml

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/config.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import type { DevServerOpenOptions } from '@rspack/core';
1+
import type { DevServerClient, DevServerOpenOptions } from '@rspack/core';
22
import type {
3-
ClientConfiguration,
43
ConnectHistoryApiFallbackOptions,
54
DevServer,
65
NormalizedStatic,
@@ -20,7 +19,7 @@ export interface ResolvedDevServer extends DevServer {
2019
liveReload: boolean;
2120
webSocketServer: false | WebSocketServerConfiguration;
2221
proxy: Required<DevServer['proxy']>;
23-
client: ClientConfiguration;
22+
client: DevServerClient;
2423
allowedHosts: 'auto' | string[] | 'all';
2524
compress: boolean;
2625
historyApiFallback: false | ConnectHistoryApiFallbackOptions;

src/server.ts

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,32 @@ import * as os from 'node:os';
1414
import * as path from 'node:path';
1515
import * as url from 'node:url';
1616
import * as util from 'node:util';
17+
import type {
18+
DevServerClient,
19+
DevServerHeaders,
20+
DevServerHost,
21+
DevServerMiddlewareHandler,
22+
DevServerProxyConfigArray,
23+
DevServerProxyConfigArrayItem,
24+
DevServerStatic,
25+
DevServerStaticItem,
26+
DevServerWebSocketURL,
27+
} from '@rspack/core';
28+
import type {
29+
HandleFunction,
30+
NextFunction,
31+
NextHandleFunction,
32+
SimpleHandleFunction,
33+
} from 'connect-next';
1734
import compression from 'http-compression';
1835
import ipaddr from 'ipaddr.js';
36+
import type { App } from 'open';
1937
import { getPort } from './getPort';
2038
import { WebsocketServer } from './servers/WebsocketServer';
2139
import type {
2240
AddressInfo,
2341
BasicApplication,
2442
BasicServer,
25-
ClientConfiguration,
2643
ClientConnection,
2744
Compiler,
2845
ConnectHistoryApiFallbackOptions,
@@ -32,37 +49,26 @@ import type {
3249
EXPECTED_ANY,
3350
FSWatcher,
3451
HTTPServer,
35-
HandleFunction,
36-
Headers,
37-
Host,
3852
IPv6,
3953
IncomingMessage,
4054
LiteralUnion,
4155
Middleware,
42-
MiddlewareHandler,
4356
MiddlewareObject,
4457
MultiCompiler,
4558
MultiStats,
4659
NetworkInterfaceInfo,
47-
NextFunction,
48-
NextHandleFunction,
4960
NormalizedOpen,
5061
NormalizedStatic,
5162
Open,
52-
OpenApp,
5363
OverlayMessageOptions,
5464
Port,
55-
ProxyConfigArray,
56-
ProxyConfigArrayItem,
5765
Request,
5866
RequestHandler,
5967
Response,
6068
ServerConfiguration,
6169
ServerOptions,
6270
ServerType,
63-
SimpleHandleFunction,
6471
Socket,
65-
Static,
6672
Stats,
6773
StatsCompilation,
6874
StatsOptions,
@@ -71,7 +77,6 @@ import type {
7177
WebSocketServer,
7278
WebSocketServerConfiguration,
7379
WebSocketServerImplementation,
74-
WebSocketURL,
7580
} from './types';
7681
import type { ConnectApplication } from './types';
7782

@@ -83,7 +88,7 @@ export interface Configuration<
8388
S extends BasicServer = HTTPServer,
8489
> {
8590
ipc?: boolean | string;
86-
host?: Host;
91+
host?: DevServerHost;
8792
port?: Port;
8893
hot?: boolean | 'only';
8994
liveReload?: boolean;
@@ -92,24 +97,24 @@ export interface Configuration<
9297
allowedHosts?: LiteralUnion<'auto' | 'all', string> | string[];
9398
historyApiFallback?: boolean | ConnectHistoryApiFallbackOptions;
9499
watchFiles?: string | string[] | WatchFiles | Array<string | WatchFiles>;
95-
static?: boolean | string | Static | Array<string | Static>;
100+
static?: DevServerStatic;
96101
server?: ServerType<A, S> | ServerConfiguration<A, S>;
97102
app?: () => Promise<A>;
98103
webSocketServer?:
99104
| boolean
100105
| LiteralUnion<'ws', string>
101106
| WebSocketServerConfiguration;
102-
proxy?: ProxyConfigArray;
107+
proxy?: DevServerProxyConfigArray;
103108
open?: boolean | string | Open | Array<string | Open>;
104109
setupExitSignals?: boolean;
105-
client?: boolean | ClientConfiguration;
110+
client?: boolean | DevServerClient;
106111
headers?:
107-
| Headers
112+
| DevServerHeaders
108113
| ((
109114
req: Request,
110115
res: Response,
111116
context: DevMiddlewareContext<Request, Response> | undefined,
112-
) => Headers);
117+
) => DevServerHeaders);
113118
onListening?: (devServer: Server<A, S>) => void;
114119
setupMiddlewares?: (
115120
middlewares: Middleware[],
@@ -315,7 +320,7 @@ class Server<
315320
}
316321
}
317322

318-
static async getHostname(hostname: Host) {
323+
static async getHostname(hostname: DevServerHost) {
319324
if (hostname === 'local-ip') {
320325
return (
321326
Server.findIp('v4', false) || Server.findIp('v6', false) || '0.0.0.0'
@@ -399,8 +404,8 @@ class Server<
399404
let webSocketURLStr = '';
400405

401406
if (this.options.webSocketServer) {
402-
const webSocketURL = (this.options.client as ClientConfiguration)
403-
.webSocketURL as WebSocketURL;
407+
const webSocketURL = (this.options.client as DevServerClient)
408+
.webSocketURL as DevServerWebSocketURL;
404409
const webSocketServer = this.options.webSocketServer as {
405410
type: WebSocketServerConfiguration['type'];
406411
options: NonNullable<WebSocketServerConfiguration['options']>;
@@ -493,7 +498,7 @@ class Server<
493498

494499
searchParams.set('pathname', pathname);
495500

496-
const client = this.options.client as ClientConfiguration;
501+
const client = this.options.client as DevServerClient;
497502

498503
if (typeof client.logging !== 'undefined') {
499504
searchParams.set('logging', client.logging);
@@ -650,7 +655,7 @@ class Server<
650655
};
651656
};
652657
const getStaticItem = (
653-
optionsForStatic?: string | Static,
658+
optionsForStatic?: string | DevServerStaticItem,
654659
): NormalizedStatic => {
655660
const getDefaultStaticOptions = () => {
656661
return {
@@ -1209,10 +1214,10 @@ class Server<
12091214

12101215
if (this.options.client) {
12111216
if (
1212-
typeof (this.options.client as ClientConfiguration)
1213-
.webSocketTransport !== 'undefined'
1217+
typeof (this.options.client as DevServerClient).webSocketTransport !==
1218+
'undefined'
12141219
) {
1215-
clientTransport = (this.options.client as ClientConfiguration)
1220+
clientTransport = (this.options.client as DevServerClient)
12161221
.webSocketTransport;
12171222
} else if (isKnownWebSocketServerImplementation) {
12181223
clientTransport = (
@@ -1434,7 +1439,7 @@ class Server<
14341439

14351440
if (
14361441
this.options.client &&
1437-
(this.options.client as ClientConfiguration).progress
1442+
(this.options.client as DevServerClient).progress
14381443
) {
14391444
this.#setupProgressPlugin();
14401445
}
@@ -1644,7 +1649,7 @@ class Server<
16441649

16451650
middlewares.push({
16461651
name: 'webpack-dev-middleware',
1647-
middleware: this.middleware as MiddlewareHandler,
1652+
middleware: this.middleware as DevServerMiddlewareHandler,
16481653
});
16491654

16501655
middlewares.push({
@@ -1771,7 +1776,7 @@ class Server<
17711776
const { createProxyMiddleware } = require('http-proxy-middleware');
17721777

17731778
const getProxyMiddleware = (
1774-
proxyConfig: ProxyConfigArrayItem,
1779+
proxyConfig: DevServerProxyConfigArrayItem,
17751780
): RequestHandler | undefined => {
17761781
const { context, ...proxyOptions } = proxyConfig;
17771782
const pathFilter = proxyOptions.pathFilter ?? context;
@@ -1874,7 +1879,7 @@ class Server<
18741879

18751880
middlewares.push({
18761881
name: 'webpack-dev-middleware',
1877-
middleware: this.middleware as MiddlewareHandler,
1882+
middleware: this.middleware as DevServerMiddlewareHandler,
18781883
});
18791884
}
18801885

@@ -1923,7 +1928,7 @@ class Server<
19231928
// it is able to handle '/index.html' request after redirect
19241929
middlewares.push({
19251930
name: 'webpack-dev-middleware',
1926-
middleware: this.middleware as MiddlewareHandler,
1931+
middleware: this.middleware as DevServerMiddlewareHandler,
19271932
});
19281933

19291934
if (staticOptions.length > 0) {
@@ -1982,7 +1987,7 @@ class Server<
19821987

19831988
if (typeof (item as MiddlewareObject).middleware === 'undefined') {
19841989
(item as MiddlewareObject).middleware =
1985-
lazyInitDevMiddleware() as unknown as MiddlewareHandler;
1990+
lazyInitDevMiddleware() as unknown as DevServerMiddlewareHandler;
19861991
}
19871992
}
19881993
}
@@ -2087,31 +2092,31 @@ class Server<
20872092

20882093
if (
20892094
this.options.client &&
2090-
(this.options.client as ClientConfiguration).progress
2095+
(this.options.client as DevServerClient).progress
20912096
) {
20922097
this.sendMessage(
20932098
[client],
20942099
'progress',
2095-
(this.options.client as ClientConfiguration).progress,
2100+
(this.options.client as DevServerClient).progress,
20962101
);
20972102
}
20982103

20992104
if (
21002105
this.options.client &&
2101-
(this.options.client as ClientConfiguration).reconnect
2106+
(this.options.client as DevServerClient).reconnect
21022107
) {
21032108
this.sendMessage(
21042109
[client],
21052110
'reconnect',
2106-
(this.options.client as ClientConfiguration).reconnect,
2111+
(this.options.client as DevServerClient).reconnect,
21072112
);
21082113
}
21092114

21102115
if (
21112116
this.options.client &&
2112-
(this.options.client as ClientConfiguration).overlay
2117+
(this.options.client as DevServerClient).overlay
21132118
) {
2114-
const overlayConfig = (this.options.client as ClientConfiguration)
2119+
const overlayConfig = (this.options.client as DevServerClient)
21152120
.overlay;
21162121

21172122
this.sendMessage(
@@ -2163,7 +2168,7 @@ class Server<
21632168
// Type assertion needed: OpenOptions is compatible at runtime but TypeScript can't verify
21642169
// the type match between our type definition and the ES module's type in CommonJS context
21652170
return open(openTarget, item.options as EXPECTED_ANY).catch(() => {
2166-
const app = item.options.app as OpenApp | undefined;
2171+
const app = item.options.app as App;
21672172
this.logger.warn(
21682173
`Unable to open "${openTarget}" page${
21692174
app
@@ -2342,12 +2347,12 @@ class Server<
23422347
// Also allow if `client.webSocketURL.hostname` provided
23432348
if (
23442349
this.options.client &&
2345-
typeof (this.options.client as ClientConfiguration).webSocketURL !==
2350+
typeof (this.options.client as DevServerClient).webSocketURL !==
23462351
'undefined'
23472352
) {
23482353
return (
2349-
((this.options.client as ClientConfiguration).webSocketURL as
2350-
| WebSocketURL['hostname']
2354+
((this.options.client as DevServerClient).webSocketURL as
2355+
| DevServerWebSocketURL['hostname']
23512356
| undefined) === value
23522357
);
23532358
}
@@ -2589,10 +2594,12 @@ class Server<
25892594
});
25902595
});
25912596
} else {
2592-
this.options.host = await Server.getHostname(this.options.host as Host);
2597+
this.options.host = await Server.getHostname(
2598+
this.options.host as DevServerHost,
2599+
);
25932600
this.options.port = await Server.getFreePort(
25942601
this.options.port as string,
2595-
this.options.host as Host,
2602+
this.options.host as DevServerHost,
25962603
);
25972604
}
25982605

0 commit comments

Comments
 (0)