Skip to content

Commit e270daf

Browse files
authored
fix: align devServer options with Rspack v2 config (#90)
1 parent ada6dba commit e270daf

File tree

5 files changed

+153
-260
lines changed

5 files changed

+153
-260
lines changed

README.md

Lines changed: 25 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -898,71 +898,31 @@ config.node(false)
898898
config.devServer : ChainedMap
899899
```
900900
901-
#### devServer allowedHosts
902-
903-
```js
904-
config.devServer.allowedHosts : ChainedSet
905-
906-
config.devServer.allowedHosts
907-
.add(value)
908-
.prepend(value)
909-
.clear()
910-
```
911-
912901
#### devServer: shorthand methods
913902
914903
```js
915904
config.devServer
916-
.after(after)
917-
.before(before)
918-
.bonjour(bonjour)
919-
.clientLogLevel(clientLogLevel)
905+
.allowedHosts(allowedHosts)
906+
.app(app)
907+
.client(client)
920908
.compress(compress)
921-
.contentBase(contentBase)
922-
.contentBasePublicPath(contentBasePublicPath)
923-
.disableHostCheck(disableHostCheck)
924-
.filename(filename)
909+
.devMiddleware(devMiddleware)
925910
.headers(headers)
926911
.historyApiFallback(historyApiFallback)
927912
.host(host)
928913
.hot(hot)
929-
.hotOnly(hotOnly)
930-
.http2(http2)
931-
.https(https)
932-
.index(index)
933-
.injectClient(injectClient)
934-
.injectHot(injectHot)
935-
.inline(inline)
936-
.lazy(lazy)
937914
.liveReload(liveReload)
938-
.mimeTypes(mimeTypes)
939-
.noInfo(noInfo)
915+
.ipc(ipc)
940916
.onListening(onListening)
941917
.open(open)
942-
.openPage(openPage)
943-
.overlay(overlay)
944-
.pfx(pfx)
945-
.pfxPassphrase(pfxPassphrase)
946918
.port(port)
947-
.progress(progress)
948919
.proxy(proxy)
949-
.public(public)
950-
.publicPath(publicPath)
951-
.quiet(quiet)
952-
.serveIndex(serveIndex)
953-
.setup(setup)
954-
.socket(socket)
955-
.sockHost(sockHost)
956-
.sockPath(sockPath)
957-
.sockPort(sockPort)
958-
.staticOptions(staticOptions)
959-
.stats(stats)
960-
.stdin(stdin)
961-
.transportMode(transportMode)
962-
.useLocalIp(useLocalIp)
963-
.watchContentBase(watchContentBase)
964-
.watchOptions(watchOptions)
965-
.writeToDisk(writeToDisk);
920+
.server(server)
921+
.setupExitSignals(setupExitSignals)
922+
.setupMiddlewares(setupMiddlewares)
923+
.static(static)
924+
.watchFiles(watchFiles)
925+
.webSocketServer(webSocketServer);
966926
```
967927
968928
#### module
@@ -1271,26 +1231,27 @@ config.merge({
12711231
devServer: {
12721232
[key]: value,
12731233

1274-
clientLogLevel,
1234+
allowedHosts,
1235+
app,
1236+
client,
12751237
compress,
1276-
contentBase,
1277-
filename,
1238+
devMiddleware,
12781239
headers,
12791240
historyApiFallback,
12801241
host,
12811242
hot,
1282-
hotOnly,
1283-
https,
1284-
inline,
1285-
lazy,
1286-
noInfo,
1287-
overlay,
1243+
ipc,
1244+
liveReload,
1245+
onListening,
1246+
open,
12881247
port,
12891248
proxy,
1290-
quiet,
1291-
setup,
1292-
stats,
1293-
watchContentBase,
1249+
server,
1250+
setupExitSignals,
1251+
setupMiddlewares,
1252+
static,
1253+
watchFiles,
1254+
webSocketServer,
12941255
},
12951256

12961257
node: {

src/DevServer.js

Lines changed: 13 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,35 @@
11
import ChainedMap from './ChainedMap.js';
2-
import ChainedSet from './ChainedSet.js';
32

43
export default class extends ChainedMap {
54
constructor(parent) {
65
super(parent);
76

8-
this.allowedHosts = new ChainedSet(this);
9-
107
this.extend([
11-
'after',
12-
'before',
13-
'bonjour',
14-
'clientLogLevel',
8+
'allowedHosts',
9+
'app',
10+
'client',
1511
'compress',
16-
'contentBase',
17-
'contentBasePublicPath',
18-
'disableHostCheck',
19-
'filename',
12+
'devMiddleware',
2013
'headers',
21-
'historyApiFallback',
2214
'host',
15+
'historyApiFallback',
2316
'hot',
24-
'hotOnly',
25-
'http2',
26-
'https',
27-
'index',
28-
'injectClient',
29-
'injectHot',
30-
'inline',
31-
'lazy',
17+
'ipc',
3218
'liveReload',
33-
'mimeTypes',
34-
'noInfo',
3519
'onListening',
3620
'open',
37-
'openPage',
38-
'overlay',
39-
'pfx',
40-
'pfxPassphrase',
4121
'port',
4222
'proxy',
43-
'progress',
44-
'public',
45-
'publicPath',
46-
'quiet',
47-
'serveIndex',
48-
'setup',
49-
'socket',
50-
'sockHost',
51-
'sockPath',
52-
'sockPort',
53-
'staticOptions',
54-
'stats',
55-
'stdin',
56-
'transportMode',
57-
'useLocalIp',
58-
'watchContentBase',
59-
'watchOptions',
60-
'writeToDisk',
23+
'server',
24+
'setupExitSignals',
25+
'setupMiddlewares',
26+
'static',
27+
'watchFiles',
28+
'webSocketServer',
6129
]);
6230
}
6331

6432
toConfig() {
65-
return this.clean({
66-
allowedHosts: this.allowedHosts.values(),
67-
...(this.entries() || {}),
68-
});
69-
}
70-
71-
merge(obj, omit = []) {
72-
if (!omit.includes('allowedHosts') && 'allowedHosts' in obj) {
73-
this.allowedHosts.merge(obj.allowedHosts);
74-
}
75-
76-
return super.merge(obj, ['allowedHosts']);
33+
return this.clean(this.entries() || {});
7734
}
7835
}

test/DevServer.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,55 @@ test('is Chainable', () => {
77
expect(devServer.end()).toBe(parent);
88
});
99

10-
test('sets allowed hosts', () => {
10+
test('sets allowed hosts arrays', () => {
1111
const devServer = new DevServer();
12-
const instance = devServer.allowedHosts.add('https://github.com').end();
12+
const instance = devServer.allowedHosts(['https://github.com']);
1313

1414
expect(instance).toBe(devServer);
1515
expect(devServer.toConfig()).toStrictEqual({
1616
allowedHosts: ['https://github.com'],
1717
});
1818
});
1919

20+
test('sets allowed hosts scalar values', () => {
21+
const devServer = new DevServer();
22+
23+
devServer.allowedHosts('all');
24+
25+
expect(devServer.toConfig()).toStrictEqual({
26+
allowedHosts: 'all',
27+
});
28+
});
29+
30+
test('merges allowedHosts values', () => {
31+
const devServer = new DevServer();
32+
33+
devServer.allowedHosts('auto');
34+
devServer.merge({
35+
allowedHosts: ['https://github.com'],
36+
});
37+
38+
expect(devServer.toConfig()).toStrictEqual({
39+
allowedHosts: ['https://github.com'],
40+
});
41+
});
42+
43+
test('preserves omitted keys during merge', () => {
44+
const devServer = new DevServer();
45+
46+
devServer.proxy('https://example.com');
47+
devServer.merge(
48+
{
49+
proxy: 'https://rspack.rs',
50+
},
51+
['proxy'],
52+
);
53+
54+
expect(devServer.toConfig()).toStrictEqual({
55+
proxy: 'https://example.com',
56+
});
57+
});
58+
2059
test('shorthand methods', () => {
2160
const devServer = new DevServer();
2261
const obj = {};

types/index.d.ts

Lines changed: 10 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { Configuration, Compiler, RuleSetRule } from '@rspack/core';
2-
import * as https from 'node:https';
1+
import { Configuration, RuleSetRule } from '@rspack/core';
32

43
// The compiler type of Rspack / webpack are mismatch,
54
// so we use a loose type here to allow using webpack plugins.
@@ -234,86 +233,15 @@ export declare namespace RspackChain {
234233
clean(value: RspackOutput['clean']): this;
235234
}
236235

237-
// await for @types/webpack-dev-server update do v4 to remove all any
238-
class DevServer extends ChainedMap<RspackChain> {
239-
allowedHosts: TypedChainedSet<this, string>;
240-
after(value: (app: any, server: any, compiler: Compiler) => void): this;
241-
before(value: (app: any, server: any, compiler: Compiler) => void): this;
242-
bonjour(value: boolean): this;
243-
clientLogLevel(
244-
value:
245-
| 'silent'
246-
| 'trace'
247-
| 'debug'
248-
| 'info'
249-
| 'warn'
250-
| 'error'
251-
| 'none'
252-
| 'warning',
253-
): this;
254-
compress(value: boolean): this;
255-
contentBase(value: boolean | string | string[]): this;
256-
contentBasePublicPath(value: string): this;
257-
disableHostCheck(value: boolean): this;
258-
filename(value: string): this;
259-
headers(value: { [header: string]: string }): this;
260-
historyApiFallback(value: boolean | any): this;
261-
host(value: string): this;
262-
hot(value: boolean): this;
263-
hotOnly(value: boolean): this;
264-
http2(value: boolean): this;
265-
https(value: boolean | https.ServerOptions): this;
266-
index(value: string): this;
267-
injectClient(value: boolean | ((compiler: Compiler) => boolean)): this;
268-
injectHot(value: boolean | ((compiler: Compiler) => boolean)): this;
269-
inline(value: boolean): this;
270-
lazy(value: boolean): this;
271-
liveReload(value: boolean): this;
272-
mimeTypes(value: Object): this;
273-
noInfo(value: boolean): this;
274-
onListening(value: (server: any) => void): this;
275-
open(value: boolean): this;
276-
openPage(value: string | string[]): this;
277-
overlay(value: boolean | { warnings?: boolean; errors?: boolean }): this;
278-
pfx(value: string): this;
279-
pfxPassphrase(value: string): this;
280-
port(value: number): this;
281-
progress(value: boolean): this;
282-
proxy(value: any): this;
283-
public(value: string): this;
284-
publicPath(publicPath: string): this;
285-
quiet(value: boolean): this;
286-
serveIndex(value: boolean): this;
287-
setup(value: (expressApp: any) => void): this;
288-
socket(value: string): this;
289-
sockHost(value: string): this;
290-
sockPath(value: string): this;
291-
sockPort(value: number): this;
292-
staticOptions(value: any): this;
293-
stats(value: Configuration['stats']): this;
294-
stdin(value: boolean): this;
295-
transportMode(
296-
value:
297-
| 'sockjs'
298-
| 'ws'
299-
| {
300-
server: 'ws';
301-
client: object;
302-
}
303-
| {
304-
client: 'sockjs';
305-
server: object;
306-
}
307-
| {
308-
client: object;
309-
server: object;
310-
},
311-
): this;
312-
useLocalIp(value: boolean): this;
313-
watchContentBase(value: boolean): this;
314-
watchOptions(value: Configuration['watchOptions']): this;
315-
writeToDisk(value: boolean): this;
316-
}
236+
type RspackDevServer = Required<NonNullable<Configuration['devServer']>>;
237+
238+
type DevServerShorthandMethods<T> = {
239+
[K in keyof RspackDevServer]-?: (value: RspackDevServer[K]) => T;
240+
};
241+
242+
class DevServer extends TypedChainedMap<RspackChain, RspackDevServer> {}
243+
244+
interface DevServer extends DevServerShorthandMethods<DevServer> {}
317245

318246
type RspackPerformance = Exclude<
319247
Required<NonNullable<Configuration['performance']>>,

0 commit comments

Comments
 (0)