diff --git a/packages/plugin/vite/src/VitePlugin.ts b/packages/plugin/vite/src/VitePlugin.ts index 97fe1ecf9..cf30406dd 100644 --- a/packages/plugin/vite/src/VitePlugin.ts +++ b/packages/plugin/vite/src/VitePlugin.ts @@ -98,17 +98,41 @@ export default class VitePlugin extends PluginBase { // Main process, Preload scripts and Worker process, etc. build = async (watch = false): Promise => { - for (const userConfig of await this.configGenerator.getBuildConfig(watch)) { - const buildResult = await vite.build({ - // Avoid recursive builds caused by users configuring @electron-forge/plugin-vite in Vite config file. - configFile: false, - ...userConfig, - }); + await Promise.all( + ( + await this.configGenerator.getBuildConfig(watch) + ).map((userConfig) => { + return new Promise((resolve, reject) => { + vite + .build({ + // Avoid recursive builds caused by users configuring @electron-forge/plugin-vite in Vite config file. + configFile: false, + ...userConfig, + plugins: [ + { + name: '@electron-forge/plugin-vite:start', + closeBundle() { + resolve(); - if (Object.keys(buildResult).includes('close')) { - this.watchers.push(buildResult as RollupWatcher); - } - } + // TODO: implement hot-restart here + }, + }, + ...(userConfig.plugins ?? []), + ], + }) + .then((result) => { + const isWatcher = (x: any): x is RollupWatcher => typeof x?.close === 'function'; + + if (isWatcher(result)) { + this.watchers.push(result); + } + + return result; + }) + .catch(reject); + }); + }) + ); }; // Renderer process