Skip to content

Commit 291174d

Browse files
bluwysapphi-red
andauthoredSep 30, 2022
refactor(types): simplify type exports (#10243)
Co-authored-by: 翠 / green <green@sapphi.red>
1 parent 931d69b commit 291174d

40 files changed

+386
-510
lines changed
 

‎.eslintignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
dist
22
playground-temp
33
temp
4-
packages/vite/client/types.d.ts

‎.eslintrc.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ module.exports = defineConfig({
131131
}
132132
},
133133
{
134-
files: ['packages/vite/src/dep-types/**', '*.spec.ts'],
134+
files: ['packages/vite/src/types/**', '*.spec.ts'],
135135
rules: {
136136
'node/no-extraneous-import': 'off'
137137
}

‎.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*.local
66
*.log
77
/.vscode/
8-
/packages/vite/client/types.d.ts
98
/packages/vite/LICENSE
109
dist
1110
dist-ssr

‎.prettierignore

-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,3 @@ playground/tsconfig-json-load-error/has-error/tsconfig.json
99
playground/html/invalid.html
1010
playground/html/valid.html
1111
playground/worker/classic-worker.js
12-
packages/vite/client/types.d.ts

‎CONTRIBUTING.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,12 @@ Avoid deps with large transitive dependencies that result in bloated size compar
218218

219219
Vite aims to be fully usable as a dependency in a TypeScript project (e.g. it should provide proper typings for VitePress), and also in `vite.config.ts`. This means technically a dependency whose types are exposed needs to be part of `dependencies` instead of `devDependencies`. However, this also means we won't be able to bundle it.
220220

221-
To get around this, we inline some of these dependencies' types in `packages/vite/src/dep-types`. This way, we can still expose the typing but bundle the dependency's source code.
221+
To get around this, we inline some of these dependencies' types in `packages/vite/src/types`. This way, we can still expose the typing but bundle the dependency's source code.
222222

223223
Use `pnpm run check-dist-types` to check that the bundled types do not rely on types in `devDependencies`. If you are adding `dependencies`, make sure to configure `tsconfig.check.json`.
224224

225+
For types shared between client and node, they should be added into `packages/vite/types`. These types are not bundled and are published as is (though they are still considered internal). Dependency types within this directory (e.g. `packages/vite/types/chokidar.d.ts`) are deprecated and should be added to `packages/vite/src/types` instead.
226+
225227
### Think Before Adding Yet Another Option
226228

227229
We already have many config options, and we should avoid fixing an issue by adding yet another one. Before adding an option, consider whether the problem:

‎docs/guide/api-plugin.md

+6-15
Original file line numberDiff line numberDiff line change
@@ -595,21 +595,12 @@ It is possible to type custom events by extending the `CustomEventMap` interface
595595

596596
```ts
597597
// events.d.ts
598-
import 'vite'
599-
import 'vite/client/types'
598+
import 'vite/types/customEvent'
600599
601-
interface MyCustomEventMap {
602-
'custom:foo': { msg: string }
603-
// 'event-key': payload
604-
}
605-
606-
// extend interface for server-side
607-
declare module 'vite' {
608-
interface CustomEventMap extends MyCustomEventMap {}
609-
}
610-
611-
// extend interface for client-side
612-
declare module 'vite/client/types' {
613-
interface CustomEventMap extends MyCustomEventMap {}
600+
declare module 'vite/types/customEvent' {
601+
interface CustomEventMap {
602+
'custom:foo': { msg: string }
603+
// 'event-key': payload
604+
}
614605
}
615606
```

‎packages/vite/api-extractor.client.json

-54
This file was deleted.

‎packages/vite/client.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// <reference path="./import-meta.d.ts" />
1+
/// <reference path="./types/importMeta.d.ts" />
22

33
// CSS modules
44
type CSSModuleClasses = { readonly [key: string]: string }

‎packages/vite/import-meta.d.ts

-10
This file was deleted.

‎packages/vite/package.json

+9-18
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,16 @@
2020
"./client": {
2121
"types": "./client.d.ts"
2222
},
23-
"./import-meta": {
24-
"types": "./import-meta.d.ts"
25-
},
26-
"./client/types": {
27-
"types": "./client/types.d.ts"
28-
},
2923
"./dist/client/*": "./dist/client/*",
3024
"./package.json": "./package.json"
3125
},
3226
"files": [
3327
"bin",
3428
"dist",
3529
"client.d.ts",
36-
"import-meta.d.ts",
3730
"index.cjs",
3831
"src/client",
39-
"types",
40-
"client/types.d.ts"
32+
"types"
4133
],
4234
"engines": {
4335
"node": "^14.18.0 || >=16.0.0"
@@ -55,13 +47,12 @@
5547
"dev": "rimraf dist && pnpm run build-bundle -w",
5648
"build": "rimraf dist && run-s build-bundle build-types",
5749
"build-bundle": "rollup --config rollup.config.ts --configPlugin typescript",
58-
"build-types": "run-p build-node-types build-client-types",
59-
"build-node-types": "run-s build-node-types-temp build-node-types-patch build-node-types-roll build-node-types-check",
60-
"build-node-types-temp": "tsc --emitDeclarationOnly --outDir temp/node -p src/node",
61-
"build-node-types-patch": "tsx scripts/patchTypes.ts",
62-
"build-node-types-roll": "api-extractor run && rimraf temp",
63-
"build-node-types-check": "tsc --project tsconfig.check.json",
64-
"build-client-types": "api-extractor run -c api-extractor.client.json",
50+
"build-types": "run-s build-types-temp build-types-pre-patch build-types-roll build-types-post-patch build-types-check",
51+
"build-types-temp": "tsc --emitDeclarationOnly --outDir temp/node -p src/node",
52+
"build-types-pre-patch": "tsx scripts/prePatchTypes.ts",
53+
"build-types-roll": "api-extractor run && rimraf temp",
54+
"build-types-post-patch": "tsx scripts/postPatchTypes.ts",
55+
"build-types-check": "tsc --project tsconfig.check.json",
6556
"lint": "eslint --cache --ext .ts src/**",
6657
"format": "prettier --write --cache --parser typescript \"src/**/*.ts\"",
6758
"prepublishOnly": "npm run build"
@@ -127,8 +118,8 @@
127118
"strip-literal": "^0.4.2",
128119
"tsconfck": "^2.0.1",
129120
"tslib": "^2.4.0",
130-
"dep-types": "link:./src/dep-types",
131-
"types": "link:./src/types",
121+
"dep-types": "link:./src/types",
122+
"types": "link:./types",
132123
"ufo": "^0.8.5",
133124
"ws": "^8.9.0"
134125
},

‎packages/vite/scripts/patchTypes.ts

-83
This file was deleted.
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { dirname, resolve } from 'node:path'
2+
import { fileURLToPath } from 'node:url'
3+
import colors from 'picocolors'
4+
import { rewriteImports } from './util'
5+
6+
const dir = dirname(fileURLToPath(import.meta.url))
7+
const nodeDts = resolve(dir, '../dist/node/index.d.ts')
8+
9+
// rewrite `types/*` import to relative import
10+
rewriteImports(nodeDts, (importPath) => {
11+
if (importPath.startsWith('types/')) {
12+
return '../../' + importPath
13+
}
14+
})
15+
16+
console.log(colors.green(colors.bold(`patched types/* imports`)))
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { dirname, relative, resolve } from 'node:path'
2+
import { fileURLToPath } from 'node:url'
3+
import colors from 'picocolors'
4+
import { rewriteImports, slash } from './util'
5+
6+
const dir = dirname(fileURLToPath(import.meta.url))
7+
const tempDir = resolve(dir, '../temp/node')
8+
const depTypesDir = resolve(dir, '../src/types')
9+
10+
// walk through the temp dts dir, find all import/export of, deps-types/*
11+
// and rewrite them into relative imports - so that api-extractor actually
12+
// includes them in the rolled-up final d.ts file.
13+
rewriteImports(tempDir, (importPath, currentFile) => {
14+
if (importPath.startsWith('dep-types/')) {
15+
const absoluteTypePath = resolve(
16+
depTypesDir,
17+
importPath.slice('dep-types/'.length)
18+
)
19+
return slash(relative(dirname(currentFile), absoluteTypePath))
20+
}
21+
})
22+
23+
console.log(colors.green(colors.bold(`patched deps-types/* imports`)))

‎packages/vite/scripts/util.ts

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { readFileSync, readdirSync, statSync, writeFileSync } from 'node:fs'
2+
import { resolve } from 'node:path'
3+
import type { ParseResult } from '@babel/parser'
4+
import { parse } from '@babel/parser'
5+
import type { File } from '@babel/types'
6+
import colors from 'picocolors'
7+
import MagicString from 'magic-string'
8+
9+
export function rewriteImports(
10+
fileOrDir: string,
11+
rewrite: (importPath: string, currentFile: string) => string | void
12+
): void {
13+
walkDir(fileOrDir, (file) => {
14+
rewriteFileImports(file, (importPath) => {
15+
return rewrite(importPath, file)
16+
})
17+
})
18+
}
19+
20+
export function slash(p: string): string {
21+
return p.replace(/\\/g, '/')
22+
}
23+
24+
function walkDir(dir: string, handleFile: (file: string) => void): void {
25+
if (statSync(dir).isDirectory()) {
26+
const files = readdirSync(dir)
27+
for (const file of files) {
28+
const resolved = resolve(dir, file)
29+
walkDir(resolved, handleFile)
30+
}
31+
} else {
32+
handleFile(dir)
33+
}
34+
}
35+
36+
function rewriteFileImports(
37+
file: string,
38+
rewrite: (importPath: string) => string | void
39+
): void {
40+
const content = readFileSync(file, 'utf-8')
41+
const str = new MagicString(content)
42+
let ast: ParseResult<File>
43+
try {
44+
ast = parse(content, {
45+
sourceType: 'module',
46+
plugins: ['typescript', 'classProperties']
47+
})
48+
} catch (e) {
49+
console.log(colors.red(`failed to parse ${file}`))
50+
throw e
51+
}
52+
for (const statement of ast.program.body) {
53+
if (
54+
statement.type === 'ImportDeclaration' ||
55+
statement.type === 'ExportNamedDeclaration' ||
56+
statement.type === 'ExportAllDeclaration'
57+
) {
58+
const source = statement.source
59+
if (source?.value) {
60+
const newImportPath = rewrite(source.value)
61+
if (newImportPath) {
62+
str.overwrite(
63+
source.start!,
64+
source.end!,
65+
JSON.stringify(newImportPath)
66+
)
67+
}
68+
}
69+
}
70+
}
71+
writeFileSync(file, str.toString())
72+
}

‎packages/vite/src/client-types.d.ts

-24
This file was deleted.

‎packages/vite/src/dep-types/package.json

-3
This file was deleted.

‎packages/vite/src/node/__tests__/plugins/importGlob/__snapshots__/fixture.test.ts.snap

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// Vitest Snapshot v1
22

33
exports[`fixture > transform 1`] = `
4-
"import * as __vite_glob_1_0 from \\"./modules/a.ts\\";import * as __vite_glob_1_1 from \\"./modules/b.ts\\";import * as __vite_glob_1_2 from \\"./modules/index.ts\\";import { name as __vite_glob_3_0 } from \\"./modules/a.ts\\";import { name as __vite_glob_3_1 } from \\"./modules/b.ts\\";import { name as __vite_glob_3_2 } from \\"./modules/index.ts\\";import { default as __vite_glob_5_0 } from \\"./modules/a.ts?raw\\";import { default as __vite_glob_5_1 } from \\"./modules/b.ts?raw\\";export const basic = /* #__PURE__ */ Object.assign({\\"./modules/a.ts\\": () => import(\\"./modules/a.ts\\"),\\"./modules/b.ts\\": () => import(\\"./modules/b.ts\\"),\\"./modules/index.ts\\": () => import(\\"./modules/index.ts\\")});
4+
"import * as __vite_glob_1_0 from \\"./modules/a.ts\\";import * as __vite_glob_1_1 from \\"./modules/b.ts\\";import * as __vite_glob_1_2 from \\"./modules/index.ts\\";import { name as __vite_glob_3_0 } from \\"./modules/a.ts\\";import { name as __vite_glob_3_1 } from \\"./modules/b.ts\\";import { name as __vite_glob_3_2 } from \\"./modules/index.ts\\";import { default as __vite_glob_5_0 } from \\"./modules/a.ts?raw\\";import { default as __vite_glob_5_1 } from \\"./modules/b.ts?raw\\";import \\"types/importMeta\\";
5+
export const basic = /* #__PURE__ */ Object.assign({\\"./modules/a.ts\\": () => import(\\"./modules/a.ts\\"),\\"./modules/b.ts\\": () => import(\\"./modules/b.ts\\"),\\"./modules/index.ts\\": () => import(\\"./modules/index.ts\\")});
56
export const basicEager = /* #__PURE__ */ Object.assign({\\"./modules/a.ts\\": __vite_glob_1_0,\\"./modules/b.ts\\": __vite_glob_1_1,\\"./modules/index.ts\\": __vite_glob_1_2});
67
export const ignore = /* #__PURE__ */ Object.assign({\\"./modules/a.ts\\": () => import(\\"./modules/a.ts\\"),\\"./modules/b.ts\\": () => import(\\"./modules/b.ts\\")});
78
export const namedEager = /* #__PURE__ */ Object.assign({\\"./modules/a.ts\\": __vite_glob_3_0,\\"./modules/b.ts\\": __vite_glob_3_1,\\"./modules/index.ts\\": __vite_glob_3_2});
@@ -19,7 +20,8 @@ export const cleverCwd2 = /* #__PURE__ */ Object.assign({\\"./modules/a.ts\\": (
1920
`;
2021

2122
exports[`fixture > transform with restoreQueryExtension 1`] = `
22-
"import * as __vite_glob_1_0 from \\"./modules/a.ts\\";import * as __vite_glob_1_1 from \\"./modules/b.ts\\";import * as __vite_glob_1_2 from \\"./modules/index.ts\\";import { name as __vite_glob_3_0 } from \\"./modules/a.ts\\";import { name as __vite_glob_3_1 } from \\"./modules/b.ts\\";import { name as __vite_glob_3_2 } from \\"./modules/index.ts\\";import { default as __vite_glob_5_0 } from \\"./modules/a.ts?raw\\";import { default as __vite_glob_5_1 } from \\"./modules/b.ts?raw\\";export const basic = /* #__PURE__ */ Object.assign({\\"./modules/a.ts\\": () => import(\\"./modules/a.ts\\"),\\"./modules/b.ts\\": () => import(\\"./modules/b.ts\\"),\\"./modules/index.ts\\": () => import(\\"./modules/index.ts\\")});
23+
"import * as __vite_glob_1_0 from \\"./modules/a.ts\\";import * as __vite_glob_1_1 from \\"./modules/b.ts\\";import * as __vite_glob_1_2 from \\"./modules/index.ts\\";import { name as __vite_glob_3_0 } from \\"./modules/a.ts\\";import { name as __vite_glob_3_1 } from \\"./modules/b.ts\\";import { name as __vite_glob_3_2 } from \\"./modules/index.ts\\";import { default as __vite_glob_5_0 } from \\"./modules/a.ts?raw\\";import { default as __vite_glob_5_1 } from \\"./modules/b.ts?raw\\";import \\"types/importMeta\\";
24+
export const basic = /* #__PURE__ */ Object.assign({\\"./modules/a.ts\\": () => import(\\"./modules/a.ts\\"),\\"./modules/b.ts\\": () => import(\\"./modules/b.ts\\"),\\"./modules/index.ts\\": () => import(\\"./modules/index.ts\\")});
2325
export const basicEager = /* #__PURE__ */ Object.assign({\\"./modules/a.ts\\": __vite_glob_1_0,\\"./modules/b.ts\\": __vite_glob_1_1,\\"./modules/index.ts\\": __vite_glob_1_2});
2426
export const ignore = /* #__PURE__ */ Object.assign({\\"./modules/a.ts\\": () => import(\\"./modules/a.ts\\"),\\"./modules/b.ts\\": () => import(\\"./modules/b.ts\\")});
2527
export const namedEager = /* #__PURE__ */ Object.assign({\\"./modules/a.ts\\": __vite_glob_3_0,\\"./modules/b.ts\\": __vite_glob_3_1,\\"./modules/index.ts\\": __vite_glob_3_2});

‎packages/vite/src/node/__tests__/plugins/importGlob/fixture-a/index.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import type { ImportMeta as ViteImportMeta } from 'types/importMeta'
2-
3-
declare global {
4-
interface ImportMeta extends ViteImportMeta {}
5-
}
1+
import 'types/importMeta'
62

73
export interface ModuleType {
84
name: string
File renamed without changes.
File renamed without changes.

‎packages/vite/src/types/customEvent.d.ts

-21
This file was deleted.

‎packages/vite/src/types/hmrPayload.d.ts

-61
This file was deleted.

‎packages/vite/src/types/hot.d.ts

-33
This file was deleted.

‎packages/vite/src/types/importGlob.d.ts

-97
This file was deleted.

‎packages/vite/src/types/importMeta.d.ts

-25
This file was deleted.
File renamed without changes.
File renamed without changes.
File renamed without changes.

‎packages/vite/types/customEvent.d.ts

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
1-
export type {
2-
CustomEventMap,
3-
InferCustomEventPayload,
4-
InvalidatePayload
5-
} from '../client/types'
1+
import type {
2+
ErrorPayload,
3+
FullReloadPayload,
4+
PrunePayload,
5+
UpdatePayload
6+
} from './hmrPayload'
7+
8+
export interface CustomEventMap {
9+
'vite:beforeUpdate': UpdatePayload
10+
'vite:beforePrune': PrunePayload
11+
'vite:beforeFullReload': FullReloadPayload
12+
'vite:error': ErrorPayload
13+
'vite:invalidate': InvalidatePayload
14+
}
15+
16+
export interface InvalidatePayload {
17+
path: string
18+
}
19+
20+
export type InferCustomEventPayload<T extends string> =
21+
T extends keyof CustomEventMap ? CustomEventMap[T] : any

‎packages/vite/types/hmrPayload.d.ts

+61-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,61 @@
1-
export type {
2-
HMRPayload,
3-
ConnectedPayload,
4-
UpdatePayload,
5-
Update,
6-
PrunePayload,
7-
FullReloadPayload,
8-
CustomPayload,
9-
ErrorPayload
10-
} from '../client/types'
1+
export type HMRPayload =
2+
| ConnectedPayload
3+
| UpdatePayload
4+
| FullReloadPayload
5+
| CustomPayload
6+
| ErrorPayload
7+
| PrunePayload
8+
9+
export interface ConnectedPayload {
10+
type: 'connected'
11+
}
12+
13+
export interface UpdatePayload {
14+
type: 'update'
15+
updates: Update[]
16+
}
17+
18+
export interface Update {
19+
type: 'js-update' | 'css-update'
20+
path: string
21+
acceptedPath: string
22+
timestamp: number
23+
/**
24+
* @experimental internal
25+
*/
26+
explicitImportRequired?: boolean | undefined
27+
}
28+
29+
export interface PrunePayload {
30+
type: 'prune'
31+
paths: string[]
32+
}
33+
34+
export interface FullReloadPayload {
35+
type: 'full-reload'
36+
path?: string
37+
}
38+
39+
export interface CustomPayload {
40+
type: 'custom'
41+
event: string
42+
data?: any
43+
}
44+
45+
export interface ErrorPayload {
46+
type: 'error'
47+
err: {
48+
[name: string]: any
49+
message: string
50+
stack: string
51+
id?: string
52+
frame?: string
53+
plugin?: string
54+
pluginCode?: string
55+
loc?: {
56+
file?: string
57+
line: number
58+
column: number
59+
}
60+
}
61+
}

‎packages/vite/types/hot.d.ts

+33-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,33 @@
1-
export type { ModuleNamespace, ViteHotContext } from '../client/types'
1+
import type { InferCustomEventPayload } from './customEvent'
2+
3+
export type ModuleNamespace = Record<string, any> & {
4+
[Symbol.toStringTag]: 'Module'
5+
}
6+
7+
export interface ViteHotContext {
8+
readonly data: any
9+
10+
accept(): void
11+
accept(cb: (mod: ModuleNamespace | undefined) => void): void
12+
accept(dep: string, cb: (mod: ModuleNamespace | undefined) => void): void
13+
accept(
14+
deps: readonly string[],
15+
cb: (mods: Array<ModuleNamespace | undefined>) => void
16+
): void
17+
18+
acceptExports(exportNames: string | readonly string[]): void
19+
acceptExports(
20+
exportNames: string | readonly string[],
21+
cb: (mod: ModuleNamespace | undefined) => void
22+
): void
23+
24+
dispose(cb: (data: any) => void): void
25+
decline(): void
26+
invalidate(): void
27+
28+
on<T extends string>(
29+
event: T,
30+
cb: (payload: InferCustomEventPayload<T>) => void
31+
): void
32+
send<T extends string>(event: T, data?: InferCustomEventPayload<T>): void
33+
}

‎packages/vite/types/importGlob.d.ts

+97-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,97 @@
1-
export type {
2-
ImportGlobOptions,
3-
GeneralImportGlobOptions,
4-
KnownAsTypeMap,
5-
ImportGlobFunction,
6-
ImportGlobEagerFunction
7-
} from '../client/types'
1+
export interface ImportGlobOptions<
2+
Eager extends boolean,
3+
AsType extends string
4+
> {
5+
/**
6+
* Import type for the import url.
7+
*/
8+
as?: AsType
9+
/**
10+
* Import as static or dynamic
11+
*
12+
* @default false
13+
*/
14+
eager?: Eager
15+
/**
16+
* Import only the specific named export. Set to `default` to import the default export.
17+
*/
18+
import?: string
19+
/**
20+
* Custom queries
21+
*/
22+
query?: string | Record<string, string | number | boolean>
23+
/**
24+
* Search files also inside `node_modules/` and hidden directories (e.g. `.git/`). This might have impact on performance.
25+
*
26+
* @default false
27+
*/
28+
exhaustive?: boolean
29+
}
30+
31+
export type GeneralImportGlobOptions = ImportGlobOptions<boolean, string>
32+
33+
export interface KnownAsTypeMap {
34+
raw: string
35+
url: string
36+
worker: Worker
37+
}
38+
39+
export interface ImportGlobFunction {
40+
/**
41+
* Import a list of files with a glob pattern.
42+
*
43+
* Overload 1: No generic provided, infer the type from `eager` and `as`
44+
*/
45+
<
46+
Eager extends boolean,
47+
As extends string,
48+
T = As extends keyof KnownAsTypeMap ? KnownAsTypeMap[As] : unknown
49+
>(
50+
glob: string | string[],
51+
options?: ImportGlobOptions<Eager, As>
52+
): (Eager extends true ? true : false) extends true
53+
? Record<string, T>
54+
: Record<string, () => Promise<T>>
55+
/**
56+
* Import a list of files with a glob pattern.
57+
*
58+
* Overload 2: Module generic provided, infer the type from `eager: false`
59+
*/
60+
<M>(
61+
glob: string | string[],
62+
options?: ImportGlobOptions<false, string>
63+
): Record<string, () => Promise<M>>
64+
/**
65+
* Import a list of files with a glob pattern.
66+
*
67+
* Overload 3: Module generic provided, infer the type from `eager: true`
68+
*/
69+
<M>(
70+
glob: string | string[],
71+
options: ImportGlobOptions<true, string>
72+
): Record<string, M>
73+
}
74+
75+
export interface ImportGlobEagerFunction {
76+
/**
77+
* Eagerly import a list of files with a glob pattern.
78+
*
79+
* Overload 1: No generic provided, infer the type from `as`
80+
*/
81+
<
82+
As extends string,
83+
T = As extends keyof KnownAsTypeMap ? KnownAsTypeMap[As] : unknown
84+
>(
85+
glob: string | string[],
86+
options?: Omit<ImportGlobOptions<boolean, As>, 'eager'>
87+
): Record<string, T>
88+
/**
89+
* Eagerly import a list of files with a glob pattern.
90+
*
91+
* Overload 2: Module generic provided
92+
*/
93+
<M>(
94+
glob: string | string[],
95+
options?: Omit<ImportGlobOptions<boolean, string>, 'eager'>
96+
): Record<string, M>
97+
}

‎packages/vite/types/importMeta.d.ts

+27-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
1-
import type {
2-
ImportMeta as ViteImportMeta,
3-
ImportMetaEnv as ViteImportMetaEnv
4-
} from '../client/types'
5-
6-
declare global {
7-
interface GlobOptions {
8-
as?: string
9-
}
10-
11-
interface ImportMeta extends ViteImportMeta {}
12-
interface ImportMetaEnv extends ViteImportMetaEnv {}
1+
// This file is an augmentation to the built-in ImportMeta interface
2+
// Thus cannot contain any top-level imports
3+
// <https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation>
4+
5+
/* eslint-disable @typescript-eslint/consistent-type-imports */
6+
7+
interface ImportMetaEnv {
8+
[key: string]: any
9+
BASE_URL: string
10+
MODE: string
11+
DEV: boolean
12+
PROD: boolean
13+
SSR: boolean
14+
}
15+
16+
interface ImportMeta {
17+
url: string
18+
19+
readonly hot?: import('./hot').ViteHotContext
20+
21+
readonly env: ImportMetaEnv
22+
23+
glob: import('./importGlob').ImportGlobFunction
24+
/**
25+
* @deprecated Use `import.meta.glob('*', { eager: true })` instead
26+
*/
27+
globEager: import('./importGlob').ImportGlobEagerFunction
1328
}

‎playground/hmr/event.d.ts

+7-14
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
1-
import 'vite'
2-
import 'vite/client/types'
1+
import 'vite/types/customEvent'
32

4-
interface MyCustomEventMap {
5-
'custom:foo': { msg: string }
6-
'custom:remote-add': { a: number; b: number }
7-
'custom:remote-add-result': { result: string }
8-
}
9-
10-
declare module 'vite' {
11-
interface CustomEventMap extends MyCustomEventMap {}
12-
}
13-
14-
declare module 'vite/client/types' {
15-
interface CustomEventMap extends MyCustomEventMap {}
3+
declare module 'vite/types/customEvent' {
4+
interface CustomEventMap {
5+
'custom:foo': { msg: string }
6+
'custom:remote-add': { a: number; b: number }
7+
'custom:remote-add-result': { result: string }
8+
}
169
}

‎pnpm-lock.yaml

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.