forked from holy-unblocker/website-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.ts
127 lines (118 loc) · 3.22 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import type { RouteType } from './src/appRoutes.js';
import { getRoutes } from './src/appRoutes.js';
import { uvPath } from '@titaniumnetwork-dev/ultraviolet';
import { createBareServer } from '@tomphttp/bare-server-node';
import react from '@vitejs/plugin-react-swc';
import { createRequire } from 'node:module';
import { resolve } from 'node:path';
import type { ConfigEnv } from 'vite';
import { defineConfig, loadEnv } from 'vite';
import { ViteMinifyPlugin } from 'vite-plugin-minify';
import { viteStaticCopy } from 'vite-plugin-static-copy';
import svgr from 'vite-plugin-svgr';
// https://vitejs.dev/config/
export default function viteConfig({ mode }: ConfigEnv) {
Object.assign(process.env, loadEnv(mode, process.cwd()));
process.env.VITE_PUBLIC_PATH ??= '';
const require = createRequire(import.meta.url);
for (const required of [
'VITE_ROUTER',
'VITE_SUPPORT_EMAIL',
'VITE_HAT_BADGE',
'VITE_DEFAULT_PROXY',
'VITE_TN_DISCORD_URL',
'VITE_BARE_APIS',
'VITE_RH_API',
'VITE_DB_API',
'VITE_THEATRE_CDN',
])
if (!(required in process.env))
throw new TypeError(`Missing environment variable: ${required}`);
const routes = getRoutes(
process.env.VITE_ROUTER as RouteType,
process.env.VITE_PUBLIC_PATH,
);
return defineConfig({
server: {
proxy: {
'/cdn/': 'https://holyubofficial.net/',
'/db/': 'https://holyubofficial.net/',
},
},
build: {
sourcemap: true,
rollupOptions: {
output: {
chunkFileNames: '[hash].js',
},
plugins: [
{
name: 'copy index.html to all the hot routes',
async generateBundle(options, bundle) {
const index = bundle['index.html'];
for (const file of routes)
bundle[file.file] = {
...index,
fileName: file.file,
};
},
},
],
},
},
plugins: [
{
name: 'serve index.html for all the hot routes',
configureServer(server) {
server.middlewares.use((req, _res, next) => {
let url = req.url;
const search = url.indexOf('?');
if (search !== -1) url = url.slice(0, search);
if (routes.some((route) => route.path === url))
req.url = '/index.html';
next();
});
},
},
{
name: 'bare server',
configureServer(server) {
const bare = createBareServer('/bare/');
server.middlewares.use((req, res, next) => {
if (bare.shouldRoute(req)) bare.routeRequest(req, res);
else next();
});
const upgraders = server.httpServer.listeners(
'upgrade',
) as Parameters<(typeof server)['httpServer']['on']>[1][];
// remover other listeners
for (const upgrader of upgraders)
server.httpServer.off('upgrade', upgrader);
server.httpServer.on('upgrade', (req, socket, head) => {
if (bare.shouldRoute(req)) bare.routeUpgrade(req, socket, head);
else for (const upgrader of upgraders) upgrader(req, socket, head);
});
},
},
react(),
svgr(),
viteStaticCopy({
targets: [
{
src: uvPath,
dest: '',
rename: 'uv',
overwrite: false,
},
{
src: resolve(require.resolve('@ruffle-rs/ruffle'), '..'),
dest: '',
rename: 'ruffle',
},
],
}),
// minify the output HTML
ViteMinifyPlugin(),
],
});
}