|
| 1 | +import { resolve } from 'path'; |
| 2 | +import { mergeConfig, UserConfig } from 'vite'; |
| 3 | +import { defineConfig, externalizeDepsPlugin } from 'electron-vite'; |
| 4 | +import { getAliasesFromTsConfig, getClientEnvironmentVariables } from './vite-config-helper'; |
| 5 | +import { createVueConfig } from './vite.config'; |
| 6 | + |
| 7 | +const MAIN_ENTRY_FILE = resolvePathFromProjectRoot('src/presentation/electron/main/index.ts'); |
| 8 | +const PRELOAD_ENTRY_FILE = resolvePathFromProjectRoot('src/presentation/electron/preload/index.ts'); |
| 9 | +const WEB_INDEX_HTML_PATH = resolvePathFromProjectRoot('src/presentation/index.html'); |
| 10 | +const DIST_DIR = resolvePathFromProjectRoot('dist_electron/'); |
| 11 | + |
| 12 | +export default defineConfig({ |
| 13 | + main: getSharedElectronConfig({ |
| 14 | + distDirSubfolder: 'main', |
| 15 | + entryFilePath: MAIN_ENTRY_FILE, |
| 16 | + }), |
| 17 | + preload: getSharedElectronConfig({ |
| 18 | + distDirSubfolder: 'preload', |
| 19 | + entryFilePath: PRELOAD_ENTRY_FILE, |
| 20 | + }), |
| 21 | + renderer: mergeConfig( |
| 22 | + createVueConfig({ |
| 23 | + supportLegacyBrowsers: false, |
| 24 | + }), |
| 25 | + { |
| 26 | + build: { |
| 27 | + outDir: resolve(DIST_DIR, 'renderer'), |
| 28 | + rollupOptions: { |
| 29 | + input: { |
| 30 | + index: WEB_INDEX_HTML_PATH, |
| 31 | + }, |
| 32 | + external: ['os', 'child_process', 'fs', 'path'], |
| 33 | + }, |
| 34 | + }, |
| 35 | + }, |
| 36 | + ), |
| 37 | +}); |
| 38 | + |
| 39 | +function getSharedElectronConfig(options: { |
| 40 | + readonly distDirSubfolder: string; |
| 41 | + readonly entryFilePath: string; |
| 42 | +}): UserConfig { |
| 43 | + return { |
| 44 | + build: { |
| 45 | + outDir: resolve(DIST_DIR, options.distDirSubfolder), |
| 46 | + lib: { |
| 47 | + entry: options.entryFilePath, |
| 48 | + }, |
| 49 | + rollupOptions: { |
| 50 | + output: { |
| 51 | + entryFileNames: '[name].cjs', // This is needed so `type="module"` works |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + plugins: [externalizeDepsPlugin()], |
| 56 | + define: { |
| 57 | + ...getClientEnvironmentVariables(), |
| 58 | + }, |
| 59 | + resolve: { |
| 60 | + alias: { |
| 61 | + ...getAliasesFromTsConfig(), |
| 62 | + }, |
| 63 | + }, |
| 64 | + }; |
| 65 | +} |
| 66 | + |
| 67 | +function resolvePathFromProjectRoot(pathSegment: string) { |
| 68 | + return resolve(__dirname, pathSegment); |
| 69 | +} |
0 commit comments