module-runner.d.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. import { a as ExternalFetchResult, c as ViteFetchResult, i as createWebSocketModuleRunnerTransport, n as ModuleRunnerTransportHandlers, o as FetchFunctionOptions, r as NormalizedModuleRunnerTransport, s as FetchResult, t as ModuleRunnerTransport } from "./chunks/moduleRunnerTransport.js";
  2. import { ModuleNamespace, ViteHotContext } from "#types/hot";
  3. import { HotPayload, Update } from "#types/hmrPayload";
  4. import { InferCustomEventPayload } from "#types/customEvent";
  5. //#region src/module-runner/sourcemap/decoder.d.ts
  6. interface SourceMapLike {
  7. version: number;
  8. mappings?: string;
  9. names?: string[];
  10. sources?: string[];
  11. sourcesContent?: string[];
  12. }
  13. declare class DecodedMap {
  14. map: SourceMapLike;
  15. _encoded: string;
  16. _decoded: undefined | number[][][];
  17. _decodedMemo: Stats;
  18. url: string;
  19. file: string;
  20. version: number;
  21. names: string[];
  22. resolvedSources: string[];
  23. constructor(map: SourceMapLike, from: string);
  24. }
  25. interface Stats {
  26. lastKey: number;
  27. lastNeedle: number;
  28. lastIndex: number;
  29. }
  30. //#endregion
  31. //#region src/shared/hmr.d.ts
  32. type CustomListenersMap = Map<string, ((data: any) => void)[]>;
  33. interface HotModule {
  34. id: string;
  35. callbacks: HotCallback[];
  36. }
  37. interface HotCallback {
  38. deps: string[];
  39. fn: (modules: Array<ModuleNamespace | undefined>) => void;
  40. }
  41. interface HMRLogger {
  42. error(msg: string | Error): void;
  43. debug(...msg: unknown[]): void;
  44. }
  45. declare class HMRClient {
  46. logger: HMRLogger;
  47. private transport;
  48. private importUpdatedModule;
  49. hotModulesMap: Map<string, HotModule>;
  50. disposeMap: Map<string, (data: any) => void | Promise<void>>;
  51. pruneMap: Map<string, (data: any) => void | Promise<void>>;
  52. dataMap: Map<string, any>;
  53. customListenersMap: CustomListenersMap;
  54. ctxToListenersMap: Map<string, CustomListenersMap>;
  55. currentFirstInvalidatedBy: string | undefined;
  56. constructor(logger: HMRLogger, transport: NormalizedModuleRunnerTransport, importUpdatedModule: (update: Update) => Promise<ModuleNamespace>);
  57. notifyListeners<T extends string>(event: T, data: InferCustomEventPayload<T>): Promise<void>;
  58. send(payload: HotPayload): void;
  59. clear(): void;
  60. prunePaths(paths: string[]): Promise<void>;
  61. protected warnFailedUpdate(err: Error, path: string | string[]): void;
  62. private updateQueue;
  63. private pendingUpdateQueue;
  64. /**
  65. * buffer multiple hot updates triggered by the same src change
  66. * so that they are invoked in the same order they were sent.
  67. * (otherwise the order may be inconsistent because of the http request round trip)
  68. */
  69. queueUpdate(payload: Update): Promise<void>;
  70. private fetchUpdate;
  71. }
  72. //#endregion
  73. //#region src/shared/ssrTransform.d.ts
  74. interface DefineImportMetadata {
  75. /**
  76. * Imported names before being transformed to `ssrImportKey`
  77. *
  78. * import foo, { bar as baz, qux } from 'hello'
  79. * => ['default', 'bar', 'qux']
  80. *
  81. * import * as namespace from 'world
  82. * => undefined
  83. */
  84. importedNames?: string[];
  85. }
  86. interface SSRImportMetadata extends DefineImportMetadata {
  87. isDynamicImport?: boolean;
  88. }
  89. //#endregion
  90. //#region src/module-runner/constants.d.ts
  91. declare const ssrModuleExportsKey = "__vite_ssr_exports__";
  92. declare const ssrImportKey = "__vite_ssr_import__";
  93. declare const ssrDynamicImportKey = "__vite_ssr_dynamic_import__";
  94. declare const ssrExportAllKey = "__vite_ssr_exportAll__";
  95. declare const ssrExportNameKey = "__vite_ssr_exportName__";
  96. declare const ssrImportMetaKey = "__vite_ssr_import_meta__";
  97. //#endregion
  98. //#region src/module-runner/runner.d.ts
  99. interface ModuleRunnerDebugger {
  100. (formatter: unknown, ...args: unknown[]): void;
  101. }
  102. declare class ModuleRunner {
  103. options: ModuleRunnerOptions;
  104. evaluator: ModuleEvaluator;
  105. private debug?;
  106. evaluatedModules: EvaluatedModules;
  107. hmrClient?: HMRClient;
  108. private readonly transport;
  109. private readonly resetSourceMapSupport?;
  110. private readonly concurrentModuleNodePromises;
  111. private isBuiltin?;
  112. private builtinsPromise?;
  113. private closed;
  114. constructor(options: ModuleRunnerOptions, evaluator?: ModuleEvaluator, debug?: ModuleRunnerDebugger | undefined);
  115. /**
  116. * URL to execute. Accepts file path, server path or id relative to the root.
  117. */
  118. import<T = any>(url: string): Promise<T>;
  119. /**
  120. * Clear all caches including HMR listeners.
  121. */
  122. clearCache(): void;
  123. /**
  124. * Clears all caches, removes all HMR listeners, and resets source map support.
  125. * This method doesn't stop the HMR connection.
  126. */
  127. close(): Promise<void>;
  128. /**
  129. * Returns `true` if the runtime has been closed by calling `close()` method.
  130. */
  131. isClosed(): boolean;
  132. private processImport;
  133. private isCircularModule;
  134. private isCircularImport;
  135. private cachedRequest;
  136. private cachedModule;
  137. private ensureBuiltins;
  138. private getModuleInformation;
  139. protected directRequest(url: string, mod: EvaluatedModuleNode, _callstack: string[]): Promise<any>;
  140. }
  141. //#endregion
  142. //#region src/module-runner/sourcemap/interceptor.d.ts
  143. interface RetrieveFileHandler {
  144. (path: string): string | null | undefined | false;
  145. }
  146. interface RetrieveSourceMapHandler {
  147. (path: string): null | {
  148. url: string;
  149. map: any;
  150. };
  151. }
  152. interface InterceptorOptions {
  153. retrieveFile?: RetrieveFileHandler;
  154. retrieveSourceMap?: RetrieveSourceMapHandler;
  155. }
  156. //#endregion
  157. //#region src/module-runner/types.d.ts
  158. interface ModuleRunnerImportMeta {
  159. url: string;
  160. env: ImportMetaEnv;
  161. hot?: ViteHotContext;
  162. dirname: string;
  163. filename: string;
  164. glob: (...args: any[]) => any;
  165. resolve(specifier: string, parent?: string): string;
  166. [key: string]: any;
  167. }
  168. interface ModuleRunnerContext {
  169. [ssrModuleExportsKey]: Record<string, any>;
  170. [ssrImportKey]: (id: string, metadata?: DefineImportMetadata) => Promise<any>;
  171. [ssrDynamicImportKey]: (id: string, options?: ImportCallOptions) => Promise<any>;
  172. [ssrExportAllKey]: (obj: any) => void;
  173. [ssrExportNameKey]: (name: string, getter: () => unknown) => void;
  174. [ssrImportMetaKey]: ModuleRunnerImportMeta;
  175. }
  176. interface ModuleEvaluator {
  177. /**
  178. * Number of prefixed lines in the transformed code.
  179. */
  180. startOffset?: number;
  181. /**
  182. * Run code that was transformed by Vite.
  183. * @param context Function context
  184. * @param code Transformed code
  185. * @param module The module node
  186. */
  187. runInlinedModule(context: ModuleRunnerContext, code: string, module: Readonly<EvaluatedModuleNode>): Promise<any>;
  188. /**
  189. * Run externalized module.
  190. * @param file File URL to the external module
  191. */
  192. runExternalModule(file: string): Promise<any>;
  193. }
  194. type ResolvedResult = (ExternalFetchResult | ViteFetchResult) & {
  195. url: string;
  196. id: string;
  197. };
  198. type FetchFunction = (id: string, importer?: string, options?: FetchFunctionOptions) => Promise<FetchResult>;
  199. interface ModuleRunnerHmr {
  200. /**
  201. * Configure HMR logger.
  202. */
  203. logger?: false | HMRLogger;
  204. }
  205. interface ModuleRunnerOptions {
  206. /**
  207. * A set of methods to communicate with the server.
  208. */
  209. transport: ModuleRunnerTransport;
  210. /**
  211. * Configure how source maps are resolved. Prefers `node` if `process.setSourceMapsEnabled` is available.
  212. * Otherwise it will use `prepareStackTrace` by default which overrides `Error.prepareStackTrace` method.
  213. * You can provide an object to configure how file contents and source maps are resolved for files that were not processed by Vite.
  214. */
  215. sourcemapInterceptor?: false | "node" | "prepareStackTrace" | InterceptorOptions;
  216. /**
  217. * Disable HMR or configure HMR options.
  218. *
  219. * @default true
  220. */
  221. hmr?: boolean | ModuleRunnerHmr;
  222. /**
  223. * Create import.meta object for the module.
  224. *
  225. * @default createDefaultImportMeta
  226. */
  227. createImportMeta?: (modulePath: string) => ModuleRunnerImportMeta | Promise<ModuleRunnerImportMeta>;
  228. /**
  229. * Custom module cache. If not provided, creates a separate module cache for each ModuleRunner instance.
  230. */
  231. evaluatedModules?: EvaluatedModules;
  232. }
  233. interface ImportMetaEnv {
  234. [key: string]: any;
  235. BASE_URL: string;
  236. MODE: string;
  237. DEV: boolean;
  238. PROD: boolean;
  239. SSR: boolean;
  240. }
  241. //#endregion
  242. //#region src/module-runner/evaluatedModules.d.ts
  243. declare class EvaluatedModuleNode {
  244. id: string;
  245. url: string;
  246. importers: Set<string>;
  247. imports: Set<string>;
  248. evaluated: boolean;
  249. meta: ResolvedResult | undefined;
  250. promise: Promise<any> | undefined;
  251. exports: any | undefined;
  252. file: string;
  253. map: DecodedMap | undefined;
  254. constructor(id: string, url: string);
  255. }
  256. declare class EvaluatedModules {
  257. readonly idToModuleMap: Map<string, EvaluatedModuleNode>;
  258. readonly fileToModulesMap: Map<string, Set<EvaluatedModuleNode>>;
  259. readonly urlToIdModuleMap: Map<string, EvaluatedModuleNode>;
  260. /**
  261. * Returns the module node by the resolved module ID. Usually, module ID is
  262. * the file system path with query and/or hash. It can also be a virtual module.
  263. *
  264. * Module runner graph will have 1 to 1 mapping with the server module graph.
  265. * @param id Resolved module ID
  266. */
  267. getModuleById(id: string): EvaluatedModuleNode | undefined;
  268. /**
  269. * Returns all modules related to the file system path. Different modules
  270. * might have different query parameters or hash, so it's possible to have
  271. * multiple modules for the same file.
  272. * @param file The file system path of the module
  273. */
  274. getModulesByFile(file: string): Set<EvaluatedModuleNode> | undefined;
  275. /**
  276. * Returns the module node by the URL that was used in the import statement.
  277. * Unlike module graph on the server, the URL is not resolved and is used as is.
  278. * @param url Server URL that was used in the import statement
  279. */
  280. getModuleByUrl(url: string): EvaluatedModuleNode | undefined;
  281. /**
  282. * Ensure that module is in the graph. If the module is already in the graph,
  283. * it will return the existing module node. Otherwise, it will create a new
  284. * module node and add it to the graph.
  285. * @param id Resolved module ID
  286. * @param url URL that was used in the import statement
  287. */
  288. ensureModule(id: string, url: string): EvaluatedModuleNode;
  289. invalidateModule(node: EvaluatedModuleNode): void;
  290. /**
  291. * Extracts the inlined source map from the module code and returns the decoded
  292. * source map. If the source map is not inlined, it will return null.
  293. * @param id Resolved module ID
  294. */
  295. getModuleSourceMapById(id: string): DecodedMap | null;
  296. clear(): void;
  297. }
  298. declare function normalizeModuleId(file: string): string;
  299. //#endregion
  300. //#region src/module-runner/esmEvaluator.d.ts
  301. declare class ESModulesEvaluator implements ModuleEvaluator {
  302. readonly startOffset: number;
  303. runInlinedModule(context: ModuleRunnerContext, code: string): Promise<any>;
  304. runExternalModule(filepath: string): Promise<any>;
  305. }
  306. //#endregion
  307. //#region src/module-runner/createImportMeta.d.ts
  308. declare function createDefaultImportMeta(modulePath: string): ModuleRunnerImportMeta;
  309. /**
  310. * Create import.meta object for Node.js.
  311. */
  312. declare function createNodeImportMeta(modulePath: string): ModuleRunnerImportMeta;
  313. //#endregion
  314. export { ESModulesEvaluator, type EvaluatedModuleNode, EvaluatedModules, type FetchFunction, type FetchFunctionOptions, type FetchResult, type HMRLogger, type InterceptorOptions, type ModuleEvaluator, ModuleRunner, type ModuleRunnerContext, type ModuleRunnerHmr, type ModuleRunnerImportMeta, type ModuleRunnerOptions, type ModuleRunnerTransport, type ModuleRunnerTransportHandlers, type ResolvedResult, type SSRImportMetadata, createDefaultImportMeta, createNodeImportMeta, createWebSocketModuleRunnerTransport, normalizeModuleId, ssrDynamicImportKey, ssrExportAllKey, ssrExportNameKey, ssrImportKey, ssrImportMetaKey, ssrModuleExportsKey };