1
1
import type { Event } from 'electron' ;
2
2
import { BrowserWindow , app , dialog , shell } from 'electron' ;
3
3
import path from 'node:path' ;
4
- import serve from 'electron-serve' ;
4
+ import prodServe from 'electron-serve' ;
5
+ import trimEnd from 'lodash-es/trimEnd.js' ;
5
6
import { runInBackground } from '../common/async/run-in-background.js' ;
6
7
import type { IpcController } from './ipc/ipc.controller.js' ;
7
8
import { newIpcController } from './ipc/ipc.controller.js' ;
@@ -25,7 +26,14 @@ const appEnvIsDev = appEnv === 'development';
25
26
// Only load dev tools when running in development.
26
27
const appEnableDevTools = appEnvIsDev && ! app . isPackaged ;
27
28
28
- const appPath = app . getAppPath ( ) ;
29
+ // When we migrated to ESM, the app path changed.
30
+ // Instead of being at the root of the project, it's the directory
31
+ // where this code file was invoked at runtime.
32
+ // As a workaround, we trim off the extra path segments.
33
+ const appPath = trimEnd (
34
+ app . getAppPath ( ) ,
35
+ path . join ( 'electron' , 'build' , 'main' )
36
+ ) ;
29
37
const appElectronPath = path . join ( appPath , 'electron' ) ;
30
38
const appBuildPath = path . join ( appElectronPath , 'build' ) ;
31
39
const appPreloadPath = path . join ( appBuildPath , 'preload' ) ;
@@ -42,13 +50,22 @@ const devAppUrl = `http://localhost:${devPort}`;
42
50
43
51
const appUrl = appEnvIsProd ? prodAppUrl : devAppUrl ;
44
52
53
+ logger . debug ( 'app paths' , {
54
+ appPath,
55
+ appElectronPath,
56
+ appBuildPath,
57
+ appPreloadPath,
58
+ prodRendererPath,
59
+ devRendererPath,
60
+ } ) ;
61
+
45
62
// Register custom protocol 'app://' to serve our app.
46
63
// Registering the protocol must be done before the app is ready.
47
64
// This is necessary for both security and for single-page apps.
48
65
// https://bishopfox.com/blog/reasonably-secure-electron
49
66
// https://github.com/sindresorhus/electron-serve
50
67
if ( appEnvIsProd ) {
51
- serve ( {
68
+ prodServe ( {
52
69
scheme : prodAppScheme ,
53
70
directory : prodRendererPath ,
54
71
} ) ;
@@ -63,10 +80,10 @@ const createMainWindow = async (): Promise<void> => {
63
80
// If running in development, serve the renderer from localhost.
64
81
// This must be done once the app is ready.
65
82
// This enables hot reloading of the renderer.
66
- const { serve } = await import ( './electron-next/dev-server.js' ) ;
67
- await serve ( {
68
- rendererPath : devRendererPath ,
83
+ const { devServe } = await import ( './electron-next/dev-server.js' ) ;
84
+ await devServe ( {
69
85
port : devPort ,
86
+ directory : devRendererPath ,
70
87
} ) ;
71
88
}
72
89
0 commit comments