-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathvite.config.ts
77 lines (70 loc) · 2.3 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
import { PluginOption, defineConfig, loadEnv } from 'vite'
import react from '@vitejs/plugin-react'
import svgr from 'vite-plugin-svgr'
import path from 'path';
import fs from 'fs';
const getDefinitionTemplate = (variable: string, content: string) => {
return `export const ${variable} = ${content} as const;`;
}
const updateDefinitonFile = async ({ filePath, content }: { filePath: string, content?: string }) => {
const fileContent = content || await fs.promises.readFile(filePath, 'utf-8');
const fileName = filePath.endsWith("panel.json") ? "panel.ts" : "keybinds.ts";
try {
const json = JSON.parse(fileContent);
const variableName = filePath.endsWith("panel.json") ? "panelDefinition" : "keybindDefinition";
fs.writeFileSync(path.join(".", "src", "API", "contexts", fileName), getDefinitionTemplate(variableName, JSON.stringify(json, null, 2)));
} catch (e) {
console.error(`Updating ${fileName} failed:`);
console.log(e);
}
}
const settingsAndKeybindsPlugin = () => {
return {
name: 'settingsAndKeybinds',
enforce: 'post',
// HMR
async handleHotUpdate({ file, read }) {
if (file.endsWith('panel.json') || file.endsWith("keybinds.json")) {
console.debug(`[vite][${path.basename(file)}] Rebuilding type definitions...`);
const content = await read();
await updateDefinitonFile({ filePath: file, content });
}
},
} satisfies PluginOption
}
// https://vitejs.dev/config/
export default defineConfig(async ({ command, mode }) => {
await updateDefinitonFile({ filePath: path.join(".", "public", "panel.json") });
await updateDefinitonFile({ filePath: path.join(".", "public", "keybinds.json") });
return (
{
plugins: [
react(),
svgr(),
settingsAndKeybindsPlugin()
],
build: {
outDir: 'build'
},
resolve: {
alias: {
"readable-stream": "vite-compatible-readable-stream"
}
},
base: mode === "development" ? '/dev/' : './',
server: {
open: "http://localhost:1349/development/",
host: 'localhost',
port: 3500,
},
optimizeDeps: {
esbuildOptions: {
// Node.js global to browser globalThis
define: {
global: 'globalThis',
},
},
},
}
)
})