Skip to content

Commit 02ad09c

Browse files
authored
feat: port webpack-dev-server (#68)
1 parent b56da26 commit 02ad09c

45 files changed

Lines changed: 7227 additions & 827 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ server.startCallback(() => {
107107
108108
## Credits
109109

110-
Thanks to the [webpack-dev-server](https://github.com/webpack/webpack-dev-server) project created by [@sokra](https://github.com/sokra)
110+
This plugin is forked from [webpack-dev-server](https://github.com/webpack/webpack-dev-server), and is used to smooth out some differences between rspack and webpack, while also providing rspack-specific new features.
111+
112+
> Thanks to the [webpack-dev-server](https://github.com/webpack/webpack-dev-server) project created by [@sokra](https://github.com/sokra)
111113
112114
## License
113115

biome.jsonc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"client-src/**/*",
66
"client/**/*.js",
77
"dist/**/*",
8-
"tests/fixtures/**/*",
8+
"tests/**/*",
9+
"scripts/**/*",
910
],
1011
},
1112
}

client-src/clients/SockJSClient.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* The following code is modified based on
3+
* https://github.com/webpack/webpack-dev-server
4+
*
5+
* MIT Licensed
6+
* Author Tobias Koppers @sokra
7+
* Copyright (c) JS Foundation and other contributors
8+
* https://github.com/webpack/webpack-dev-server/blob/main/LICENSE
9+
*/
10+
11+
import SockJS from '../modules/sockjs-client/index.js';
12+
import { CommunicationClient } from '../type.js';
13+
import { log } from '../utils/log.js';
14+
15+
export default class SockJSClient implements CommunicationClient {
16+
sock: WebSocket;
17+
constructor(url: string) {
18+
// SockJS requires `http` and `https` protocols
19+
this.sock = new SockJS(
20+
url.replace(/^ws:/i, 'http:').replace(/^wss:/i, 'https:'),
21+
);
22+
this.sock.onerror = (error) => {
23+
log.error(error);
24+
};
25+
}
26+
27+
onOpen(fn: (...args: unknown[]) => void) {
28+
this.sock.onopen = fn;
29+
}
30+
31+
onClose(fn: (...args: unknown[]) => void) {
32+
this.sock.onclose = fn;
33+
}
34+
35+
// call f with the message string as the first argument
36+
onMessage(fn: (...args: unknown[]) => void) {
37+
this.sock.onmessage = (err) => {
38+
fn(err.data);
39+
};
40+
}
41+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* The following code is modified based on
3+
* https://github.com/webpack/webpack-dev-server
4+
*
5+
* MIT Licensed
6+
* Author Tobias Koppers @sokra
7+
* Copyright (c) JS Foundation and other contributors
8+
* https://github.com/webpack/webpack-dev-server/blob/main/LICENSE
9+
*/
10+
11+
import { CommunicationClient } from '../type.js';
12+
import { log } from '../utils/log.js';
13+
14+
export default class WebSocketClient implements CommunicationClient {
15+
private client: WebSocket;
16+
17+
constructor(url: string) {
18+
this.client = new WebSocket(url);
19+
this.client.onerror = (error: Event) => {
20+
log.error(error);
21+
};
22+
}
23+
24+
onOpen(fn: (...args: unknown[]) => void): void {
25+
this.client.onopen = fn;
26+
}
27+
28+
onClose(fn: (...args: unknown[]) => void): void {
29+
this.client.onclose = fn;
30+
}
31+
32+
// call fn with the message string as the first argument
33+
onMessage(fn: (...args: unknown[]) => void): void {
34+
this.client.onmessage = (event: MessageEvent<string>) => {
35+
fn(event.data);
36+
};
37+
}
38+
}

0 commit comments

Comments
 (0)