feat(electron): improve electron build configuration

- Add dedicated electron build scripts for each platform
- Configure Vite rollup options for electron builds
- Update electron-builder configuration with proper app metadata
- Add clean script and rimraf dependency
- Set proper appId and build settings for production builds
- Enable asar packaging for better security
This commit is contained in:
Matthew Raymer
2025-02-12 03:44:05 +00:00
parent 8d8b90ac19
commit c9f78ae09b
5 changed files with 276 additions and 71 deletions

72
scripts/build-electron.js Normal file
View File

@@ -0,0 +1,72 @@
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);
console.log('Copied web files to:', wwwDir);
// Copy and process main.js
const mainJsSrc = path.resolve(__dirname, '../src/electron/main.js');
const mainJsDest = path.join(distElectronDir, 'main.js');
await fs.copy(mainJsSrc, mainJsDest);
console.log('Copied main.js to:', mainJsDest);
// 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);