Skip to content

Commit c8ac86f

Browse files
committed
feat: runtime .env
1 parent d11b9b7 commit c8ac86f

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/helpers/ipcHandlers.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,10 +1005,22 @@ class IPCHandlers {
10051005

10061006
// --- OpenWhispr Cloud API handlers ---
10071007

1008+
// In production, VITE_* env vars aren't available in the main process because
1009+
// Vite only inlines them into the renderer bundle at build time. Load the
1010+
// runtime-env.json that the Vite build writes to src/dist/ as a fallback.
1011+
const runtimeEnv = (() => {
1012+
const fs = require("fs");
1013+
const envPath = path.join(__dirname, "..", "dist", "runtime-env.json");
1014+
try {
1015+
if (fs.existsSync(envPath)) return JSON.parse(fs.readFileSync(envPath, "utf8"));
1016+
} catch {}
1017+
return {};
1018+
})();
1019+
10081020
const getApiUrl = () =>
1009-
process.env.OPENWHISPR_API_URL || process.env.VITE_OPENWHISPR_API_URL || "";
1021+
process.env.OPENWHISPR_API_URL || process.env.VITE_OPENWHISPR_API_URL || runtimeEnv.VITE_OPENWHISPR_API_URL || "";
10101022

1011-
const getAuthUrl = () => process.env.NEON_AUTH_URL || process.env.VITE_NEON_AUTH_URL || "";
1023+
const getAuthUrl = () => process.env.NEON_AUTH_URL || process.env.VITE_NEON_AUTH_URL || runtimeEnv.VITE_NEON_AUTH_URL || "";
10121024

10131025
const getSessionCookies = async (event) => {
10141026
const win = BrowserWindow.fromWebContents(event.sender);

src/vite.config.mjs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { defineConfig, loadEnv } from 'vite'
22
import react from '@vitejs/plugin-react'
33
import tailwindcss from '@tailwindcss/vite'
44
import path from 'path'
5+
import fs from 'fs'
56
import { fileURLToPath } from 'url'
67

78
const __filename = fileURLToPath(import.meta.url)
@@ -27,7 +28,23 @@ export default defineConfig(({ mode }) => {
2728
const devServerPort = parseDevServerPort(rawPort)
2829

2930
return {
30-
plugins: [react(), tailwindcss()],
31+
plugins: [
32+
react(),
33+
tailwindcss(),
34+
{
35+
name: 'write-runtime-env',
36+
writeBundle() {
37+
const runtimeEnv = {
38+
VITE_OPENWHISPR_API_URL: env.VITE_OPENWHISPR_API_URL || '',
39+
VITE_NEON_AUTH_URL: env.VITE_NEON_AUTH_URL || '',
40+
}
41+
fs.writeFileSync(
42+
path.resolve(__dirname, 'dist', 'runtime-env.json'),
43+
JSON.stringify(runtimeEnv)
44+
)
45+
},
46+
},
47+
],
3148
base: './', // Use relative paths for file:// protocol in Electron
3249
envDir, // Load .env from project root
3350
resolve: {

0 commit comments

Comments
 (0)