electron-vite/bin/electron-bytecode-main.cjs
murrayee 4847045063 fix(bytecode): support Electron 42 and newer
Compile bytecode inside a real Electron main process on Electron 42+ so the V8 read-only snapshot checksum matches at runtime. Batch all chunks into one compiler process and retain the legacy run-as-node path for older Electron releases.

Resolve Electron through its package entry to support lazy binary installation, and add Electron 42/43 build targets.

Closes #911
2026-08-20 16:58:22 +08:00

33 lines
924 B
JavaScript

const fs = require('node:fs')
const Module = require('node:module')
const v8 = require('node:v8')
const vm = require('node:vm')
const { app } = require('electron')
v8.setFlagsFromString('--no-lazy')
v8.setFlagsFromString('--no-flush-bytecode')
app.disableHardwareAcceleration()
async function compile() {
const manifestPath = process.argv[2]
if (!manifestPath) {
throw new Error('Bytecode compiler manifest path is required')
}
const jobs = JSON.parse(fs.readFileSync(manifestPath, 'utf8'))
for (const job of jobs) {
const code = fs.readFileSync(job.input, 'utf8')
const script = new vm.Script(Module.wrap(code), { produceCachedData: true })
fs.writeFileSync(job.output, script.createCachedData())
}
}
app
.whenReady()
.then(compile)
.then(() => app.exit(0))
.catch(error => {
process.stderr.write(`${error instanceof Error ? error.stack : String(error)}\n`)
app.exit(1)
})