fix: production crash due to undefined import.meta.env by adding env utility and updating build script
This commit is contained in:
23
src/utils/env.ts
Normal file
23
src/utils/env.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Safely access environment variables across different runtimes and builders.
|
||||
* Supports Vite's import.meta.env and Bun's process.env (used in bun build).
|
||||
*/
|
||||
export const getEnv = (key: string, defaultValue = ""): string => {
|
||||
// 1. Try Vite's import.meta.env
|
||||
try {
|
||||
if (typeof import.meta.env !== "undefined" && import.meta.env[key]) {
|
||||
return import.meta.env[key];
|
||||
}
|
||||
} catch {}
|
||||
|
||||
// 2. Try process.env (injected by bun build --env)
|
||||
try {
|
||||
if (typeof process !== "undefined" && process.env[key]) {
|
||||
return process.env[key];
|
||||
}
|
||||
} catch {}
|
||||
|
||||
return defaultValue;
|
||||
};
|
||||
|
||||
export const VITE_PUBLIC_URL = getEnv("VITE_PUBLIC_URL", "http://localhost:3000");
|
||||
Reference in New Issue
Block a user