/ vite.config.mts
vite.config.mts
  1  import { fileURLToPath } from 'url'
  2  import { defineConfig, loadEnv } from 'vite'
  3  import ElectronPlugin, { ElectronOptions } from 'vite-plugin-electron'
  4  import RendererPlugin from 'vite-plugin-electron-renderer'
  5  import VuetifyPlugin from 'vite-plugin-vuetify'
  6  import VueJsx from '@vitejs/plugin-vue-jsx'
  7  import Vue from '@vitejs/plugin-vue'
  8  import { rmSync } from 'fs'
  9  import { resolve, dirname } from 'path'
 10  import { builtinModules } from 'module'
 11  
 12  const isDevEnv = process.env.NODE_ENV === 'development'
 13  
 14  export default defineConfig(({ mode }) => {
 15    process.env = {
 16      ...(isDevEnv
 17        ? {
 18            ELECTRON_ENABLE_LOGGING: 'true'
 19          }
 20        : {}),
 21      ...process.env,
 22      ...loadEnv(mode, process.cwd())
 23    }
 24  
 25    rmSync('dist', { recursive: true, force: true })
 26  
 27    const electronPluginConfigs: ElectronOptions[] = [
 28      {
 29        entry: 'src/main/index.ts',
 30        onstart({ startup }) {
 31          startup()
 32        },
 33        vite: {
 34          root: resolve('.'),
 35          build: {
 36            assetsDir: '.',
 37            outDir: 'dist/main',
 38            rollupOptions: {
 39              external: ['electron', ...builtinModules]
 40            }
 41          }
 42        }
 43      },
 44      {
 45        entry: 'src/preload/index.ts',
 46        onstart({ reload }) {
 47          reload()
 48        },
 49        vite: {
 50          root: resolve('.'),
 51          build: {
 52            outDir: 'dist/preload'
 53          }
 54        }
 55      }
 56    ]
 57  
 58    return {
 59      define: {
 60        __VUE_I18N_FULL_INSTALL__: true,
 61        __VUE_I18N_LEGACY_API__: false,
 62        __INTLIFY_PROD_DEVTOOLS__: false
 63      },
 64      resolve: {
 65        extensions: ['.mjs', '.js', '.ts', '.vue', '.json', '.scss'],
 66        alias: {
 67          '@': resolve(dirname(fileURLToPath(import.meta.url)), 'src')
 68        }
 69      },
 70      base: './',
 71      root: resolve('./src/renderer'),
 72      publicDir: resolve('./src/renderer/public'),
 73      clearScreen: false,
 74      build: {
 75        sourcemap: isDevEnv,
 76        minify: 'terser',
 77        terserOptions: {
 78          compress: {
 79            drop_console: true,
 80            drop_debugger: true
 81          }
 82        },
 83        rollupOptions: {
 84          input: {
 85            main: resolve('./src/renderer/index.html'),
 86            splash: resolve('./src/renderer/splash.html')
 87          }
 88        },
 89        outDir: resolve('./dist')
 90      },
 91      plugins: [
 92        Vue(),
 93        VueJsx(),
 94        // Docs: https://github.com/vuetifyjs/vuetify-loader
 95        VuetifyPlugin({
 96          autoImport: true
 97        }),
 98        // Docs: https://github.com/electron-vite/vite-plugin-electron
 99        ElectronPlugin(electronPluginConfigs),
100        RendererPlugin()
101      ]
102    }
103  })