1
1
import { app , BrowserWindow , shell , ipcMain } from 'electron'
2
- import { release } from 'node:os'
3
- import { dirname , join } from 'node:path'
4
2
import { fileURLToPath } from 'node:url'
3
+ import path from 'node:path'
4
+ import os from 'node:os'
5
5
import { update } from './update'
6
6
7
7
globalThis . __filename = fileURLToPath ( import . meta. url )
8
- globalThis . __dirname = dirname ( __filename )
8
+ globalThis . __dirname = path . dirname ( __filename )
9
9
10
10
// The built directory structure
11
11
//
12
12
// ├─┬ dist-electron
13
13
// │ ├─┬ main
14
14
// │ │ └── index.js > Electron-Main
15
15
// │ └─┬ preload
16
- // │ └── index.mjs > Preload-Scripts
16
+ // │ └── index.mjs > Preload-Scripts
17
17
// ├─┬ dist
18
18
// │ └── index.html > Electron-Renderer
19
19
//
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
25
29
26
30
// Disable GPU Acceleration for Windows 7
27
- if ( release ( ) . startsWith ( '6.1' ) ) app . disableHardwareAcceleration ( )
31
+ if ( os . release ( ) . startsWith ( '6.1' ) ) app . disableHardwareAcceleration ( )
28
32
29
33
// Set application name for Windows 10+ notifications
30
34
if ( process . platform === 'win32' ) app . setAppUserModelId ( app . getName ( ) )
@@ -34,21 +38,14 @@ if (!app.requestSingleInstanceLock()) {
34
38
process . exit ( 0 )
35
39
}
36
40
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
-
42
41
let 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' )
47
44
48
45
async function createWindow ( ) {
49
46
win = new BrowserWindow ( {
50
47
title : 'Main window' ,
51
- icon : join ( process . env . VITE_PUBLIC , 'favicon.ico' ) ,
48
+ icon : path . join ( process . env . VITE_PUBLIC , 'favicon.ico' ) ,
52
49
webPreferences : {
53
50
preload,
54
51
// Warning: Enable nodeIntegration and disable contextIsolation is not secure in production
@@ -60,8 +57,8 @@ async function createWindow() {
60
57
} ,
61
58
} )
62
59
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 )
65
62
// Open devTool if the app is not packaged
66
63
win . webContents . openDevTools ( )
67
64
} else {
@@ -79,7 +76,7 @@ async function createWindow() {
79
76
return { action : 'deny' }
80
77
} )
81
78
82
- // Apply electron-updater
79
+ // Auto update
83
80
update ( win )
84
81
}
85
82
@@ -117,10 +114,9 @@ ipcMain.handle('open-win', (_, arg) => {
117
114
} ,
118
115
} )
119
116
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 } ` )
122
119
} else {
123
120
childWindow . loadFile ( indexHtml , { hash : arg } )
124
121
}
125
122
} )
126
-
0 commit comments