Skip to content

Commit

Permalink
fix(types): correct import of types (#4003)
Browse files Browse the repository at this point in the history
  • Loading branch information
GrygrFlzr committed Jun 28, 2021
1 parent d97b33a commit 4954636
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 75 deletions.
71 changes: 1 addition & 70 deletions packages/vite/client.d.ts
Original file line number Diff line number Diff line change
@@ -1,74 +1,5 @@
/// <reference lib="dom" />

import {
ErrorPayload,
FullReloadPayload,
PrunePayload,
UpdatePayload
} from 'types/hmrPayload'

interface ImportMeta {
url: string

readonly hot?: {
readonly data: any

accept(): void
accept(cb: (mod: any) => void): void
accept(dep: string, cb: (mod: any) => void): void
accept(deps: readonly string[], cb: (mods: any[]) => void): void

/**
* @deprecated
*/
acceptDeps(): never

dispose(cb: (data: any) => void): void
decline(): void
invalidate(): void

on: {
(event: 'vite:beforeUpdate', cb: (payload: UpdatePayload) => void): void
(event: 'vite:beforePrune', cb: (payload: PrunePayload) => void): void
(
event: 'vite:beforeFullReload',
cb: (payload: FullReloadPayload) => void
): void
(event: 'vite:error', cb: (payload: ErrorPayload) => void): void
<T extends string>(
event: CustomEventName<T>,
cb: (data: any) => void
): void
}
}

readonly env: ImportMetaEnv

glob(pattern: string): Record<
string,
() => Promise<{
[key: string]: any
}>
>

globEager(pattern: string): Record<
string,
{
[key: string]: any
}
>
}
interface ImportMetaEnv {
[key: string]: string | boolean | undefined
BASE_URL: string
MODE: string
DEV: boolean
PROD: boolean
}

// See https://stackoverflow.com/a/63549561.
type CustomEventName<T extends string> = (T extends `vite:${T}` ? never : T) &
(`vite:${T}` extends T ? never : T)
/// <reference types="vite/types/importMeta" />

// CSS modules
type CSSModuleClasses = { readonly [key: string]: string }
Expand Down
1 change: 1 addition & 0 deletions packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"dist",
"client.d.ts",
"src",
"types",
"!/src/**/__tests__/"
],
"engines": {
Expand Down
5 changes: 1 addition & 4 deletions packages/vite/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Update,
UpdatePayload
} from 'types/hmrPayload'
import { CustomEventName } from 'types/customEvent'
import { ErrorOverlay, overlayId } from './overlay'
import './env'

Expand Down Expand Up @@ -167,10 +168,6 @@ function notifyListeners(event: string, data: any): void {
}
}

// See https://stackoverflow.com/a/63549561.
type CustomEventName<T extends string> = (T extends `vite:${T}` ? never : T) &
(`vite:${T}` extends T ? never : T)

const enableOverlay = __HMR_ENABLE_OVERLAY__

function createErrorOverlay(err: ErrorPayload['err']) {
Expand Down
5 changes: 5 additions & 0 deletions packages/vite/types/customEvent.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// See https://stackoverflow.com/a/63549561.
export type CustomEventName<T extends string> = (T extends `vite:${T}`
? never
: T) &
(`vite:${T}` extends T ? never : T)
37 changes: 36 additions & 1 deletion packages/vite/types/importMeta.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,45 @@ interface ImportMeta {
decline(): void
invalidate(): void

on(event: string, cb: (...args: any[]) => void): void
on: {
(
event: 'vite:beforeUpdate',
cb: (payload: import('./hmrPayload').UpdatePayload) => void
): void
(
event: 'vite:beforePrune',
cb: (payload: import('./hmrPayload').PrunePayload) => void
): void
(
event: 'vite:beforeFullReload',
cb: (payload: import('./hmrPayload').FullReloadPayload) => void
): void
(
event: 'vite:error',
cb: (payload: import('./hmrPayload').ErrorPayload) => void
): void
<T extends string>(
event: import('./customEvent').CustomEventName<T>,
cb: (data: any) => void
): void
}
}

readonly env: ImportMetaEnv

glob(pattern: string): Record<
string,
() => Promise<{
[key: string]: any
}>
>

globEager(pattern: string): Record<
string,
{
[key: string]: any
}
>
}

interface ImportMetaEnv {
Expand Down

0 comments on commit 4954636

Please sign in to comment.