Skip to content

Commit e44d4e3

Browse files
committed
chore: improve dev command
1 parent 730d34f commit e44d4e3

19 files changed

+263
-243
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ To use **Univer Clipsheet**, ensure you have the following installed:
9494
2. Start the development server:
9595

9696
```bash
97-
pnpm run dev-server // hmr-server
9897
pnpm run dev
9998
```
10099

packages/hmr/lib/constant.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
export const LOCAL_RELOAD_SOCKET_PORT = 8081;
22
export const LOCAL_RELOAD_SOCKET_URL = `ws://localhost:${LOCAL_RELOAD_SOCKET_PORT}`;
3+
4+
export const DO_UPDATE = 'do_update';
5+
export const DONE_UPDATE = 'done_update';
6+
export const BUILD_COMPLETE = 'build_complete';

packages/hmr/lib/debounce.ts

-1
This file was deleted.

packages/hmr/lib/initClient.ts

-40
This file was deleted.

packages/hmr/lib/initReloadServer.ts

-51
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { DO_UPDATE, DONE_UPDATE, LOCAL_RELOAD_SOCKET_URL } from '../constant';
2+
import MessageInterpreter from '../interpreter';
3+
4+
export default function initClient({ id, onUpdate }: { id: string; onUpdate: () => void }) {
5+
const ws = new WebSocket(LOCAL_RELOAD_SOCKET_URL);
6+
7+
ws.onopen = () => {
8+
ws.addEventListener('message', event => {
9+
const message = MessageInterpreter.receive(String(event.data));
10+
11+
if (message.type === DO_UPDATE && message.id === id) {
12+
onUpdate();
13+
ws.send(MessageInterpreter.send({ type: DONE_UPDATE }));
14+
return;
15+
}
16+
});
17+
};
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import type { WebSocket } from 'ws';
2+
import { WebSocketServer } from 'ws';
3+
import { BUILD_COMPLETE, DO_UPDATE, DONE_UPDATE, LOCAL_RELOAD_SOCKET_PORT, LOCAL_RELOAD_SOCKET_URL } from '../constant';
4+
import MessageInterpreter from '../interpreter';
5+
6+
const clientsThatNeedToUpdate: Set<WebSocket> = new Set();
7+
8+
function initReloadServer() {
9+
const wss = new WebSocketServer({ port: LOCAL_RELOAD_SOCKET_PORT });
10+
11+
wss.on('listening', () => {
12+
console.log(`[HMR] Server listening at ${LOCAL_RELOAD_SOCKET_URL}`);
13+
});
14+
15+
wss.on('connection', ws => {
16+
clientsThatNeedToUpdate.add(ws);
17+
18+
ws.addEventListener('close', () => {
19+
clientsThatNeedToUpdate.delete(ws);
20+
});
21+
22+
ws.addEventListener('message', event => {
23+
if (typeof event.data !== 'string') return;
24+
25+
const message = MessageInterpreter.receive(event.data);
26+
27+
if (message.type === DONE_UPDATE) {
28+
ws.close();
29+
}
30+
31+
if (message.type === BUILD_COMPLETE) {
32+
clientsThatNeedToUpdate.forEach((ws: WebSocket) =>
33+
ws.send(MessageInterpreter.send({ type: DO_UPDATE, id: message.id })),
34+
);
35+
}
36+
});
37+
});
38+
39+
wss.on('error', error => {
40+
console.error(`[HMR] Failed to start server at ${LOCAL_RELOAD_SOCKET_URL}`);
41+
throw error;
42+
});
43+
}
44+
45+
initReloadServer();
+22-21
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
1-
import initClient from '../initClient';
1+
import initClient from '../initializers/initClient';
22

33
function addRefresh() {
4-
let pendingReload = false;
4+
let pendingReload = false;
55

6-
initClient({
7-
// @ts-expect-error
8-
id: __HMR_ID,
9-
onUpdate: () => {
6+
initClient({
7+
// @ts-expect-error That's because of the dynamic code loading
8+
id: __HMR_ID,
9+
onUpdate: () => {
1010
// disable reload when tab is hidden
11-
if (document.hidden) {
12-
pendingReload = true;
13-
return;
14-
}
15-
reload();
16-
},
17-
});
11+
if (document.hidden) {
12+
pendingReload = true;
13+
return;
14+
}
15+
reload();
16+
},
17+
});
1818

1919
// reload
20-
function reload(): void {
21-
pendingReload = false;
22-
window.location.reload();
23-
}
20+
function reload(): void {
21+
pendingReload = false;
22+
window.location.reload();
23+
}
2424

2525
// reload when tab is visible
26-
function reloadWhenTabIsVisible(): void {
27-
!document.hidden && pendingReload && reload();
28-
}
29-
document.addEventListener('visibilitychange', reloadWhenTabIsVisible);
26+
function reloadWhenTabIsVisible(): void {
27+
!document.hidden && pendingReload && reload();
28+
}
29+
30+
document.addEventListener('visibilitychange', reloadWhenTabIsVisible);
3031
}
3132

3233
addRefresh();

packages/hmr/lib/injections/reload.ts

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
import initClient from '../initClient';
1+
import initClient from '../initializers/initClient';
22

33
function addReload() {
4-
const reload = () => {
5-
// @ts-expect-error
6-
chrome.runtime.reload();
7-
};
4+
const reload = () => {
5+
chrome.runtime.reload();
6+
};
87

9-
initClient({
10-
// @ts-expect-error
11-
id: __HMR_ID,
12-
onUpdate: reload,
13-
});
8+
initClient({
9+
// @ts-expect-error That's because of the dynamic code loading
10+
id: __HMR_ID,
11+
onUpdate: reload,
12+
});
1413
}
1514

1615
addReload();

packages/hmr/lib/interpreter/index.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import type { SerializedMessage, WebSocketMessage } from './types';
1+
import type { SerializedMessage, WebSocketMessage } from '../types';
22

33
export default class MessageInterpreter {
4-
private constructor() {}
4+
// eslint-disable-next-line @typescript-eslint/no-empty-function
5+
private constructor() {}
56

6-
static send(message: WebSocketMessage): SerializedMessage {
7-
return JSON.stringify(message);
8-
}
7+
static send(message: WebSocketMessage): SerializedMessage {
8+
return JSON.stringify(message);
9+
}
910

10-
static receive(serializedMessage: SerializedMessage): WebSocketMessage {
11-
return JSON.parse(serializedMessage);
12-
}
11+
static receive(serializedMessage: SerializedMessage): WebSocketMessage {
12+
return JSON.parse(serializedMessage);
13+
}
1314
}

packages/hmr/lib/interpreter/types.ts

-11
This file was deleted.

packages/hmr/lib/plugins/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './watch-rebuild-plugin';
22
export * from './make-entry-point-plugin';
3+
export * from './watch-public-plugin';

0 commit comments

Comments
 (0)