From 88b7ecc52cfc90b17cfb50ee055e9fe8808828e2 Mon Sep 17 00:00:00 2001 From: Alex Barnsley <8069294+alexbarnsley@users.noreply.github.com> Date: Mon, 5 Aug 2024 19:37:20 +0100 Subject: [PATCH] chore: add base vite config --- resources/vite.config.js | 47 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 resources/vite.config.js diff --git a/resources/vite.config.js b/resources/vite.config.js new file mode 100644 index 00000000..74f72b04 --- /dev/null +++ b/resources/vite.config.js @@ -0,0 +1,47 @@ +import { loadEnv } from "vite"; +import fs from 'fs'; +import { homedir } from "os"; +import { resolve } from "path"; + +export function getValetHome() { + let valetPath = resolve(homedir(), '.config/valet'); + if (fs.existsSync(valetPath)) { + return valetPath; + } + + valetPath = resolve(homedir(), '.valet'); + if (fs.existsSync(valetPath)) { + return valetPath; + } + + return null; +} + +// Variation of https://freek.dev/2276-making-vite-and-valet-play-nice-together +export function detectServerConfig(mode) { + const valetPath = getValetHome(); + if (!valetPath) { + return; + } + + const host = loadEnv(mode, process.cwd()).VITE_HOST ?? 'localhost'; + let keyPath = resolve(valetPath, `Certificates/${host}.key`); + let certificatePath = resolve(valetPath, `Certificates/${host}.crt`); + + if (!fs.existsSync(keyPath)) { + return; + } + + if (!fs.existsSync(certificatePath)) { + return; + } + + return { + host, + port: 3000, + https: { + key: fs.readFileSync(keyPath), + cert: fs.readFileSync(certificatePath), + }, + }; +}