Skip to content

Commit

Permalink
stall requests until bundler is done (startup)
Browse files Browse the repository at this point in the history
  • Loading branch information
drinking-code committed Apr 6, 2023
1 parent f29f1ed commit 1ff7fab
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/compiler/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
outputPath, refsAndTemplatesFilePath,
virtualFilesPath
} from './client-script/generate-data-files'
import {resolveBundlerReadyPromise} from './bundler/bundler-ready-promise'

export const isProduction = process.env.BUN_ENV === 'production'
export const inputFilePath = '/input.js'
Expand Down Expand Up @@ -113,6 +114,7 @@ function handleResult(result: BuildResult) {
: contents)
})
if (!measuredEnd) {
resolveBundlerReadyPromise()
addMarker('bundler', 'end')
measuredEnd = true
}
Expand Down
7 changes: 7 additions & 0 deletions src/compiler/bundler/bundler-ready-promise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
let resolveBundlerReadyPromise
const bundlerReadyPromise = new Promise(res => resolveBundlerReadyPromise = res)

export function getBundlerReadyPromise() {
return bundlerReadyPromise
}
export {resolveBundlerReadyPromise}
1 change: 0 additions & 1 deletion src/compiler/client-script/generate-data-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export async function generateClientScriptFile() {
let inputFile = ''
const hfs = getVolume()
inputFile += getAssetsFilePaths().map(path => `import '${path}'`).join(newLine)
console.log(inputFile)
inputFile += newLine
iterateObject(getStateListenersAsCode(), ([fileName, fileContents]) => {
if (fileName === stateListenersFileName) {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/generate-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function clientTemplatesToJs(): string {
mapMapToArray(getClientTemplates(), ([componentId, template]) => {
code += wrapQuoteIfStartsWithNumber(componentId)
code += ':'
code += `'${template.replace(/'/, '\\\'')}'`
code += `'${template.replace(/(['"])/g, '\\$1')}'`
code += ','
})
code += '},'
Expand Down
4 changes: 2 additions & 2 deletions src/server/bun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ export default function cherrySoda(entry): (req: Request) => Promise<Response> {
process.env.CHERRY_COLA_ENTRY = entry
const {fs, outputPath} = compile(entry)
const render = getRenderer()
let serveStaticListener: (req: Request) => Response = serveStatic(outputPath, fs)
let serveStaticListener: (req: Request) => Promise<Response> = serveStatic(outputPath, fs)

return async (req) => {
// todo: routing; next if request should not be handled
const res: Response = serveStaticListener(req)
const res: Response = await serveStaticListener(req)
if (res.status < 400) return res
// try {
return new Response(render(), {
Expand Down
7 changes: 5 additions & 2 deletions src/server/serve-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import * as nfs from 'fs'

import mime from 'mime'

export default function serveStatic(outputPath: string, fs: { existsSync: Function, readFileSync: Function } = nfs): (req: Request) => Response {
return (req) => {
import {getBundlerReadyPromise} from '../compiler/bundler/bundler-ready-promise'

export default function serveStatic(outputPath: string, fs: { existsSync: Function, readFileSync: Function } = nfs): (req: Request) => Promise<Response> {
return async (req) => {
await getBundlerReadyPromise()
const url = new URL(req.url)
const filePath = path.join(outputPath, url.pathname)
if (url.pathname === '/' || !fs.existsSync(filePath))
Expand Down

0 comments on commit 1ff7fab

Please sign in to comment.