import { defineConfig } from 'vite'; import { resolve } from 'path'; import electron from 'vite-plugin-electron'; import renderer from 'vite-plugin-electron-renderer'; import react from '@vitejs/plugin-react'; import tailwindcss from '@tailwindcss/vite'; import electronBytecode from './plugins/bytenode/vite-plugin-electron-encrypt'; const projectAliases = { "@src": resolve(__dirname, "./src"), "@runtime": resolve(__dirname, "./runtime-shared"), "@electron": resolve(__dirname, "./electron"), "@service": resolve(__dirname, "./electron/service"), }; function isMainProcessExternal(id: string): boolean { if (!id || id.startsWith('\0')) return false; if (id.startsWith('.') || id.startsWith('/') || /^[A-Za-z]:[\\/]/.test(id)) return false; // Project-specific aliases that should be bundled (not external) const internalAliases = [ '@src/', '@electron/', '@runtime/', '@service/', ]; // Check if the ID starts with any internal alias for (const alias of internalAliases) { if (id.startsWith(alias)) return false; } // Externalize @electron/ packages and other @scoped packages if (id.startsWith('@/') || id.startsWith('@electron/')) return false; // Externalize all other modules (npm packages) return true; } // https://vitejs.dev/config/ export default defineConfig(({ mode }) => { // Determine if we're in development mode const isDev = mode === 'development'; // Development server URL - in dev mode, this will be the Vite dev server URL // In production, this should be undefined // Prefer VITE_DEV_SERVER_URL environment variable if set (e.g., when port is auto-selected) const devServerUrl = isDev ? (process.env.VITE_DEV_SERVER_URL || 'http://localhost:5173') : undefined; return { // Required for Electron: all asset URLs must be relative because the renderer // loads via file:// in production. vite-plugin-electron-renderer sets this // automatically, but we declare it explicitly so the intent is clear. base: './', plugins: [ react(), tailwindcss(), electron([ { // Main process entry file entry: 'electron/main.ts', onstart(options) { options.startup(); }, vite: { plugins: [electronBytecode()], define: { // Inject the dev server URL for use in main process MAIN_WINDOW_VITE_DEV_SERVER_URL: JSON.stringify(devServerUrl), 'process.env.VITE_SERVICE_URL': JSON.stringify(process.env.VITE_SERVICE_URL || 'http://8.138.234.141/ingress'), }, resolve: { alias: projectAliases, }, build: { outDir: 'dist-electron/main', rollupOptions: { external: isMainProcessExternal, }, watch: isDev ? { exclude: ['**/electron/scripts/**', '**/scripts.meta.json'], } : undefined, }, }, }, { // Preload scripts entry file entry: 'electron/preload/index.ts', onstart(options) { options.reload(); }, vite: { build: { outDir: 'dist-electron/preload', rollupOptions: { external: ['electron'], output: { entryFileNames: 'preload.js', }, }, watch: isDev ? { exclude: ['**/electron/scripts/**', '**/scripts.meta.json'], } : undefined, }, resolve: { alias: projectAliases, }, }, }, ]), renderer(), ], css: { transformer: 'lightningcss', }, resolve: { alias: projectAliases, }, server: { port: 5173, watch: { ignored: ['**/electron/scripts/**', '**/scripts.meta.json'], }, proxy: { '/ingress': { target: 'http://8.138.234.141', changeOrigin: true, }, }, }, build: { outDir: 'dist', emptyOutDir: true, }, } });