Skip to content

Commit

Permalink
fix: add before prefix for each event
Browse files Browse the repository at this point in the history
  • Loading branch information
fwouts committed Jun 17, 2021
1 parent 112b7af commit 64c7aff
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 20 deletions.
6 changes: 3 additions & 3 deletions docs/guide/api-hmr.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ For now, calling `import.meta.hot.invalidate()` simply reloads the page.
Listen to an HMR event.
The following HMR events are dispatched by Vite automatically:
- `'vite:update'` when an update is received (e.g. a module is replaced)
- `'vite:full-reload'` when a full reload is about to occur
- `'vite:prune'` when modules that are no longer needed are pruned
- `'vite:beforeUpdate'` when an update is about to be applied (e.g. a module will be replaced)
- `'vite:beforeFullReload'` when a full reload is about to occur
- `'vite:beforePrune'` when modules that are no longer needed are about to be pruned
- `'vite:error'` when an error occurs (e.g. syntax error)
Custom HMR events can also be sent from plugins. See [handleHotUpdate](./api-plugin#handlehotupdate) for more details.
Expand Down
12 changes: 6 additions & 6 deletions packages/playground/hmr/__tests__/hmr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ if (!isBuild) {
await untilUpdated(() => el.textContent(), '2')

expect(browserLogs).toMatchObject([
'>>> vite:update -- update',
'>>> vite:beforeUpdate -- update',
'foo was: 1',
'(self-accepting 1) foo is now: 2',
'(self-accepting 2) foo is now: 2',
Expand All @@ -32,7 +32,7 @@ if (!isBuild) {
await untilUpdated(() => el.textContent(), '3')

expect(browserLogs).toMatchObject([
'>>> vite:update -- update',
'>>> vite:beforeUpdate -- update',
'foo was: 2',
'(self-accepting 1) foo is now: 3',
'(self-accepting 2) foo is now: 3',
Expand All @@ -50,7 +50,7 @@ if (!isBuild) {
await untilUpdated(() => el.textContent(), '2')

expect(browserLogs).toMatchObject([
'>>> vite:update -- update',
'>>> vite:beforeUpdate -- update',
'(dep) foo was: 1',
'(dep) foo from dispose: 1',
'(single dep) foo is now: 2',
Expand All @@ -67,7 +67,7 @@ if (!isBuild) {
await untilUpdated(() => el.textContent(), '3')

expect(browserLogs).toMatchObject([
'>>> vite:update -- update',
'>>> vite:beforeUpdate -- update',
'(dep) foo was: 2',
'(dep) foo from dispose: 2',
'(single dep) foo is now: 3',
Expand All @@ -88,7 +88,7 @@ if (!isBuild) {
await untilUpdated(() => el.textContent(), '2')

expect(browserLogs).toMatchObject([
'>>> vite:update -- update',
'>>> vite:beforeUpdate -- update',
'(dep) foo was: 3',
'(dep) foo from dispose: 3',
'(single dep) foo is now: 3',
Expand All @@ -105,7 +105,7 @@ if (!isBuild) {
await untilUpdated(() => el.textContent(), '3')

expect(browserLogs).toMatchObject([
'>>> vite:update -- update',
'>>> vite:beforeUpdate -- update',
'(dep) foo was: 3',
'(dep) foo from dispose: 3',
'(single dep) foo is now: 3',
Expand Down
4 changes: 2 additions & 2 deletions packages/playground/hmr/hmr.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ if (import.meta.hot) {
console.log(`foo was:`, foo)
})

import.meta.hot.on('vite:update', (event) => {
console.log(`>>> vite:update -- ${event.type}`)
import.meta.hot.on('vite:beforeUpdate', (event) => {
console.log(`>>> vite:beforeUpdate -- ${event.type}`)
})

import.meta.hot.on('vite:error', (event) => {
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ interface ImportMeta {
invalidate(): void

on: {
(event: 'vite:update', cb: (payload: UpdatePayload) => void): void
(event: 'vite:prune', cb: (payload: PrunePayload) => void): void
(event: 'vite:beforeUpdate', cb: (payload: UpdatePayload) => void): void
(event: 'vite:beforePrune', cb: (payload: PrunePayload) => void): void
(
event: 'vite:full-reload',
event: 'vite:beforeFullReload',
cb: (payload: FullReloadPayload) => void
): void
(event: 'vite:error', cb: (payload: ErrorPayload) => void): void
Expand Down
15 changes: 9 additions & 6 deletions packages/vite/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async function handleMessage(payload: HMRPayload) {
setInterval(() => socket.send('ping'), __HMR_TIMEOUT__)
break
case 'update':
notifyListeners('vite:update', payload)
notifyListeners('vite:beforeUpdate', payload)
// if this is the first update and there's already an error overlay, it
// means the page opened with existing server compile error and the whole
// module script failed to load (since one of the nested imports is 500).
Expand Down Expand Up @@ -98,7 +98,7 @@ async function handleMessage(payload: HMRPayload) {
break
}
case 'full-reload':
notifyListeners('vite:full-reload', payload)
notifyListeners('vite:beforeFullReload', payload)
if (payload.path && payload.path.endsWith('.html')) {
// if html file is edited, only reload the page if the browser is
// currently on that page.
Expand All @@ -116,7 +116,7 @@ async function handleMessage(payload: HMRPayload) {
}
break
case 'prune':
notifyListeners('vite:prune', payload)
notifyListeners('vite:beforePrune', payload)
// After an HMR update, some modules are no longer imported on the page
// but they may have left behind side effects that need to be cleaned up
// (.e.g style injections)
Expand Down Expand Up @@ -145,10 +145,13 @@ async function handleMessage(payload: HMRPayload) {
}
}

function notifyListeners(event: 'vite:update', payload: UpdatePayload): void
function notifyListeners(event: 'vite:prune', payload: PrunePayload): void
function notifyListeners(
event: 'vite:full-reload',
event: 'vite:beforeUpdate',
payload: UpdatePayload
): void
function notifyListeners(event: 'vite:beforePrune', payload: PrunePayload): void
function notifyListeners(
event: 'vite:beforeFullReload',
payload: FullReloadPayload
): void
function notifyListeners(event: 'vite:error', payload: ErrorPayload): void
Expand Down

0 comments on commit 64c7aff

Please sign in to comment.