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

Dev server should not start when plugin config server to be middlewareMode #2094

Closed
axe-me opened this issue Feb 19, 2021 · 4 comments
Closed

Comments

@axe-me
Copy link
Contributor

axe-me commented Feb 19, 2021

Describe the bug

When a plugin has the dev server config middlewareMode to be true, the CLI should not try to start the dev server. In my case, I would use my own server instead of the vite built-in one.

config: () => ({
  server: {
      middlewareMode: true,
  },
}),

Reproduction

https://github.com/axe-me/dev-server-shouldnt-start

System Info

  • vite version: 2.x
  • Operating System: macOS
  • Node version: 14
  • Package manager (npm/yarn/pnpm) and version: npm 6.x

Logs (Optional if provided reproduction)

vite

error when starting dev server:
Error: Cannot call server.listen in middleware mode.

Solution

dont run await server.listen() when resolved config has middlewareMode enabled

@axe-me axe-me changed the title Dev server should not start when plugin config server to be Dev server should not start when plugin config server to be middlewareMode Feb 19, 2021
underfin added a commit to rolldown/vite that referenced this issue Feb 19, 2021
@axe-me
Copy link
Contributor Author

axe-me commented Feb 19, 2021

@underfin Thank you for looking at this. I created a PR as well yesterday, for some reason it did not link to this issue. #2095
Anyway the way you doing it looks better. I gonna close mine.

@yyx990803
Copy link
Member

I don't know if this should be a supported case, since this is allowing a plugin to replace the actual Vite http server and breaks the fix in #2101 breaks the API contract where listen() should aways return the server instance. Starting a server in configureServer also feels quite wrong.

I think a better, more explicit usage is to expose a CLI binary from your plugin which starts the server and uses Vite in middleware instead. So instead of vite, the user runs vite-express instead.

@axe-me
Copy link
Contributor Author

axe-me commented Feb 19, 2021

A CLI bin makes sense, that actually was my first attempt. The reason I switch to the plugin approach is the ViteDevServer not expose the handleHotUpdate hook which is needed for me to know when to restart the node server. #2077 (comment)

I could create a PR to add some lifecycle hooks options to the dev server if you think it's OK.

Another idea is to add a handler to dev server and invoke it in hmr.send, so that the node server would know all the events the hmr sending out. I can create a PR for this as well, see which way you prefer.

@yyx990803

@axe-me
Copy link
Contributor Author

axe-me commented Feb 21, 2021

I just realized I could just connect to the WebSocket server created by vite. None of those proposals above aren't needed anymore.

@github-actions github-actions bot locked and limited conversation to collaborators Jul 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants