Skip to content

Commit

Permalink
fix(types): explicitly set Vite hooks' this to void (#9885)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored Aug 29, 2022
1 parent 6be971e commit 2d2f2e5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
11 changes: 9 additions & 2 deletions packages/vite/src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ export interface Plugin extends RollupPlugin {
/**
* Apply the plugin only for serve or build, or on certain conditions.
*/
apply?: 'serve' | 'build' | ((config: UserConfig, env: ConfigEnv) => boolean)
apply?:
| 'serve'
| 'build'
| ((this: void, config: UserConfig, env: ConfigEnv) => boolean)
/**
* Modify vite config before it's resolved. The hook can either mutate the
* passed-in config directly, or return a partial config object that will be
Expand All @@ -66,14 +69,17 @@ export interface Plugin extends RollupPlugin {
*/
config?: ObjectHook<
(
this: void,
config: UserConfig,
env: ConfigEnv
) => UserConfig | null | void | Promise<UserConfig | null | void>
>
/**
* Use this hook to read and store the final resolved vite config.
*/
configResolved?: ObjectHook<(config: ResolvedConfig) => void | Promise<void>>
configResolved?: ObjectHook<
(this: void, config: ResolvedConfig) => void | Promise<void>
>
/**
* Configure the vite server. The hook receives the {@link ViteDevServer}
* instance. This can also be used to store a reference to the server
Expand Down Expand Up @@ -126,6 +132,7 @@ export interface Plugin extends RollupPlugin {
*/
handleHotUpdate?: ObjectHook<
(
this: void,
ctx: HmrContext
) => Array<ModuleNode> | void | Promise<Array<ModuleNode> | void>
>
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,7 @@ export interface IndexHtmlTransformContext {
}

export type IndexHtmlTransformHook = (
this: void,
html: string,
ctx: IndexHtmlTransformContext
) => IndexHtmlTransformResult | void | Promise<IndexHtmlTransformResult | void>
Expand Down
11 changes: 7 additions & 4 deletions packages/vite/src/node/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,13 @@ export interface PreviewServer {
printUrls(): void
}

export type PreviewServerHook = (server: {
middlewares: Connect.Server
httpServer: http.Server
}) => (() => void) | void | Promise<(() => void) | void>
export type PreviewServerHook = (
this: void,
server: {
middlewares: Connect.Server
httpServer: http.Server
}
) => (() => void) | void | Promise<(() => void) | void>

/**
* Starts the Vite server in preview mode, to simulate a production deployment
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export interface FileSystemServeOptions {
}

export type ServerHook = (
this: void,
server: ViteDevServer
) => (() => void) | void | Promise<(() => void) | void>

Expand Down

0 comments on commit 2d2f2e5

Please sign in to comment.