LazyBoy2/web/vite.config.ts

49 lines
1.5 KiB
TypeScript

import { existsSync, readFileSync } from "node:fs";
import { homedir } from "node:os";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { defineConfig, loadEnv, type ServerOptions } from "vite";
import react from "@vitejs/plugin-react";
const rootDir = resolve(dirname(fileURLToPath(import.meta.url)), "..");
function localCerts(): { cert: Buffer; key: Buffer } | undefined {
const home = homedir();
for (const dir of [join(home, ".lazyboy", "certs"), join(home, ".grokboy", "certs")]) {
const cert = join(dir, "server.pem");
const key = join(dir, "server.key");
if (existsSync(cert) && existsSync(key)) {
return { cert: readFileSync(cert), key: readFileSync(key) };
}
}
return undefined;
}
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, rootDir, "");
const version = (process.env.LAZYBOY_VERSION || env.LAZYBOY_VERSION || "0.1.0").trim() || "0.1.0";
const https = localCerts();
const backend = https ? "https://127.0.0.1:8787" : "http://127.0.0.1:8787";
const server: ServerOptions = {
port: 5173,
host: true,
proxy: {
"/api": { target: backend, secure: false },
"/novnc": { target: backend, ws: true, secure: false },
"/lazyboy-ca.crt": { target: backend, secure: false },
},
};
if (https) server.https = https;
return {
plugins: [react()],
define: {
"import.meta.env.LAZYBOY_VERSION": JSON.stringify(version),
},
server,
preview: {
port: 4173,
},
};
});