| 1 | /* |
| 2 | * @Author: nevin |
| 3 | * @Date: 2025-01-17 19:25:29 |
| 4 | * @LastEditTime: 2025-02-27 16:41:49 |
| 5 | * @LastEditors: nevin |
| 6 | * @Description: |
| 7 | */ |
| 8 | import { rmSync } from 'node:fs'; |
| 9 | import path from 'node:path'; |
| 10 | import { defineConfig } from 'vite'; |
| 11 | import react from '@vitejs/plugin-react'; |
| 12 | import electron from 'vite-plugin-electron/simple'; |
| 13 | import pkg from './package.json'; |
| 14 | import svgr from "vite-plugin-svgr"; |
| 15 | import { createSvgIconsPlugin } from "vite-plugin-svg-icons"; |
| 16 | |
| 17 | // https://vitejs.dev/config/ |
| 18 | export default defineConfig(({ command }) => { |
| 19 | rmSync('dist-electron', { recursive: true, force: true }); |
| 20 | |
| 21 | const isServe = command === 'serve'; |
| 22 | const isBuild = command === 'build'; |
| 23 | const sourcemap = isServe || !!process.env.VSCODE_DEBUG; |
| 24 | |
| 25 | return { |
| 26 | resolve: { |
| 27 | alias: { |
| 28 | '@': path.join(__dirname, 'src'), |
| 29 | '@@': path.join(__dirname, 'commont'), |
| 30 | buffer: 'buffer', |
| 31 | }, |
| 32 | }, |
| 33 | css: { |
| 34 | preprocessorOptions: { |
| 35 | scss: { |
| 36 | api: 'modern-compiler', |
| 37 | }, |
| 38 | }, |
| 39 | }, |
| 40 | plugins: [ |
| 41 | react(), |
| 42 | electron({ |
| 43 | main: { |
| 44 | // Shortcut of `build.lib.entry` |
| 45 | entry: 'electron/main/index.ts', |
| 46 | onstart(args) { |
| 47 | if (process.env.VSCODE_DEBUG) { |
| 48 | console.log( |
| 49 | /* For `.vscode/.debug.script.mjs` */ '[startup] Electron App', |
| 50 | ); |
| 51 | } else { |
| 52 | args.startup(); |
| 53 | } |
| 54 | }, |
| 55 | vite: { |
| 56 | build: { |
| 57 | sourcemap, |
| 58 | minify: isBuild, |
| 59 | outDir: 'dist-electron/main', |
| 60 | rollupOptions: { |
| 61 | external: Object.keys( |
| 62 | 'dependencies' in pkg ? pkg.dependencies : {}, |
| 63 | ), |
| 64 | }, |
| 65 | }, |
| 66 | }, |
| 67 | }, |
| 68 | preload: { |
| 69 | // Shortcut of `build.rollupOptions.input`. |
| 70 | // Preload scripts may contain Web assets, so use the `build.rollupOptions.input` instead `build.lib.entry`. |
| 71 | input: 'electron/preload/index.ts', |
| 72 | vite: { |
| 73 | build: { |
| 74 | sourcemap: sourcemap ? 'inline' : undefined, // #332 |
| 75 | minify: isBuild, |
| 76 | outDir: 'dist-electron/preload', |
| 77 | rollupOptions: { |
| 78 | external: Object.keys( |
| 79 | 'dependencies' in pkg ? pkg.dependencies : {}, |
| 80 | ), |
| 81 | }, |
| 82 | }, |
| 83 | }, |
| 84 | }, |
| 85 | // Ployfill the Electron and Node.js API for Renderer process. |
| 86 | // If you want use Node.js in Renderer process, the `nodeIntegration` needs to be enabled in the Main process. |
| 87 | // See 👉 https://github.com/electron-vite/vite-plugin-electron-renderer |
| 88 | renderer: {}, |
| 89 | }), |
| 90 | svgr({ svgrOptions: { icon: true } }), |
| 91 | createSvgIconsPlugin({ |
| 92 | iconDirs: [path.resolve(process.cwd(), "src/assets/svgs")], |
| 93 | }) |
| 94 | ], |
| 95 | server: |
| 96 | process.env.VSCODE_DEBUG && |
| 97 | (() => { |
| 98 | const url = new URL(pkg.debug.env.VITE_DEV_SERVER_URL); |
| 99 | return { |
| 100 | host: url.hostname, |
| 101 | port: +url.port, |
| 102 | }; |
| 103 | })(), |
| 104 | clearScreen: false, |
| 105 | }; |
| 106 | }); |
| 107 |