import { createApp } from 'vue' import { createRouter, createWebHistory } from 'vue-router' import { createPinia } from 'pinia' // Core plugin system import { pluginManager } from './core/plugin-manager' import { eventBus } from './core/event-bus' import { container } from './core/di-container' // App configuration import appConfig from './app.config' // Base modules import baseModule from './modules/base' import nostrFeedModule from './modules/nostr-feed' import chatModule from './modules/chat' import eventsModule from './modules/events' import marketModule from './modules/market' // Root component import App from './App.vue' // Styles import './assets/index.css' // Use existing i18n setup import { i18n } from './i18n' /** * Initialize and start the modular application */ export async function createAppInstance() { console.log('🚀 Starting modular application...') // Create Vue app const app = createApp(App) // Create router const router = createRouter({ history: createWebHistory(), routes: [ // Default route - will be populated by modules { path: '/', name: 'home', component: () => import('./pages/Home.vue'), meta: { requiresAuth: true } }, { path: '/login', name: 'login', component: () => import('./pages/LoginDemo.vue'), meta: { requiresAuth: false } } ] }) // Use existing i18n setup // Create Pinia store const pinia = createPinia() // Install core plugins app.use(router) app.use(pinia) app.use(i18n) // Initialize plugin manager pluginManager.init(app, router) // Register modules based on configuration const moduleRegistrations = [] // Register base module first (required) if (appConfig.modules.base.enabled) { moduleRegistrations.push( pluginManager.register(baseModule, appConfig.modules.base) ) } // Register nostr-feed module if (appConfig.modules['nostr-feed'].enabled) { moduleRegistrations.push( pluginManager.register(nostrFeedModule, appConfig.modules['nostr-feed']) ) } // Register chat module if (appConfig.modules.chat.enabled) { moduleRegistrations.push( pluginManager.register(chatModule, appConfig.modules.chat) ) } // Register events module if (appConfig.modules.events.enabled) { moduleRegistrations.push( pluginManager.register(eventsModule, appConfig.modules.events) ) } // Register market module if (appConfig.modules.market.enabled) { moduleRegistrations.push( pluginManager.register(marketModule, appConfig.modules.market) ) } // Wait for all modules to register await Promise.all(moduleRegistrations) // Install all enabled modules await pluginManager.installAll() // Set up auth guard router.beforeEach(async (to, _from, next) => { // Import the auth composable to ensure we're using the same instance as the rest of the app const { auth } = await import('@/composables/useAuth') if (to.meta.requiresAuth && !auth.isAuthenticated.value) { console.log('Auth guard: User not authenticated, redirecting to login') next('/login') } else if (to.path === '/login' && auth.isAuthenticated.value) { console.log('Auth guard: User already authenticated, redirecting to home') next('/') } else { console.log(`Auth guard: Allowing navigation to ${to.path}`) next() } }) // Global error handling app.config.errorHandler = (err, _vm, info) => { console.error('Global error:', err, info) eventBus.emit('app:error', { error: err, info }, 'app') } // Development helpers if (appConfig.features.developmentMode) { // Expose debugging helpers globally ;(window as any).__pluginManager = pluginManager ;(window as any).__eventBus = eventBus ;(window as any).__container = container console.log('🔧 Development mode enabled') console.log('Available globals: __pluginManager, __eventBus, __container') } console.log('✅ Application initialized successfully') return { app, router } } /** * Start the application */ export async function startApp() { try { const { app } = await createAppInstance() // Mount the app app.mount('#app') console.log('🎉 Application started!') // Emit app started event eventBus.emit('app:started', {}, 'app') } catch (error) { console.error('💥 Failed to start application:', error) // Show error to user document.getElementById('app')!.innerHTML = `
${error instanceof Error ? error.message : 'Unknown error'}
Please refresh the page or contact support.