Files
fusion/packages/desktop/scripts/build.ts
gsxdsm 1f3a15ec1c FN-7360: fix Windows desktop runtime packaging
Fix packaged Windows desktop startup by preserving Node-safe runtime dependencies and dashboard registry assets.

- Externalize Electron main/preload npm packages so CommonJS updater dependencies load natively.
- Load the dashboard plugin registry manifest through file IO and copy it into dashboard server dist from all build paths.
- Split Windows NSIS and portable artifact names and verify required Electron runtime resources in CI.
- Cover the packaging/runtime invariants with desktop and dashboard regression tests.

Files changed:
 .changeset/fn-7360-desktop-windows.md              |  7 +++
 .github/workflows/desktop-windows.yml              | 21 ++++++++
 packages/dashboard/package.json                    |  2 +-
 .../src/__tests__/plugin-registry-dist.test.ts     | 32 ++++++++++++
 .../src/__tests__/routes-plugin-registry.test.ts   | 10 ++--
 packages/dashboard/src/plugin-routes.ts            | 27 +++++++++-
 packages/desktop/README.md                         |  5 +-
 packages/desktop/electron-builder.yml              |  9 ++++
 packages/desktop/scripts/build.ts                  | 29 ++++++++--
 packages/desktop/scripts/workspace-tools.ts        |  4 ++
 .../desktop/src/__tests__/build-bundling.test.ts   | 61 ++++++++++++++++++++++
 .../src/__tests__/electron-builder-config.test.ts  | 15 ++++++
 packages/desktop/src/__tests__/ipc.test.ts         | 15 ++++++
 .../desktop/src/__tests__/local-runtime.test.ts    | 20 +++++++
 14 files changed, 247 insertions(+), 10 deletions(-)

Fusion-Task-Id: FN-7360

Fusion-Task-Lineage: a2d88130-c24f-43e2-932a-1e174b6afa35

Co-authored-by: Fusion (runfusion.ai) <noreply@runfusion.ai>
2026-07-01 08:01:59 -07:00

115 lines
4.5 KiB
TypeScript

import { build } from "esbuild";
import { cp, mkdir, rm, stat } from "node:fs/promises";
import { join } from "node:path";
import { buildDashboard, buildDashboardClient, packageRoot, workspaceRoot } from "./workspace-tools";
const dashboardRoot = join(workspaceRoot, "packages", "dashboard");
const dashboardClientDir = join(dashboardRoot, "dist", "client");
const dashboardRegistryManifestSource = join(dashboardRoot, "src", "registry-manifest.json");
const dashboardRegistryManifestDist = join(dashboardRoot, "dist", "registry-manifest.json");
const desktopDistDir = join(packageRoot, "dist");
const desktopClientDistDir = join(desktopDistDir, "client");
// FNXC:DesktopBuild 2026-06-25-09:45:
// Every workspace @fusion/* package and native (.node) module must stay external
// to the Electron main/preload bundles — they resolve from node_modules at runtime.
// @fusion/engine was missing here, so esbuild followed local-runtime.ts's dynamic
// `import("@fusion/engine")` and tried to bundle engine's transitive node-pty
// (@homebridge/node-pty-prebuilt-multiarch) native binaries, failing with
// "No loader is configured for .node files" and breaking every desktop release build.
const sharedExternals = [
"electron",
"@fusion/core",
"@fusion/dashboard",
"@fusion/engine",
"better-sqlite3",
];
const mainExternals = sharedExternals;
const preloadExternals = sharedExternals;
async function ensureDashboardBuild(): Promise<void> {
// FNXC:DesktopBuild 2026-07-01-11:35:
// Windows release packaging invokes only `@fusion/desktop build` before electron-builder.
// Build the dashboard server dist and copy registry-manifest.json here so the packaged
// embedded runtime never depends on a separate `@fusion/dashboard build` workflow step.
console.log("[desktop:build] Building dashboard server runtime...");
await buildDashboard();
await cp(dashboardRegistryManifestSource, dashboardRegistryManifestDist);
console.log("[desktop:build] Building dashboard client for file:// desktop loading...");
await buildDashboardClient();
try {
await stat(dashboardClientDir);
} catch {
throw new Error(`Dashboard client assets not found: ${dashboardClientDir}`);
}
try {
await stat(dashboardRegistryManifestDist);
} catch {
throw new Error(`Dashboard registry manifest not found: ${dashboardRegistryManifestDist}`);
}
}
async function buildElectronEntrypoints(): Promise<void> {
console.log("[desktop:build] Bundling Electron main/preload with esbuild...");
await Promise.all([
build({
entryPoints: [join(packageRoot, "src", "main.ts")],
outfile: join(desktopDistDir, "main.js"),
bundle: true,
format: "esm",
platform: "node",
target: "node22",
sourcemap: true,
// FNXC:DesktopBuild 2026-07-01-07:31:
// Windows Electron main output is ESM, but electron-updater loads CJS deps
// such as fs-extra/graceful-fs that dynamically require built-ins. Keep all
// npm packages external so Node/Electron evaluates those CJS modules natively
// instead of esbuild emitting a __require("fs") trap in dist/main.js.
packages: "external",
external: mainExternals,
logLevel: "info",
}),
build({
entryPoints: [join(packageRoot, "src", "preload.ts")],
outfile: join(desktopDistDir, "preload.js"),
bundle: true,
// Preload scripts must be CommonJS — Electron loads them via the
// sandboxed Node context, not as ESM. With format:"esm" the
// contextBridge calls silently no-op and window.fusionShell /
// window.fusionAPI stay undefined, which made the dashboard fall
// through to "can't reach the Fusion backend" and the launch gate
// always bypass.
format: "cjs",
platform: "node",
target: "node22",
sourcemap: true,
packages: "external",
external: preloadExternals,
logLevel: "info",
}),
]);
}
async function copyDashboardClient(): Promise<void> {
console.log("[desktop:build] Copying dashboard client into desktop dist/client...");
await cp(dashboardClientDir, desktopClientDistDir, { recursive: true });
}
async function main(): Promise<void> {
await rm(desktopDistDir, { recursive: true, force: true });
await mkdir(desktopDistDir, { recursive: true });
await ensureDashboardBuild();
await buildElectronEntrypoints();
await copyDashboardClient();
console.log("[desktop:build] Desktop build complete");
}
void main().catch((error) => {
console.error("[desktop:build] Build failed", error);
process.exitCode = 1;
});