import type { ElectronOptions } from 'vite-plugin-electron'; import { rmSync } from 'node:fs'; import { builtinModules } from 'node:module'; import { dirname, resolve } from 'node:path'; import process from 'node:process'; import { fileURLToPath } from 'node:url'; import Vue from '@vitejs/plugin-vue'; import VueJsx from '@vitejs/plugin-vue-jsx'; import AutoImport from 'unplugin-auto-import/vite'; import { defineConfig, loadEnv } from 'vite'; import ElectronPlugin from 'vite-plugin-electron'; import RendererPlugin from 'vite-plugin-electron-renderer'; import VuetifyPlugin from 'vite-plugin-vuetify'; const isDevEnv = process.env.NODE_ENV === 'development'; export default defineConfig(({ mode }) => { process.env = { ...(isDevEnv ? { ELECTRON_ENABLE_LOGGING: 'true', } : {}), ...process.env, ...loadEnv(mode, process.cwd()), }; rmSync('dist', { recursive: true, force: true }); const electronPluginConfigs: ElectronOptions[] = [ { entry: 'src/main/index.ts', onstart({ startup }) { const debugArgs = [ '.', '--inspect=9228', '--remote-debugging-port=9229', ]; startup(debugArgs); }, vite: { root: resolve('.'), base: './', publicDir: resolve('./src/public'), build: { sourcemap: true, assetsDir: '.', outDir: 'dist/main', rollupOptions: { external: ['electron', ...builtinModules], }, }, }, }, { entry: 'src/preload/index.ts', onstart({ reload }) { reload(); }, vite: { root: resolve('.'), build: { outDir: 'dist/preload', }, }, }, ]; if (isDevEnv) { electronPluginConfigs.push({ entry: 'src/main/index.dev.ts', vite: { root: resolve('.'), build: { outDir: 'dist/main', }, }, }); } return { define: { __VUE_I18N_FULL_INSTALL__: true, __VUE_I18N_LEGACY_API__: false, __INTLIFY_PROD_DEVTOOLS__: false, }, resolve: { extensions: ['.mjs', '.js', '.ts', '.vue', '.json', '.scss'], alias: { '@': resolve(dirname(fileURLToPath(import.meta.url)), 'src'), }, }, base: './', root: resolve('./src/renderer'), publicDir: resolve('./src/public'), clearScreen: false, build: { sourcemap: isDevEnv, minify: !isDevEnv, outDir: resolve('./dist'), }, plugins: [ Vue(), VueJsx(), // Auto import commonly used APIs AutoImport({ imports: [ 'vue', 'vue-router', 'vue-i18n', '@vueuse/core', 'pinia', ], dirs: [ './src/renderer/composables', './src/renderer/utils', './src/renderer/store', './src/renderer/components', ], vueTemplate: true, }), // Docs: https://github.com/vuetifyjs/vuetify-loader VuetifyPlugin({ autoImport: true, }), // Docs: https://github.com/gxmari007/vite-plugin-eslint // EslintPlugin(), // Docs: https://github.com/electron-vite/vite-plugin-electron ElectronPlugin(electronPluginConfigs), RendererPlugin(), ], }; });