@@ -21,9 +21,9 @@ export interface ElectronOptions {
2121 /**
2222 * Electron App startup function.
2323 * It will mount the Electron App child-process to `process.electronApp`.
24- * @param argv default value `['.', '--no-sandbox']`
25- * @param options options for `child_process.spawn`
26- * @param customElectronPkg custom electron package name (default: 'electron')
24+ * - argv: electron startup arguments (default `['.', '--no-sandbox']`)
25+ * - options: options for `child_process.spawn`
26+ * - customElectronPkg: custom electron package name (default: ` 'electron'` )
2727 */
2828 startup : ( argv ?: string [ ] , options ?: SpawnOptions , customElectronPkg ?: string ) => Promise < void >
2929 /** Reload Electron-Renderer */
@@ -37,6 +37,7 @@ export function build(isESM: boolean, options: ElectronOptions): ReturnType<type
3737
3838export default function electron (
3939 isESM : boolean ,
40+ root : string ,
4041 options : ElectronOptions | ElectronOptions [ ] ,
4142) : Plugin [ ] {
4243 const optionsArray = Array . isArray ( options ) ? options : [ options ]
@@ -57,6 +58,13 @@ export default function electron(
5758 {
5859 name : 'vite-plugin-electron:dev' ,
5960 apply : 'serve' ,
61+ configResolved ( config ) {
62+ if ( config . root !== root ) {
63+ throw new Error (
64+ `Renderer's root (${ config . root } ) is not same as electron's root (${ root } ). Please setup \`root\` in electron plugin` ,
65+ )
66+ }
67+ } ,
6068 configureServer ( server ) {
6169 server . httpServer ?. once ( 'listening' , ( ) => {
6270 Object . assign ( process . env , {
@@ -72,6 +80,7 @@ export default function electron(
7280 options . vite . root ??= server . config . root
7381 options . vite . envDir ??= server . config . envDir
7482 options . vite . envPrefix ??= server . config . envPrefix
83+ const defaultArgs = [ options . vite . root || '.' , '--no-sandbox' ]
7584
7685 options . vite . build ??= { }
7786 if ( ! Object . keys ( options . vite . build ) . includes ( 'watch' ) ) {
@@ -87,10 +96,11 @@ export default function electron(
8796 if ( ++ closeBundleCount < entryCount ) {
8897 return
8998 }
90-
9199 if ( options . onstart ) {
92100 options . onstart . call ( this , {
93- startup,
101+ async startup ( args = defaultArgs , ...opt ) {
102+ await startup ( args , ...opt )
103+ } ,
94104 // Why not use Vite's built-in `/@vite/client` to implement Hot reload?
95105 // Because Vite only inserts `/@vite/client` into the `*.html` entry file, the preload scripts are usually a `*.js` file.
96106 // @see - https://github.com/vitejs/vite/blob/v5.2.11/packages/vite/src/node/server/middlewares/indexHtml.ts#L399
@@ -101,12 +111,12 @@ export default function electron(
101111 // For Electron apps that don't need to use the renderer process.
102112 startup . send ( 'electron-vite&type=hot-reload' )
103113 } else {
104- startup ( )
114+ startup ( defaultArgs )
105115 }
106116 } ,
107117 } )
108118 } else {
109- startup ( )
119+ startup ( defaultArgs )
110120 }
111121 } ,
112122 } )
@@ -143,7 +153,7 @@ export default function electron(
143153}
144154
145155interface StartupFn {
146- ( ) : Promise < void >
156+ ( argv ?: string [ ] , options ?: SpawnOptions , customElectronPkg ?: string ) : Promise < void >
147157 send : ( message : string ) => void
148158 hookedProcessExit : boolean
149159 exit : ( ) => Promise < void >
@@ -158,8 +168,8 @@ interface StartupFn {
158168 */
159169export const startup : StartupFn = async (
160170 argv = [ '.' , '--no-sandbox' ] ,
161- options ?: SpawnOptions ,
162- customElectronPkg ?: string ,
171+ options ?,
172+ customElectronPkg ?,
163173) => {
164174 const { spawn } = await import ( 'node:child_process' )
165175 const electron = await import ( customElectronPkg ?? 'electron' )
0 commit comments