Skip to content

Commit

Permalink
feat: add shortcut for toggling HMR updates
Browse files Browse the repository at this point in the history
Press "h" to toggle HMR updates on/off.

Useful when you hit a bug and don't want your code edits to refresh the browser (which would mean losing the error logs and app state).
  • Loading branch information
aleclarson committed Dec 8, 2021
1 parent 3e32758 commit 7a926c4
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/vite/src/node/server/shortcuts.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import chalk from 'chalk'
import type { ViteDevServer } from '..'
import { openBrowser, resolveBrowserUrl } from './openBrowser'

Expand Down Expand Up @@ -58,5 +59,22 @@ export const SHORTCUTS: Shortcut[] = [
action(server: ViteDevServer): void {
server.restart(true)
}
},
{
key: 'h',
name: 'toggle hmr',
action({ config }: ViteDevServer): void {
/**
* Mutating the server config works because Vite reads from
* it on every file change, instead of caching its value.
*
* Since `undefined` is treated as `true`, we have to
* use `!== true` to flip the boolean value.
*/
config.server.hmr = config.server.hmr !== true
config.logger.info(
chalk.cyanBright(`hmr ${config.server.hmr ? `enabled` : `disabled`}`)
)
}
}
]

0 comments on commit 7a926c4

Please sign in to comment.