市场夺宝奇兵
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.

306 lines
11 KiB

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