Skip to content

Commit

Permalink
Attempt at bundling the types
Browse files Browse the repository at this point in the history
  • Loading branch information
garronej committed Feb 20, 2024
1 parent da4c6d9 commit 10a36c5
Show file tree
Hide file tree
Showing 11 changed files with 522 additions and 1 deletion.
242 changes: 242 additions & 0 deletions client.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
/// <reference path="./types/importMeta.d.ts" />

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

declare module "*.module.css" {
const classes: CSSModuleClasses;
export default classes;
}
declare module "*.module.scss" {
const classes: CSSModuleClasses;
export default classes;
}
declare module "*.module.sass" {
const classes: CSSModuleClasses;
export default classes;
}
declare module "*.module.less" {
const classes: CSSModuleClasses;
export default classes;
}
declare module "*.module.styl" {
const classes: CSSModuleClasses;
export default classes;
}
declare module "*.module.stylus" {
const classes: CSSModuleClasses;
export default classes;
}
declare module "*.module.pcss" {
const classes: CSSModuleClasses;
export default classes;
}
declare module "*.module.sss" {
const classes: CSSModuleClasses;
export default classes;
}

// CSS
declare module "*.css" {}
declare module "*.scss" {}
declare module "*.sass" {}
declare module "*.less" {}
declare module "*.styl" {}
declare module "*.stylus" {}
declare module "*.pcss" {}
declare module "*.sss" {}

// Built-in asset types
// see `src/node/constants.ts`

// images
declare module "*.apng" {
const src: string;
export default src;
}
declare module "*.png" {
const src: string;
export default src;
}
declare module "*.jpg" {
const src: string;
export default src;
}
declare module "*.jpeg" {
const src: string;
export default src;
}
declare module "*.jfif" {
const src: string;
export default src;
}
declare module "*.pjpeg" {
const src: string;
export default src;
}
declare module "*.pjp" {
const src: string;
export default src;
}
declare module "*.gif" {
const src: string;
export default src;
}
declare module "*.svg" {
const src: string;
export default src;
}
declare module "*.ico" {
const src: string;
export default src;
}
declare module "*.webp" {
const src: string;
export default src;
}
declare module "*.avif" {
const src: string;
export default src;
}

// media
declare module "*.mp4" {
const src: string;
export default src;
}
declare module "*.webm" {
const src: string;
export default src;
}
declare module "*.ogg" {
const src: string;
export default src;
}
declare module "*.mp3" {
const src: string;
export default src;
}
declare module "*.wav" {
const src: string;
export default src;
}
declare module "*.flac" {
const src: string;
export default src;
}
declare module "*.aac" {
const src: string;
export default src;
}
declare module "*.opus" {
const src: string;
export default src;
}
declare module "*.mov" {
const src: string;
export default src;
}
declare module "*.m4a" {
const src: string;
export default src;
}
declare module "*.vtt" {
const src: string;
export default src;
}

// fonts
declare module "*.woff" {
const src: string;
export default src;
}
declare module "*.woff2" {
const src: string;
export default src;
}
declare module "*.eot" {
const src: string;
export default src;
}
declare module "*.ttf" {
const src: string;
export default src;
}
declare module "*.otf" {
const src: string;
export default src;
}

// other
declare module "*.webmanifest" {
const src: string;
export default src;
}
declare module "*.pdf" {
const src: string;
export default src;
}
declare module "*.txt" {
const src: string;
export default src;
}

// wasm?init
declare module "*.wasm?init" {
const initWasm: (options?: WebAssembly.Imports) => Promise<WebAssembly.Instance>;
export default initWasm;
}

// web worker
declare module "*?worker" {
const workerConstructor: {
new (options?: { name?: string }): Worker;
};
export default workerConstructor;
}

declare module "*?worker&inline" {
const workerConstructor: {
new (options?: { name?: string }): Worker;
};
export default workerConstructor;
}

declare module "*?worker&url" {
const src: string;
export default src;
}

