mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-15 14:22:43 +00:00
Electron 40 ships Node 24.15, where tsx's ESM load hook returns null and crashes with ERR_INVALID_RETURN_PROPERTY_VALUE. Bundle main+preload via esbuild for `npm run dev` and always load the JS preload from dist/.
65 lines
2.1 KiB
JavaScript
65 lines
2.1 KiB
JavaScript
#!/usr/bin/env node
|
|
// bundle-electron-main.mjs — bundles electron/main.ts and electron/preload.ts
|
|
// into self-contained js files in dist/ so the packaged app doesn't need
|
|
// node_modules/ or tsx at runtime.
|
|
//
|
|
// Output:
|
|
// dist/electron-main.mjs (MJS bundle — entry point for packaged app)
|
|
// dist/electron-preload.js (CJS bundle — loaded via BrowserWindow preload)
|
|
//
|
|
// `electron` and `node-pty` are external (provided by the runtime / staged
|
|
// separately via stage-native-deps).
|
|
import { build } from 'esbuild'
|
|
import { resolve, dirname } from 'node:path'
|
|
import { fileURLToPath } from 'node:url'
|
|
import { mkdirSync } from 'node:fs'
|
|
|
|
const here = dirname(fileURLToPath(import.meta.url))
|
|
const root = resolve(here, '..')
|
|
const distDir = resolve(root, 'dist')
|
|
mkdirSync(distDir, { recursive: true })
|
|
|
|
const mainEntry = resolve(root, 'electron/main.ts')
|
|
const mainOut = resolve(distDir, 'electron-main.mjs')
|
|
const preloadEntry = resolve(root, 'electron/preload.ts')
|
|
const preloadOut = resolve(distDir, 'electron-preload.js')
|
|
|
|
const external = ['electron', 'node-pty', 'fs']
|
|
// Production bundles bake packaged=true so unpackaged `electron .` still
|
|
// behaves like a packaged build. Dev bundles (`--dev`) leave the env alone
|
|
// so HERMES_DESKTOP_DEV_SERVER / source-tree resolution keep working.
|
|
const isDev = process.argv.includes('--dev')
|
|
const define = isDev
|
|
? {}
|
|
: { 'process.env.HERMES_DESKTOP_IS_PACKAGED': JSON.stringify(true) }
|
|
|
|
// Bundle main.ts → dist/electron-main.mjs
|
|
await build({
|
|
entryPoints: [mainEntry],
|
|
bundle: true,
|
|
platform: 'node',
|
|
format: 'esm',
|
|
target: 'node20',
|
|
outfile: mainOut,
|
|
external,
|
|
banner: {
|
|
js: "import { createRequire } from 'module'; const require = createRequire(import.meta.url);",
|
|
},
|
|
define,
|
|
logLevel: 'info',
|
|
})
|
|
console.log(`bundled ${mainOut}${isDev ? ' (dev)' : ''}`)
|
|
|
|
// Bundle preload.ts → dist/electron-preload.js
|
|
await build({
|
|
entryPoints: [preloadEntry],
|
|
bundle: true,
|
|
platform: 'node',
|
|
format: 'cjs',
|
|
target: 'node20',
|
|
outfile: preloadOut,
|
|
external,
|
|
define,
|
|
logLevel: 'info',
|
|
})
|
|
console.log(`bundled ${preloadOut}${isDev ? ' (dev)' : ''}`)
|