29 lines
818 B
TypeScript
29 lines
818 B
TypeScript
|
|
/**
|
|
* Safely retrieves environment variables in both Vite (browser) and Node environments.
|
|
* Prevents "ReferenceError: process is not defined" crashes in production builds.
|
|
*/
|
|
export const getEnvVar = (key: string): string => {
|
|
// 1. Try Vite / Modern Browser approach (import.meta.env)
|
|
try {
|
|
// @ts-ignore
|
|
if (typeof import.meta !== 'undefined' && import.meta.env && import.meta.env[key]) {
|
|
// @ts-ignore
|
|
return import.meta.env[key];
|
|
}
|
|
} catch (e) {
|
|
// Ignore errors if import.meta is not supported
|
|
}
|
|
|
|
// 2. Try Node / Webpack / Polyfilled approach (process.env)
|
|
try {
|
|
if (typeof process !== 'undefined' && process.env && process.env[key]) {
|
|
return process.env[key] || '';
|
|
}
|
|
} catch (e) {
|
|
// Ignore errors if process is not defined
|
|
}
|
|
|
|
return '';
|
|
};
|