forked from jsnbuchanan/crowd-funder-for-time-pwa
- 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.
84 lines
2.5 KiB
JavaScript
84 lines
2.5 KiB
JavaScript
const path = require('path');
|
|
const fs = require('fs-extra');
|
|
|
|
async function main() {
|
|
try {
|
|
console.log('Starting electron build process...');
|
|
|
|
// Create dist directory if it doesn't exist
|
|
const distElectronDir = path.resolve(__dirname, '../dist-electron');
|
|
await fs.ensureDir(distElectronDir);
|
|
|
|
// Copy web files
|
|
const wwwDir = path.join(distElectronDir, 'www');
|
|
await fs.ensureDir(wwwDir);
|
|
await fs.copy('dist', wwwDir);
|
|
|
|
// Copy and fix index.html
|
|
const indexPath = path.join(wwwDir, 'index.html');
|
|
let indexContent = await fs.readFile(indexPath, 'utf8');
|
|
|
|
// Fix paths in index.html
|
|
indexContent = indexContent
|
|
.replace(/src="\//g, 'src="\./')
|
|
.replace(/href="\//g, 'href="\./')
|
|
.replace(/src="\.\.\/assets\//g, 'src="./www/assets/')
|
|
.replace(/href="\.\.\/assets\//g, 'href="./www/assets/');
|
|
|
|
await fs.writeFile(indexPath, indexContent);
|
|
|
|
console.log('Copied and fixed web files in:', wwwDir);
|
|
|
|
// Copy main process files
|
|
console.log('Copying main process files...');
|
|
const mainProcessFiles = [
|
|
['src/electron/main.js', 'main.js'],
|
|
['src/electron/preload.js', 'preload.js']
|
|
];
|
|
|
|
for (const [src, dest] of mainProcessFiles) {
|
|
const destPath = path.join(distElectronDir, dest);
|
|
console.log(`Copying ${src} to ${destPath}`);
|
|
await fs.copy(src, destPath);
|
|
}
|
|
|
|
// Create package.json for production
|
|
const devPackageJson = require('../package.json');
|
|
const prodPackageJson = {
|
|
name: devPackageJson.name,
|
|
version: devPackageJson.version,
|
|
description: devPackageJson.description,
|
|
author: devPackageJson.author,
|
|
main: 'main.js',
|
|
private: true,
|
|
};
|
|
|
|
await fs.writeJson(
|
|
path.join(distElectronDir, 'package.json'),
|
|
prodPackageJson,
|
|
{ spaces: 2 }
|
|
);
|
|
|
|
// Verify the build
|
|
console.log('\nVerifying build structure:');
|
|
const files = await fs.readdir(distElectronDir);
|
|
console.log('Files in dist-electron:', files);
|
|
|
|
if (!files.includes('main.js')) {
|
|
throw new Error('main.js not found in build directory');
|
|
}
|
|
if (!files.includes('preload.js')) {
|
|
throw new Error('preload.js not found in build directory');
|
|
}
|
|
if (!files.includes('package.json')) {
|
|
throw new Error('package.json not found in build directory');
|
|
}
|
|
|
|
console.log('Build completed successfully!');
|
|
} catch (error) {
|
|
console.error('Build failed:', error);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
main();
|