-
Notifications
You must be signed in to change notification settings - Fork 21
/
vite.config.mts
108 lines (95 loc) · 2.54 KB
/
vite.config.mts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { svelte } from "@sveltejs/vite-plugin-svelte";
import { defineConfig } from "vite";
import { extractFromPackage } from "npm-pkgbuild";
import { WebSocketServer, WebSocket } from "ws";
const wsPort = 5001;
export default defineConfig(async ({ command, mode }) => {
const res = extractFromPackage(
{
dir: new URL("./", import.meta.url).pathname,
mode
},
process.env
);
const first = await res.next();
const pkg = first.value;
const properties = pkg.properties;
const base = properties["http.path"];
const production = mode === "production";
process.env["VITE_NAME"] = properties.name;
process.env["VITE_DESCRIPTION"] = properties.description;
process.env["VITE_VERSION"] = properties.version;
if(!production) {
MyWebSocketServer();
}
return {
base,
root: "tests/app/src",
plugins: [
svelte({
compilerOptions: {
dev: !production
}
})
],
server: { host: true },
build: {
outDir: "../../../build",
target: "esnext",
emptyOutDir: true,
minify: production,
sourcemap: true
}
};
});
function MyWebSocketServer() {
const wss = new WebSocketServer({ port: wsPort });
let timer;
let n = 0;
wss.on("connection", ws => {
ws.on("message", message => {
message = message.toString();
try {
const m = message.match(/(\w+)\((\w+)\)/);
if (m) {
switch (m[1]) {
case "disconnect":
{
wss.close();
console.log(`close and reopen after ${parseInt(m[2])}ms`);
setTimeout(
() => MyWebSocketServer(),
parseInt(m[2])
);
}
break;
case "timer": {
if (timer) {
clearInterval(timer);
}
if (m[2] === "on") {
timer = setInterval(() => {
n++;
wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(`timer ${n}`));
}
});
}, 1000);
}
}
}
return;
}
wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(">" + JSON.parse(message)));
}
});
} catch (e) {
console.log(e);
}
});
ws.send(JSON.stringify("x"));
});
}