Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use vite.createServer with Bun.serve? #12212

Open
Blankeos opened this issue Jun 27, 2024 · 2 comments
Open

How to use vite.createServer with Bun.serve? #12212

Blankeos opened this issue Jun 27, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@Blankeos
Copy link

What is the problem this feature would solve?

Since Bun.serve doesn't have http.Incoming and http.ServerResponse, I'm wondering how to implement an equivalent of this Node code in Bun.

In Node:

import { Hono } from 'hono'
import { serve, type HttpBindings } from '@hono/node-server'
import type { ViteDevServer } from 'vite'

const base = process.env.BASE || '/'

const app = new Hono<{ Bindings: HttpBindings }>()

let vite: ViteDevServer
if (!isProduction) {
    const { createServer } = await import('vite')
    vite = await createServer({
        server: { middlewareMode: true },
        appType: 'custom',
        base
    })
    app.use(async (c, next) => {
        const viteDevMiddleware = () => new Promise<void>((resolve) => {
            vite.middlewares(c.env.incoming, c.env.outgoing, () => resolve())
        });
        await viteDevMiddleware()
        await next()
    })
}

serve({
    fetch: app.fetch,
    port
});

What is the feature you are proposing to solve the problem?

Maybe an adapter for Bun.serve with http.Incoming and http.ServerResponse if it's possible?

What alternatives have you considered?

No response

@Blankeos Blankeos added the enhancement New feature or request label Jun 27, 2024
@huseeiin
Copy link
Contributor

this is not an issue in bun. you cannot use connect middleware directly with Bun.serve. you should use express or polka or fastify with @fastify/middie

@Blankeos
Copy link
Author

Blankeos commented Aug 4, 2024

After some experimenting, this one actually works: https://github.com/vikejs/vike-node/blob/main/packages/vike-node/src/runtime/adapters/connectToWeb.ts

it's by @nitedani. Here's my current code with working Bun + Vite HMR + Custom Websocket handler + Watching of backend changes (like in controllers).

Only caveats I noticed (reproducible in the above example):

  • bun run --watch server.ts will cause an infinite loop when Vite.createServer is called (Basically same problem as this Include/exclude config for bun run --watch #5278 (comment)). So I'm using nodemon because it can exclude watching of other files).
  • In incognito HMR will break. Something about Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec.
  • HMR + Custom Websocket Handler works. BUT watching backend changes is scuffed because it will "lose connection" to the server and reconnect again. (losing frontend state)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants