Repository navigation
Expand file tree
/
Copy pathvite.config.js
More file actions
73 lines (68 loc) · 2.12 KB
/
Copy pathvite.config.js
File metadata and controls
73 lines (68 loc) · 2.12 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
import { defineConfig } from 'vite';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const projectDir = path.dirname(fileURLToPath(import.meta.url));
const runtimeConfigDir = process.env.PHYSICS_LAB_CONFIG_DIR
? path.resolve(process.env.PHYSICS_LAB_CONFIG_DIR)
: path.resolve(projectDir, '..');
function isRuntimeConfigurationRequest(requestPath) {
return requestPath === '/physics-lab.settings.json' || requestPath.startsWith('/experiments/');
}
function runtimeConfigPlugin() {
return {
name: 'physics-lab-runtime-config',
configureServer(server) {
server.middlewares.use('/runtime-config', (request, response) => {
const requestPath = new URL(request.url || '/', 'http://localhost').pathname;
if (!isRuntimeConfigurationRequest(requestPath)) {
response.statusCode = 404;
response.end('Runtime configuration file not found');
return;
}
const root = path.resolve(runtimeConfigDir);
const filePath = path.resolve(root, `.${requestPath}`);
const relative = path.relative(root, filePath);
if (relative.startsWith('..') || path.isAbsolute(relative)) {
response.statusCode = 403;
response.end('Forbidden');
return;
}
fs.readFile(filePath, (error, data) => {
if (error) {
response.statusCode = 404;
response.end('Runtime configuration file not found');
return;
}
response.setHeader('Content-Type', path.extname(filePath) === '.json' ? 'application/json' : 'text/plain');
response.end(data);
});
});
},
};
}
export default defineConfig({
root: './client',
plugins: [runtimeConfigPlugin()],
server: {
host: '0.0.0.0',
hmr: true,
allowedHosts: true,
port: 3000,
proxy: {
'/message': {
target: 'http://localhost:3001',
changeOrigin: true
},
'/ws': {
target: 'ws://localhost:3001',
ws: true,
changeOrigin: true
}
}
},
build: {
outDir: '../dist',
emptyOutDir: true
}
});