module-runner.d.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 isCircularRequest;
  134. private cachedRequest;
  135. private cachedModule;
  136. private ensureBuiltins;
  137. private getModuleInformation;
  138. protected directRequest(url: string, mod: EvaluatedModuleNode, _callstack: string[]): Promise<any>;
  139. }
  140. //#endregion
  141. //#region src/module-runner/sourcemap/interceptor.d.ts
  142. interface RetrieveFileHandler {
  143. (path: string): string | null | undefined | false;
  144. }
  145. interface RetrieveSourceMapHandler {
  146. (path: string): null | {
  147. url: string;
  148. map: any;
  149. };
  150. }
  151. interface InterceptorOptions {
  152. retrieveFile?: RetrieveFileHandler;
  153. retrieveSourceMap?: RetrieveSourceMapHandler;
  154. }
  155. //#endregion
  156. //#region src/module-runner/types.d.ts
  157. interface ModuleRunnerImportMeta {
  158. url: string;
  159. env: ImportMetaEnv;
  160. hot?: ViteHotContext;
  161. dirname: string;
  162. filename: string;
  163. glob: (...args: any[]) => any;
  164. resolve(specifier: string, parent?: string): string;
  165. [key: string]: any;
  166. }
  167. interface ModuleRunnerContext {
  168. [ssrModuleExportsKey]: Record<string, any>;
  169. [ssrImportKey]: (id: string, metadata?: DefineImportMetadata) => Promise<any>;
  170. [ssrDynamicImportKey]: (id: string, options?: ImportCallOptions) => Promise<any>;
  171. [ssrExportAllKey]: (obj: any) => void;
  172. [ssrExportNameKey]: (name: string, getter: () => unknown) => void;
  173. [ssrImportMetaKey]: ModuleRunnerImportMeta;
  174. }
  175. interface ModuleEvaluator {
  176. /**
  177. * Number of prefixed lines in the transformed code.
  178. */
  179. startOffset?: number;
  180. /**
  181. * Run code that was transformed by Vite.
  182. * @param context Function context
  183. * @param code Transformed code
  184. * @param module The module node
  185. */
  186. runInlinedModule(context: ModuleRunnerContext, code: string, module: Readonly<EvaluatedModuleNode>): Promise<any>;
  187. /**
  188. * Run externalized module.
  189. * @param file File URL to the external module
  190. */
  191. runExternalModule(file: string): Promise<any>;
  192. }
  193. type ResolvedResult = (ExternalFetchResult | ViteFetchResult) & {
  194. url: string;
  195. id: string;
  196. };
  197. type FetchFunction = (id: string, importer?: string, options?: FetchFunctionOptions) => Promise<FetchResult>;
  198. interface ModuleRunnerHmr {
  199. /**
  200. * Configure HMR logger.
  201. */
  202. logger?: false | HMRLogger;
  203. }
  204. interface ModuleRunnerOptions {
  205. /**
  206. * A set of methods to communicate with the server.
  207. */
  208. transport: ModuleRunnerTransport;
  209. /**
  210. * Configure how source maps are resolved. Prefers `node` if `process.setSourceMapsEnabled` is available.
  211. * Otherwise it will use `prepareStackTrace` by default which overrides `Error.prepareStackTrace` method.
  212. * You can provide an object to configure how file contents and source maps are resolved for files that were not processed by Vite.
  213. */
  214. sourcemapInterceptor?: false | "node" | "prepareStackTrace" | InterceptorOptions;
  215. /**
  216. * Disable HMR or configure HMR options.
  217. *
  218. * @default true
  219. */
  220. hmr?: boolean | ModuleRunnerHmr;
  221. /**
  222. * Create import.meta object for the module.
  223. *
  224. * @default createDefaultImportMeta
  225. */
  226. createImportMeta?: (modulePath: string) => ModuleRunnerImportMeta | Promise<ModuleRunnerImportMeta>;
  227. /**
  228. * Custom module cache. If not provided, creates a separate module cache for each ModuleRunner instance.
  229. */
  230. evaluatedModules?: EvaluatedModules;
  231. }
  232. interface ImportMetaEnv {
  233. [key: string]: any;
  234. BASE_URL: string;
  235. MODE: string;
  236. DEV: boolean;
  237. PROD: boolean;
  238. SSR: boolean;
  239. }
  240. //#endregion
  241. //#region src/module-runner/evaluatedModules.d.ts
  242. declare class EvaluatedModuleNode {
  243. id: string;
  244. url: string;
  245. importers: Set<string>;
  246. imports: Set<string>;
  247. evaluated: boolean;
  248. meta: ResolvedResult | undefined;
  249. promise: Promise<any> | undefined;
  250. exports: any;
  251. file: string;
  252. map: DecodedMap | undefined;
  253. constructor(id: string, url: string);
  254. }
  255. declare class EvaluatedModules {
  256. readonly idToModuleMap: Map<string, EvaluatedModuleNode>;
  257. readonly fileToModulesMap: Map<string, Set<EvaluatedModuleNode>>;
  258. readonly urlToIdModuleMap: Map<string, EvaluatedModuleNode>;
  259. /**
  260. * Returns the module node by the resolved module ID. Usually, module ID is
  261. * the file system path with query and/or hash. It can also be a virtual module.
  262. *
  263. * Module runner graph will have 1 to 1 mapping with the server module graph.
  264. * @param id Resolved module ID
  265. */
  266. getModuleById(id: string): EvaluatedModuleNode | undefined;
  267. /**
  268. * Returns all modules related to the file system path. Different modules
  269. * might have different query parameters or hash, so it's possible to have
  270. * multiple modules for the same file.
  271. * @param file The file system path of the module
  272. */
  273. getModulesByFile(file: string): Set<EvaluatedModuleNode> | undefined;
  274. /**
  275. * Returns the module node by the URL that was used in the import statement.
  276. * Unlike module graph on the server, the URL is not resolved and is used as is.
  277. * @param url Server URL that was used in the import statement
  278. */
  279. getModuleByUrl(url: string): EvaluatedModuleNode | undefined;
  280. /**
  281. * Ensure that module is in the graph. If the module is already in the graph,
  282. * it will return the existing module node. Otherwise, it will create a new
  283. * module node and add it to the graph.
  284. * @param id Resolved module ID
  285. * @param url URL that was used in the import statement
  286. */
  287. ensureModule(id: string, url: string): EvaluatedModuleNode;
  288. invalidateModule(node: EvaluatedModuleNode): void;
  289. /**
  290. * Extracts the inlined source map from the module code and returns the decoded
  291. * source map. If the source map is not inlined, it will return null.
  292. * @param id Resolved module ID
  293. */
  294. getModuleSourceMapById(id: string): DecodedMap | null;
  295. clear(): void;
  296. }
  297. declare function normalizeModuleId(file: string): string;
  298. //#endregion
  299. //#region src/module-runner/esmEvaluator.d.ts
  300. declare class ESModulesEvaluator implements ModuleEvaluator {
  301. readonly startOffset: number;
  302. runInlinedModule(context: ModuleRunnerContext, code: string): Promise<any>;
  303. runExternalModule(filepath: string): Promise<any>;
  304. }
  305. //#endregion
  306. //#region src/module-runner/createImportMeta.d.ts
  307. declare function createDefaultImportMeta(modulePath: string): ModuleRunnerImportMeta;
  308. /**
  309. * Create import.meta object for Node.js.
  310. */
  311. declare function createNodeImportMeta(modulePath: string): ModuleRunnerImportMeta;
  312. //#endregion
  313. 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 };