vite-plugin-react-server 0.3.10 → 0.3.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (318) hide show
  1. package/dist/client.d.ts +1 -1
  2. package/dist/client.d.ts.map +1 -1
  3. package/dist/client.js +1 -1
  4. package/dist/index.d.ts +3 -6
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +2 -2
  7. package/dist/index.js.map +1 -1
  8. package/dist/package.json +16 -14
  9. package/dist/plugin/assertServerCondition.d.ts +5 -1
  10. package/dist/plugin/assertServerCondition.d.ts.map +1 -1
  11. package/dist/plugin/assertServerCondition.js +1 -3
  12. package/dist/plugin/checkFilesExist.d.ts +2 -2
  13. package/dist/plugin/checkFilesExist.d.ts.map +1 -1
  14. package/dist/plugin/checkFilesExist.js +43 -58
  15. package/dist/plugin/checkFilesExist.js.map +1 -1
  16. package/dist/plugin/collect-css-manifest.d.ts.map +1 -1
  17. package/dist/plugin/collect-css-manifest.js +5 -0
  18. package/dist/plugin/collect-css-manifest.js.map +1 -1
  19. package/dist/plugin/components.js +10 -15
  20. package/dist/plugin/config/createModuleIdGenerator.d.ts +11 -0
  21. package/dist/plugin/config/createModuleIdGenerator.d.ts.map +1 -0
  22. package/dist/plugin/config/createModuleIdGenerator.js +44 -0
  23. package/dist/plugin/config/createModuleIdGenerator.js.map +1 -0
  24. package/dist/plugin/config/defaults.d.ts +27 -24
  25. package/dist/plugin/config/defaults.d.ts.map +1 -1
  26. package/dist/plugin/config/defaults.js +26 -31
  27. package/dist/plugin/config/defaults.js.map +1 -1
  28. package/dist/plugin/config/getPaths.js +1 -1
  29. package/dist/plugin/config/resolveOptions.d.ts +1 -1
  30. package/dist/plugin/config/resolveOptions.d.ts.map +1 -1
  31. package/dist/plugin/config/resolveOptions.js +223 -16
  32. package/dist/plugin/config/resolveOptions.js.map +1 -1
  33. package/dist/plugin/config/resolvePages.d.ts +2 -0
  34. package/dist/plugin/config/resolvePages.d.ts.map +1 -1
  35. package/dist/plugin/config/resolvePages.js.map +1 -1
  36. package/dist/plugin/config/resolveUserConfig.d.ts +2 -2
  37. package/dist/plugin/config/resolveUserConfig.d.ts.map +1 -1
  38. package/dist/plugin/config/resolveUserConfig.js +149 -50
  39. package/dist/plugin/config/resolveUserConfig.js.map +1 -1
  40. package/dist/plugin/helpers/getBundleManifest.d.ts +25 -0
  41. package/dist/plugin/helpers/getBundleManifest.d.ts.map +1 -0
  42. package/dist/plugin/helpers/getBundleManifest.js +72 -0
  43. package/dist/plugin/helpers/getBundleManifest.js.map +1 -0
  44. package/dist/plugin/helpers/inputNormalizer.d.ts +14 -1
  45. package/dist/plugin/helpers/inputNormalizer.d.ts.map +1 -1
  46. package/dist/plugin/helpers/inputNormalizer.js +128 -16
  47. package/dist/plugin/helpers/inputNormalizer.js.map +1 -1
  48. package/dist/plugin/helpers/tryManifest.d.ts +3 -1
  49. package/dist/plugin/helpers/tryManifest.d.ts.map +1 -1
  50. package/dist/plugin/helpers/tryManifest.js +1 -1
  51. package/dist/plugin/helpers/tryManifest.js.map +1 -1
  52. package/dist/plugin/loader/createBuildLoader.d.ts +6 -2
  53. package/dist/plugin/loader/createBuildLoader.d.ts.map +1 -1
  54. package/dist/plugin/loader/createBuildLoader.js +37 -9
  55. package/dist/plugin/loader/createBuildLoader.js.map +1 -1
  56. package/dist/plugin/loader/createPageLoader.d.ts.map +1 -1
  57. package/dist/plugin/loader/createPageLoader.js +0 -7
  58. package/dist/plugin/loader/css-loader.d.ts +16 -0
  59. package/dist/plugin/loader/css-loader.d.ts.map +1 -0
  60. package/dist/plugin/loader/css-loader.js +70 -0
  61. package/dist/plugin/loader/css-loader.js.map +1 -0
  62. package/dist/plugin/loader/react-loader.d.ts +17 -0
  63. package/dist/plugin/loader/react-loader.d.ts.map +1 -0
  64. package/dist/plugin/loader/react-loader.js +647 -0
  65. package/dist/plugin/loader/react-loader.js.map +1 -0
  66. package/dist/plugin/loader/rsc/messageHandler.d.ts +2 -0
  67. package/dist/plugin/loader/rsc/messageHandler.d.ts.map +1 -0
  68. package/dist/plugin/loader/rsc/messageHandler.js +1 -0
  69. package/dist/plugin/loader/rsc/rsc-worker.development.d.ts +2 -0
  70. package/dist/plugin/loader/rsc/rsc-worker.development.d.ts.map +1 -0
  71. package/dist/plugin/loader/rsc/rsc-worker.development.js +1 -0
  72. package/dist/plugin/plugin.d.ts +0 -1
  73. package/dist/plugin/plugin.d.ts.map +1 -1
  74. package/dist/plugin/plugin.js +2 -1
  75. package/dist/plugin/preserver/plugin.d.ts.map +1 -1
  76. package/dist/plugin/preserver/plugin.js +3 -2
  77. package/dist/plugin/preserver/plugin.js.map +1 -1
  78. package/dist/plugin/react-client/index.d.ts +2 -1
  79. package/dist/plugin/react-client/index.d.ts.map +1 -1
  80. package/dist/plugin/react-client/index.js +19 -1
  81. package/dist/plugin/react-client/index.js.map +1 -0
  82. package/dist/plugin/react-client/plugin.d.ts +2 -2
  83. package/dist/plugin/react-client/plugin.d.ts.map +1 -1
  84. package/dist/plugin/react-client/plugin.js +283 -10
  85. package/dist/plugin/react-client/plugin.js.map +1 -1
  86. package/dist/plugin/react-server/createHandler.d.ts +2 -2
  87. package/dist/plugin/react-server/createHandler.d.ts.map +1 -1
  88. package/dist/plugin/react-server/createHandler.js +15 -9
  89. package/dist/plugin/react-server/createHandler.js.map +1 -1
  90. package/dist/plugin/react-server/createRscStream.d.ts +15 -3
  91. package/dist/plugin/react-server/createRscStream.d.ts.map +1 -1
  92. package/dist/plugin/react-server/createRscStream.js +53 -36
  93. package/dist/plugin/react-server/createRscStream.js.map +1 -1
  94. package/dist/plugin/react-server/createSsrHandler.d.ts +2 -2
  95. package/dist/plugin/react-server/createSsrHandler.d.ts.map +1 -1
  96. package/dist/plugin/react-server/createSsrHandler.js +5 -12
  97. package/dist/plugin/react-server/index.js +18 -9
  98. package/dist/plugin/react-server/index.js.map +1 -0
  99. package/dist/plugin/react-server/plugin.d.ts.map +1 -1
  100. package/dist/plugin/react-server/plugin.js +147 -137
  101. package/dist/plugin/react-server/plugin.js.map +1 -1
  102. package/dist/plugin/transformer/plugin.d.ts +2 -1
  103. package/dist/plugin/transformer/plugin.d.ts.map +1 -1
  104. package/dist/plugin/transformer/plugin.js +68 -75
  105. package/dist/plugin/transformer/plugin.js.map +1 -1
  106. package/dist/plugin/transformer/types.d.ts +4 -0
  107. package/dist/plugin/transformer/types.d.ts.map +1 -1
  108. package/dist/plugin/types.d.ts +56 -20
  109. package/dist/plugin/types.d.ts.map +1 -1
  110. package/dist/plugin/utils/logger.d.ts +9 -0
  111. package/dist/plugin/utils/logger.d.ts.map +1 -0
  112. package/dist/plugin/utils/logger.js +68 -0
  113. package/dist/plugin/utils/logger.js.map +1 -0
  114. package/dist/plugin/worker/createWorker.d.ts +1 -0
  115. package/dist/plugin/worker/createWorker.d.ts.map +1 -1
  116. package/dist/plugin/worker/createWorker.js +23 -36
  117. package/dist/plugin/worker/createWorker.js.map +1 -1
  118. package/dist/plugin/worker/html/html-worker.production.js +5 -1
  119. package/dist/plugin/worker/html/html-worker.production.js.map +1 -1
  120. package/dist/plugin/worker/html/messageHandler.d.ts.map +1 -1
  121. package/dist/plugin/worker/html/messageHandler.js +33 -28
  122. package/dist/plugin/worker/html/messageHandler.js.map +1 -1
  123. package/dist/plugin/worker/html/plugin.d.ts.map +1 -1
  124. package/dist/plugin/worker/html/plugin.js +10 -5
  125. package/dist/plugin/worker/html/renderPages.d.ts +7 -6
  126. package/dist/plugin/worker/html/renderPages.d.ts.map +1 -1
  127. package/dist/plugin/worker/html/renderPages.js +147 -93
  128. package/dist/plugin/worker/html/renderPages.js.map +1 -1
  129. package/dist/plugin/worker/loader.d.ts +1 -11
  130. package/dist/plugin/worker/loader.d.ts.map +1 -1
  131. package/dist/plugin/worker/loader.js +2 -2
  132. package/dist/plugin/worker/loader.js.map +1 -1
  133. package/dist/plugin/worker/plugin.d.ts +10 -1
  134. package/dist/plugin/worker/plugin.d.ts.map +1 -1
  135. package/dist/plugin/worker/plugin.js +10 -1
  136. package/dist/plugin/worker/rsc/index.d.ts +1 -3
  137. package/dist/plugin/worker/rsc/index.d.ts.map +1 -1
  138. package/dist/plugin/worker/rsc/index.js +1 -9
  139. package/dist/plugin/worker/rsc/index.js.map +1 -1
  140. package/dist/plugin/worker/rsc/messageHandler.d.ts +3 -0
  141. package/dist/plugin/worker/rsc/messageHandler.d.ts.map +1 -0
  142. package/dist/plugin/worker/rsc/messageHandler.js +107 -0
  143. package/dist/plugin/worker/rsc/messageHandler.js.map +1 -0
  144. package/dist/plugin/worker/rsc/plugin.d.ts.map +1 -1
  145. package/dist/plugin/worker/rsc/plugin.js +74 -80
  146. package/dist/plugin/worker/rsc/rsc-worker.development.d.ts +32 -0
  147. package/dist/plugin/worker/rsc/rsc-worker.development.d.ts.map +1 -0
  148. package/dist/plugin/worker/rsc/rsc-worker.development.js +43 -0
  149. package/dist/plugin/worker/rsc/rsc-worker.development.js.map +1 -0
  150. package/dist/plugin/worker/rsc/rsc-worker.js +4 -106
  151. package/dist/plugin/worker/rsc/rsc-worker.production.d.ts +2 -0
  152. package/dist/plugin/worker/rsc/rsc-worker.production.d.ts.map +1 -0
  153. package/dist/plugin/worker/rsc/rsc-worker.production.js +14 -0
  154. package/dist/plugin/worker/rsc/rsc-worker.production.js.map +1 -0
  155. package/dist/plugin/worker/rsc/state.d.ts +11 -0
  156. package/dist/plugin/worker/rsc/state.d.ts.map +1 -0
  157. package/dist/plugin/worker/rsc/state.js +12 -0
  158. package/dist/plugin/worker/rsc/state.js.map +1 -0
  159. package/dist/plugin/worker/types.d.ts +62 -35
  160. package/dist/plugin/worker/types.d.ts.map +1 -1
  161. package/dist/server.d.ts +1 -3
  162. package/dist/server.d.ts.map +1 -1
  163. package/dist/server.js +1 -3
  164. package/dist/server.js.map +1 -1
  165. package/dist/tsconfig.tsbuildinfo +1 -1
  166. package/package.json +16 -14
  167. package/plugin/assertServerCondition.ts +2 -3
  168. package/plugin/checkFilesExist.ts +46 -66
  169. package/plugin/collect-css-manifest.ts +5 -1
  170. package/plugin/config/createModuleIdGenerator.ts +52 -0
  171. package/plugin/config/defaults.ts +27 -20
  172. package/plugin/config/resolveOptions.ts +311 -25
  173. package/plugin/config/resolvePages.ts +1 -1
  174. package/plugin/config/resolveUserConfig.ts +195 -61
  175. package/plugin/helpers/getBundleManifest.ts +113 -0
  176. package/plugin/helpers/inputNormalizer.ts +186 -25
  177. package/plugin/helpers/tryManifest.ts +3 -1
  178. package/plugin/loader/createBuildLoader.ts +50 -9
  179. package/plugin/loader/createPageLoader.ts +1 -7
  180. package/plugin/loader/css-loader.ts +96 -0
  181. package/plugin/loader/react-loader.ts +945 -0
  182. package/plugin/loader/rsc/messageHandler.tsx +1 -0
  183. package/plugin/loader/rsc/rsc-worker.development.ts +1 -0
  184. package/plugin/plugin.ts +2 -1
  185. package/plugin/preserver/plugin.ts +2 -1
  186. package/plugin/react-client/index.ts +12 -1
  187. package/plugin/react-client/plugin.ts +351 -11
  188. package/plugin/react-server/createHandler.ts +16 -18
  189. package/plugin/react-server/createRscStream.ts +75 -42
  190. package/plugin/react-server/createSsrHandler.ts +7 -26
  191. package/plugin/react-server/plugin.ts +192 -155
  192. package/plugin/transformer/plugin.ts +70 -104
  193. package/plugin/transformer/types.ts +4 -0
  194. package/plugin/types/global.d.ts +8 -0
  195. package/plugin/types.ts +120 -64
  196. package/plugin/utils/logger.ts +52 -0
  197. package/plugin/worker/createWorker.ts +43 -44
  198. package/plugin/worker/html/html-worker.production.tsx +7 -2
  199. package/plugin/worker/html/messageHandler.ts +42 -35
  200. package/plugin/worker/html/plugin.ts +15 -11
  201. package/plugin/worker/html/renderPages.ts +177 -123
  202. package/plugin/worker/loader.ts +4 -13
  203. package/plugin/worker/plugin.ts +10 -1
  204. package/plugin/worker/rsc/index.ts +4 -13
  205. package/plugin/worker/rsc/messageHandler.tsx +143 -0
  206. package/plugin/worker/rsc/plugin.ts +38 -37
  207. package/plugin/worker/rsc/rsc-worker.development.ts +107 -0
  208. package/plugin/worker/rsc/rsc-worker.production.ts +13 -0
  209. package/plugin/worker/rsc/rsc-worker.tsx +5 -128
  210. package/plugin/worker/rsc/state.ts +37 -0
  211. package/plugin/worker/types.ts +83 -38
  212. package/scripts/check-react-version.mjs +17 -7
  213. package/scripts/react+0.0.0-experimental-b3a95caf-20250113.patch +143 -4170
  214. package/scripts/react-dom+0.0.0-experimental-b3a95caf-20250113.patch +14271 -90079
  215. package/dist/node_modules/magic-string/dist/magic-string.es.js +0 -1283
  216. package/dist/node_modules/magic-string/dist/magic-string.es.js.map +0 -1
  217. package/dist/plugin/build/createClientBuildConfig.d.ts +0 -3
  218. package/dist/plugin/build/createClientBuildConfig.d.ts.map +0 -1
  219. package/dist/plugin/build/createClientBuildConfig.js +0 -14
  220. package/dist/plugin/build/createServerBuildConfig.d.ts +0 -12
  221. package/dist/plugin/build/createServerBuildConfig.d.ts.map +0 -1
  222. package/dist/plugin/build/createServerBuildConfig.js +0 -40
  223. package/dist/plugin/build/createSharedBuildConfig.d.ts +0 -5
  224. package/dist/plugin/build/createSharedBuildConfig.d.ts.map +0 -1
  225. package/dist/plugin/build/createSharedBuildConfig.js +0 -28
  226. package/dist/plugin/build/mergeInputs.d.ts +0 -9
  227. package/dist/plugin/build/mergeInputs.d.ts.map +0 -1
  228. package/dist/plugin/build/mergeInputs.js +0 -56
  229. package/dist/plugin/components.js.map +0 -1
  230. package/dist/plugin/config/moduleIdDefault.d.ts +0 -8
  231. package/dist/plugin/config/moduleIdDefault.d.ts.map +0 -1
  232. package/dist/plugin/config/moduleIdDefault.js +0 -23
  233. package/dist/plugin/config/moduleIdDefault.js.map +0 -1
  234. package/dist/plugin/helpers/createClientInputNormalizer.d.ts +0 -8
  235. package/dist/plugin/helpers/createClientInputNormalizer.d.ts.map +0 -1
  236. package/dist/plugin/helpers/createClientInputNormalizer.js +0 -35
  237. package/dist/plugin/helpers/createServerInputNormalizer.d.ts +0 -9
  238. package/dist/plugin/helpers/createServerInputNormalizer.d.ts.map +0 -1
  239. package/dist/plugin/helpers/createServerInputNormalizer.js +0 -37
  240. package/dist/plugin/helpers/createStaticInputNormalizer.d.ts +0 -7
  241. package/dist/plugin/helpers/createStaticInputNormalizer.d.ts.map +0 -1
  242. package/dist/plugin/helpers/createStaticInputNormalizer.js +0 -18
  243. package/dist/plugin/helpers/getModuleManifest.d.ts +0 -12
  244. package/dist/plugin/helpers/getModuleManifest.d.ts.map +0 -1
  245. package/dist/plugin/helpers/getModuleManifest.js +0 -24
  246. package/dist/plugin/helpers/getModuleManifest.js.map +0 -1
  247. package/dist/plugin/helpers/inputNormalizerWorker.d.ts +0 -11
  248. package/dist/plugin/helpers/inputNormalizerWorker.d.ts.map +0 -1
  249. package/dist/plugin/helpers/inputNormalizerWorker.js +0 -30
  250. package/dist/plugin/helpers/normalizedRelativePath.d.ts +0 -11
  251. package/dist/plugin/helpers/normalizedRelativePath.d.ts.map +0 -1
  252. package/dist/plugin/helpers/normalizedRelativePath.js +0 -36
  253. package/dist/plugin/helpers/resolveFilePath.d.ts +0 -13
  254. package/dist/plugin/helpers/resolveFilePath.d.ts.map +0 -1
  255. package/dist/plugin/helpers/resolveFilePath.js +0 -74
  256. package/dist/plugin/helpers/resolveWorkerModule.d.ts +0 -6
  257. package/dist/plugin/helpers/resolveWorkerModule.d.ts.map +0 -1
  258. package/dist/plugin/helpers/resolveWorkerModule.js +0 -24
  259. package/dist/plugin/helpers/validateModuleBase.d.ts +0 -3
  260. package/dist/plugin/helpers/validateModuleBase.d.ts.map +0 -1
  261. package/dist/plugin/helpers/validateModuleBase.js +0 -16
  262. package/dist/plugin/helpers/validateResolvedConfig.d.ts +0 -3
  263. package/dist/plugin/helpers/validateResolvedConfig.d.ts.map +0 -1
  264. package/dist/plugin/helpers/validateResolvedConfig.js +0 -17
  265. package/dist/plugin/react-server/createDevMiddleware.d.ts +0 -8
  266. package/dist/plugin/react-server/createDevMiddleware.d.ts.map +0 -1
  267. package/dist/plugin/react-server/createDevMiddleware.js +0 -68
  268. package/dist/plugin/react-server/createDevServer.d.ts +0 -4
  269. package/dist/plugin/react-server/createDevServer.d.ts.map +0 -1
  270. package/dist/plugin/react-server/createDevServer.js +0 -4
  271. package/dist/plugin/react-server/createReactNodeStreamer.d.ts +0 -10
  272. package/dist/plugin/react-server/createReactNodeStreamer.d.ts.map +0 -1
  273. package/dist/plugin/react-server/createReactNodeStreamer.js +0 -7
  274. package/dist/plugin/transformer/transformer-client-components.d.ts +0 -21
  275. package/dist/plugin/transformer/transformer-client-components.d.ts.map +0 -1
  276. package/dist/plugin/transformer/transformer-client-components.js +0 -61
  277. package/dist/plugin/transformer/transformer-client-components.js.map +0 -1
  278. package/dist/plugin/transformer/transformer-server-actions.d.ts +0 -29
  279. package/dist/plugin/transformer/transformer-server-actions.d.ts.map +0 -1
  280. package/dist/plugin/transformer/transformer-server-actions.js +0 -76
  281. package/dist/plugin/transformer/transformer-server-actions.js.map +0 -1
  282. package/dist/plugin/worker/rsc/createRscStream.d.ts +0 -5
  283. package/dist/plugin/worker/rsc/createRscStream.d.ts.map +0 -1
  284. package/dist/plugin/worker/rsc/createRscStream.js +0 -39
  285. package/dist/plugin/worker/rsc/createRscStream.js.map +0 -1
  286. package/dist/plugin/worker/rsc/development.d.ts +0 -5
  287. package/dist/plugin/worker/rsc/development.d.ts.map +0 -1
  288. package/dist/plugin/worker/rsc/development.js +0 -13
  289. package/dist/plugin/worker/rsc/development.js.map +0 -1
  290. package/dist/plugin/worker/rsc/plugin.js.map +0 -1
  291. package/dist/plugin/worker/rsc/production.d.ts +0 -5
  292. package/dist/plugin/worker/rsc/production.d.ts.map +0 -1
  293. package/dist/plugin/worker/rsc/production.js +0 -13
  294. package/dist/plugin/worker/rsc/production.js.map +0 -1
  295. package/plugin/build/createClientBuildConfig.ts +0 -21
  296. package/plugin/build/createServerBuildConfig.ts +0 -66
  297. package/plugin/build/createSharedBuildConfig.ts +0 -35
  298. package/plugin/build/mergeInputs.ts +0 -58
  299. package/plugin/config/moduleIdDefault.ts +0 -23
  300. package/plugin/helpers/createClientInputNormalizer.ts +0 -48
  301. package/plugin/helpers/createServerInputNormalizer.ts +0 -52
  302. package/plugin/helpers/createStaticInputNormalizer.ts +0 -26
  303. package/plugin/helpers/getModuleManifest.ts +0 -31
  304. package/plugin/helpers/inputNormalizerWorker.ts +0 -47
  305. package/plugin/helpers/normalizedRelativePath.ts +0 -59
  306. package/plugin/helpers/resolveFilePath.ts +0 -108
  307. package/plugin/helpers/resolveWorkerModule.ts +0 -41
  308. package/plugin/helpers/validateModuleBase.ts +0 -30
  309. package/plugin/helpers/validateResolvedConfig.ts +0 -21
  310. package/plugin/react-server/createDevMiddleware.ts +0 -91
  311. package/plugin/react-server/createDevServer.ts +0 -9
  312. package/plugin/react-server/createReactNodeStreamer.ts +0 -26
  313. package/plugin/transformer/transformer-client-components.ts +0 -94
  314. package/plugin/transformer/transformer-server-actions.ts +0 -110
  315. package/plugin/worker/rsc/createRscStream.ts +0 -42
  316. package/plugin/worker/rsc/development.ts +0 -6
  317. package/plugin/worker/rsc/production.ts +0 -6
  318. package/scripts/react-server-dom-esm+0.0.1.patch +0 -24775
