UNPKG

@replyke/core

Version:

Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.

95 lines 3.19 kB
// Environment detection utility for cross-platform compatibility // Works with both traditional React apps and Vite-based apps // Helper function to safely access Vite's import.meta.env function getViteEnv() { try { // Use dynamic access to avoid TypeScript import.meta issues const globalThis_ = globalThis; if (typeof window !== 'undefined' && globalThis_.__vite_env) { return globalThis_.__vite_env; } // Try to access import.meta via eval to avoid compile-time issues if (typeof window !== 'undefined') { try { const importMeta = new Function('return typeof import !== "undefined" && import.meta')(); if (importMeta && importMeta.env) { return importMeta.env; } } catch { // Ignore errors when import.meta is not available } } return null; } catch { return null; } } /** * Safe way to check if we're in development mode * Works with both Node.js process.env and Vite import.meta.env */ export function isDevelopment() { // Check if we have process.env (Node.js or bundler that provides it) if (typeof process !== 'undefined' && process.env) { return process.env.NODE_ENV === 'development'; } // Check Vite environment const viteEnv = getViteEnv(); if (viteEnv) { return viteEnv.MODE === 'development'; } // Fallback to false (assume production for safety) return false; } /** * Safe way to check if we're in production mode */ export function isProduction() { // Check if we have process.env (Node.js or bundler that provides it) if (typeof process !== 'undefined' && process.env) { return process.env.NODE_ENV === 'production'; } // Check Vite environment const viteEnv = getViteEnv(); if (viteEnv) { return viteEnv.MODE === 'production'; } // Fallback to true (assume production for safety) return true; } /** * Get API base URL from environment variables * Supports both REACT_APP_ and VITE_ prefixes */ export function getApiBaseUrl() { // Check process.env (traditional React apps) if (typeof process !== 'undefined' && process.env) { return process.env.REACT_APP_API_BASE_URL || 'https://api.replyke.com/v7'; } // Check Vite environment const viteEnv = getViteEnv(); if (viteEnv) { return viteEnv.VITE_API_BASE_URL || 'https://api.replyke.com/v7'; } // Fallback to default return 'https://api.replyke.com/v7'; } /** * Get any environment variable with fallback * Tries both VITE_ and REACT_APP_ prefixes */ export function getEnvVar(name, defaultValue = '') { // Check process.env if (typeof process !== 'undefined' && process.env) { return process.env[`REACT_APP_${name}`] || process.env[`VITE_${name}`] || defaultValue; } // Check Vite environment const viteEnv = getViteEnv(); if (viteEnv) { return viteEnv[`VITE_${name}`] || viteEnv[`REACT_APP_${name}`] || defaultValue; } return defaultValue; } //# sourceMappingURL=env.js.map