From 5f348f9b1c8cbfb7a8f313605f9d4b518230f312 Mon Sep 17 00:00:00 2001 From: Pine Date: Fri, 14 Aug 2026 23:25:54 +0800 Subject: [PATCH] fix(desktop): bundle the preload to CommonJS so the dshApp bridge loads under sandbox MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The renderer opens with sandbox:true, and Electron sandboxed preload scripts only support CommonJS. The preload was emitted as ESM (package.json is "type": "module"), so it failed to load silently and window.dshApp was never injected. The About section then fell back to plain-web mode: current version stayed on the '—' fallback and check-for-updates reported up-to-date without querying. Build the preload with esbuild into lib/types/preload.cjs and point the main process at it. Co-Authored-By: Claude --- apps/desktop/electron-builder.yml | 1 + apps/desktop/package.json | 5 ++++- apps/desktop/src/main.ts | 10 ++++++++-- pnpm-lock.yaml | 3 +++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/apps/desktop/electron-builder.yml b/apps/desktop/electron-builder.yml index 8207ee4588..31bb176175 100644 --- a/apps/desktop/electron-builder.yml +++ b/apps/desktop/electron-builder.yml @@ -24,6 +24,7 @@ directories: output: dist files: - lib/types/**/*.js + - lib/types/**/*.cjs - package.json # The self-contained harness (build/harness) lands at Contents/Resources/harness # in the packaged app; src/main.ts spawns the bundled node + dsh CLI from there. diff --git a/apps/desktop/package.json b/apps/desktop/package.json index 65484da5b0..50f702f53a 100644 --- a/apps/desktop/package.json +++ b/apps/desktop/package.json @@ -8,11 +8,13 @@ "main": "lib/types/main.js", "files": [ "lib/types/*.js", + "lib/types/*.cjs", "lib/types/*.d.ts", "electron-builder.yml" ], "scripts": { - "build": "tsc -b tsconfig.json", + "build": "tsc -b tsconfig.json && pnpm run build:preload", + "build:preload": "esbuild src/preload.ts --bundle --platform=node --format=cjs --external:electron --outfile=lib/types/preload.cjs", "build:harness": "node scripts/build-harness.mjs", "generate-release-json": "node scripts/generate-release-json.mjs", "stage-release": "node scripts/stage-release.mjs", @@ -27,6 +29,7 @@ "@types/node": "^22.0.0", "electron": "^42.4.1", "electron-builder": "^25.1.8", + "esbuild": "^0.28.1", "typescript": "^6.0.3", "vitest": "^4.1.8" } diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts index 7d781501c2..173132ae1b 100644 --- a/apps/desktop/src/main.ts +++ b/apps/desktop/src/main.ts @@ -50,8 +50,14 @@ const PRODUCT_NAME = 'DeepSeek Harness' /** Window icon: the branded PNG in build/, resolved from this ES module's URL. */ const APP_ICON = fileURLToPath(new URL('../../build/icon.png', import.meta.url)) -/** Renderer preload (compiled to lib/types/preload.js), resolved from this ES module's URL. */ -const PRELOAD = fileURLToPath(new URL('./preload.js', import.meta.url)) +/** + * Renderer preload, resolved from this ES module's URL. The preload is bundled + * to CommonJS (`lib/types/preload.cjs`) rather than left as ESM: the renderer + * runs `sandbox: true`, and Electron sandboxed preload scripts only support + * CommonJS. An ESM preload would silently fail to load and drop the `dshApp` + * bridge from the SPA. + */ +const PRELOAD = fileURLToPath(new URL('./preload.cjs', import.meta.url)) /** * The self-contained harness runtime bundled into the packaged app by diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a346f46d40..79adb16a69 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -371,6 +371,9 @@ importers: electron-builder: specifier: ^25.1.8 version: 25.1.8(electron-builder-squirrel-windows@25.1.8) + esbuild: + specifier: ^0.28.1 + version: 0.28.1 typescript: specifier: ^6.0.3 version: 6.0.3