diff --git a/.eslintignore b/.eslintignore index 833faed81beb2b..2974196847e467 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,4 +1,3 @@ dist playground-temp temp -packages/vite/client/types.d.ts diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 2ce0e4fbf59c67..6333ec251973c0 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -131,7 +131,7 @@ module.exports = defineConfig({ } }, { - files: ['packages/vite/src/dep-types/**', '*.spec.ts'], + files: ['packages/vite/src/types/**', '*.spec.ts'], rules: { 'node/no-extraneous-import': 'off' } diff --git a/.gitignore b/.gitignore index 8aafb6ab143259..7f1cab0e1f4356 100644 --- a/.gitignore +++ b/.gitignore @@ -5,7 +5,6 @@ *.local *.log /.vscode/ -/packages/vite/client/types.d.ts /packages/vite/LICENSE dist dist-ssr diff --git a/.prettierignore b/.prettierignore index be3598b2ccb5b3..b1ea458b9bb9d8 100644 --- a/.prettierignore +++ b/.prettierignore @@ -9,4 +9,3 @@ playground/tsconfig-json-load-error/has-error/tsconfig.json playground/html/invalid.html playground/html/valid.html playground/worker/classic-worker.js -packages/vite/client/types.d.ts diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f2f04ce43f86c7..78271bb6768b3c 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -218,10 +218,12 @@ Avoid deps with large transitive dependencies that result in bloated size compar 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. -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. +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. 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`. +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. + ### Think Before Adding Yet Another Option 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: diff --git a/docs/guide/api-plugin.md b/docs/guide/api-plugin.md index 33d8ba0e9802b7..c18c6daca3263f 100644 --- a/docs/guide/api-plugin.md +++ b/docs/guide/api-plugin.md @@ -595,21 +595,12 @@ It is possible to type custom events by extending the `CustomEventMap` interface ```ts // events.d.ts -import 'vite' -import 'vite/client/types' +import 'vite/types/customEvent' -interface MyCustomEventMap { - 'custom:foo': { msg: string } - // 'event-key': payload -} - -// extend interface for server-side -declare module 'vite' { - interface CustomEventMap extends MyCustomEventMap {} -} - -// extend interface for client-side -declare module 'vite/client/types' { - interface CustomEventMap extends MyCustomEventMap {} +declare module 'vite/types/customEvent' { + interface CustomEventMap { + 'custom:foo': { msg: string } + // 'event-key': payload + } } ``` diff --git a/packages/vite/api-extractor.client.json b/packages/vite/api-extractor.client.json deleted file mode 100644 index 81188aa4870d54..00000000000000 --- a/packages/vite/api-extractor.client.json +++ /dev/null @@ -1,54 +0,0 @@ -{ - "$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json", - - "projectFolder": "./src/client", - - "mainEntryPointFilePath": "./src/client-types.d.ts", - - "dtsRollup": { - "enabled": true, - "untrimmedFilePath": "", - "publicTrimmedFilePath": "./client/types.d.ts" - }, - - "apiReport": { - "enabled": false - }, - - "docModel": { - "enabled": false - }, - - "tsdocMetadata": { - "enabled": false - }, - - "messages": { - "compilerMessageReporting": { - "default": { - "logLevel": "warning" - } - }, - - "extractorMessageReporting": { - "default": { - "logLevel": "warning", - "addToApiReportFile": true - }, - - "ae-missing-release-tag": { - "logLevel": "none" - } - }, - - "tsdocMessageReporting": { - "default": { - "logLevel": "warning" - }, - - "tsdoc-undefined-tag": { - "logLevel": "none" - } - } - } -} diff --git a/packages/vite/client.d.ts b/packages/vite/client.d.ts index 5ee6940825b119..e99b4a526b7a58 100644 --- a/packages/vite/client.d.ts +++ b/packages/vite/client.d.ts @@ -1,4 +1,4 @@ -/// +/// // CSS modules type CSSModuleClasses = { readonly [key: string]: string } diff --git a/packages/vite/import-meta.d.ts b/packages/vite/import-meta.d.ts deleted file mode 100644 index 73baf3c106c74f..00000000000000 --- a/packages/vite/import-meta.d.ts +++ /dev/null @@ -1,10 +0,0 @@ -import type { - ImportMeta as ViteImportMeta, - ImportMetaEnv as ViteImportMetaEnv - // eslint-disable-next-line node/no-missing-import -- use .js for `moduleResolution: "nodenext"` -} from './client/types.js' - -declare global { - interface ImportMeta extends ViteImportMeta {} - interface ImportMetaEnv extends ViteImportMetaEnv {} -} diff --git a/packages/vite/package.json b/packages/vite/package.json index 935a1dffa13461..a567a7afc3790c 100644 --- a/packages/vite/package.json +++ b/packages/vite/package.json @@ -20,12 +20,6 @@ "./client": { "types": "./client.d.ts" }, - "./import-meta": { - "types": "./import-meta.d.ts" - }, - "./client/types": { - "types": "./client/types.d.ts" - }, "./dist/client/*": "./dist/client/*", "./package.json": "./package.json" }, @@ -33,11 +27,9 @@ "bin", "dist", "client.d.ts", - "import-meta.d.ts", "index.cjs", "src/client", - "types", - "client/types.d.ts" + "types" ], "engines": { "node": "^14.18.0 || >=16.0.0" @@ -55,13 +47,12 @@ "dev": "rimraf dist && pnpm run build-bundle -w", "build": "rimraf dist && run-s build-bundle build-types", "build-bundle": "rollup --config rollup.config.ts --configPlugin typescript", - "build-types": "run-p build-node-types build-client-types", - "build-node-types": "run-s build-node-types-temp build-node-types-patch build-node-types-roll build-node-types-check", - "build-node-types-temp": "tsc --emitDeclarationOnly --outDir temp/node -p src/node", - "build-node-types-patch": "tsx scripts/patchTypes.ts", - "build-node-types-roll": "api-extractor run && rimraf temp", - "build-node-types-check": "tsc --project tsconfig.check.json", - "build-client-types": "api-extractor run -c api-extractor.client.json", + "build-types": "run-s build-types-temp build-types-pre-patch build-types-roll build-types-post-patch build-types-check", + "build-types-temp": "tsc --emitDeclarationOnly --outDir temp/node -p src/node", + "build-types-pre-patch": "tsx scripts/prePatchTypes.ts", + "build-types-roll": "api-extractor run && rimraf temp", + "build-types-post-patch": "tsx scripts/postPatchTypes.ts", + "build-types-check": "tsc --project tsconfig.check.json", "lint": "eslint --cache --ext .ts src/**", "format": "prettier --write --cache --parser typescript \"src/**/*.ts\"", "prepublishOnly": "npm run build" @@ -127,8 +118,8 @@ "strip-literal": "^0.4.2", "tsconfck": "^2.0.1", "tslib": "^2.4.0", - "dep-types": "link:./src/dep-types", - "types": "link:./src/types", + "dep-types": "link:./src/types", + "types": "link:./types", "ufo": "^0.8.5", "ws": "^8.9.0" }, diff --git a/packages/vite/scripts/patchTypes.ts b/packages/vite/scripts/patchTypes.ts deleted file mode 100644 index 8d86c9c7b76292..00000000000000 --- a/packages/vite/scripts/patchTypes.ts +++ /dev/null @@ -1,83 +0,0 @@ -import { readFileSync, readdirSync, statSync, writeFileSync } from 'node:fs' -import { dirname, relative, resolve } from 'node:path' -import { fileURLToPath } from 'node:url' -import type { ParseResult } from '@babel/parser' -import { parse } from '@babel/parser' -import type { File, StringLiteral } from '@babel/types' -import colors from 'picocolors' -import MagicString from 'magic-string' - -const dir = dirname(fileURLToPath(import.meta.url)) -const tempDir = resolve(dir, '../temp/node') -const typesDir = resolve(dir, '../src/types') -const depTypesDir = resolve(dir, '../src/dep-types') - -// walk through the temp dts dir, find all import/export of types/*, deps-types/* -// and rewrite them into relative imports - so that api-extractor actually -// includes them in the rolled-up final d.ts file. -walkDir(tempDir) -console.log(colors.green(colors.bold(`patched types/*, deps-types/* imports`))) - -function slash(p: string): string { - return p.replace(/\\/g, '/') -} - -function walkDir(dir: string): void { - const files = readdirSync(dir) - for (const file of files) { - const resolved = resolve(dir, file) - const isDir = statSync(resolved).isDirectory() - if (isDir) { - walkDir(resolved) - } else { - rewriteFile(resolved) - } - } -} - -function rewriteFile(file: string): void { - const content = readFileSync(file, 'utf-8') - const str = new MagicString(content) - let ast: ParseResult - try { - ast = parse(content, { - sourceType: 'module', - plugins: ['typescript', 'classProperties'] - }) - } catch (e) { - console.log(colors.red(`failed to parse ${file}`)) - throw e - } - for (const statement of ast.program.body) { - if ( - statement.type === 'ImportDeclaration' || - statement.type === 'ExportNamedDeclaration' || - statement.type === 'ExportAllDeclaration' - ) { - const source = statement.source - if (source?.value.startsWith('types/')) { - rewriteSource(str, source, file, typesDir, 'types') - } else if (source?.value.startsWith('dep-types/')) { - rewriteSource(str, source, file, depTypesDir, 'dep-types') - } - } - } - writeFileSync(file, str.toString()) -} - -function rewriteSource( - str: MagicString, - source: StringLiteral, - rewritingFile: string, - typesDir: string, - typesDirName: string -) { - const absoluteTypePath = resolve( - typesDir, - source.value.slice(typesDirName.length + 1) - ) - const relativeTypePath = slash( - relative(dirname(rewritingFile), absoluteTypePath) - ) - str.overwrite(source.start!, source.end!, JSON.stringify(relativeTypePath)) -} diff --git a/packages/vite/scripts/postPatchTypes.ts b/packages/vite/scripts/postPatchTypes.ts new file mode 100644 index 00000000000000..95f3fc217efcb0 --- /dev/null +++ b/packages/vite/scripts/postPatchTypes.ts @@ -0,0 +1,16 @@ +import { dirname, resolve } from 'node:path' +import { fileURLToPath } from 'node:url' +import colors from 'picocolors' +import { rewriteImports } from './util' + +const dir = dirname(fileURLToPath(import.meta.url)) +const nodeDts = resolve(dir, '../dist/node/index.d.ts') + +// rewrite `types/*` import to relative import +rewriteImports(nodeDts, (importPath) => { + if (importPath.startsWith('types/')) { + return '../../' + importPath + } +}) + +console.log(colors.green(colors.bold(`patched types/* imports`))) diff --git a/packages/vite/scripts/prePatchTypes.ts b/packages/vite/scripts/prePatchTypes.ts new file mode 100644 index 00000000000000..eda006b476209d --- /dev/null +++ b/packages/vite/scripts/prePatchTypes.ts @@ -0,0 +1,23 @@ +import { dirname, relative, resolve } from 'node:path' +import { fileURLToPath } from 'node:url' +import colors from 'picocolors' +import { rewriteImports, slash } from './util' + +const dir = dirname(fileURLToPath(import.meta.url)) +const tempDir = resolve(dir, '../temp/node') +const depTypesDir = resolve(dir, '../src/types') + +// walk through the temp dts dir, find all import/export of, deps-types/* +// and rewrite them into relative imports - so that api-extractor actually +// includes them in the rolled-up final d.ts file. +rewriteImports(tempDir, (importPath, currentFile) => { + if (importPath.startsWith('dep-types/')) { + const absoluteTypePath = resolve( + depTypesDir, + importPath.slice('dep-types/'.length) + ) + return slash(relative(dirname(currentFile), absoluteTypePath)) + } +}) + +console.log(colors.green(colors.bold(`patched deps-types/* imports`))) diff --git a/packages/vite/scripts/util.ts b/packages/vite/scripts/util.ts new file mode 100644 index 00000000000000..4cc867afe82801 --- /dev/null +++ b/packages/vite/scripts/util.ts @@ -0,0 +1,72 @@ +import { readFileSync, readdirSync, statSync, writeFileSync } from 'node:fs' +import { resolve } from 'node:path' +import type { ParseResult } from '@babel/parser' +import { parse } from '@babel/parser' +import type { File } from '@babel/types' +import colors from 'picocolors' +import MagicString from 'magic-string' + +export function rewriteImports( + fileOrDir: string, + rewrite: (importPath: string, currentFile: string) => string | void +): void { + walkDir(fileOrDir, (file) => { + rewriteFileImports(file, (importPath) => { + return rewrite(importPath, file) + }) + }) +} + +export function slash(p: string): string { + return p.replace(/\\/g, '/') +} + +function walkDir(dir: string, handleFile: (file: string) => void): void { + if (statSync(dir).isDirectory()) { + const files = readdirSync(dir) + for (const file of files) { + const resolved = resolve(dir, file) + walkDir(resolved, handleFile) + } + } else { + handleFile(dir) + } +} + +function rewriteFileImports( + file: string, + rewrite: (importPath: string) => string | void +): void { + const content = readFileSync(file, 'utf-8') + const str = new MagicString(content) + let ast: ParseResult + try { + ast = parse(content, { + sourceType: 'module', + plugins: ['typescript', 'classProperties'] + }) + } catch (e) { + console.log(colors.red(`failed to parse ${file}`)) + throw e + } + for (const statement of ast.program.body) { + if ( + statement.type === 'ImportDeclaration' || + statement.type === 'ExportNamedDeclaration' || + statement.type === 'ExportAllDeclaration' + ) { + const source = statement.source + if (source?.value) { + const newImportPath = rewrite(source.value) + if (newImportPath) { + str.overwrite( + source.start!, + source.end!, + JSON.stringify(newImportPath) + ) + } + } + } + } + writeFileSync(file, str.toString()) +} diff --git a/packages/vite/src/client-types.d.ts b/packages/vite/src/client-types.d.ts deleted file mode 100644 index cca320bc5d4e1b..00000000000000 --- a/packages/vite/src/client-types.d.ts +++ /dev/null @@ -1,24 +0,0 @@ -export type { - CustomEventMap, - InferCustomEventPayload, - InvalidatePayload -} from './types/customEvent' -export type { - HMRPayload, - ConnectedPayload, - UpdatePayload, - Update, - PrunePayload, - FullReloadPayload, - CustomPayload, - ErrorPayload -} from './types/hmrPayload' -export type { ModuleNamespace, ViteHotContext } from './types/hot' -export type { - ImportGlobOptions, - GeneralImportGlobOptions, - KnownAsTypeMap, - ImportGlobFunction, - ImportGlobEagerFunction -} from './types/importGlob' -export type { ImportMetaEnv, ImportMeta } from './types/importMeta' diff --git a/packages/vite/src/dep-types/package.json b/packages/vite/src/dep-types/package.json deleted file mode 100644 index 3d6a75c81455a0..00000000000000 --- a/packages/vite/src/dep-types/package.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "//": "this file is just here to make pnpm happy with --frozen-lockfile" -} diff --git a/packages/vite/src/node/__tests__/plugins/importGlob/__snapshots__/fixture.test.ts.snap b/packages/vite/src/node/__tests__/plugins/importGlob/__snapshots__/fixture.test.ts.snap index f4103222acebdd..e638850c4ce0b0 100644 --- a/packages/vite/src/node/__tests__/plugins/importGlob/__snapshots__/fixture.test.ts.snap +++ b/packages/vite/src/node/__tests__/plugins/importGlob/__snapshots__/fixture.test.ts.snap @@ -1,7 +1,8 @@ // Vitest Snapshot v1 exports[`fixture > transform 1`] = ` -"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\\")}); +"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\\"; +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\\")}); 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}); export const ignore = /* #__PURE__ */ Object.assign({\\"./modules/a.ts\\": () => import(\\"./modules/a.ts\\"),\\"./modules/b.ts\\": () => import(\\"./modules/b.ts\\")}); 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\\": ( `; exports[`fixture > transform with restoreQueryExtension 1`] = ` -"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\\")}); +"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\\"; +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\\")}); 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}); export const ignore = /* #__PURE__ */ Object.assign({\\"./modules/a.ts\\": () => import(\\"./modules/a.ts\\"),\\"./modules/b.ts\\": () => import(\\"./modules/b.ts\\")}); 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}); diff --git a/packages/vite/src/node/__tests__/plugins/importGlob/fixture-a/index.ts b/packages/vite/src/node/__tests__/plugins/importGlob/fixture-a/index.ts index ccf47d4edca6cf..452336631be666 100644 --- a/packages/vite/src/node/__tests__/plugins/importGlob/fixture-a/index.ts +++ b/packages/vite/src/node/__tests__/plugins/importGlob/fixture-a/index.ts @@ -1,8 +1,4 @@ -import type { ImportMeta as ViteImportMeta } from 'types/importMeta' - -declare global { - interface ImportMeta extends ViteImportMeta {} -} +import 'types/importMeta' export interface ModuleType { name: string diff --git a/packages/vite/src/dep-types/alias.d.ts b/packages/vite/src/types/alias.d.ts similarity index 100% rename from packages/vite/src/dep-types/alias.d.ts rename to packages/vite/src/types/alias.d.ts diff --git a/packages/vite/src/dep-types/anymatch.d.ts b/packages/vite/src/types/anymatch.d.ts similarity index 100% rename from packages/vite/src/dep-types/anymatch.d.ts rename to packages/vite/src/types/anymatch.d.ts diff --git a/packages/vite/src/dep-types/chokidar.d.ts b/packages/vite/src/types/chokidar.d.ts similarity index 100% rename from packages/vite/src/dep-types/chokidar.d.ts rename to packages/vite/src/types/chokidar.d.ts diff --git a/packages/vite/src/dep-types/commonjs.d.ts b/packages/vite/src/types/commonjs.d.ts similarity index 100% rename from packages/vite/src/dep-types/commonjs.d.ts rename to packages/vite/src/types/commonjs.d.ts diff --git a/packages/vite/src/dep-types/connect.d.ts b/packages/vite/src/types/connect.d.ts similarity index 100% rename from packages/vite/src/dep-types/connect.d.ts rename to packages/vite/src/types/connect.d.ts diff --git a/packages/vite/src/types/customEvent.d.ts b/packages/vite/src/types/customEvent.d.ts deleted file mode 100644 index 839e17dd729eda..00000000000000 --- a/packages/vite/src/types/customEvent.d.ts +++ /dev/null @@ -1,21 +0,0 @@ -import type { - ErrorPayload, - FullReloadPayload, - PrunePayload, - UpdatePayload -} from './hmrPayload' - -export interface CustomEventMap { - 'vite:beforeUpdate': UpdatePayload - 'vite:beforePrune': PrunePayload - 'vite:beforeFullReload': FullReloadPayload - 'vite:error': ErrorPayload - 'vite:invalidate': InvalidatePayload -} - -export interface InvalidatePayload { - path: string -} - -export type InferCustomEventPayload = - T extends keyof CustomEventMap ? CustomEventMap[T] : any diff --git a/packages/vite/src/dep-types/dynamicImportVars.d.ts b/packages/vite/src/types/dynamicImportVars.d.ts similarity index 100% rename from packages/vite/src/dep-types/dynamicImportVars.d.ts rename to packages/vite/src/types/dynamicImportVars.d.ts diff --git a/packages/vite/src/types/hmrPayload.d.ts b/packages/vite/src/types/hmrPayload.d.ts deleted file mode 100644 index 839095009e76fb..00000000000000 --- a/packages/vite/src/types/hmrPayload.d.ts +++ /dev/null @@ -1,61 +0,0 @@ -export type HMRPayload = - | ConnectedPayload - | UpdatePayload - | FullReloadPayload - | CustomPayload - | ErrorPayload - | PrunePayload - -export interface ConnectedPayload { - type: 'connected' -} - -export interface UpdatePayload { - type: 'update' - updates: Update[] -} - -export interface Update { - type: 'js-update' | 'css-update' - path: string - acceptedPath: string - timestamp: number - /** - * @experimental internal - */ - explicitImportRequired?: boolean | undefined -} - -export interface PrunePayload { - type: 'prune' - paths: string[] -} - -export interface FullReloadPayload { - type: 'full-reload' - path?: string -} - -export interface CustomPayload { - type: 'custom' - event: string - data?: any -} - -export interface ErrorPayload { - type: 'error' - err: { - [name: string]: any - message: string - stack: string - id?: string - frame?: string - plugin?: string - pluginCode?: string - loc?: { - file?: string - line: number - column: number - } - } -} diff --git a/packages/vite/src/types/hot.d.ts b/packages/vite/src/types/hot.d.ts deleted file mode 100644 index 985f3a47197c43..00000000000000 --- a/packages/vite/src/types/hot.d.ts +++ /dev/null @@ -1,33 +0,0 @@ -import type { InferCustomEventPayload } from './customEvent' - -export type ModuleNamespace = Record & { - [Symbol.toStringTag]: 'Module' -} - -export interface ViteHotContext { - readonly data: any - - accept(): void - accept(cb: (mod: ModuleNamespace | undefined) => void): void - accept(dep: string, cb: (mod: ModuleNamespace | undefined) => void): void - accept( - deps: readonly string[], - cb: (mods: Array) => void - ): void - - acceptExports(exportNames: string | readonly string[]): void - acceptExports( - exportNames: string | readonly string[], - cb: (mod: ModuleNamespace | undefined) => void - ): void - - dispose(cb: (data: any) => void): void - decline(): void - invalidate(): void - - on( - event: T, - cb: (payload: InferCustomEventPayload) => void - ): void - send(event: T, data?: InferCustomEventPayload): void -} diff --git a/packages/vite/src/dep-types/http-proxy.d.ts b/packages/vite/src/types/http-proxy.d.ts similarity index 100% rename from packages/vite/src/dep-types/http-proxy.d.ts rename to packages/vite/src/types/http-proxy.d.ts diff --git a/packages/vite/src/types/importGlob.d.ts b/packages/vite/src/types/importGlob.d.ts deleted file mode 100644 index 321e3804a7e09a..00000000000000 --- a/packages/vite/src/types/importGlob.d.ts +++ /dev/null @@ -1,97 +0,0 @@ -export interface ImportGlobOptions< - Eager extends boolean, - AsType extends string -> { - /** - * Import type for the import url. - */ - as?: AsType - /** - * Import as static or dynamic - * - * @default false - */ - eager?: Eager - /** - * Import only the specific named export. Set to `default` to import the default export. - */ - import?: string - /** - * Custom queries - */ - query?: string | Record - /** - * Search files also inside `node_modules/` and hidden directories (e.g. `.git/`). This might have impact on performance. - * - * @default false - */ - exhaustive?: boolean -} - -export type GeneralImportGlobOptions = ImportGlobOptions - -export interface KnownAsTypeMap { - raw: string - url: string - worker: Worker -} - -export interface ImportGlobFunction { - /** - * Import a list of files with a glob pattern. - * - * Overload 1: No generic provided, infer the type from `eager` and `as` - */ - < - Eager extends boolean, - As extends string, - T = As extends keyof KnownAsTypeMap ? KnownAsTypeMap[As] : unknown - >( - glob: string | string[], - options?: ImportGlobOptions - ): (Eager extends true ? true : false) extends true - ? Record - : Record Promise> - /** - * Import a list of files with a glob pattern. - * - * Overload 2: Module generic provided, infer the type from `eager: false` - */ - ( - glob: string | string[], - options?: ImportGlobOptions - ): Record Promise> - /** - * Import a list of files with a glob pattern. - * - * Overload 3: Module generic provided, infer the type from `eager: true` - */ - ( - glob: string | string[], - options: ImportGlobOptions - ): Record -} - -export interface ImportGlobEagerFunction { - /** - * Eagerly import a list of files with a glob pattern. - * - * Overload 1: No generic provided, infer the type from `as` - */ - < - As extends string, - T = As extends keyof KnownAsTypeMap ? KnownAsTypeMap[As] : unknown - >( - glob: string | string[], - options?: Omit, 'eager'> - ): Record - /** - * Eagerly import a list of files with a glob pattern. - * - * Overload 2: Module generic provided - */ - ( - glob: string | string[], - options?: Omit, 'eager'> - ): Record -} diff --git a/packages/vite/src/types/importMeta.d.ts b/packages/vite/src/types/importMeta.d.ts deleted file mode 100644 index 794c27c73c102c..00000000000000 --- a/packages/vite/src/types/importMeta.d.ts +++ /dev/null @@ -1,25 +0,0 @@ -import type { ViteHotContext } from './hot' -import type { ImportGlobEagerFunction, ImportGlobFunction } from './importGlob' - -export interface ImportMetaEnv { - [key: string]: any - BASE_URL: string - MODE: string - DEV: boolean - PROD: boolean - SSR: boolean -} - -export interface ImportMeta { - url: string - - readonly hot?: ViteHotContext - - readonly env: ImportMetaEnv - - glob: ImportGlobFunction - /** - * @deprecated Use `import.meta.glob('*', { eager: true })` instead - */ - globEager: ImportGlobEagerFunction -} diff --git a/packages/vite/src/dep-types/shims.d.ts b/packages/vite/src/types/shims.d.ts similarity index 100% rename from packages/vite/src/dep-types/shims.d.ts rename to packages/vite/src/types/shims.d.ts diff --git a/packages/vite/src/dep-types/terser.d.ts b/packages/vite/src/types/terser.d.ts similarity index 100% rename from packages/vite/src/dep-types/terser.d.ts rename to packages/vite/src/types/terser.d.ts diff --git a/packages/vite/src/dep-types/ws.d.ts b/packages/vite/src/types/ws.d.ts similarity index 100% rename from packages/vite/src/dep-types/ws.d.ts rename to packages/vite/src/types/ws.d.ts diff --git a/packages/vite/types/customEvent.d.ts b/packages/vite/types/customEvent.d.ts index d5bdbde98984fb..839e17dd729eda 100644 --- a/packages/vite/types/customEvent.d.ts +++ b/packages/vite/types/customEvent.d.ts @@ -1,5 +1,21 @@ -export type { - CustomEventMap, - InferCustomEventPayload, - InvalidatePayload -} from '../client/types' +import type { + ErrorPayload, + FullReloadPayload, + PrunePayload, + UpdatePayload +} from './hmrPayload' + +export interface CustomEventMap { + 'vite:beforeUpdate': UpdatePayload + 'vite:beforePrune': PrunePayload + 'vite:beforeFullReload': FullReloadPayload + 'vite:error': ErrorPayload + 'vite:invalidate': InvalidatePayload +} + +export interface InvalidatePayload { + path: string +} + +export type InferCustomEventPayload = + T extends keyof CustomEventMap ? CustomEventMap[T] : any diff --git a/packages/vite/types/hmrPayload.d.ts b/packages/vite/types/hmrPayload.d.ts index 8bbb5e2fdf3e57..839095009e76fb 100644 --- a/packages/vite/types/hmrPayload.d.ts +++ b/packages/vite/types/hmrPayload.d.ts @@ -1,10 +1,61 @@ -export type { - HMRPayload, - ConnectedPayload, - UpdatePayload, - Update, - PrunePayload, - FullReloadPayload, - CustomPayload, - ErrorPayload -} from '../client/types' +export type HMRPayload = + | ConnectedPayload + | UpdatePayload + | FullReloadPayload + | CustomPayload + | ErrorPayload + | PrunePayload + +export interface ConnectedPayload { + type: 'connected' +} + +export interface UpdatePayload { + type: 'update' + updates: Update[] +} + +export interface Update { + type: 'js-update' | 'css-update' + path: string + acceptedPath: string + timestamp: number + /** + * @experimental internal + */ + explicitImportRequired?: boolean | undefined +} + +export interface PrunePayload { + type: 'prune' + paths: string[] +} + +export interface FullReloadPayload { + type: 'full-reload' + path?: string +} + +export interface CustomPayload { + type: 'custom' + event: string + data?: any +} + +export interface ErrorPayload { + type: 'error' + err: { + [name: string]: any + message: string + stack: string + id?: string + frame?: string + plugin?: string + pluginCode?: string + loc?: { + file?: string + line: number + column: number + } + } +} diff --git a/packages/vite/types/hot.d.ts b/packages/vite/types/hot.d.ts index 83727b7019997e..985f3a47197c43 100644 --- a/packages/vite/types/hot.d.ts +++ b/packages/vite/types/hot.d.ts @@ -1 +1,33 @@ -export type { ModuleNamespace, ViteHotContext } from '../client/types' +import type { InferCustomEventPayload } from './customEvent' + +export type ModuleNamespace = Record & { + [Symbol.toStringTag]: 'Module' +} + +export interface ViteHotContext { + readonly data: any + + accept(): void + accept(cb: (mod: ModuleNamespace | undefined) => void): void + accept(dep: string, cb: (mod: ModuleNamespace | undefined) => void): void + accept( + deps: readonly string[], + cb: (mods: Array) => void + ): void + + acceptExports(exportNames: string | readonly string[]): void + acceptExports( + exportNames: string | readonly string[], + cb: (mod: ModuleNamespace | undefined) => void + ): void + + dispose(cb: (data: any) => void): void + decline(): void + invalidate(): void + + on( + event: T, + cb: (payload: InferCustomEventPayload) => void + ): void + send(event: T, data?: InferCustomEventPayload): void +} diff --git a/packages/vite/types/importGlob.d.ts b/packages/vite/types/importGlob.d.ts index 7951020049d0dc..321e3804a7e09a 100644 --- a/packages/vite/types/importGlob.d.ts +++ b/packages/vite/types/importGlob.d.ts @@ -1,7 +1,97 @@ -export type { - ImportGlobOptions, - GeneralImportGlobOptions, - KnownAsTypeMap, - ImportGlobFunction, - ImportGlobEagerFunction -} from '../client/types' +export interface ImportGlobOptions< + Eager extends boolean, + AsType extends string +> { + /** + * Import type for the import url. + */ + as?: AsType + /** + * Import as static or dynamic + * + * @default false + */ + eager?: Eager + /** + * Import only the specific named export. Set to `default` to import the default export. + */ + import?: string + /** + * Custom queries + */ + query?: string | Record + /** + * Search files also inside `node_modules/` and hidden directories (e.g. `.git/`). This might have impact on performance. + * + * @default false + */ + exhaustive?: boolean +} + +export type GeneralImportGlobOptions = ImportGlobOptions + +export interface KnownAsTypeMap { + raw: string + url: string + worker: Worker +} + +export interface ImportGlobFunction { + /** + * Import a list of files with a glob pattern. + * + * Overload 1: No generic provided, infer the type from `eager` and `as` + */ + < + Eager extends boolean, + As extends string, + T = As extends keyof KnownAsTypeMap ? KnownAsTypeMap[As] : unknown + >( + glob: string | string[], + options?: ImportGlobOptions + ): (Eager extends true ? true : false) extends true + ? Record + : Record Promise> + /** + * Import a list of files with a glob pattern. + * + * Overload 2: Module generic provided, infer the type from `eager: false` + */ + ( + glob: string | string[], + options?: ImportGlobOptions + ): Record Promise> + /** + * Import a list of files with a glob pattern. + * + * Overload 3: Module generic provided, infer the type from `eager: true` + */ + ( + glob: string | string[], + options: ImportGlobOptions + ): Record +} + +export interface ImportGlobEagerFunction { + /** + * Eagerly import a list of files with a glob pattern. + * + * Overload 1: No generic provided, infer the type from `as` + */ + < + As extends string, + T = As extends keyof KnownAsTypeMap ? KnownAsTypeMap[As] : unknown + >( + glob: string | string[], + options?: Omit, 'eager'> + ): Record + /** + * Eagerly import a list of files with a glob pattern. + * + * Overload 2: Module generic provided + */ + ( + glob: string | string[], + options?: Omit, 'eager'> + ): Record +} diff --git a/packages/vite/types/importMeta.d.ts b/packages/vite/types/importMeta.d.ts index 97a9518f5d8e4f..da338b6833bed6 100644 --- a/packages/vite/types/importMeta.d.ts +++ b/packages/vite/types/importMeta.d.ts @@ -1,13 +1,28 @@ -import type { - ImportMeta as ViteImportMeta, - ImportMetaEnv as ViteImportMetaEnv -} from '../client/types' - -declare global { - interface GlobOptions { - as?: string - } - - interface ImportMeta extends ViteImportMeta {} - interface ImportMetaEnv extends ViteImportMetaEnv {} +// This file is an augmentation to the built-in ImportMeta interface +// Thus cannot contain any top-level imports +// + +/* eslint-disable @typescript-eslint/consistent-type-imports */ + +interface ImportMetaEnv { + [key: string]: any + BASE_URL: string + MODE: string + DEV: boolean + PROD: boolean + SSR: boolean +} + +interface ImportMeta { + url: string + + readonly hot?: import('./hot').ViteHotContext + + readonly env: ImportMetaEnv + + glob: import('./importGlob').ImportGlobFunction + /** + * @deprecated Use `import.meta.glob('*', { eager: true })` instead + */ + globEager: import('./importGlob').ImportGlobEagerFunction } diff --git a/playground/hmr/event.d.ts b/playground/hmr/event.d.ts index 5b4818294ed744..151a9cc3b861cd 100644 --- a/playground/hmr/event.d.ts +++ b/playground/hmr/event.d.ts @@ -1,16 +1,9 @@ -import 'vite' -import 'vite/client/types' +import 'vite/types/customEvent' -interface MyCustomEventMap { - 'custom:foo': { msg: string } - 'custom:remote-add': { a: number; b: number } - 'custom:remote-add-result': { result: string } -} - -declare module 'vite' { - interface CustomEventMap extends MyCustomEventMap {} -} - -declare module 'vite/client/types' { - interface CustomEventMap extends MyCustomEventMap {} +declare module 'vite/types/customEvent' { + interface CustomEventMap { + 'custom:foo': { msg: string } + 'custom:remote-add': { a: number; b: number } + 'custom:remote-add-result': { result: string } + } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ab25134fb26652..21b4f10a506cf5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -231,7 +231,7 @@ importers: cors: ^2.8.5 cross-spawn: ^7.0.3 debug: ^4.3.4 - dep-types: link:./src/dep-types + dep-types: link:./src/types dotenv: ^14.3.2 dotenv-expand: ^5.1.0 es-module-lexer: ^1.0.3 @@ -267,7 +267,7 @@ importers: strip-literal: ^0.4.2 tsconfck: ^2.0.1 tslib: ^2.4.0 - types: link:./src/types + types: link:./types ufo: ^0.8.5 ws: ^8.9.0 dependencies: @@ -298,7 +298,7 @@ importers: cors: 2.8.5 cross-spawn: 7.0.3 debug: 4.3.4 - dep-types: link:src/dep-types + dep-types: link:src/types dotenv: 14.3.2 dotenv-expand: 5.1.0 es-module-lexer: 1.0.3 @@ -329,7 +329,7 @@ importers: strip-literal: 0.4.2 tsconfck: 2.0.1 tslib: 2.4.0 - types: link:src/types + types: link:types ufo: 0.8.5 ws: 8.9.0