'use strict' const fs = require('fs') const path = require('path') const { spawn } = require('child_process') const bytenode = require('bytenode') const FILES_TO_COMPILE = ['main.js'] const backups = new Map() const generatedJsc = new Set() let restoring = false async function backupAndCompile() { for (const file of FILES_TO_COMPILE) { const absPath = path.join(process.cwd(), file) if (!fs.existsSync(absPath)) continue const backupPath = `${absPath}.backup` fs.copyFileSync(absPath, backupPath) backups.set(absPath, backupPath) const parsed = path.parse(absPath) const jscPath = path.join(parsed.dir, `${parsed.name}.jsc`) const existedBefore = fs.existsSync(jscPath) await bytenode.compileFile({ filename: absPath, output: jscPath, electron: true, compileAsModule: true }) if (!existedBefore) generatedJsc.add(jscPath) console.log(`[V8 Builder] Compiled ${file} -> ${path.basename(jscPath)}`) const relBase = parsed.name const requireLine = file === 'main.js' ? `module.exports = require('./${relBase}.jsc')` : `require('./${relBase}.jsc')` fs.writeFileSync(absPath, ["'use strict'", "require('bytenode')", requireLine, ''].join('\n'), 'utf8') } } function restoreFiles() { if (restoring) return restoring = true console.log('\n[V8 Builder] Restoring original source files...') for (const [absPath, backupPath] of backups) { try { if (fs.existsSync(backupPath)) { fs.copyFileSync(backupPath, absPath) fs.unlinkSync(backupPath) console.log(`[V8 Builder] Restored ${path.basename(absPath)}`) } } catch (err) { console.error(`[V8 Builder] restore failed for ${path.basename(absPath)}: ${err.message}`) } } for (const jscPath of generatedJsc) { try { if (fs.existsSync(jscPath)) fs.unlinkSync(jscPath) } catch {} } } process.on('SIGINT', () => { restoreFiles(); process.exit(1) }) process.on('uncaughtException', (err) => { console.error(err); restoreFiles(); process.exit(1) }) async function run() { try { console.log('=== Starting V8 Compilation and Build ===') await backupAndCompile() const builderCli = require.resolve('electron-builder/out/cli/cli.js') const extraArgs = process.argv.slice(2) const args = [builderCli, ...extraArgs] const child = spawn(process.execPath, args, { stdio: 'inherit', windowsHide: true, shell: false, env: { ...process.env } }) child.on('exit', (code) => { restoreFiles(); process.exit(code ?? 1) }) child.on('error', (err) => { console.error(`[V8 Builder] failed: ${err.message}`); restoreFiles(); process.exit(1) }) } catch (err) { console.error(`[V8 Builder] prebuild failed: ${err.message}`) restoreFiles() process.exit(1) } } run()