You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

288 lines
10 KiB

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