-
-
Notifications
You must be signed in to change notification settings - Fork 226
/
vite.config.ts
93 lines (86 loc) · 2.61 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
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
import react from '@vitejs/plugin-react-swc';
import { consola } from 'consola';
import dotenv from 'dotenv';
import { resolve } from 'node:path';
import * as process from 'node:process';
import { defineConfig } from 'vite';
dotenv.config();
const isProduction = process.env.NODE_ENV === 'production';
const SD_HOST = process.env.SD_HOST || '127.0.0.1';
const SD_PORT = process.env.SD_PORT || 7860;
consola.info('Proxy:', `http://${SD_HOST}:${SD_PORT}`);
export default defineConfig({
base: '/dev',
build: {
cssMinify: true,
emptyOutDir: true,
minify: 'terser',
outDir: './javascript',
rollupOptions: {
input: resolve(__dirname, 'src/main.tsx'),
output: {
assetFileNames: `[name].[ext]`,
chunkFileNames: `[name].js`,
entryFileNames: `[name].js`,
},
},
},
define: {
'process.env': process.env,
},
plugins: [
react({ devTarget: 'esnext', tsDecorators: true }),
!isProduction && {
configureServer: (server) => {
server.middlewares.use((_request, res, next) => {
res.setHeader('Cross-Origin-Embedder-Policy', 'unsafe-none');
res.setHeader('Cross-Origin-Opener-Policy', 'unsafe-non');
next();
});
},
name: 'configure-response-headers',
},
!isProduction && {
configureServer: (server) => {
server.middlewares.use(async(_request, res, next): Promise<void> => {
if (
_request.originalUrl === '/dev' ||
_request.originalUrl === '/dev?__theme=dark' ||
_request.originalUrl === '/dev?__theme=light'
) {
const response = await fetch(`http://${SD_HOST}:${SD_PORT}/`);
let updatedResponse = await response.text();
const toAdd = `
<script type="module" src="/dev/src/_react_refresh.js"></script>
<script type="module" src="/dev/src/main.tsx"></script>
`;
updatedResponse = updatedResponse.replace('</body>', `</body>${toAdd}`);
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
res.setHeader('charset', 'utf8');
res.end(updatedResponse);
return;
}
next();
});
},
name: 'route-default-to-index',
},
],
resolve: {
alias: {
'@': resolve(__dirname, 'src'),
},
},
server: {
host: '127.0.0.1',
port: 8000,
proxy: {
'/queue/join': {
target: `ws://${SD_HOST}:${SD_PORT}`,
ws: true,
},
'^(?!.*dev).*$': `http://${SD_HOST}:${SD_PORT}`,
},
},
});