11import { app , BrowserWindow , shell , ipcMain } from 'electron'
2- import { release } from 'node:os'
3- import { dirname , join } from 'node:path'
42import { fileURLToPath } from 'node:url'
3+ import path from 'node:path'
4+ import os from 'node:os'
55import { update } from './update'
66
77globalThis . __filename = fileURLToPath ( import . meta. url )
8- globalThis . __dirname = dirname ( __filename )
8+ globalThis . __dirname = path . dirname ( __filename )
99
1010// The built directory structure
1111//
1212// ├─┬ dist-electron
1313// │ ├─┬ main
1414// │ │ └── index.js > Electron-Main
1515// │ └─┬ preload
16- // │ └── index.mjs > Preload-Scripts
16+ // │ └── index.mjs > Preload-Scripts
1717// ├─┬ dist
1818// │ └── index.html > Electron-Renderer
1919//
20- process . env . DIST_ELECTRON = join ( __dirname , '../' )
21- process . env . DIST = join ( process . env . DIST_ELECTRON , '../dist' )
22- process . env . VITE_PUBLIC = process . env . VITE_DEV_SERVER_URL
23- ? join ( process . env . DIST_ELECTRON , '../public' )
24- : process . env . DIST
20+ process . env . APP_ROOT = path . join ( __dirname , '../..' )
21+
22+ export const MAIN_DIST = path . join ( process . env . APP_ROOT , 'dist-electron' )
23+ export const RENDERER_DIST = path . join ( process . env . APP_ROOT , 'dist' )
24+ export const VITE_DEV_SERVER_URL = process . env . VITE_DEV_SERVER_URL
25+
26+ process . env . VITE_PUBLIC = VITE_DEV_SERVER_URL
27+ ? path . join ( process . env . APP_ROOT , 'public' )
28+ : RENDERER_DIST
2529
2630// Disable GPU Acceleration for Windows 7
27- if ( release ( ) . startsWith ( '6.1' ) ) app . disableHardwareAcceleration ( )
31+ if ( os . release ( ) . startsWith ( '6.1' ) ) app . disableHardwareAcceleration ( )
2832
2933// Set application name for Windows 10+ notifications
3034if ( process . platform === 'win32' ) app . setAppUserModelId ( app . getName ( ) )
@@ -34,21 +38,14 @@ if (!app.requestSingleInstanceLock()) {
3438 process . exit ( 0 )
3539}
3640
37- // Remove electron security warnings
38- // This warning only shows in development mode
39- // Read more on https://www.electronjs.org/docs/latest/tutorial/security
40- // process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true'
41-
4241let win : BrowserWindow | null = null
43- // Here, you can also use other preload
44- const preload = join ( __dirname , '../preload/index.mjs' )
45- const url = process . env . VITE_DEV_SERVER_URL
46- const indexHtml = join ( process . env . DIST , 'index.html' )
42+ const preload = path . join ( __dirname , '../preload/index.mjs' )
43+ const indexHtml = path . join ( RENDERER_DIST , 'index.html' )
4744
4845async function createWindow ( ) {
4946 win = new BrowserWindow ( {
5047 title : 'Main window' ,
51- icon : join ( process . env . VITE_PUBLIC , 'favicon.ico' ) ,
48+ icon : path . join ( process . env . VITE_PUBLIC , 'favicon.ico' ) ,
5249 webPreferences : {
5350 preload,
5451 // Warning: Enable nodeIntegration and disable contextIsolation is not secure in production
@@ -60,8 +57,8 @@ async function createWindow() {
6057 } ,
6158 } )
6259
63- if ( url ) { // electron-vite-vue #298
64- win . loadURL ( url )
60+ if ( VITE_DEV_SERVER_URL ) { // #298
61+ win . loadURL ( VITE_DEV_SERVER_URL )
6562 // Open devTool if the app is not packaged
6663 win . webContents . openDevTools ( )
6764 } else {
@@ -79,7 +76,7 @@ async function createWindow() {
7976 return { action : 'deny' }
8077 } )
8178
82- // Apply electron-updater
79+ // Auto update
8380 update ( win )
8481}
8582
@@ -117,10 +114,9 @@ ipcMain.handle('open-win', (_, arg) => {
117114 } ,
118115 } )
119116
120- if ( process . env . VITE_DEV_SERVER_URL ) {
121- childWindow . loadURL ( `${ url } #${ arg } ` )
117+ if ( VITE_DEV_SERVER_URL ) {
118+ childWindow . loadURL ( `${ VITE_DEV_SERVER_URL } #${ arg } ` )
122119 } else {
123120 childWindow . loadFile ( indexHtml , { hash : arg } )
124121 }
125122} )
126-
0 commit comments