forked from trent_larson/crowd-funder-for-time-pwa
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:
@@ -1,61 +1,48 @@
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
const fs = require('fs-extra');
|
||||
|
||||
async function main() {
|
||||
try {
|
||||
console.log('Starting electron build process...');
|
||||
|
||||
// Clean directories
|
||||
|
||||
// Create dist directory if it doesn't exist
|
||||
const distElectronDir = path.resolve(__dirname, '../dist-electron');
|
||||
const buildDir = path.resolve(__dirname, '../dist-electron-build');
|
||||
await fs.emptyDir(distElectronDir);
|
||||
await fs.emptyDir(buildDir);
|
||||
console.log('Cleaned directories');
|
||||
await fs.ensureDir(distElectronDir);
|
||||
|
||||
// First build the web app if it doesn't exist
|
||||
const webDist = path.resolve(__dirname, '../dist');
|
||||
if (!await fs.pathExists(webDist)) {
|
||||
console.log('Web dist not found, building web app first...');
|
||||
throw new Error('Please run \'npm run build\' first to build the web app');
|
||||
}
|
||||
|
||||
// Copy web files to www directory
|
||||
// Copy web files
|
||||
const wwwDir = path.join(distElectronDir, 'www');
|
||||
await fs.copy(webDist, wwwDir);
|
||||
await fs.ensureDir(wwwDir);
|
||||
await fs.copy('dist', wwwDir);
|
||||
|
||||
// Fix paths in index.html
|
||||
// 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
|
||||
// Fix absolute paths to be relative
|
||||
.replace(/src="\//g, 'src="\./')
|
||||
.replace(/href="\//g, 'href="\./')
|
||||
// Fix relative asset paths
|
||||
.replace(/src="\.\.\/assets\//g, 'src="./www/assets/')
|
||||
.replace(/href="\.\.\/assets\//g, 'href="./www/assets/')
|
||||
// Fix modulepreload paths specifically
|
||||
.replace(/<link [^>]*rel="modulepreload"[^>]*href="(?!\.?\/www\/)(\/\.\/)?assets\//g, '<link rel="modulepreload" as="script" crossorigin="" href="./www/assets/')
|
||||
.replace(/<link [^>]*rel="modulepreload"[^>]*href="(?!\.?\/www\/)(\/)?assets\//g, '<link rel="modulepreload" as="script" crossorigin="" href="./www/assets/')
|
||||
// Fix stylesheet paths
|
||||
.replace(/<link [^>]*rel="stylesheet"[^>]*href="(?!\.?\/www\/)(\/\.\/)?assets\//g, '<link rel="stylesheet" crossorigin="" href="./www/assets/')
|
||||
.replace(/<link [^>]*rel="stylesheet"[^>]*href="(?!\.?\/www\/)(\/)?assets\//g, '<link rel="stylesheet" crossorigin="" href="./www/assets/')
|
||||
// Fix any remaining asset paths that don't already have www
|
||||
.replace(/(['"]\/?)((?!www\/)(assets\/))/, '"./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',
|
||||
['src/electron/main.js', 'main.js'],
|
||||
['src/electron/preload.js', 'preload.js']
|
||||
];
|
||||
|
||||
for (const file of mainProcessFiles) {
|
||||
const destPath = path.join(distElectronDir, path.basename(file));
|
||||
await fs.copy(file, destPath);
|
||||
for (const [src, dest] of mainProcessFiles) {
|
||||
const destPath = path.join(distElectronDir, dest);
|
||||
console.log(`Copying ${src} to ${destPath}`);
|
||||
await fs.copy(src, destPath);
|
||||
}
|
||||
|
||||
// Create the production package.json
|
||||
// Create package.json for production
|
||||
const devPackageJson = require('../package.json');
|
||||
const prodPackageJson = {
|
||||
name: devPackageJson.name,
|
||||
@@ -72,26 +59,26 @@ async function main() {
|
||||
{ spaces: 2 }
|
||||
);
|
||||
|
||||
// Verify the structure
|
||||
// Verify the build
|
||||
console.log('\nVerifying build structure:');
|
||||
const printDir = async (dir, prefix = '') => {
|
||||
const items = await fs.readdir(dir);
|
||||
for (const item of items) {
|
||||
const fullPath = path.join(dir, item);
|
||||
const stat = await fs.stat(fullPath);
|
||||
console.log(`${prefix}${item}${stat.isDirectory() ? '/' : ''}`);
|
||||
if (stat.isDirectory()) {
|
||||
await printDir(fullPath, `${prefix} `);
|
||||
}
|
||||
}
|
||||
};
|
||||
await printDir(distElectronDir);
|
||||
const files = await fs.readdir(distElectronDir);
|
||||
console.log('Files in dist-electron:', files);
|
||||
|
||||
console.log('\nBuild completed successfully!');
|
||||
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().catch(console.error);
|
||||
main();
|
||||
Reference in New Issue
Block a user