Files
crowd-funder-from-jason/scripts/build-electron.js
Matthew Raymer e6138bd0e2 fix: improve electron asset path handling
- Add extraResources config to package.json to properly include www directory
- Update path rewriting in build-electron.js to handle all asset types
- Fix modulepreload and stylesheet paths in index.html
- Improve path handling in main.js for packaged app
- Clean up file protocol handling and service worker management

This fixes the issue with assets not loading in the packaged Electron app
by ensuring all paths are correctly rewritten to reference the www directory
in the app resources.
2025-02-12 07:47:18 +00:00

97 lines
3.6 KiB
JavaScript

const fs = require('fs-extra');
const path = require('path');
async function main() {
try {
console.log('Starting electron build process...');
// Clean directories
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');
// 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
const wwwDir = path.join(distElectronDir, 'www');
await fs.copy(webDist, wwwDir);
// Fix paths in index.html
const indexPath = path.join(wwwDir, 'index.html');
let indexContent = await fs.readFile(indexPath, 'utf8');
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/');
await fs.writeFile(indexPath, indexContent);
console.log('Copied and fixed web files in:', wwwDir);
// Copy main process files
const mainProcessFiles = [
'src/electron/main.js',
];
for (const file of mainProcessFiles) {
const destPath = path.join(distElectronDir, path.basename(file));
await fs.copy(file, destPath);
}
// Create the production package.json
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 structure
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);
console.log('\nBuild completed successfully!');
} catch (error) {
console.error('Build failed:', error);
process.exit(1);
}
}
main().catch(console.error);