From 3ac3de94967d60df1245cea119d6a03a7f857d54 Mon Sep 17 00:00:00 2001 From: William Wills Date: Mon, 25 Nov 2024 17:01:33 -0500 Subject: [PATCH] feat: removed unnecessary express server for renderer to load ui --- package-lock.json | 4 ++-- package.json | 1 - src/main.ts | 45 +++++++++++++++++++++++---------------------- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0e6f7d39..7aa7d332 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "core-registry-cadt-ui", - "version": "2.0.0", + "version": "2.0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "core-registry-cadt-ui", - "version": "2.0.0", + "version": "2.0.1", "dependencies": { "@emotion/react": "^11.13.0", "@emotion/styled": "^11.13.0", diff --git a/package.json b/package.json index 1af67991..e826e014 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,6 @@ "components": "^0.1.0", "dayjs": "^1.11.12", "diff": "^5.2.0", - "express": "^4.19.2", "flowbite": "^2.5.1", "flowbite-react": "^0.7.8", "flowbite-typography": "^1.0.3", diff --git a/src/main.ts b/src/main.ts index 352a0694..ca9ef389 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,21 +1,10 @@ import { app, BrowserWindow, shell } from 'electron'; import path from 'path'; -import express from 'express'; +import { fileURLToPath } from 'url'; -//electron -const serverPort = 61310; -const appServer = express(); - -const buildPath = path.join(app.getAppPath(), 'build/renderer'); // Path to your Vite build - -appServer.use(express.static(buildPath)); // Serve static files from the build directory - -// Catch-all handler to return index.html for any non-static file request -appServer.get('*', (_, res) => { - res.sendFile(path.join(buildPath, 'index.html')); -}); - -appServer.listen(serverPort); +// Manually define __dirname +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); function createWindow() { const win = new BrowserWindow({ @@ -25,17 +14,29 @@ function createWindow() { contextIsolation: false, nodeIntegration: true, webviewTag: true, - // preload: path.join(__dirname, 'preload.js'), }, }); // Load URL based on the environment - const loadUrl = - process.env.NODE_ENV === 'development' - ? 'http://localhost:5175/' // Development URL - : `http://localhost:${serverPort}/`; // Production URL served by Express - - win.loadURL(loadUrl); + if (process.env.NODE_ENV === 'development') { + console.log('loading app from dev node server'); + win.loadURL('http://localhost:5175/'); // Development URL + } else { + // load app from packaged static html + const indexPath = path.join(__dirname, 'renderer', 'index.html'); + console.log('Loading file:', indexPath); // Log to confirm the correct path + win.loadFile(indexPath).catch((err) => { + console.error('Failed to load index.html:', err); + }); + + // app is a SPA. reload index.html when the app tries to load a virtual route + win.webContents.on('did-fail-load', (_event, _errorCode, _errorDescription, validatedURL) => { + console.log(`Failed to load: ${validatedURL}, loading index.html instead`); + win.loadFile(indexPath).catch((err) => { + console.error('Failed to load index.html:', err); + }); + }); + } win.webContents.setWindowOpenHandler(({ url }) => { shell.openExternal(url);