Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(types): bundle client types #9966

Merged
merged 9 commits into from
Sep 24, 2022
Prev Previous commit
Next Next commit
refactor(types): bundle client types
  • Loading branch information
sapphi-red committed Sep 2, 2022
commit 22bad14370c478f07ba57d76adb66e3f6f6c06d0
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
dist
playground-temp
temp

packages/vite/client/types.d.ts
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*.local
*.log
/.vscode/
/packages/vite/client/types.d.ts
/packages/vite/LICENSE
dist
dist-ssr
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ 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
4 changes: 2 additions & 2 deletions docs/guide/api-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -595,9 +595,9 @@ It is possible to type custom events by extending the `CustomEventMap` interface

```ts
// events.d.ts
import 'vite/types/customEvent'
import 'vite/client/types'

declare module 'vite/types/customEvent' {
declare module 'vite/client/types' {
interface CustomEventMap {
'custom:foo': { msg: string }
// 'event-key': payload
Expand Down
54 changes: 54 additions & 0 deletions packages/vite/api-extractor.client.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"$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"
}
}
}
}
2 changes: 1 addition & 1 deletion packages/vite/client.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// <reference path="./types/importMeta.d.ts" />
/// <reference path="./import-meta.d.ts" />

// CSS modules
type CSSModuleClasses = { readonly [key: string]: string }
Expand Down
9 changes: 9 additions & 0 deletions packages/vite/import-meta.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type {
ImportMeta as ViteImportMeta,
ImportMetaEnv as ViteImportMetaEnv
} from './client/types'

declare global {
interface ImportMeta extends ViteImportMeta {}
interface ImportMetaEnv extends ViteImportMetaEnv {}
}
19 changes: 13 additions & 6 deletions packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,21 @@
"./client": {
"types": "./client.d.ts"
},
"./client/types": {
"types": "./client/types.d.ts"
},
"./dist/client/*": "./dist/client/*",
"./package.json": "./package.json"
},
"files": [
"bin",
"dist",
"client.d.ts",
"import-meta.d.ts",
"index.cjs",
"src/client",
"types"
"types",
"client/types.d.ts"
],
"engines": {
"node": "^14.18.0 || >=16.0.0"
Expand All @@ -47,11 +52,13 @@
"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-s build-temp-types patch-types roll-types check-dist-types",
"build-temp-types": "tsc --emitDeclarationOnly --outDir temp/node -p src/node",
"patch-types": "tsx scripts/patchTypes.ts",
"roll-types": "api-extractor run && rimraf temp",
"check-dist-types": "tsc --project tsconfig.check.json",
"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",
"lint": "eslint --cache --ext .ts src/**",
"format": "prettier --write --cache --parser typescript \"src/**/*.ts\"",
"prepublishOnly": "npm run build"
Expand Down
23 changes: 23 additions & 0 deletions packages/vite/src/client-types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export type {
CustomEventMap,
InferCustomEventPayload
} 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'
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// 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\\";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\\")});
"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\\")});
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});
Expand All @@ -20,8 +19,7 @@ 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\\";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\\")});
"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\\")});
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});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import '../../../../../../types/importMeta'
import type { ImportMeta as ViteImportMeta } from 'types/importMeta'

declare global {
interface ImportMeta extends ViteImportMeta {}
}

export interface ModuleType {
name: string
Expand Down
36 changes: 13 additions & 23 deletions packages/vite/src/types/importMeta.d.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,25 @@
// This file is an augmentation to the built-in ImportMeta interface
// Thus cannot contain any top-level imports
// <https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation>
import type { ViteHotContext } from './hot'
import type { ImportGlobEagerFunction, ImportGlobFunction } from './importGlob'

/* eslint-disable @typescript-eslint/consistent-type-imports */

// Duplicate of import('../src/node/importGlob').GlobOptions in order to
// avoid breaking the production client type. Because this file is referenced
// in vite/client.d.ts and in production src/node/importGlob.ts doesn't exist.
interface GlobOptions {
as?: string
export interface ImportMetaEnv {
[key: string]: any
BASE_URL: string
MODE: string
DEV: boolean
PROD: boolean
SSR: boolean
}

interface ImportMeta {
export interface ImportMeta {
url: string

readonly hot?: import('./hot').ViteHotContext
readonly hot?: ViteHotContext

readonly env: ImportMetaEnv

glob: import('./importGlob').ImportGlobFunction
glob: ImportGlobFunction
/**
* @deprecated Use `import.meta.glob('*', { eager: true })` instead
*/
globEager: import('./importGlob').ImportGlobEagerFunction
}

interface ImportMetaEnv {
[key: string]: any
BASE_URL: string
MODE: string
DEV: boolean
PROD: boolean
SSR: boolean
globEager: ImportGlobEagerFunction
}
4 changes: 2 additions & 2 deletions playground/hmr/event.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'vite/types/customEvent'
import 'vite/client/types'

declare module 'vite/types/customEvent' {
declare module 'vite/client/types' {
interface CustomEventMap {
'custom:foo': { msg: string }
'custom:remote-add': { a: number; b: number }
Expand Down