forked from trent_larson/crowd-funder-for-time-pwa
- Completely rewrite main.js for reliable asset loading - Update preload.js with proper security context isolation - Fix file:// protocol handling for application resources - Add proper error logging and reporting in Electron context - Disable service workers in Electron environment - Fix path resolution for assets in packaged application - Add prerequisite checking for Electron builds - Update electron-builder configuration The changes resolve persistent issues with: 1. Missing assets in packaged application 2. Incorrect path resolution in production builds 3. Service worker conflicts in desktop environment 4. Security context handling in preload script 5. Electron build process reliability
61 lines
1.7 KiB
JavaScript
61 lines
1.7 KiB
JavaScript
/**
|
|
* Fix path resolution issues in the Electron build
|
|
*/
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const glob = require('glob');
|
|
|
|
// Fix asset paths in HTML file
|
|
function fixHtmlPaths() {
|
|
const htmlFile = path.join(__dirname, '../dist-electron/index.html');
|
|
if (fs.existsSync(htmlFile)) {
|
|
let html = fs.readFileSync(htmlFile, 'utf8');
|
|
|
|
// Convert absolute paths to relative
|
|
html = html.replace(/src="\//g, 'src="./');
|
|
html = html.replace(/href="\//g, 'href="./');
|
|
|
|
fs.writeFileSync(htmlFile, html);
|
|
console.log('✅ Fixed paths in index.html');
|
|
}
|
|
}
|
|
|
|
// Fix asset imports in JS files
|
|
function fixJsPaths() {
|
|
const jsFiles = glob.sync('dist-electron/assets/*.js');
|
|
|
|
jsFiles.forEach(file => {
|
|
let content = fs.readFileSync(file, 'utf8');
|
|
|
|
// Replace absolute imports with relative ones
|
|
const originalContent = content;
|
|
content = content.replace(/["']\/assets\//g, '"./assets/');
|
|
|
|
if (content !== originalContent) {
|
|
fs.writeFileSync(file, content);
|
|
console.log(`✅ Fixed paths in ${path.basename(file)}`);
|
|
}
|
|
});
|
|
}
|
|
|
|
// Add base href to HTML
|
|
function addBaseHref() {
|
|
const htmlFile = path.join(__dirname, '../dist-electron/index.html');
|
|
if (fs.existsSync(htmlFile)) {
|
|
let html = fs.readFileSync(htmlFile, 'utf8');
|
|
|
|
// Add base href if not present
|
|
if (!html.includes('<base href=')) {
|
|
html = html.replace('</head>', '<base href="./">\n</head>');
|
|
fs.writeFileSync(htmlFile, html);
|
|
console.log('✅ Added base href to index.html');
|
|
}
|
|
}
|
|
}
|
|
|
|
// Run all fixes
|
|
fixHtmlPaths();
|
|
fixJsPaths();
|
|
addBaseHref();
|
|
|
|
console.log('🎉 Electron path fixes completed');
|