-
Notifications
You must be signed in to change notification settings - Fork 1
/
vite.config.ts
51 lines (43 loc) · 1.11 KB
/
vite.config.ts
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
/**
* 开发时,项目启动
*/
import { createServer, defineConfig } from 'vite';
import path from 'path';
import net from 'net';
import http from 'http';
function getRandomPort() {
const server = net.createServer();
return new Promise<number>((resolve, reject) => {
server.on('error', reject);
server.listen(0, () => {
const { port } = server.address() as net.AddressInfo;
server.close(() => {
resolve(port);
});
});
});
}
(async () => {
const WebPort = await getRandomPort();
const nodeServer = await createServer({
configFile: path.join(__dirname, 'vite.config.node.ts')
});
nodeServer.config.env.WebPort = WebPort;
await nodeServer.listen();
const webServer = await createServer({
configFile: path.join(__dirname, 'vite.config.web.ts'),
server: {
port: WebPort,
hmr: {
port: WebPort
}
}
});
await webServer.listen();
nodeServer.printUrls();
// 请求服务,以生效当前server
http.request(`http://127.0.0.1:${nodeServer.config.server.port}`).end();
})();
export default defineConfig({
logLevel: 'silent'
});