Skip to content

Commit 65814b7

Browse files
committed
feat: serve web locally
1 parent 5565b43 commit 65814b7

5 files changed

Lines changed: 66 additions & 7 deletions

File tree

‎bun.lock‎

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "AI-powered development tool",
55
"private": true,
66
"type": "module",
7-
"packageManager": "[email protected].5",
7+
"packageManager": "[email protected].6",
88
"scripts": {
99
"dev": "bun run --cwd packages/opencode --conditions=browser src/index.ts",
1010
"typecheck": "bun turbo typecheck",
@@ -21,7 +21,7 @@
2121
"packages/slack"
2222
],
2323
"catalog": {
24-
"@types/bun": "1.3.5",
24+
"@types/bun": "1.3.6",
2525
"@octokit/rest": "22.0.0",
2626
"@hono/zod-validator": "0.4.2",
2727
"ulid": "3.0.1",

‎packages/opencode/script/build.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import path from "path"
55
import fs from "fs"
66
import { $ } from "bun"
77
import { fileURLToPath } from "url"
8+
import { readdir } from "node:fs/promises";
89

910
const __filename = fileURLToPath(import.meta.url)
1011
const __dirname = path.dirname(__filename)
@@ -127,6 +128,56 @@ for (const item of targets) {
127128
const bunfsRoot = item.os === "win32" ? "B:/~BUN/root/" : "/$bunfs/root/"
128129
const workerRelativePath = path.relative(dir, parserWorker).replaceAll("\\", "/")
129130

131+
await Bun.spawn(["bun", "run", "build"], {
132+
cwd: "../app",
133+
stdout: "inherit",
134+
stderr: "inherit",
135+
}).exited;
136+
137+
const files = await readdir("../app/dist", { recursive: true, withFileTypes: true });
138+
let importFilesScriptPart = ""
139+
let routesScriptPart = ""
140+
for (let i = 0; i < files.length; ++i) {
141+
const file = files[i]
142+
if (file.isFile()) {
143+
const serveBlob = file.name.endsWith('.woff') || file.name.endsWith('.woff2')
144+
const path = file.parentPath + "/" + file.name;
145+
const route = path.substring(11) // cut ../app/dist
146+
let header = ""
147+
if (file.name.endsWith('.js')) {
148+
header = ", jsHeader"
149+
}
150+
if (file.name.endsWith('.css')) {
151+
header = ", cssHeader"
152+
}
153+
if (serveBlob) {
154+
importFilesScriptPart += `import file${i} from '${path}'\n`
155+
routesScriptPart += `'${route}': new Response(Bun.file(file${i})),\n`
156+
} else {
157+
importFilesScriptPart += `import file${i} from '${path}' with { type: 'text' }\n`
158+
routesScriptPart += `'${route}': new Response(file${i}${header}),\n`
159+
}
160+
}
161+
}
162+
163+
const serveWebScript = `
164+
import indexFile from '../app/dist/index.html' with { type: 'text' }
165+
${importFilesScriptPart}
166+
167+
const httpHeader = { headers: { 'Content-Type': 'text/html;charset=utf-8'}}
168+
const jsHeader = { headers: { 'Content-Type': 'text/javascript;charset=utf-8'}}
169+
const cssHeader = { headers: { 'Content-Type': 'text/css;charset=utf-8'}}
170+
const routes = {
171+
'/': new Response(indexFile, httpHeader),
172+
${routesScriptPart}
173+
}
174+
175+
Bun.serve({
176+
port: 3000,
177+
routes
178+
});
179+
`
180+
130181
await Bun.build({
131182
conditions: ["browser"],
132183
tsconfig: "./tsconfig.json",
@@ -143,6 +194,9 @@ for (const item of targets) {
143194
execArgv: [`--user-agent=opencode/${Script.version}`, "--use-system-ca", "--"],
144195
windows: {},
145196
},
197+
files: {
198+
"./src/serveWeb.ts": serveWebScript
199+
},
146200
entrypoints: ["./src/index.ts", parserWorker, workerPath],
147201
define: {
148202
OPENCODE_VERSION: `'${Script.version}'`,

‎packages/opencode/src/index.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ import { WebCommand } from "./cli/cmd/web"
2727
import { PrCommand } from "./cli/cmd/pr"
2828
import { SessionCommand } from "./cli/cmd/session"
2929

30+
const isInsideBunBinary = typeof OPENCODE_VERSION === 'string'
31+
if (isInsideBunBinary) {
32+
// This file is build dynamically in packages/opencode/script/build.ts
33+
require('./src/serveWeb.ts')
34+
}
35+
3036
process.on("unhandledRejection", (e) => {
3137
Log.Default.error("rejection", {
3238
e: e instanceof Error ? e.message : e,

‎packages/opencode/src/server/server.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,8 +499,7 @@ export namespace Server {
499499
)
500500
.all("/*", async (c) => {
501501
const path = c.req.path
502-
503-
const response = await proxy(`https://app.opencode.ai${path}`, {
502+
const response = await proxy(`http://localhost:3000${path}`, {
504503
...c.req,
505504
headers: {
506505
...c.req.raw.headers,

0 commit comments

Comments
 (0)