@@ -0,0 +1,945 @@
1
+ import * as acorn from "acorn-loose";
2
+ import { basename } from "path";
3
+
4
+ import {
5
+ // @ts-ignore
6
+ setSourceMapsSupport,
7
+ SourceMap,
8
+ type LoadHookContext,
9
+ type ResolveHookContext,
10
+ } from "node:module";
11
+ import type { MessagePort } from "node:worker_threads";
12
+
13
+ let stashedGetSource: any = null;
14
+ let stashedResolve: any = null;
15
+
16
+ // Enable source maps with full support
17
+ setSourceMapsSupport(true, {
18
+ nodeModules: true, // Enable for node_modules files
19
+ generatedCode: true, // Enable for generated code
20
+ });
21
+
22
+ // Add type for our context with port
23
+ interface LoaderContext {
24
+ data?: { port: MessagePort };
25
+ }
26
+
27
+ // Store port globally for use in load hook
28
+ let loaderPort: MessagePort | undefined;
29
+
30
+ export async function getSource(
31
+ url: string,
32
+ context: any,
33
+ defaultGetSource: any
34
+ ) {
35
+ // We stash this in case we end up needing to resolve export * statements later.
36
+ stashedGetSource = defaultGetSource;
37
+ return defaultGetSource(url, context, defaultGetSource);
38
+ }
39
+
40
+ function addExportedEntry(
41
+ exportedEntries: any,
42
+ localNames: any,
43
+ localName: any,
44
+ exportedName: any,
45
+ type: any,
46
+ loc: any
47
+ ) {
48
+ if (localNames.has(localName)) {
49
+ // If the same local name is exported more than once, we only need one of the names.
50
+ return;
51
+ }
52
+
53
+ exportedEntries.push({
54
+ localName,
55
+ exportedName,
56
+ type,
57
+ loc,
58
+ originalLine: -1,
59
+ originalColumn: -1,
60
+ originalSource: -1,
61
+ nameIndex: -1,
62
+ });
63
+ }
64
+
65
+ function addLocalExportedNames(
66
+ exportedEntries: any,
67
+ localNames: any,
68
+ node: any
69
+ ) {
70
+ switch (node.type) {
71
+ case "Identifier":
72
+ addExportedEntry(
73
+ exportedEntries,
74
+ localNames,
75
+ node.name,
76
+ node.name,
77
+ null,
78
+ node.loc
79
+ );
80
+ return;
81
+
82
+ case "ObjectPattern":
83
+ for (let i = 0; i < node.properties.length; i++)
84
+ addLocalExportedNames(exportedEntries, localNames, node.properties[i]);
85
+
86
+ return;
87
+
88
+ case "ArrayPattern":
89
+ for (let i = 0; i < node.elements.length; i++) {
90
+ const element = node.elements[i];
91
+ if (element)
92
+ addLocalExportedNames(exportedEntries, localNames, element);
93
+ }
94
+
95
+ return;
96
+
97
+ case "Property":
98
+ addLocalExportedNames(exportedEntries, localNames, node.value);
99
+ return;
100
+
101
+ case "AssignmentPattern":
102
+ addLocalExportedNames(exportedEntries, localNames, node.left);
103
+ return;
104
+
105
+ case "RestElement":
106
+ addLocalExportedNames(exportedEntries, localNames, node.argument);
107
+ return;
108
+
109
+ case "ParenthesizedExpression":
110
+ addLocalExportedNames(exportedEntries, localNames, node.expression);
111
+ return;
112
+ }
113
+ }
114
+
115
+ function transformServerModule(
116
+ source: string,
117
+ program: any,
118
+ url: string,
119
+ sourceMap: any,
120
+ loader: any,
121
+ port: MessagePort | undefined
122
+ ) {
123
+ const body = program.body; // This entry list needs to be in source location order.
124
+
125
+ const exportedEntries: any[] = []; // Dedupe set.
126
+
127
+ const localNames = new Set();
128
+
129
+ for (let i = 0; i < body.length; i++) {
130
+ const node = body[i];
131
+
132
+ switch (node.type) {
133
+ case "ExportAllDeclaration":
134
+ // If export * is used, the other file needs to explicitly opt into "use server" too.
135
+ break;
136
+
137
+ case "ExportDefaultDeclaration":
138
+ if (node.declaration.type === "Identifier") {
139
+ addExportedEntry(
140
+ exportedEntries,
141
+ localNames,
142
+ node.declaration.name,
143
+ "default",
144
+ null,
145
+ node.declaration.loc
146
+ );
147
+ } else if (node.declaration.type === "FunctionDeclaration") {
148
+ if (node.declaration.id) {
149
+ addExportedEntry(
150
+ exportedEntries,
151
+ localNames,
152
+ node.declaration.id.name,
153
+ "default",
154
+ "function",
155
+ node.declaration.id.loc
156
+ );
157
+ }
158
+ }
159
+
160
+ continue;
161
+
162
+ case "ExportNamedDeclaration":
163
+ if (node.declaration) {
164
+ if (node.declaration.type === "VariableDeclaration") {
165
+ const declarations = node.declaration.declarations;
166
+
167
+ for (let j = 0; j < declarations.length; j++) {
168
+ addLocalExportedNames(
169
+ exportedEntries,
170
+ localNames,
171
+ declarations[j].id
172
+ );
173
+ }
174
+ } else {
175
+ const name = node.declaration.id.name;
176
+ addExportedEntry(
177
+ exportedEntries,
178
+ localNames,
179
+ name,
180
+ name,
181
+ node.declaration.type === "FunctionDeclaration"
182
+ ? "function"
183
+ : null,
184
+ node.declaration.id.loc
185
+ );
186
+ }
187
+ }
188
+
189
+ if (node.specifiers) {
190
+ const specifiers = node.specifiers;
191
+
192
+ for (let j = 0; j < specifiers.length; j++) {
193
+ const specifier = specifiers[j];
194
+ addExportedEntry(
195
+ exportedEntries,
196
+ localNames,
197
+ specifier.local.name,
198
+ specifier.exported.name,
199
+ null,
200
+ specifier.local.loc
201
+ );
202
+ }
203
+ }
204
+
205
+ continue;
206
+ }
207
+ }
208
+
209
+ let mappings =
210
+ sourceMap && typeof sourceMap.mappings === "string"
211
+ ? sourceMap.mappings
212
+ : "";
213
+ let newSrc = source;
214
+
215
+ if (exportedEntries.length > 0) {
216
+ let lastSourceIndex = 0;
217
+ let lastOriginalLine = 0;
218
+ let lastOriginalColumn = 0;
219
+ let lastNameIndex = 0;
220
+ let sourceLineCount = 0;
221
+ let lastMappedLine = 0;
222
+
223
+ if (sourceMap) {
224
+ // We iterate source mapping entries and our matched exports in parallel to source map
225
+ // them to their original location.
226
+ let nextEntryIdx = 0;
227
+ let nextEntryLine = exportedEntries[nextEntryIdx].loc.start.line;
228
+ let nextEntryColumn = exportedEntries[nextEntryIdx].loc.start.column;
229
+ readMappings(
230
+ mappings,
231
+ (
232
+ generatedLine: number,
233
+ generatedColumn: number,
234
+ sourceIndex: number,
235
+ originalLine: number,
236
+ originalColumn: number,
237
+ nameIndex: number
238
+ ) => {
239
+ if (
240
+ generatedLine > nextEntryLine ||
241
+ (generatedLine === nextEntryLine &&
242
+ generatedColumn > nextEntryColumn)
243
+ ) {
244
+ // We're past the entry which means that the best match we have is the previous entry.
245
+ if (lastMappedLine === nextEntryLine) {
246
+ // Match
247
+ exportedEntries[nextEntryIdx].originalLine = lastOriginalLine;
248
+ exportedEntries[nextEntryIdx].originalColumn = lastOriginalColumn;
249
+ exportedEntries[nextEntryIdx].originalSource = lastSourceIndex;
250
+ exportedEntries[nextEntryIdx].nameIndex = lastNameIndex;
251
+ }
252
+
253
+ nextEntryIdx++;
254
+
255
+ if (nextEntryIdx < exportedEntries.length) {
256
+ nextEntryLine = exportedEntries[nextEntryIdx].loc.start.line;
257
+ nextEntryColumn = exportedEntries[nextEntryIdx].loc.start.column;
258
+ } else {
259
+ nextEntryLine = -1;
260
+ nextEntryColumn = -1;
261
+ }
262
+ }
263
+
264
+ lastMappedLine = generatedLine;
265
+
266
+ if (sourceIndex > -1) {
267
+ lastSourceIndex = sourceIndex;
268
+ }
269
+
270
+ if (originalLine > -1) {
271
+ lastOriginalLine = originalLine;
272
+ }
273
+
274
+ if (originalColumn > -1) {
275
+ lastOriginalColumn = originalColumn;
276
+ }
277
+
278
+ if (nameIndex > -1) {
279
+ lastNameIndex = nameIndex;
280
+ }
281
+ }
282
+ );
283
+
284
+ if (nextEntryIdx < exportedEntries.length) {
285
+ if (lastMappedLine === nextEntryLine) {
286
+ // Match
287
+ exportedEntries[nextEntryIdx].originalLine = lastOriginalLine;
288
+ exportedEntries[nextEntryIdx].originalColumn = lastOriginalColumn;
289
+ exportedEntries[nextEntryIdx].originalSource = lastSourceIndex;
290
+ exportedEntries[nextEntryIdx].nameIndex = lastNameIndex;
291
+ }
292
+ }
293
+
294
+ for (
295
+ let lastIdx = mappings.length - 1;
296
+ lastIdx >= 0 && mappings[lastIdx] === ";";
297
+ lastIdx--
298
+ ) {
299
+ // If the last mapped lines don't contain any segments, we don't get a callback from readMappings
300
+ // so we need to pad the number of mapped lines, with one for each empty line.
301
+ lastMappedLine++;
302
+ }
303
+
304
+ sourceLineCount = program.loc.end.line;
305
+
306
+ if (sourceLineCount < lastMappedLine) {
307
+ throw new Error(
308
+ "The source map has more mappings than there are lines."
309
+ );
310
+ } // If the original source string had more lines than there are mappings in the source map.
311
+ // Add some extra padding of unmapped lines so that any lines that we add line up.
312
+
313
+ for (
314
+ let extraLines = sourceLineCount - lastMappedLine;
315
+ extraLines > 0;
316
+ extraLines--
317
+ ) {
318
+ mappings += ";";
319
+ }
320
+ } else {
321
+ // If a file doesn't have a source map then we generate a blank source map that just
322
+ // contains the original content and segments pointing to the original lines.
323
+ sourceLineCount = 1;
324
+ let idx = -1;
325
+
326
+ while ((idx = source.indexOf("\n", idx + 1)) !== -1) {
327
+ sourceLineCount++;
328
+ }
329
+
330
+ mappings = "AAAA" + ";AACA".repeat(sourceLineCount - 1);
331
+ sourceMap = new SourceMap({
332
+ version: 3,
333
+ file: basename(url),
334
+ sources: [url],
335
+ sourcesContent: [source],
336
+ names: [],
337
+ mappings: mappings,
338
+ sourceRoot: "",
339
+ });
340
+ lastSourceIndex = 0;
341
+ lastOriginalLine = sourceLineCount;
342
+ lastOriginalColumn = 0;
343
+ lastNameIndex = -1;
344
+ lastMappedLine = sourceLineCount;
345
+
346
+ for (let i = 0; i < exportedEntries.length; i++) {
347
+ // Point each entry to original location.
348
+ const entry = exportedEntries[i];
349
+ entry.originalSource = 0;
350
+ entry.originalLine = entry.loc.start.line; // We use column zero since we do the short-hand line-only source maps above.
351
+
352
+ entry.originalColumn = 0; // entry.loc.start.column;
353
+ }
354
+ }
355
+
356
+ newSrc += "\n\n;";
357
+ newSrc +=
358
+ 'import {registerServerReference} from "react-server-dom-esm/server";\n';
359
+
360
+ if (mappings) {
361
+ mappings += ";;";
362
+ }
363
+
364
+ const createMapping = createMappingsSerializer(); // Create an empty mapping pointing to where we last left off to reset the counters.
365
+
366
+ let generatedLine = 1;
367
+ createMapping(
368
+ generatedLine,
369
+ 0,
370
+ lastSourceIndex,
371
+ lastOriginalLine,
372
+ lastOriginalColumn,
373
+ lastNameIndex
374
+ );
375
+
376
+ for (let i = 0; i < exportedEntries.length; i++) {
377
+ const entry = exportedEntries[i];
378
+ generatedLine++;
379
+
380
+ if (entry.type !== "function") {
381
+ // We first check if the export is a function and if so annotate it.
382
+ newSrc += "if (typeof " + entry.localName + ' === "function") ';
383
+ }
384
+
385
+ newSrc += "registerServerReference(" + entry.localName + ",";
386
+ newSrc += JSON.stringify(url) + ",";
387
+ newSrc += JSON.stringify(entry.exportedName) + ");\n";
388
+ mappings += createMapping(
389
+ generatedLine,
390
+ 0,
391
+ entry.originalSource,
392
+ entry.originalLine,
393
+ entry.originalColumn,
394
+ entry.nameIndex
395
+ );
396
+ }
397
+ }
398
+
399
+ if (sourceMap) {
400
+ // Override with an new mappings and serialize an inline source map.
401
+ sourceMap.mappings = mappings;
402
+ newSrc +=
403
+ "//# sourceMappingURL=data:application/json;charset=utf-8;base64," +
404
+ Buffer.from(JSON.stringify(sourceMap)).toString("base64");
405
+ }
406
+ if (port) {
407
+ port.postMessage({
408
+ type: "SERVER_MODULE",
409
+ url,
410
+ source: newSrc,
411
+ });
412
+ }
413
+
414
+ return newSrc;
415
+ }
416
+
417
+ function addExportNames(names: any, node: any) {
418
+ switch (node.type) {
419
+ case "Identifier":
420
+ names.push(node.name);
421
+ return;
422
+
423
+ case "ObjectPattern":
424
+ for (let i = 0; i < node.properties.length; i++)
425
+ addExportNames(names, node.properties[i]);
426
+
427
+ return;
428
+
429
+ case "ArrayPattern":
430
+ for (let i = 0; i < node.elements.length; i++) {
431
+ const element = node.elements[i];
432
+ if (element) addExportNames(names, element);
433
+ }
434
+
435
+ return;
436
+
437
+ case "Property":
438
+ addExportNames(names, node.value);
439
+ return;
440
+
441
+ case "AssignmentPattern":
442
+ addExportNames(names, node.left);
443
+ return;
444
+
445
+ case "RestElement":
446
+ addExportNames(names, node.argument);
447
+ return;
448
+
449
+ case "ParenthesizedExpression":
450
+ addExportNames(names, node.expression);
451
+ return;
452
+ }
453
+ }
454
+
455
+ function resolveClientImport(specifier: string, parentURL: string) {
456
+ const conditions = ["node", "import"];
457
+
458
+ if (stashedResolve === null) {
459
+ throw new Error(
460
+ "Expected resolve to have been called before transformSource"
461
+ );
462
+ }
463
+
464
+ return stashedResolve(
465
+ specifier,
466
+ {
467
+ conditions,
468
+ parentURL,
469
+ },
470
+ stashedResolve
471
+ );
472
+ }
473
+
474
+ async function parseExportNamesInto(
475
+ body: any,
476
+ names: any,
477
+ parentURL: string,
478
+ loader: any
479
+ ) {
480
+ for (let i = 0; i < body.length; i++) {
481
+ const node = body[i];
482
+
483
+ switch (node.type) {
484
+ case "ExportAllDeclaration":
485
+ if (node.exported) {
486
+ addExportNames(names, node.exported);
487
+ continue;
488
+ } else {
489
+ const _await$resolveClientI = await resolveClientImport(
490
+ node.source.value,
491
+ parentURL
492
+ ),
493
+ url = _await$resolveClientI.url;
494
+
495
+ const _await$loader = await loader(
496
+ url,
497
+ {
498
+ format: "module",
499
+ conditions: [],
500
+ importAttributes: {},
501
+ },
502
+ loader
503
+ ),
504
+ source = _await$loader.source;
505
+
506
+ if (typeof source !== "string") {
507
+ throw new Error("Expected the transformed source to be a string.");
508
+ }
509
+
510
+ let childBody;
511
+
512
+ try {
513
+ childBody = acorn.parse(source, {
514
+ ecmaVersion: "2024" as never,
515
+ sourceType: "module",
516
+ }).body;
517
+ } catch (x) {
518
+ // eslint-disable-next-line react-internal/no-production-logging
519
+ console.error("Error parsing %s %s", url, (x as Error)?.message);
520
+ continue;
521
+ }
522
+
523
+ await parseExportNamesInto(childBody, names, url, loader);
524
+ continue;
525
+ }
526
+
527
+ case "ExportDefaultDeclaration":
528
+ names.push("default");
529
+ continue;
530
+
531
+ case "ExportNamedDeclaration":
532
+ if (node.declaration) {
533
+ if (node.declaration.type === "VariableDeclaration") {
534
+ const declarations = node.declaration.declarations;
535
+
536
+ for (let j = 0; j < declarations.length; j++) {
537
+ addExportNames(names, declarations[j].id);
538
+ }
539
+ } else {
540
+ addExportNames(names, node.declaration.id);
541
+ }
542
+ }
543
+
544
+ if (node.specifiers) {
545
+ const specifiers = node.specifiers;
546
+
547
+ for (let j = 0; j < specifiers.length; j++) {
548
+ addExportNames(names, specifiers[j].exported);
549
+ }
550
+ }
551
+
552
+ continue;
553
+ }
554
+ }
555
+ }
556
+
557
+ async function transformClientModule(
558
+ program: any,
559
+ url: string,
560
+ sourceMap: any,
561
+ loader: any
562
+ ) {
563
+ const body = program.body;
564
+ const names: any[] = [];
565
+ await parseExportNamesInto(body, names, url, loader);
566
+
567
+
568
+ if (names.length === 0) {
569
+ console.log("[react-loader] No exports found in:", url);
570
+ return "";
571
+ }
572
+
573
+ let newSrc =
574
+ 'import {registerClientReference} from "react-server-dom-esm/server";\n';
575
+
576
+ for (let i = 0; i < names.length; i++) {
577
+ const name = names[i];
578
+
579
+ const errorMessage =
580
+ name === "default"
581
+ ? `Attempted to call the default export of ${url} from the server but it's on the client`
582
+ : `Attempted to call ${name}() from the server but ${name} is on the client`;
583
+
584
+ const fullError = `${errorMessage}. It's not possible to invoke a client function from the server, it can only be rendered as a Component or passed to props of a Client Component.`;
585
+
586
+ // Convert file:// URL to relative path for the browser
587
+ const browserUrl = url.replace("file://", "").replace(process.cwd(), "");
588
+
589
+ if (name === "default") {
590
+ newSrc += `export default registerClientReference(function() { throw new Error(${JSON.stringify(
591
+ fullError
592
+ )}); }, ${JSON.stringify(browserUrl)}, ${JSON.stringify(name)});\n`;
593
+ } else {
594
+ newSrc += `export const ${name} = registerClientReference(function() { throw new Error(${JSON.stringify(
595
+ fullError
596
+ )}); }, ${JSON.stringify(browserUrl)}, ${JSON.stringify(name)});\n`;
597
+ }
598
+ }
599
+
600
+ // Create source map
601
+ if (sourceMap) {
602
+ const newSourceMap = {
603
+ version: 3,
604
+ file: basename(url),
605
+ sources: [url],
606
+ sourcesContent: [program.source],
607
+ names: [],
608
+ mappings: "AAAA;" + ";".repeat(names.length), // Simple line mapping
609
+ sourceRoot: "",
610
+ };
611
+
612
+ newSrc +=
613
+ "\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,";
614
+ newSrc += Buffer.from(JSON.stringify(newSourceMap)).toString("base64");
615
+ }
616
+
617
+ return newSrc;
618
+ }
619
+
620
+ async function loadClientImport(url: string, defaultTransformSource: any) {
621
+ if (stashedGetSource === null) {
622
+ throw new Error(
623
+ "Expected getSource to have been called before transformSource"
624
+ );
625
+ } // TODO: Validate that this is another module by calling getFormat.
626
+
627
+ const _await$stashedGetSour = await stashedGetSource(
628
+ url,
629
+ {
630
+ format: "module",
631
+ },
632
+ stashedGetSource
633
+ ),
634
+ source = _await$stashedGetSour.source;
635
+
636
+ const result = await defaultTransformSource(
637
+ source,
638
+ {
639
+ format: "module",
640
+ url,
641
+ },
642
+ defaultTransformSource
643
+ );
644
+ return {
645
+ format: "module",
646
+ source: result.source,
647
+ };
648
+ }
649
+
650
+ export async function transformModuleIfNeeded(
651
+ source: string,
652
+ url: string,
653
+ loader: any,
654
+ port?: MessagePort // Make port parameter optional
655
+ ) {
656
+ if (
657
+ source.indexOf("use client") === -1 &&
658
+ source.indexOf("use server") === -1
659
+ ) {
660
+ return source;
661
+ }
662
+
663
+ let program;
664
+ try {
665
+ program = acorn.parse(source, {
666
+ ecmaVersion: "2024" as never,
667
+ sourceType: "module",
668
+ locations: true,
669
+ });
670
+ } catch (x) {
671
+ console.error(
672
+ "[react-loader] Error parsing %s: %s",
673
+ url,
674
+ (x as Error)?.message
675
+ );
676
+ return source;
677
+ }
678
+
679
+ let useClient = false;
680
+ let useServer = false; // Keep this for server transforms
681
+
682
+ // Check for directives
683
+ for (const node of program.body) {
684
+ if (node.type !== "ExpressionStatement" || !node.directive) continue;
685
+
686
+ if (node.directive === "use client") {
687
+ useClient = true;
688
+ if (port) {
689
+ port.postMessage({
690
+ type: "CLIENT_COMPONENT",
691
+ url,
692
+ source,
693
+ });
694
+ }
695
+ break;
696
+ }
697
+ if (node.directive === "use server") {
698
+ useServer = true;
699
+ break;
700
+ }
701
+ }
702
+
703
+ if (useClient) {
704
+ return transformClientModule(program, url, undefined, loader);
705
+ } else if (useServer) {
706
+ return transformServerModule(source, program, url, undefined, loader, port);
707
+ }
708
+
709
+ return source;
710
+ }
711
+
712
+ function readMappings(
713
+ mappings: string,
714
+ callback: (
715
+ generatedLine: number,
716
+ generatedColumn: number,
717
+ sourceIndex: number,
718
+ originalLine: number,
719
+ originalColumn: number,
720
+ nameIndex: number
721
+ ) => void
722
+ ) {
723
+ let line = 1;
724
+ let column = 0;
725
+ let sourceIndex = 0;
726
+ let originalLine = 0;
727
+ let originalColumn = 0;
728
+ let nameIndex = 0;
729
+
730
+ let index = 0;
731
+ while (index < mappings.length) {
732
+ if (mappings[index] === ";") {
733
+ line++;
734
+ column = 0;
735
+ index++;
736
+ continue;
737
+ }
738
+ if (mappings[index] === ",") {
739
+ index++;
740
+ continue;
741
+ }
742
+
743
+ let [
744
+ generatedColumnDelta = 0,
745
+ sourceIndexDelta = 0,
746
+ originalLineDelta = 0,
747
+ originalColumnDelta = 0,
748
+ nameIndexDelta = 0,
749
+ ] = decodeVLQ(mappings.slice(index));
750
+
751
+ // Update positions
752
+ column += generatedColumnDelta;
753
+ sourceIndex += sourceIndexDelta;
754
+ originalLine += originalLineDelta;
755
+ originalColumn += originalColumnDelta;
756
+ nameIndex += nameIndexDelta;
757
+
758
+ // Skip the encoded segment
759
+ while (index < mappings.length && !/[,;]/.test(mappings[index])) {
760
+ index++;
761
+ }
762
+
763
+ callback(
764
+ line,
765
+ column,
766
+ sourceIndex,
767
+ originalLine,
768
+ originalColumn,
769
+ nameIndex
770
+ );
771
+ }
772
+ }
773
+
774
+ function createMappingsSerializer() {
775
+ let previousGeneratedLine = 1;
776
+ let previousGeneratedColumn = 0;
777
+ let previousOriginalFile = 0;
778
+ let previousOriginalLine = 0;
779
+ let previousOriginalColumn = 0;
780
+ let previousNameIndex = 0;
781
+
782
+ return function (
783
+ generatedLine: number,
784
+ generatedColumn: number,
785
+ originalFile: number,
786
+ originalLine: number,
787
+ originalColumn: number,
788
+ nameIndex: number
789
+ ): string {
790
+ // Reset column when moving to a new line
791
+ if (generatedLine > previousGeneratedLine) {
792
+ previousGeneratedColumn = 0;
793
+ let lines = "";
794
+ for (let i = previousGeneratedLine; i < generatedLine; i++) {
795
+ lines += ";";
796
+ }
797
+ previousGeneratedLine = generatedLine;
798
+ if (lines) return lines;
799
+ }
800
+
801
+ // Calculate deltas
802
+ const segment = [
803
+ generatedColumn - previousGeneratedColumn,
804
+ originalFile - previousOriginalFile,
805
+ originalLine - previousOriginalLine,
806
+ originalColumn - previousOriginalColumn,
807
+ ];
808
+
809
+ if (nameIndex >= 0) {
810
+ segment.push(nameIndex - previousNameIndex);
811
+ }
812
+
813
+ // Update previous values
814
+ previousGeneratedColumn = generatedColumn;
815
+ previousOriginalFile = originalFile;
816
+ previousOriginalLine = originalLine;
817
+ previousOriginalColumn = originalColumn;
818
+ previousNameIndex = nameIndex;
819
+
820
+ return encodeVLQ(segment) + ",";
821
+ };
822
+ }
823
+
824
+ // VLQ encoding helpers
825
+ const VLQ_SHIFT = 5;
826
+ const VLQ_CONTINUATION_BIT = 1 << VLQ_SHIFT;
827
+ const VLQ_VALUE_MASK = VLQ_CONTINUATION_BIT - 1;
828
+ const BASE64_CHARS =
829
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
830
+
831
+ function encodeVLQ(numbers: number[]): string {
832
+ return numbers
833
+ .map((num) => {
834
+ // Convert to VLQ
835
+ const vlq = num < 0 ? (-num << 1) | 1 : num << 1;
836
+
837
+ let result = "";
838
+ let value = vlq;
839
+
840
+ do {
841
+ let digit = value & VLQ_VALUE_MASK;
842
+ value >>>= VLQ_SHIFT;
843
+ if (value > 0) {
844
+ digit |= VLQ_CONTINUATION_BIT;
845
+ }
846
+ result += BASE64_CHARS[digit];
847
+ } while (value > 0);
848
+
849
+ return result;
850
+ })
851
+ .join("");
852
+ }
853
+
854
+ function decodeVLQ(str: string): number[] {
855
+ const numbers: number[] = [];
856
+ let value = 0;
857
+ let shift = 0;
858
+ let index = 0;
859
+
860
+ while (index < str.length && !/[,;]/.test(str[index])) {
861
+ const digit = BASE64_CHARS.indexOf(str[index]);
862
+ if (digit === -1) break;
863
+
864
+ value += (digit & VLQ_VALUE_MASK) << shift;
865
+
866
+ if ((digit & VLQ_CONTINUATION_BIT) === 0) {
867
+ const negate = value & 1;
868
+ value >>>= 1;
869
+ numbers.push(negate ? -value : value);
870
+ value = shift = 0;
871
+ } else {
872
+ shift += VLQ_SHIFT;
873
+ }
874
+
875
+ index++;
876
+ }
877
+
878
+ return numbers;
879
+ }
880
+
881
+ // Initialize hook
882
+ export async function initialize(data: { port: MessagePort }) {
883
+ loaderPort = data.port; // Store port
884
+ data.port.postMessage({ type: "INITIALIZED" });
885
+ data.port.unref();
886
+ }
887
+
888
+ // Resolve hook
889
+ export async function resolve(
890
+ specifier: string,
891
+ context: ResolveHookContext,
892
+ nextResolve: any
893
+ ) {
894
+ return nextResolve(specifier, context);
895
+ }
896
+
897
+ // Load hook
898
+ export async function load(
899
+ url: string,
900
+ context: LoadHookContext & LoaderContext,
901
+ nextLoad: any
902
+ ) {
903
+ const result = await nextLoad(url, context);
904
+ if (result.format === "module") {
905
+ const newSrc = await transformModuleIfNeeded(
906
+ result.source,
907
+ url,
908
+ nextLoad,
909
+ loaderPort ?? undefined
910
+ );
911
+ return { ...result, source: newSrc };
912
+ }
913
+ return result;
914
+ }
915
+
916
+ // Transform hook
917
+ export async function transformSource(
918
+ source: string,
919
+ context: any,
920
+ defaultTransformSource: any
921
+ ) {
922
+ const transformed = await defaultTransformSource(
923
+ source,
924
+ context,
925
+ defaultTransformSource
926
+ );
927
+
928
+ if (context.format === "module") {
929
+ const transformedSource = transformed.source;
930
+ if (typeof transformedSource !== "string") {
931
+ throw new Error("Expected source to have been transformed to a string.");
932
+ }
933
+
934
+ const newSrc = await transformModuleIfNeeded(
935
+ transformedSource,
936
+ context.url,
937
+ (url: string, ctx: any, defaultLoad: any) =>
938
+ loadClientImport(url, defaultTransformSource),
939
+ context.data?.port!
940
+ );
941
+ return { source: newSrc };
942
+ }
943
+
944
+ return transformed;
945
+ }