UNPKG

@airsurfer09/web-handsfree

Version:

A React package for integrating Convai's AI-powered voice assistants with LiveKit for real-time audio/video conversations

96 lines 3.55 kB
/** * Logger utility that only logs in development mode. * Completely silent in production builds and npm packages. * * This logger is designed to be tree-shakeable and completely removed * from production builds when not used. * * To test logger behavior: * - Development: Set NODE_ENV=development or run on localhost * - Production: Set NODE_ENV=production or deploy to production * * In npm packages, this logger will be completely silent by default. */ // Environment detection with multiple fallbacks const detectEnvironment = () => { // Check if we're in a browser environment const isBrowser = typeof window !== 'undefined'; // Safe process.env access (Vite/Webpack replaces this at build time) const getEnv = (key) => { try { // @ts-ignore - process.env is replaced at build time by bundlers return typeof process !== 'undefined' && process.env ? process.env[key] : undefined; } catch { return undefined; } }; // Priority 1: Check NEXT_PUBLIC_ENVIRONMENT first (most explicit) const publicEnv = getEnv('NEXT_PUBLIC_ENVIRONMENT'); if (publicEnv === 'PRODUCTION') { return 'production'; } if (publicEnv === 'PREVIEW' || publicEnv === 'STAGING') { return 'development'; } // Priority 2: Check NODE_ENV and REACT_APP_ENV (Vite replaces import.meta.env.MODE) const nodeEnv = getEnv('NODE_ENV'); const reactEnv = getEnv('REACT_APP_ENV'); if (nodeEnv === 'production' || reactEnv === 'production') { return 'production'; } if (nodeEnv === 'development' || reactEnv === 'development') { return 'development'; } // Priority 3: Check for Vite dev server const viteDevUrl = getEnv('VITE_DEV_SERVER_URL'); if (viteDevUrl !== undefined) { return 'development'; } // Priority 4: Check browser environment (only if no explicit env vars are set) if (isBrowser) { // Localhost is typically development if (window.location?.hostname === 'localhost' || window.location?.hostname === '127.0.0.1') { return 'development'; } // Check for development ports if (window.location?.port && ['3000', '3001', '5173', '8080'].includes(window.location.port)) { return 'development'; } } // Default to production for safety return 'production'; }; const environment = detectEnvironment(); const isDevelopment = environment === 'development'; // No-op function that gets completely removed by tree shaking const noop = () => { // This function is intentionally empty and will be removed in production }; // Development logger functions const devLogger = { log: (...args) => console.log('[Convai]', ...args), warn: (...args) => console.warn('[Convai]', ...args), error: (...args) => console.error('[Convai]', ...args), info: (...args) => console.info('[Convai]', ...args), debug: (...args) => console.debug('[Convai]', ...args), trace: (...args) => console.trace('[Convai]', ...args), }; // Production logger functions (all no-ops) const prodLogger = { log: noop, warn: noop, error: noop, info: noop, debug: noop, trace: noop, }; // Export the appropriate logger based on environment export const logger = isDevelopment ? devLogger : prodLogger; // Export environment info for debugging export const loggerConfig = { environment, isDevelopment, isProduction: !isDevelopment, }; //# sourceMappingURL=logger.js.map