mirror of
https://github.com/alex8088/electron-vite.git
synced 2026-11-02 14:51:51 +08:00
16 lines
692 B
JavaScript
16 lines
692 B
JavaScript
const vm = require('node:vm')
|
|
const v8 = require('node:v8')
|
|
|
|
v8.setFlagsFromString('--no-lazy')
|
|
v8.setFlagsFromString('--no-flush-bytecode')
|
|
|
|
const params = ['exports', 'require', 'module', '__filename', '__dirname']
|
|
|
|
module.exports = function compile(code) {
|
|
if (typeof code !== 'string') throw new TypeError('Compilation source must be a string')
|
|
const { cachedData } = vm.compileFunction(code, params, { produceCachedData: true })
|
|
if (!Buffer.isBuffer(cachedData)) throw new Error('V8 did not produce cached data')
|
|
// Explicit wire format works across both Node IPC and Electron structured clone.
|
|
return { sourceLength: code.length, cachedData: cachedData.toString('base64') }
|
|
}
|