bolt.diy/electron/main/utils/vite-server.ts
Derek Wang 1ce6ad6b59
Some checks failed
Docker Publish / docker-build-publish (push) Has been cancelled
Update Stable Branch / prepare-release (push) Has been cancelled
feat: electron desktop app without express server (#1136)
* feat: add electron app

* refactor: using different approach

* chore: update commit hash to 02621e3545

* fix: working dev but prod showing not found and lint fix

* fix: add icon

* fix: resolve server file load issue

* fix: eslint and prettier wip

* fix: only load server build once

* fix: forward request for other ports

* fix: use cloudflare {} to avoid crash

* fix: no need for appLogger

* fix: forward cookie

* fix: update script and update preload loading path

* chore: minor update for appId

* fix: store and load all cookies

* refactor: split main/index.ts

* refactor: group electron main files into two folders

* fix: update electron build configs

* fix: update auto update feat

* fix: vite-plugin-node-polyfills need to be in dependencies for dmg version to work

* ci: trigger build for electron branch

* ci: mark draft if it's from branch commit

* ci: add icons for windows and linux

* fix: update icons for windows

* fix: add author in package.json

* ci: use softprops/action-gh-release@v2

* fix: use path to join

* refactor: refactor path logic for working in both mac and windows

* fix: still need vite-plugin-node-polyfills dependencies

* fix: update vite-electron.config.ts

* ci: sign mac app

* refactor: assets folder

* ci: notarization

* ci: add NODE_OPTIONS

* ci: window only nsis dist

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2025-03-20 00:22:06 +05:30

45 lines
1.2 KiB
TypeScript

import { app } from 'electron';
import type { ViteDevServer } from 'vite';
let viteServer: ViteDevServer | undefined;
// Conditionally import Vite only in development
export async function initViteServer() {
if (!(global.process.env.NODE_ENV === 'production' || app.isPackaged)) {
const vite = await import('vite');
viteServer = await vite.createServer({
root: '.',
envDir: process.cwd(), // load .env files from the root directory.
});
}
}
/*
*
* take care of vite-dev-server.
*
*/
app.on('before-quit', async (_event) => {
if (!viteServer) {
return;
}
/*
* ref: https://stackoverflow.com/questions/68750716/electron-app-throwing-quit-unexpectedly-error-message-on-mac-when-quitting-the-a
* event.preventDefault();
*/
try {
console.log('will close vite-dev-server.');
await viteServer.close();
console.log('closed vite-dev-server.');
// app.quit(); // Not working. causes recursively 'before-quit' events.
app.exit(); // Not working expectedly SOMETIMES. Still throws exception and macOS shows dialog.
// global.process.exit(0); // Not working well... I still see exceptional dialog.
} catch (err) {
console.log('failed to close Vite server:', err);
}
});
export { viteServer };