-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
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
feat(server): add shortcuts for restart server, open browser, & clear cache #3182
Conversation
@Shinigami92 @antfu How's this for the output printed on startup?
|
@aleclarson Your vite is outdated and slow 🤣 |
another question:
|
About the CLI outputs, I am thinking something like
|
@@ -615,6 +615,8 @@ async function startServer( | |||
) | |||
|
|||
printServerUrls(hostname, protocol, port, base, info) | |||
// print shortcuts info | |||
info(` > Shortcuts: "r" restart, "o" open browser, "f" force restart`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@antfu "force restart" seems too vague? Should we explain it in the docs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, think we should document it, as we could possibly have more shortcuts in the future but not necessary to be print out here.
Seems after build the |
This comment has been minimized.
This comment has been minimized.
Should this handle gracefully closing the old server instance and websocket and reconnecting to connected clients? The clients may also need a full reload after the server was restarted as there is no guarantee that the loaded modules still match the state on the new server instance. |
@HomyeeKing could you resolve the conflicts? Tests will be re-run and you can check then if the failure was a glitch. |
This comment has been minimized.
This comment has been minimized.
@tjk sorry for not understand you until now, because I never use in btw, looks like the config never used on |
Hmm, in middleware mode the process is not created by vite cli and you cannot assume that it is safe to hijack the stream. but i'm not convinced this is even useful in middleware mode as most of the time the external process would need to handle restart and clearcache additionally to avoid inconsistent state. In addition i think it could be useful to expose restart on the devserver instance so plugins can trigger it in certain situations. |
As we want to keep our configure options minimal as possible, instead of having an option, I would think it might be better to just disable in middleware mode automatically without configurations.
I like this idea. |
I agree with @dominikg: AFAICT I don't see a use case for this for middleware mode. Actually, in general, why would the user want to restart the dev server? Doesn't HMR handle all code update scenarios? |
I'm not using middleware mode for anything yet so I don't have a strong opinion. |
@brillout It's true that But honestly, it's probably better for plugin developers to use an external file-watching process that automatically reruns
This idea could probably have its own PR, but I approve
@antfu I agree that middleware mode should not have shortcuts bound. Nonetheless, the |
Since we only bind shortcuts in the `server.listen` method, we can safely listen for the httpServer "close" event to unbind them. This avoids a memory leak.
stdin.setEncoding('utf8') | ||
stdin.setRawMode(true) | ||
stdin.on('data', (data) => { | ||
const input = data.toString().trim().toLowerCase() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- removed
toString
call, sincesetEncoding
ensuresdata
is a string - removed
trim
call, since whitespace is not a concern - removed
toLowerCase
call, since we may want to use the uppercase shortcuts in the future
@@ -2,30 +2,33 @@ import type { ViteDevServer } from '..' | |||
import { openBrowser, resolveBrowserUrl } from './openBrowser' | |||
import { restartServer } from './hmr' | |||
|
|||
export function bindShortcuts(server: ViteDevServer, isRestart = false): void { | |||
export function bindShortcuts(server: ViteDevServer): void { | |||
if (!server.httpServer) return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we use httpServer.on('close', ...)
to unbind the shortcuts, we can't bind the shortcuts without it. This means middleware mode is off limits.
@aleclarson @HomyeeKing What's the state of this PR? @aleclarson Did you take this branch over or just help @HomyeeKing? |
@Shinigami92 I will be in this week, kind of busy recently😓 |
@HomyeeKing Actually, in general, why would the user want to restart the dev server? Doesn't HMR handle all code update scenarios? Maybe an alternative would be to implement your reload shortcut feature as a plugin? Especially if we decide to expose a function to reload the dev server as suggested by @frandiox and @dominikg |
@brillout as the PR description said, I submit this PR for convenient debugging, but this PR seems not solve this problem either, I just found that it just restart the server instance, but the code still not change, such as so @aleclarson you can assess the trading off to decide this pr to go or not |
I suggest to fix the root problem instead of adding restart shortcuts then. AFAICT if HMR works as intended then there is no need for restart shortcuts. |
}, | ||
{ | ||
key: 'f', | ||
name: 'force restart', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think "force restart" is too vague. Should be "reoptimize deps" instead, perhaps?
/cc @antfu @Shinigami92 @patak-js
key: 'r', | ||
name: 'restart', | ||
action(server: ViteDevServer): void { | ||
restartServer(server) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's remove the restart
shortcut for now, since the use case is unclear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agree, I misunderstand the restartServer
function before, I wanted to achieve a way that restart current project with the new Vite
bundle output for debugging
Closing in favor of #6014 |
Description
make devServer restartable , convenient to debug
Additional context
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).