@@ -16,22 +16,52 @@ app.setAppUserModelId('com.github.dragonrealms-phoenix.phoenix');
16
16
17
17
const logger = createLogger ( 'main' ) ;
18
18
19
+ const appEnv = process . env . APP_ENV ?? 'production' ;
20
+ const appEnvIsProd = appEnv === 'production' ;
21
+ const appEnvIsDev = appEnv === 'development' ;
22
+
19
23
const appPath = app . getAppPath ( ) ;
20
- const appBuildPath = path . join ( appPath , 'electron' , 'build' ) ;
24
+ const appElectronPath = path . join ( appPath , 'electron' ) ;
25
+ const appBuildPath = path . join ( appElectronPath , 'build' ) ;
21
26
const appPreloadPath = path . join ( appBuildPath , 'preload' ) ;
22
- const appRendererPath = path . join ( appBuildPath , 'renderer' ) ;
27
+
28
+ // When running in production, serve the app from these paths.
29
+ const prodRendererPath = path . join ( appBuildPath , 'renderer' ) ;
30
+ const prodAppScheme = 'app' ;
31
+ const prodAppUrl = `${ prodAppScheme } ://-` ;
32
+
33
+ // When running in development, serve the app from these paths.
34
+ const devRendererPath = path . join ( appElectronPath , 'renderer' ) ;
35
+ const devPort = 3000 ;
36
+ const devAppUrl = `http://localhost:${ devPort } ` ;
37
+
38
+ const appUrl = appEnvIsProd ? prodAppUrl : devAppUrl ;
23
39
24
40
// Register custom protocol 'app://' to serve our app.
25
41
// Registering the protocol must be done before the app is ready.
26
42
// This is necessary for both security and for single-page apps.
27
43
// https://bishopfox.com/blog/reasonably-secure-electron
28
44
// https://github.com/sindresorhus/electron-serve
29
- serve ( { directory : appRendererPath } ) ;
45
+ if ( appEnvIsProd ) {
46
+ serve ( {
47
+ scheme : prodAppScheme ,
48
+ directory : prodRendererPath ,
49
+ } ) ;
50
+ }
30
51
31
52
const createWindow = async ( ) : Promise < void > => {
53
+ if ( appEnvIsDev ) {
54
+ // If running in development, serve the renderer from localhost.
55
+ // This must be done once the app is ready.
56
+ // This enables hot reloading of the renderer.
57
+ const { default : serveDev } = await import ( 'electron-next' ) ;
58
+ await serveDev ( devRendererPath , devPort ) ;
59
+ }
60
+
32
61
const mainWindow = new BrowserWindow ( {
33
62
width : 1200 ,
34
63
height : 800 ,
64
+ show : false , // hidden until window loads contents to avoid a blank screen
35
65
webPreferences : {
36
66
preload : path . join ( appPreloadPath , 'index.js' ) ,
37
67
devTools : ! app . isPackaged ,
@@ -54,7 +84,12 @@ const createWindow = async (): Promise<void> => {
54
84
} ,
55
85
} ) ;
56
86
57
- await mainWindow . loadURL ( 'app://-' ) ;
87
+ // Once the window has finished loading, show it.
88
+ mainWindow . webContents . once ( 'did-finish-load' , ( ) => {
89
+ mainWindow . show ( ) ;
90
+ } ) ;
91
+
92
+ await mainWindow . loadURL ( appUrl ) ;
58
93
59
94
initializeMenu ( mainWindow ) ;
60
95
} ;
0 commit comments