module-runner.d.ts 10 KB

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