mirror of
https://github.com/backnotprop/plannotator.git
synced 2026-09-14 14:17:26 +08:00
Fix: embed HTML at compile time for binary distribution
The server was reading dist/index.html from filesystem at runtime, which fails in compiled standalone binaries. Changed to use Bun's import attributes to embed the HTML at compile time. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -8,7 +8,9 @@
|
||||
*/
|
||||
|
||||
import { $ } from "bun";
|
||||
import { join, dirname } from "path";
|
||||
|
||||
// Embed the built HTML at compile time
|
||||
import indexHtml from "../dist/index.html" with { type: "text" };
|
||||
|
||||
// Read hook event from stdin
|
||||
const eventJson = await Bun.stdin.text();
|
||||
@@ -33,10 +35,6 @@ const decisionPromise = new Promise<{ approved: boolean; feedback?: string }>(
|
||||
(resolve) => { resolveDecision = resolve; }
|
||||
);
|
||||
|
||||
// Resolve paths relative to this script
|
||||
const serverDir = dirname(import.meta.path);
|
||||
const distDir = join(serverDir, "..", "dist");
|
||||
|
||||
const server = Bun.serve({
|
||||
port: 0, // Random available port - critical for multi-instance support
|
||||
|
||||
@@ -65,43 +63,13 @@ const server = Bun.serve({
|
||||
return Response.json({ ok: true });
|
||||
}
|
||||
|
||||
// Serve static files from dist/
|
||||
let filePath = url.pathname;
|
||||
if (filePath === "/" || filePath === "") {
|
||||
filePath = "/index.html";
|
||||
}
|
||||
|
||||
const file = Bun.file(join(distDir, filePath));
|
||||
if (await file.exists()) {
|
||||
const contentType = getContentType(filePath);
|
||||
return new Response(file, {
|
||||
headers: contentType ? { "Content-Type": contentType } : {}
|
||||
});
|
||||
}
|
||||
|
||||
// Fallback to index.html for SPA routing
|
||||
const indexFile = Bun.file(join(distDir, "index.html"));
|
||||
if (await indexFile.exists()) {
|
||||
return new Response(indexFile, {
|
||||
headers: { "Content-Type": "text/html" }
|
||||
});
|
||||
}
|
||||
|
||||
return new Response("Not found", { status: 404 });
|
||||
// Serve embedded HTML for all other routes (SPA)
|
||||
return new Response(indexHtml, {
|
||||
headers: { "Content-Type": "text/html" }
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
function getContentType(path: string): string | null {
|
||||
if (path.endsWith(".html")) return "text/html";
|
||||
if (path.endsWith(".js")) return "application/javascript";
|
||||
if (path.endsWith(".css")) return "text/css";
|
||||
if (path.endsWith(".json")) return "application/json";
|
||||
if (path.endsWith(".svg")) return "image/svg+xml";
|
||||
if (path.endsWith(".png")) return "image/png";
|
||||
if (path.endsWith(".ico")) return "image/x-icon";
|
||||
return null;
|
||||
}
|
||||
|
||||
// Log to stderr so it doesn't interfere with hook stdout
|
||||
console.error(`Plannotator server running on http://localhost:${server.port}`);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user