declare module "*?sharedworker" {
const sharedWorkerConstructor: {
new (options?: { name?: string }): SharedWorker;
};
export default sharedWorkerConstructor;
}

declare module "*?sharedworker&inline" {
const sharedWorkerConstructor: {
new (options?: { name?: string }): SharedWorker;
};
export default sharedWorkerConstructor;
}

declare module "*?sharedworker&url" {
const src: string;
export default src;
}

declare module "*?raw" {
const src: string;
export default src;
}

declare module "*?url" {
const src: string;
export default src;
}

declare module "*?inline" {
const src: string;
export default src;
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"types": "dist/index.d.ts",
"exports": {
".": "./dist/index.js",
"./*": "./dist/*.js"
"./*": "./dist/*.js",
"./client": "./client.d.ts"
},
"lint-staged": {
"*.{ts,tsx,json,md}": [
Expand Down
5 changes: 5 additions & 0 deletions scripts/link-in-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ fs.writeFileSync(
)
);

fs.writeFileSync(
pathJoin(pathJoin(__dirname, "client.d.ts")),
Buffer.from(["// Replaced at runtime", "", "export type ImportMetaEnv ={};"].join("\n"), "utf8")
);

const commonThirdPartyDeps = (() => {
// For example [ "@emotion" ] it's more convenient than
// having to list every sub emotion packages (@emotion/css @emotion/utils ...)
Expand Down
31 changes: 31 additions & 0 deletions types/customEvent.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { ErrorPayload, FullReloadPayload, PrunePayload, UpdatePayload } from "./hmrPayload";

export interface CustomEventMap {
"vite:beforeUpdate": UpdatePayload;
"vite:afterUpdate": UpdatePayload;
"vite:beforePrune": PrunePayload;
"vite:beforeFullReload": FullReloadPayload;
"vite:error": ErrorPayload;
"vite:invalidate": InvalidatePayload;
"vite:ws:connect": WebSocketConnectionPayload;
"vite:ws:disconnect": WebSocketConnectionPayload;
}

export interface WebSocketConnectionPayload {
/**
* @experimental
* We expose this instance experimentally to see potential usage.
* This might be removed in the future if we didn't find reasonable use cases.
* If you find this useful, please open an issue with details so we can discuss and make it stable API.
*/
webSocket: WebSocket;
}

export interface InvalidatePayload {
path: string;
message: string | undefined;
}

export type InferCustomEventPayload<T extends string> = T extends keyof CustomEventMap
? CustomEventMap[T]
: any;
63 changes: 63 additions & 0 deletions types/hmrPayload.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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;
/** @internal */
explicitImportRequired?: boolean;
/** @internal */
isWithinCircularImport?: boolean;
/** @internal */
ssrInvalidates?: string[];
}

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;
};
};
}
27 changes: 27 additions & 0 deletions types/hot.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { InferCustomEventPayload } from "./customEvent";

export type ModuleNamespace = Record<string, any> & {
[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<ModuleNamespace | undefined>) => void): void;

acceptExports(
exportNames: string | readonly string[],
cb?: (mod: ModuleNamespace | undefined) => void
): void;

dispose(cb: (data: any) => void): void;
prune(cb: (data: any) => void): void;
invalidate(message?: string): void;

on<T extends string>(event: T, cb: (payload: InferCustomEventPayload<T>) => void): void;
off<T extends string>(event: T, cb: (payload: InferCustomEventPayload<T>) => void): void;
send<T extends string>(event: T, data?: InferCustomEventPayload<T>): void;
}
5 changes: 5 additions & 0 deletions types/import-meta.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference path="./importMeta.d.ts" />

// https://github.com/microsoft/TypeScript/issues/45096
// TypeScript has a bug that makes <reference types="vite/types/importMeta" />
// not possible in userland. This file provides a workaround for now.
Loading

0 comments on commit 10a36c5

Please sign in to comment.