module-runner.d.ts 11 KB

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