WIP (fix): improve electron build configuration and service worker handling

- Properly disable service workers in electron builds
- Add CSP headers for electron security
- Fix path resolution in electron context
- Improve preload script error handling and IPC setup
- Update build scripts for better electron/capacitor compatibility
- Fix router path handling in electron context
- Remove electron-builder dependency
- Streamline build process and output structure

This change improves the stability and security of electron builds while
maintaining PWA functionality in web builds. Service workers are now
properly disabled in electron context, and path resolution issues are
fixed.
This commit is contained in:
Matthew Raymer
2025-02-12 13:17:25 +00:00
parent 976976e2ea
commit f0d0f63672
10 changed files with 310 additions and 2735 deletions

View File

@@ -1,5 +1,55 @@
const { contextBridge } = require("electron");
const { contextBridge, ipcRenderer } = require('electron');
contextBridge.exposeInMainWorld("api", {
logMessage: (message) => console.log(`[Electron]: ${message}`),
});
// Use a more direct path resolution approach
const getPath = (pathType) => {
switch(pathType) {
case 'userData':
return process.env.APPDATA || (
process.platform === 'darwin'
? `${process.env.HOME}/Library/Application Support`
: `${process.env.HOME}/.local/share`
);
case 'home':
return process.env.HOME;
case 'appPath':
return process.resourcesPath;
default:
return '';
}
};
console.log('Preload script starting...');
try {
contextBridge.exposeInMainWorld('electronAPI', {
// Path utilities
getPath,
// IPC functions
send: (channel, data) => {
const validChannels = ['toMain'];
if (validChannels.includes(channel)) {
ipcRenderer.send(channel, data);
}
},
receive: (channel, func) => {
const validChannels = ['fromMain'];
if (validChannels.includes(channel)) {
ipcRenderer.on(channel, (event, ...args) => func(...args));
}
},
// Environment info
env: {
isElectron: true,
isDev: process.env.NODE_ENV === 'development'
},
// Path utilities
getBasePath: () => {
return process.env.NODE_ENV === 'development' ? '/' : './';
}
});
console.log('Preload script completed successfully');
} catch (error) {
console.error('Error in preload script:', error);
}