@@ -5,6 +5,7 @@ import path from "path"
55import fs from "fs"
66import { $ } from "bun"
77import { fileURLToPath } from "url"
8+ import { readdir } from "node:fs/promises" ;
89
910const __filename = fileURLToPath ( import . meta. url )
1011const __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 } '` ,
0 commit comments