Preview-pane guest pages (Streamlit's traceback "Ask Google" / "Ask …" buttons, plain `<a target="_blank">` anchors) could not open anything: the `<webview>` has no `allowpopups`, so Chromium drops the popup before any handler runs. Opening from `setWindowOpenHandler` is banned (GHSA-9f4c-93c8-jc8g, window-open-policy.ts), so this adds an explicit click bridge instead: - main.ts installs a guest preload through `will-attach-webview`, keyed on the `persist:hermes-preview` partition only. - The preload forwards a clicked `_blank` anchor's resolved href to the host renderer via `ipcRenderer.sendToHost`; it opens nothing itself. - PreviewPane admits the URL and routes it through the existing audited `hermes:openExternal` IPC. Salvaged from PR #112959 (squash of its two commits). Fixes #112941
88 lines
3.0 KiB
JavaScript
88 lines
3.0 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)
|
|
// dist/preview-guest-preload.js (CJS bundle — preview <webview> guest 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'
|
|
import { buildCommandScreenshotMonitor } from './build-command-screenshot-monitor.mjs'
|
|
|
|
const here = dirname(fileURLToPath(import.meta.url))
|
|
const root = resolve(here, '..')
|
|
const distDir = resolve(root, 'dist')
|
|
mkdirSync(distDir, { recursive: true })
|
|
// Stage for both --dev and release bundles; non-mac hosts skip this helper.
|
|
buildCommandScreenshotMonitor({ distDir })
|
|
|
|
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', 'get-windows', '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)' : ''}`)
|
|
|
|
// Bundle preview-guest-preload-entry.ts → dist/preview-guest-preload.js
|
|
// (main.ts hands this path to the preview webview via will-attach-webview)
|
|
const guestPreloadEntry = resolve(root, 'electron/preview-guest-preload-entry.ts')
|
|
const guestPreloadOut = resolve(distDir, 'preview-guest-preload.js')
|
|
|
|
await build({
|
|
entryPoints: [guestPreloadEntry],
|
|
bundle: true,
|
|
platform: 'node',
|
|
format: 'cjs',
|
|
target: 'node20',
|
|
outfile: guestPreloadOut,
|
|
external,
|
|
define,
|
|
logLevel: 'info',
|
|
})
|
|
console.log(`bundled ${guestPreloadOut}${isDev ? ' (dev)' : ''}`)
|