experimental-runtime.mjs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. //#region ../../crates/rolldown/src/runtime/runtime-base.js
  2. var __create = Object.create;
  3. var __defProp = Object.defineProperty;
  4. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  5. var __getOwnPropNames = Object.getOwnPropertyNames;
  6. var __getProtoOf = Object.getPrototypeOf;
  7. var __hasOwnProp = Object.prototype.hasOwnProperty;
  8. var __exportAll = (all, no_symbols) => {
  9. let target = {};
  10. for (var name in all) __defProp(target, name, {
  11. get: all[name],
  12. enumerable: true
  13. });
  14. if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
  15. return target;
  16. };
  17. var __copyProps = (to, from, except, desc) => {
  18. if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
  19. key = keys[i];
  20. if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
  21. get: ((k) => from[k]).bind(null, key),
  22. enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
  23. });
  24. }
  25. return to;
  26. };
  27. var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
  28. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
  29. value: mod,
  30. enumerable: true
  31. }) : target, mod));
  32. var __toCommonJS = (mod) => __hasOwnProp.call(mod, "module.exports") ? mod["module.exports"] : __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  33. //#endregion
  34. //#region ../../crates/rolldown_plugin_hmr/src/runtime/runtime-extra-dev-common.js
  35. var Module = class {
  36. /**
  37. * @type {{ exports: any }}
  38. */
  39. exportsHolder = { exports: null };
  40. /**
  41. * @type {string}
  42. */
  43. id;
  44. /**
  45. * @param {string} id
  46. */
  47. constructor(id) {
  48. this.id = id;
  49. }
  50. get exports() {
  51. return this.exportsHolder.exports;
  52. }
  53. };
  54. /**
  55. * Compiler-emitted module-graph delta — topology (static + dynamic edges).
  56. * `ids[0, localCount)` are the modules this payload carries; `ids[localCount, …)` are foreign edge targets.
  57. * `edges[i]` / `dynamicEdges[i]` are the static / dynamic-`import()` out-edges of `ids[i]`.
  58. * `bindings[i][j]` are the export names `ids[i]` imports through `edges[i][j]`. `bindings` holds
  59. * only some rows; a missing row, or a missing or `null` entry, means the whole namespace.
  60. * `dynamicEdges` also holds only some rows; a missing row means no dynamic edges.
  61. * @typedef {{ ids: string[], localCount: number, edges: number[][], bindings?: Record<number, (string[] | null)[]>, dynamicEdges?: Record<number, number[]> }} ModuleGraphDelta
  62. * @typedef {{ createModuleHotContext(moduleId: string): any, onModuleCacheRemoval(moduleId: string): void }} DevRuntimeHooks
  63. */
  64. var MissingFactoryError = class extends Error {
  65. /**
  66. * @param {string} id
  67. */
  68. constructor(id) {
  69. super(`No factory registered for module ${id}`);
  70. this.id = id;
  71. }
  72. };
  73. var DevRuntime = class {
  74. /**
  75. * Client ID generated at runtime initialization, used for lazy compilation requests.
  76. * @type {string}
  77. */
  78. clientId;
  79. /**
  80. * @param {string} clientId
  81. */
  82. constructor(clientId) {
  83. this.clientId = clientId;
  84. }
  85. /**
  86. * Static import edges from `registerGraph` — entries persist across `removeModuleCache`
  87. * and change only by replacement from a newer payload (last write wins).
  88. * `bindings` is the payload row as sent; `getImportedBindings` resolves a missing entry.
  89. * @type {Map<string, { edges: string[], bindings: (string[] | null)[] | undefined }>}
  90. */
  91. staticImports = /* @__PURE__ */ new Map();
  92. /**
  93. * Reverse index over the static imports.
  94. * @type {Map<string, Set<string>>}
  95. */
  96. importers = /* @__PURE__ */ new Map();
  97. /**
  98. * Dynamic `import()` edges from `registerGraph`, keyed by importer — mirror of
  99. * `staticImports` for the dynamic reverse index.
  100. * @type {Map<string, { edges: string[] }>}
  101. */
  102. dynamicImports = /* @__PURE__ */ new Map();
  103. /**
  104. * Reverse index over the dynamic imports.
  105. * @type {Map<string, Set<string>>}
  106. */
  107. dynamicImporters = /* @__PURE__ */ new Map();
  108. /**
  109. * The module cache. Membership means "this module's side effects ran in this tab" —
  110. * registration is emitted ahead of every module body, and nothing un-registers on
  111. * unwind, so a factory that throws mid-body stays registered. A `Map` rather than a
  112. * plain object: HMR eviction deletes entries, and a `delete` on an object drops V8
  113. * into dictionary mode, taxing every later lookup on the hottest read path.
  114. * @type {Map<string, Module>}
  115. */
  116. moduleCache = /* @__PURE__ */ new Map();
  117. /**
  118. * Re-runnable factories from HMR patches and lazy chunks. The initial bundle stays
  119. * scope-hoisted and contributes none.
  120. * @type {Map<string, (id: string) => void>}
  121. */
  122. factories = /* @__PURE__ */ new Map();
  123. /**
  124. * Installed by the dev client at boot. The runtime is a store + executor and makes
  125. * no HMR decisions; accepting, disposing, and reloading live behind these hooks.
  126. * @type {DevRuntimeHooks | null}
  127. */
  128. hooks = null;
  129. /**
  130. * @param {ModuleGraphDelta} delta
  131. */
  132. registerGraph(delta) {
  133. for (let i = 0; i < delta.localCount; i++) {
  134. const id = delta.ids[i];
  135. const edges = delta.edges[i].map((j) => delta.ids[j]);
  136. for (const target of this.staticImports.get(id)?.edges ?? []) this.importers.get(target)?.delete(id);
  137. for (const target of edges) {
  138. let importerSet = this.importers.get(target);
  139. if (!importerSet) {
  140. importerSet = /* @__PURE__ */ new Set();
  141. this.importers.set(target, importerSet);
  142. }
  143. importerSet.add(id);
  144. }
  145. this.staticImports.set(id, {
  146. edges,
  147. bindings: delta.bindings?.[i]
  148. });
  149. const dynamicEdges = (delta.dynamicEdges?.[i] ?? []).map((j) => delta.ids[j]);
  150. for (const target of this.dynamicImports.get(id)?.edges ?? []) this.dynamicImporters.get(target)?.delete(id);
  151. for (const target of dynamicEdges) {
  152. let importerSet = this.dynamicImporters.get(target);
  153. if (!importerSet) {
  154. importerSet = /* @__PURE__ */ new Set();
  155. this.dynamicImporters.set(target, importerSet);
  156. }
  157. importerSet.add(id);
  158. }
  159. this.dynamicImports.set(id, { edges: dynamicEdges });
  160. }
  161. }
  162. /**
  163. * @param {string} id
  164. * @param {(id: string) => void} fn
  165. */
  166. registerFactory(id, fn) {
  167. this.factories.set(id, fn);
  168. }
  169. /**
  170. * @param {string} id
  171. * @param {{ exports: any }} [exportsHolder]
  172. */
  173. registerModule(id, exportsHolder = { exports: {} }) {
  174. const module = new Module(id);
  175. module.exportsHolder = exportsHolder;
  176. this.moduleCache.set(id, module);
  177. }
  178. /**
  179. * @param {string} id
  180. * @returns {string[]}
  181. */
  182. getImporters(id) {
  183. const dynamic = this.dynamicImporters.get(id);
  184. if (!dynamic || dynamic.size === 0) return [...this.importers.get(id) ?? []];
  185. return [.../* @__PURE__ */ new Set([...this.importers.get(id) ?? [], ...dynamic])];
  186. }
  187. /**
  188. * `"*"` means the whole namespace, an empty list a side-effect-only import, `undefined` no edge.
  189. * @param {string} importer
  190. * @param {string} id
  191. * @returns {string[] | undefined}
  192. */
  193. getImportedBindings(importer, id) {
  194. const record = this.staticImports.get(importer);
  195. const j = record ? record.edges.indexOf(id) : -1;
  196. const names = j === -1 ? void 0 : record?.bindings?.[j] ?? ["*"];
  197. if (!this.dynamicImports.get(importer)?.edges.includes(id)) return names;
  198. if (!names) return ["*"];
  199. return names.includes("*") ? names : [...names, "*"];
  200. }
  201. /**
  202. * @param {string} id
  203. */
  204. isExecuted(id) {
  205. return this.moduleCache.has(id);
  206. }
  207. /**
  208. * @param {string} id
  209. */
  210. hasFactory(id) {
  211. return this.factories.has(id);
  212. }
  213. /**
  214. * Module-cache delete only — static imports and factories persist. Removal is what
  215. * re-arms a cache-gated factory for `initModule`.
  216. * @param {string} id
  217. */
  218. removeModuleCache(id) {
  219. this.moduleCache.delete(id);
  220. this.hooks?.onModuleCacheRemoval(id);
  221. }
  222. /**
  223. * The one re-execution gate: registered → return the live exports; otherwise run the
  224. * mapped factory (which registers itself first, then runs the body).
  225. * @param {string} id
  226. */
  227. initModule(id) {
  228. if (this.moduleCache.has(id)) return this.loadExports(id);
  229. const factory = this.factories.get(id);
  230. if (!factory) throw new MissingFactoryError(id);
  231. factory(id);
  232. return this.loadExports(id);
  233. }
  234. /**
  235. * @param {string} id
  236. */
  237. loadExports(id) {
  238. const module = this.moduleCache.get(id);
  239. if (module) return module.exportsHolder.exports;
  240. else {
  241. console.warn(`Module ${id} not found`);
  242. return {};
  243. }
  244. }
  245. /**
  246. * @param {string} moduleId
  247. */
  248. createModuleHotContext(moduleId) {
  249. if (this.hooks) return this.hooks.createModuleHotContext(moduleId);
  250. throw new Error("createModuleHotContext requires installed hooks or an override");
  251. }
  252. /** @internal */
  253. __toESM = __toESM;
  254. /** @internal */
  255. __toCommonJS = __toCommonJS;
  256. /** @internal */
  257. __exportAll = __exportAll;
  258. /**
  259. * @param {boolean} [isNodeMode]
  260. * @returns {(mod: any) => any}
  261. * @internal
  262. */
  263. __toDynamicImportESM = (isNodeMode) => (mod) => __toESM(mod.default, isNodeMode);
  264. /** @internal */
  265. __reExport = __reExport;
  266. };
  267. //#endregion
  268. export { DevRuntime, MissingFactoryError };