Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(type): support typing for custom events (#7476)
- Loading branch information
Showing
12 changed files
with
96 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import 'vite/types/customEvent' | ||
|
||
declare module 'vite/types/customEvent' { | ||
interface CustomEventMap { | ||
'custom:foo': { msg: string } | ||
'custom:remote-add': { a: number; b: number } | ||
'custom:remote-add-result': { result: string } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"include": ["."], | ||
"exclude": ["**/dist/**"], | ||
"compilerOptions": { | ||
"target": "es2019", | ||
"module": "esnext", | ||
"outDir": "dist", | ||
"allowJs": true, | ||
"esModuleInterop": true, | ||
"moduleResolution": "node", | ||
"baseUrl": ".", | ||
"jsx": "preserve", | ||
"types": ["vite/client", "jest", "node"] | ||
} | ||
} |
15 changes: 7 additions & 8 deletions
15
packages/playground/hmr/vite.config.js → packages/playground/hmr/vite.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,21 @@ | ||
/** | ||
* @type {import('vite').UserConfig} | ||
*/ | ||
module.exports = { | ||
import { defineConfig } from 'vite' | ||
|
||
export default defineConfig({ | ||
plugins: [ | ||
{ | ||
name: 'mock-custom', | ||
async handleHotUpdate({ file, read, server }) { | ||
if (file.endsWith('customFile.js')) { | ||
const content = await read() | ||
const msg = content.match(/export const msg = '(\w+)'/)[1] | ||
server.ws.send('foo', { msg }) | ||
server.ws.send('custom:foo', { msg }) | ||
} | ||
}, | ||
configureServer(server) { | ||
server.ws.on('remote-add', ({ a, b }, client) => { | ||
client.send('remote-add-result', { result: a + b }) | ||
server.ws.on('custom:remote-add', ({ a, b }, client) => { | ||
client.send('custom:remote-add-result', { result: a + b }) | ||
}) | ||
} | ||
} | ||
] | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,16 @@ | ||
// See https://stackoverflow.com/a/63549561. | ||
export type CustomEventName<T extends string> = (T extends `vite:${T}` | ||
? never | ||
: T) & | ||
(`vite:${T}` extends T ? never : T) | ||
import type { | ||
ErrorPayload, | ||
FullReloadPayload, | ||
PrunePayload, | ||
UpdatePayload | ||
} from './hmrPayload' | ||
|
||
export interface CustomEventMap { | ||
'vite:beforeUpdate': UpdatePayload | ||
'vite:beforePrune': PrunePayload | ||
'vite:beforeFullReload': FullReloadPayload | ||
'vite:error': ErrorPayload | ||
} | ||
|
||
export type InferCustomEventPayload<T extends string> = | ||
T extends keyof CustomEventMap ? CustomEventMap[T] : unknown |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters