-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvite.config.ts
More file actions
85 lines (80 loc) · 2.06 KB
/
vite.config.ts
File metadata and controls
85 lines (80 loc) · 2.06 KB
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
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import { quasar, transformAssetUrls } from '@quasar/vite-plugin';
import { spawn, type ChildProcess } from 'child_process';
import path from 'path';
import { fileURLToPath } from 'url';
import net from 'net';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const API_PORT = 3001;
/** Returns true if something is already accepting connections on port (so we should not spawn). */
function portHasServer(port: number): Promise<boolean> {
return new Promise((resolve) => {
const socket = new net.Socket();
const onError = () => {
socket.destroy();
resolve(false);
};
socket.setTimeout(500);
socket.once('error', onError);
socket.once('timeout', onError);
socket.once('connect', () => {
socket.destroy();
resolve(true);
});
socket.connect(port, '127.0.0.1');
});
}
function backendPlugin() {
let server: ChildProcess | null = null;
return {
name: 'run-backend',
async configureServer() {
if (await portHasServer(API_PORT)) {
return;
}
server = spawn('node', ['server/index.js'], {
cwd: path.resolve(__dirname),
stdio: 'inherit',
env: { ...process.env, PORT: String(API_PORT) }
});
server.on('error', (err) => console.error('Backend failed to start:', err));
server.on('exit', (code) => {
if (code !== null && code !== 0) {
console.warn('Backend exited with code', code);
}
server = null;
});
await new Promise((r) => setTimeout(r, 1500));
},
closeBundle() {
if (server) {
server.kill();
server = null;
}
}
};
}
export default defineConfig({
plugins: [
vue({
template: { transformAssetUrls }
}),
quasar({
sassVariables: true
}),
backendPlugin()
],
server: {
port: 5173,
proxy: {
'/api': {
target: 'http://localhost:3001',
changeOrigin: true
}
}
},
optimizeDeps: {
include: ['punycode', 'events']
}
});