-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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: add off method to ViteHotContext (issue #14185) #14518
Conversation
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.
The implementation looks great. Can we add a test for it around
Lines 107 to 115 in 56c5f4f
import.meta.hot.on('custom:foo', ({ msg }) => { | |
text('.custom', msg) | |
}) | |
// send custom event to server to calculate 1 + 2 | |
import.meta.hot.send('custom:remote-add', { a: 1, b: 2 }) | |
import.meta.hot.on('custom:remote-add-result', ({ result }) => { | |
text('.custom-communication', result) | |
}) |
vite/playground/hmr/__tests__/hmr.spec.ts
Lines 174 to 183 in 56c5f4f
test('plugin hmr handler + custom event', async () => { | |
const el = await page.$('.custom') | |
editFile('customFile.js', (code) => code.replace('custom', 'edited')) | |
await untilUpdated(() => el.textContent(), 'edited') | |
}) | |
test('plugin client-server communication', async () => { | |
const el = await page.$('.custom-communication') | |
await untilUpdated(() => el.textContent(), '3') | |
}) |
(new test cases, but those are existing examples)
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.
Looks great!
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.
Would you add off
to the document?
Lines 35 to 39 in 6868480
// `InferCustomEventPayload` provides types for built-in Vite events | |
on<T extends string>( | |
event: T, | |
cb: (payload: InferCustomEventPayload<T>) => void, | |
): void |
Lines 167 to 183 in 6868480
## `hot.on(event, cb)` | |
Listen to an HMR event. | |
The following HMR events are dispatched by Vite automatically: | |
- `'vite:beforeUpdate'` when an update is about to be applied (e.g. a module will be replaced) | |
- `'vite:afterUpdate'` when an update has just been applied (e.g. a module has been 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:invalidate'` when a module is invalidated with `import.meta.hot.invalidate()` | |
- `'vite:error'` when an error occurs (e.g. syntax error) | |
- `'vite:ws:disconnect'` when the WebSocket connection is lost | |
- `'vite:ws:connect'` when the WebSocket connection is (re-)established | |
Custom HMR events can also be sent from plugins. See [handleHotUpdate](./api-plugin#handlehotupdate) for more details. | |
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.
Thanks!
Description
This pr adds the ability to remove listeners added using import.meta.hot.on() as
requested in issue #14185
fix #14185
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).