Compare commits

...
Author SHA1 Message Date
bincxz b3bf7c25fc fix: load shared container runtime in Vite renderer 2026-10-21 11:03:51 +08:00
3 changed files with 71 additions and 3 deletions

No files matched your search

+3 -3
View File
@@ -1,4 +1,4 @@
import { runtimeFailureMessage } from "./containerRuntime.shared.cjs";
import { runtimeFailureMessage } from "@/domain/systemManager/containerRuntime.shared.cjs";
export type {
ContainerRuntimeId,
@@ -6,7 +6,7 @@ export type {
KubeContextInfo,
KubectlPodInfo,
RuntimeProbeFlags,
} from "./containerRuntime.shared.cjs";
} from "@/domain/systemManager/containerRuntime.shared.cjs";
export {
NETCATTY_POPUP_HISTORY_MARKER,
@@ -24,7 +24,7 @@ export {
reconcileContainerSource,
rememberedContainerSource,
sourcesFromCapabilities,
} from "./containerRuntime.shared.cjs";
} from "@/domain/systemManager/containerRuntime.shared.cjs";
export function describeRuntimeFailure(
result: { error?: string; errorCode?: string } | null | undefined,
+62
View File
@@ -0,0 +1,62 @@
"use strict";
const test = require("node:test");
const assert = require("node:assert/strict");
const { spawnSync } = require("node:child_process");
const path = require("node:path");
// Use native ESM linking, rather than Node's CommonJS interop or a bundler,
// so a missing browser export fails exactly as it does in the renderer.
async function checkDevImport() {
const { createServer } = await import("vite");
const { SourceTextModule, createContext } = await import("node:vm");
const { mkdtempSync, rmSync } = await import("node:fs");
const { createRequire } = await import("node:module");
const assert = (await import("node:assert/strict")).default;
const require = createRequire(`${process.cwd()}/package.json`);
const temp = require("./electron/bridges/tempDirBridge.cjs");
const cacheDir = mkdtempSync(`${temp.getTempFilePath("container-runtime-vite")}-`);
let server;
try {
server = await createServer({
cacheDir,
logLevel: "error",
server: { port: 0, strictPort: false, open: false },
optimizeDeps: { entries: [], noDiscovery: true },
});
await server.listen();
const origin = server.resolvedUrls.local[0];
const context = createContext({});
const modules = new Map();
async function load(url) {
if (!modules.has(url)) {
modules.set(url, (async () => {
const response = await fetch(url);
assert.equal(response.status, 200, url);
return new SourceTextModule(await response.text(), { context, identifier: url });
})());
}
return modules.get(url);
}
const entry = await load(new URL("/domain/systemManager/containerRuntime.ts", origin).href);
await entry.link((specifier, importer) => load(new URL(specifier, importer.identifier).href));
await entry.evaluate();
const runtime = entry.namespace;
assert.equal(runtime.firstContainerRuntime(["podman"]), "podman");
assert.equal(runtime.buildContainerExecShellCommand("podman", "abc").includes("podman exec"), true);
assert.equal(runtime.describeRuntimeFailure({ errorCode: "nerdctlUnreachable" }, key => key, "fallback"), "systemManager.errors.nerdctlUnreachable");
assert.equal(runtime.describeRuntimeFailure({ error: "original error" }, key => key, "fallback"), "original error");
assert.equal(runtime.describeRuntimeFailure(null, key => key, "fallback"), "fallback");
} finally {
await server?.close();
rmSync(cacheDir, { recursive: true, force: true });
}
}
test("Vite serves the renderer container runtime with browser-compatible exports", { timeout: 60_000 }, () => {
const result = spawnSync(process.execPath, [
"--experimental-vm-modules", "--input-type=module", "-e",
`(${checkDevImport.toString()})().catch(error => { console.error(error); process.exitCode = 1; });`,
], { cwd: path.resolve(__dirname, ".."), encoding: "utf8", timeout: 55_000 });
assert.equal(result.status, 0, result.error?.message || `${result.stdout}\n${result.stderr}`);
});
+6
View File
@@ -83,6 +83,10 @@ export default defineConfig(() => {
},
},
build: {
// The shared runtime is also required by Electron; convert it for browser builds.
commonjsOptions: {
include: [/node_modules/, /domain\/systemManager\/containerRuntime\.shared\.cjs$/],
},
chunkSizeWarningLimit: 3000,
target: 'esnext', // Required for top-level await in WASM modules
sourcemap: false, // Disable source maps to avoid missing map file warnings
@@ -124,6 +128,8 @@ export default defineConfig(() => {
plugins: [suppressMonacoSourcemapWarning(), devContentSecurityPolicyPlugin(), warnOnOutdatedOptimizeDep(), tailwindcss(), react()],
optimizeDeps: {
include: [
// Keep the renderer alias pre-bundled so dev serves ESM instead of raw CommonJS.
'@/domain/systemManager/containerRuntime.shared.cjs',
'ai',
'@ai-sdk/openai',
'@ai-sdk/anthropic',