Read ssl cert and key for https from .env file

This commit is contained in:
phil 2025-01-31 04:36:38 +01:00
parent d17ba9f735
commit df4e3a97c0
5 changed files with 20 additions and 11 deletions

View file

@ -1,5 +1,13 @@
//
// Webcrypto is required for docoding the tokens, and thus https is compulsary.
//
// Read an environment file (.env) at the project's root,
// with 2 variables VITE_SSL_KEY and VITE_SSL_CERT defining the location
// of the ssl key and cert
//
import { fileURLToPath, URL } from 'node:url'
import { defineConfig, UserConfig } from 'vite'
import { defineConfig, UserConfig, loadEnv } from 'vite'
import fs from 'fs';
import path from 'path';
@ -7,7 +15,8 @@ import path from 'path';
import vue from '@vitejs/plugin-vue'
//import vueDevTools from 'vite-plugin-vue-devtools'
import { settings } from "./settings"
// Read the env file
const env = loadEnv("dev", ".")
let baseSettings: UserConfig = {
plugins: [
@ -21,11 +30,11 @@ let baseSettings: UserConfig = {
},
}
if (settings.key && settings.cert) {
if (env.VITE_SSL_CERT && env.VITE_SSL_KEY) {
baseSettings['server'] = {
https: {
key: fs.readFileSync(path.resolve(settings.key)),
cert: fs.readFileSync(path.resolve(settings.cert))
key: fs.readFileSync(path.resolve(env.VITE_SSL_KEY)),
cert: fs.readFileSync(path.resolve(env.VITE_SSL_CERT))
}
}
}