| 1 | import { rmSync } from 'node:fs' |
| 2 | import path from 'node:path' |
| 3 | import { defineConfig } from 'vite' |
| 4 | import react from '@vitejs/plugin-react' |
| 5 | import electron from 'vite-plugin-electron' |
| 6 | import renderer from 'vite-plugin-electron-renderer' |
| 7 | import pkg from './package.json' |
| 8 | |
| 9 | // https://vitejs.dev/config/ |
| 10 | export default defineConfig(({ command }) => { |
| 11 | rmSync('dist-electron', { recursive: true, force: true }) |
| 12 | |
| 13 | const isServe = command === 'serve' |
| 14 | const isBuild = command === 'build' |
| 15 | const sourcemap = isServe || !!process.env.VSCODE_DEBUG |
| 16 | |
| 17 | return { |
| 18 | resolve: { |
| 19 | alias: { |
| 20 | '@': path.join(__dirname, 'src') |
| 21 | }, |
| 22 | }, |
| 23 | plugins: [ |
| 24 | react(), |
| 25 | electron([ |
| 26 | { |
| 27 | // Main-Process entry file of the Electron App. |
| 28 | entry: 'electron/main/index.tsx', |
| 29 | onstart(options) { |
| 30 | if (process.env.VSCODE_DEBUG) { |
| 31 | console.log(/* For `.vscode/.debug.script.mjs` */'[startup] Electron App') |
| 32 | } else { |
| 33 | options.startup() |
| 34 | } |
| 35 | }, |
| 36 | vite: { |
| 37 | build: { |
| 38 | sourcemap, |
| 39 | minify: isBuild, |
| 40 | outDir: 'dist-electron/main', |
| 41 | rollupOptions: { |
| 42 | external: Object.keys('dependencies' in pkg ? pkg.dependencies : {}), |
| 43 | }, |
| 44 | }, |
| 45 | }, |
| 46 | }, |
| 47 | { |
| 48 | entry: 'electron/preload/index.tsx', |
| 49 | onstart(options) { |
| 50 | // Notify the Renderer-Process to reload the page when the Preload-Scripts build is complete, |
| 51 | // instead of restarting the entire Electron App. |
| 52 | options.reload() |
| 53 | }, |
| 54 | vite: { |
| 55 | build: { |
| 56 | sourcemap: sourcemap ? 'inline' : undefined, // #332 |
| 57 | minify: isBuild, |
| 58 | outDir: 'dist-electron/preload', |
| 59 | rollupOptions: { |
| 60 | external: Object.keys('dependencies' in pkg ? pkg.dependencies : {}), |
| 61 | }, |
| 62 | }, |
| 63 | }, |
| 64 | } |
| 65 | ]), |
| 66 | // Use Node.js API in the Renderer-process |
| 67 | renderer(), |
| 68 | ], |
| 69 | server: process.env.VSCODE_DEBUG && (() => { |
| 70 | const url = new URL(pkg.debug.env.VITE_DEV_SERVER_URL) |
| 71 | return { |
| 72 | host: url.hostname, |
| 73 | port: +url.port, |
| 74 | } |
| 75 | })(), |
| 76 | clearScreen: false, |
| 77 | } |
| 78 | }) |
| 79 |