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

feat(server): add shortcuts for restart server, open browser, & clear cache #3182

Closed
wants to merge 25 commits into from

Conversation

HomyeeKing
Copy link
Contributor

@HomyeeKing HomyeeKing commented Apr 28, 2021

Description

make devServer restartable , convenient to debug

Additional context


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@Shinigami92 Shinigami92 added the p2-nice-to-have Not breaking anything but nice to have (priority) label Apr 28, 2021
packages/vite/src/node/cli.ts Outdated Show resolved Hide resolved
@aleclarson
Copy link
Member

@Shinigami92 @antfu How's this for the output printed on startup?


  vite v2.3.2 dev server running at:

  > Local: http://localhost:3000/
  > Network: use `--host` to expose

  ready in 593ms.

  Available commands:
    Press "r" to restart the dev server.
    Press "f" to rebuild the optimized dependency cache.
    Press "o" to open the browser.

@Shinigami92
Copy link
Member

@Shinigami92 @antfu How's this for the output printed on startup?


  vite v2.3.2 dev server running at:

  > Local: http://localhost:3000/
  > Network: use `--host` to expose

  ready in 593ms.

  Available commands:
    Press "r" to restart the dev server.
    Press "f" to rebuild the optimized dependency cache.
    Press "o" to open the browser.

@aleclarson Your vite is outdated and slow 🤣
But yes, looking good

@HomyeeKing
Copy link
Contributor Author

HomyeeKing commented May 28, 2021

another question:

  • hide input console ?
  • auto linebreak ?
  • using enum to express key binding?

@antfu
Copy link
Member

antfu commented May 29, 2021

About the CLI outputs, I am thinking something like

  vite v2.3.2 dev server running at:

  > Local: http://localhost:3000/
  > Network: use `--host` to expose
  > Shortcuts: "r" restart, "o" open browser, "f" force restart

  ready in 593ms.

packages/vite/src/node/cli.ts Outdated Show resolved Hide resolved
Shinigami92
Shinigami92 previously approved these changes May 30, 2021
@HomyeeKing HomyeeKing marked this pull request as ready for review May 30, 2021 10:38
@@ -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`)
Copy link
Member

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?

Copy link
Member

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.

packages/vite/src/node/cli.ts Outdated Show resolved Hide resolved
packages/vite/src/node/server/index.ts Outdated Show resolved Hide resolved
@Shinigami92
Copy link
Member

Seems after build the package.json couldn't be found/resolved at runtime anymore 🤔
When watching into the changes after last green tick, I assume it has something to do with function scope or so
Hope this helps to find the error

@HomyeeKing

This comment has been minimized.

@dominikg
Copy link
Contributor

dominikg commented Jun 25, 2021

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.

@patak-dev
Copy link
Member

patak-dev commented Jul 4, 2021

@HomyeeKing could you resolve the conflicts? Tests will be re-run and you can check then if the failure was a glitch.

@HomyeeKing

This comment has been minimized.

@HomyeeKing HomyeeKing marked this pull request as draft July 26, 2021 06:07
@HomyeeKing HomyeeKing marked this pull request as ready for review July 26, 2021 06:38
@HomyeeKing
Copy link
Contributor Author

HomyeeKing commented Jul 26, 2021

@tjk sorry for not understand you until now, because I never use in middlewareMode and shortcuts seem usefule in this case

btw, looks like the config never used on middlewareMode, like printUrl, open in browser something
we can do it all by shortcuts now I guess

@dominikg
Copy link
Contributor

dominikg commented Jul 26, 2021

Hmm, in middleware mode the process is not created by vite cli and you cannot assume that it is safe to hijack the stream.
This would be a breaking change if it defaulted to true, so the default should be false.

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.
cc @brillout, @frandiox, @GrygrFlzr , what do you think?

In addition i think it could be useful to expose restart on the devserver instance so plugins can trigger it in certain situations.
Currently this is possible in a rather hacky way:
see @antfu 's vite-plugin-restart or how it is done in vite-plugin-svelte

@antfu
Copy link
Member

antfu commented Jul 26, 2021

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.

In addition i think it could be useful to expose restart on the devserver instance so plugins can trigger it in certain situations.

I like this idea.

@brillout
Copy link
Contributor

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?

@frandiox
Copy link
Contributor

I'm not using middleware mode for anything yet so I don't have a strong opinion.
Perhaps instead of having an actual option with docs etc, it would be enough to make sure the bindShortcuts function is exported publicly so any developer can use it optionally? bindShortcuts(myServer) or anything like that.

@HomyeeKing HomyeeKing marked this pull request as draft July 26, 2021 12:13
@HomyeeKing HomyeeKing marked this pull request as ready for review July 26, 2021 13:41
@aleclarson
Copy link
Member

aleclarson commented Jul 26, 2021

Actually, in general, why would the user want to restart the dev server? Doesn't HMR handle all code update scenarios?

@brillout It's true that vite.config.js changes already restart the server without this PR. I think manual restart is meant for plugin developers, though @HomyeeKing might have another use case in mind as well.

But honestly, it's probably better for plugin developers to use an external file-watching process that automatically reruns vite command when any file in the plugin directory changes. So there may not be a solid enough use case to warrant a "restart server" shortcut, unless I'm missing something.

In addition i think it could be useful to expose restart on the devserver instance so plugins can trigger it in certain situations.

This idea could probably have its own PR, but I approve

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.

@antfu I agree that middleware mode should not have shortcuts bound. Nonetheless, the server.bindShortcuts option is still necessary if we keep the bindShortcuts call within server.listen (which I think is wise, because that's where the logging needs to happen, and we don't want to log the shortcuts if they're not bound).

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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • removed toString call, since setEncoding ensures data 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
Copy link
Member

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.

@Shinigami92
Copy link
Member

@aleclarson @HomyeeKing What's the state of this PR?
Seems there are some issues in the tests and it needs a rebase?

@aleclarson Did you take this branch over or just help @HomyeeKing?

@HomyeeKing
Copy link
Contributor Author

@Shinigami92 I will be in this week, kind of busy recently😓

@brillout
Copy link
Contributor

brillout commented Aug 2, 2021

@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

@HomyeeKing
Copy link
Contributor Author

@brillout as the PR description said, I submit this PR for convenient debugging,
I don't know how you guys debug, for me , I have to rerun vite dev in my project to watch the newest change with the vite in yarn dev

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 DEBUG info

so @aleclarson you can assess the trading off to decide this pr to go or not

@brillout
Copy link
Contributor

brillout commented Aug 3, 2021

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',
Copy link
Member

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)
Copy link
Member

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.

Copy link
Contributor Author

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

@aleclarson
Copy link
Member

Closing in favor of #6014

@aleclarson aleclarson closed this Dec 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants