77import {
88 resolveServerUrl ,
99 resolveViteConfig ,
10+ resolveInput ,
11+ mockIndexHtml ,
1012 withExternalBuiltins ,
1113 treeKillSync ,
1214} from './utils'
@@ -51,6 +53,7 @@ export default function electron(options: ElectronOptions | ElectronOptions[]):
5153 const optionsArray = Array . isArray ( options ) ? options : [ options ]
5254 let userConfig : UserConfig
5355 let configEnv : ConfigEnv
56+ let mockdInput : Awaited < ReturnType < typeof mockIndexHtml > > | undefined
5457
5558 return [
5659 {
@@ -89,11 +92,14 @@ export default function electron(options: ElectronOptions | ElectronOptions[]):
8992 options . onstart . call ( this , {
9093 startup,
9194 // Why not use Vite's built-in `/@vite/client` to implement Hot reload?
92- // Because Vite only inserts `/@vite/client` into the `*.html` entry file.
95+ // Because Vite only inserts `/@vite/client` into the `*.html` entry file, the preload scripts are usually a `*.js` file .
9396 // @see - https://github.com/vitejs/vite/blob/v5.2.11/packages/vite/src/node/server/middlewares/indexHtml.ts#L399
9497 reload ( ) {
9598 if ( process . electronApp ) {
9699 ( server . hot || server . ws ) . send ( { type : 'full-reload' } )
100+
101+ // For Electron apps that don't need to use the renderer process.
102+ startup . send ( 'electron-vite&type=hot-reload' )
97103 } else {
98104 startup ( )
99105 }
@@ -120,7 +126,15 @@ export default function electron(options: ElectronOptions | ElectronOptions[]):
120126 // Make sure that Electron can be loaded into the local file using `loadFile` after packaging.
121127 config . base ??= './'
122128 } ,
129+ async configResolved ( config ) {
130+ const input = resolveInput ( config )
131+ if ( input == null ) {
132+ mockdInput = await mockIndexHtml ( config )
133+ }
134+ } ,
123135 async closeBundle ( ) {
136+ mockdInput ?. remove ( )
137+
124138 for ( const options of optionsArray ) {
125139 options . vite ??= { }
126140 options . vite . mode ??= configEnv . mode
@@ -154,7 +168,10 @@ export async function startup(
154168 await startup . exit ( )
155169
156170 // Start Electron.app
157- process . electronApp = spawn ( electronPath , argv , { stdio : 'inherit' , ...options } )
171+ process . electronApp = spawn ( electronPath , argv , {
172+ stdio : [ 'inherit' , 'inherit' , 'inherit' , 'ipc' ] ,
173+ ...options ,
174+ } )
158175
159176 // Exit command after Electron.app exits
160177 process . electronApp . once ( 'exit' , process . exit )
@@ -164,6 +181,14 @@ export async function startup(
164181 process . once ( 'exit' , startup . exit )
165182 }
166183}
184+
185+ startup . send = ( message : string ) => {
186+ if ( process . electronApp ) {
187+ // Based on { stdio: [,,, 'ipc'] }
188+ process . electronApp . send ?.( message )
189+ }
190+ }
191+
167192startup . hookedProcessExit = false
168193startup . exit = async ( ) => {
169194 if ( process . electronApp ) {
0 commit comments