vite-plugin-react-server 0.3.4 → 0.3.6

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 (484) hide show
  1. package/README.md +113 -222
  2. package/bin/patch.mjs +84 -0
  3. package/dist/client.d.ts +3 -0
  4. package/dist/client.d.ts.map +1 -0
  5. package/dist/client.js +7 -0
  6. package/dist/client.js.map +1 -0
  7. package/dist/index.d.ts +7 -2
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +32 -3
  10. package/dist/index.js.map +1 -1
  11. package/dist/node_modules/magic-string/dist/magic-string.es.js +1283 -0
  12. package/dist/node_modules/magic-string/dist/magic-string.es.js.map +1 -0
  13. package/dist/package.json +130 -0
  14. package/dist/plugin/assertServerCondition.d.ts.map +1 -0
  15. package/dist/plugin/assertServerCondition.js +15 -0
  16. package/dist/plugin/build/createClientBuildConfig.d.ts +3 -0
  17. package/dist/plugin/build/createClientBuildConfig.d.ts.map +1 -0
  18. package/dist/plugin/build/createClientBuildConfig.js +14 -0
  19. package/dist/plugin/build/createServerBuildConfig.d.ts +12 -0
  20. package/dist/plugin/build/createServerBuildConfig.d.ts.map +1 -0
  21. package/dist/plugin/build/createServerBuildConfig.js +40 -0
  22. package/dist/plugin/build/createSharedBuildConfig.d.ts +5 -0
  23. package/dist/plugin/build/createSharedBuildConfig.d.ts.map +1 -0
  24. package/dist/plugin/build/createSharedBuildConfig.js +28 -0
  25. package/dist/plugin/build/mergeInputs.d.ts +9 -0
  26. package/dist/plugin/build/mergeInputs.d.ts.map +1 -0
  27. package/dist/plugin/build/mergeInputs.js +56 -0
  28. package/dist/plugin/checkFilesExist.d.ts +3 -0
  29. package/dist/plugin/checkFilesExist.d.ts.map +1 -0
  30. package/dist/plugin/checkFilesExist.js +78 -0
  31. package/dist/plugin/checkFilesExist.js.map +1 -0
  32. package/dist/{collect-css-manifest.d.ts → plugin/collect-css-manifest.d.ts} +2 -2
  33. package/dist/plugin/collect-css-manifest.d.ts.map +1 -0
  34. package/dist/{src → plugin}/collect-css-manifest.js +8 -5
  35. package/dist/plugin/collect-css-manifest.js.map +1 -0
  36. package/dist/plugin/components.d.ts.map +1 -0
  37. package/dist/{src → plugin}/components.js +9 -4
  38. package/dist/plugin/components.js.map +1 -0
  39. package/dist/plugin/config/defaults.d.ts +51 -0
  40. package/dist/plugin/config/defaults.d.ts.map +1 -0
  41. package/dist/plugin/config/defaults.js +52 -0
  42. package/dist/plugin/config/defaults.js.map +1 -0
  43. package/dist/plugin/config/getCondition.d.ts +2 -0
  44. package/dist/plugin/config/getCondition.d.ts.map +1 -0
  45. package/dist/plugin/config/getCondition.js +11 -0
  46. package/dist/plugin/config/getCondition.js.map +1 -0
  47. package/dist/plugin/config/getPaths.d.ts +5 -0
  48. package/dist/plugin/config/getPaths.d.ts.map +1 -0
  49. package/dist/plugin/config/getPaths.js +21 -0
  50. package/dist/plugin/config/getPaths.js.map +1 -0
  51. package/dist/plugin/config/getWorkerPath.d.ts +2 -0
  52. package/dist/plugin/config/getWorkerPath.d.ts.map +1 -0
  53. package/dist/plugin/config/getWorkerPath.js +4 -0
  54. package/dist/plugin/config/index.d.ts +8 -0
  55. package/dist/plugin/config/index.d.ts.map +1 -0
  56. package/dist/plugin/config/index.js +7 -0
  57. package/dist/plugin/config/moduleIdDefault.d.ts +8 -0
  58. package/dist/plugin/config/moduleIdDefault.d.ts.map +1 -0
  59. package/dist/plugin/config/moduleIdDefault.js +23 -0
  60. package/dist/plugin/config/moduleIdDefault.js.map +1 -0
  61. package/dist/plugin/config/resolveOptions.d.ts +9 -0
  62. package/dist/plugin/config/resolveOptions.d.ts.map +1 -0
  63. package/dist/plugin/config/resolveOptions.js +56 -0
  64. package/dist/plugin/config/resolveOptions.js.map +1 -0
  65. package/dist/plugin/config/resolvePages.d.ts +9 -0
  66. package/dist/plugin/config/resolvePages.d.ts.map +1 -0
  67. package/dist/plugin/config/resolvePages.js +38 -0
  68. package/dist/plugin/config/resolvePages.js.map +1 -0
  69. package/dist/plugin/config/resolveUserConfig.d.ts +18 -0
  70. package/dist/plugin/config/resolveUserConfig.d.ts.map +1 -0
  71. package/dist/plugin/config/resolveUserConfig.js +84 -0
  72. package/dist/plugin/config/resolveUserConfig.js.map +1 -0
  73. package/dist/plugin/copy-dir.d.ts.map +1 -0
  74. package/dist/plugin/copy-dir.js +20 -0
  75. package/dist/plugin/getCondition.d.ts +2 -0
  76. package/dist/plugin/getCondition.d.ts.map +1 -0
  77. package/dist/plugin/getCondition.js +1 -0
  78. package/dist/plugin/getEnv.d.ts.map +1 -0
  79. package/dist/plugin/getEnv.js +107 -0
  80. package/dist/plugin/helpers/createClientInputNormalizer.d.ts +8 -0
  81. package/dist/plugin/helpers/createClientInputNormalizer.d.ts.map +1 -0
  82. package/dist/plugin/helpers/createClientInputNormalizer.js +35 -0
  83. package/dist/plugin/helpers/createServerInputNormalizer.d.ts +9 -0
  84. package/dist/plugin/helpers/createServerInputNormalizer.d.ts.map +1 -0
  85. package/dist/plugin/helpers/createServerInputNormalizer.js +37 -0
  86. package/dist/plugin/helpers/createStaticInputNormalizer.d.ts +7 -0
  87. package/dist/plugin/helpers/createStaticInputNormalizer.d.ts.map +1 -0
  88. package/dist/plugin/helpers/createStaticInputNormalizer.js +18 -0
  89. package/dist/plugin/helpers/getModuleManifest.d.ts +12 -0
  90. package/dist/plugin/helpers/getModuleManifest.d.ts.map +1 -0
  91. package/dist/plugin/helpers/getModuleManifest.js +24 -0
  92. package/dist/plugin/helpers/getModuleManifest.js.map +1 -0
  93. package/dist/plugin/helpers/inputNormalizer.d.ts +3 -0
  94. package/dist/plugin/helpers/inputNormalizer.d.ts.map +1 -0
  95. package/dist/plugin/helpers/inputNormalizer.js +35 -0
  96. package/dist/plugin/helpers/inputNormalizer.js.map +1 -0
  97. package/dist/plugin/helpers/inputNormalizerWorker.d.ts +11 -0
  98. package/dist/plugin/helpers/inputNormalizerWorker.d.ts.map +1 -0
  99. package/dist/plugin/helpers/inputNormalizerWorker.js +30 -0
  100. package/dist/plugin/helpers/normalizedRelativePath.d.ts.map +1 -0
  101. package/dist/plugin/helpers/normalizedRelativePath.js +36 -0
  102. package/dist/plugin/helpers/resolveFilePath.d.ts +13 -0
  103. package/dist/plugin/helpers/resolveFilePath.d.ts.map +1 -0
  104. package/dist/plugin/helpers/resolveFilePath.js +74 -0
  105. package/dist/plugin/helpers/resolveWorkerModule.d.ts +6 -0
  106. package/dist/plugin/helpers/resolveWorkerModule.d.ts.map +1 -0
  107. package/dist/plugin/helpers/resolveWorkerModule.js +24 -0
  108. package/dist/plugin/helpers/tryManifest.d.ts.map +1 -0
  109. package/dist/{src → plugin}/helpers/tryManifest.js +11 -6
  110. package/dist/plugin/helpers/tryManifest.js.map +1 -0
  111. package/dist/plugin/helpers/validateModuleBase.d.ts +3 -0
  112. package/dist/plugin/helpers/validateModuleBase.d.ts.map +1 -0
  113. package/dist/plugin/helpers/validateModuleBase.js +16 -0
  114. package/dist/plugin/helpers/validateResolvedConfig.d.ts +3 -0
  115. package/dist/plugin/helpers/validateResolvedConfig.d.ts.map +1 -0
  116. package/dist/plugin/helpers/validateResolvedConfig.js +17 -0
  117. package/dist/plugin/index.d.ts +3 -0
  118. package/dist/plugin/index.d.ts.map +1 -0
  119. package/dist/plugin/index.js +1 -0
  120. package/dist/plugin/loader/createBuildLoader.d.ts +9 -0
  121. package/dist/plugin/loader/createBuildLoader.d.ts.map +1 -0
  122. package/dist/plugin/loader/createBuildLoader.js +38 -0
  123. package/dist/plugin/loader/createBuildLoader.js.map +1 -0
  124. package/dist/plugin/loader/createCssLoader.d.ts +30 -0
  125. package/dist/plugin/loader/createCssLoader.d.ts.map +1 -0
  126. package/dist/plugin/loader/createCssLoader.js +35 -0
  127. package/dist/{html → plugin/loader}/createPageLoader.d.ts +2 -4
  128. package/dist/plugin/loader/createPageLoader.d.ts.map +1 -0
  129. package/dist/plugin/loader/createPageLoader.js +57 -0
  130. package/dist/plugin/manifest.d.ts.map +1 -0
  131. package/dist/plugin/manifest.js +13 -0
  132. package/dist/plugin/module-graph.d.ts.map +1 -0
  133. package/dist/plugin/module-graph.js +35 -0
  134. package/dist/plugin/plugin.d.ts +7 -0
  135. package/dist/plugin/plugin.d.ts.map +1 -0
  136. package/dist/plugin/plugin.js +8 -0
  137. package/dist/plugin/preserver/index.d.ts +2 -0
  138. package/dist/plugin/preserver/index.d.ts.map +1 -0
  139. package/dist/plugin/preserver/index.js +1 -0
  140. package/dist/plugin/preserver/plugin.d.ts +3 -0
  141. package/dist/plugin/preserver/plugin.d.ts.map +1 -0
  142. package/dist/plugin/preserver/plugin.js +90 -0
  143. package/dist/plugin/preserver/plugin.js.map +1 -0
  144. package/dist/plugin/react-client/index.d.ts +2 -0
  145. package/dist/plugin/react-client/index.d.ts.map +1 -0
  146. package/dist/plugin/react-client/index.js +1 -0
  147. package/dist/plugin/react-client/plugin.d.ts +4 -0
  148. package/dist/plugin/react-client/plugin.d.ts.map +1 -0
  149. package/dist/plugin/react-client/plugin.js +34 -0
  150. package/dist/plugin/react-client/plugin.js.map +1 -0
  151. package/dist/plugin/react-server/createDevMiddleware.d.ts.map +1 -0
  152. package/dist/plugin/react-server/createDevMiddleware.js +68 -0
  153. package/dist/plugin/react-server/createDevServer.d.ts.map +1 -0
  154. package/dist/plugin/react-server/createDevServer.js +4 -0
  155. package/dist/plugin/react-server/createHandler.d.ts +17 -0
  156. package/dist/plugin/react-server/createHandler.d.ts.map +1 -0
  157. package/dist/{src → plugin}/react-server/createHandler.js +28 -17
  158. package/dist/plugin/react-server/createHandler.js.map +1 -0
  159. package/dist/plugin/react-server/createReactNodeStreamer.d.ts.map +1 -0
  160. package/dist/plugin/react-server/createReactNodeStreamer.js +7 -0
  161. package/dist/{react-server → plugin/react-server}/createRscStream.d.ts +1 -1
  162. package/dist/plugin/react-server/createRscStream.d.ts.map +1 -0
  163. package/dist/{src → plugin}/react-server/createRscStream.js +14 -13
  164. package/dist/plugin/react-server/createRscStream.js.map +1 -0
  165. package/dist/{react-server → plugin/react-server}/createSsrHandler.d.ts +1 -1
  166. package/dist/plugin/react-server/createSsrHandler.d.ts.map +1 -0
  167. package/dist/plugin/react-server/createSsrHandler.js +102 -0
  168. package/dist/plugin/react-server/index.d.ts +3 -0
  169. package/dist/plugin/react-server/index.d.ts.map +1 -0
  170. package/dist/plugin/react-server/index.js +10 -0
  171. package/dist/plugin/react-server/plugin.d.ts +7 -0
  172. package/dist/plugin/react-server/plugin.d.ts.map +1 -0
  173. package/dist/plugin/react-server/plugin.js +377 -0
  174. package/dist/plugin/react-server/plugin.js.map +1 -0
  175. package/dist/plugin/resolvePage.d.ts.map +1 -0
  176. package/dist/{src → plugin}/resolvePage.js +8 -4
  177. package/dist/plugin/resolvePage.js.map +1 -0
  178. package/dist/plugin/resolveProps.d.ts.map +1 -0
  179. package/dist/{src → plugin}/resolveProps.js +21 -8
  180. package/dist/plugin/resolveProps.js.map +1 -0
  181. package/dist/plugin/server.d.ts +2 -0
  182. package/dist/plugin/server.d.ts.map +1 -0
  183. package/dist/plugin/server.js +1 -0
  184. package/dist/plugin/transformer/index.d.ts +2 -0
  185. package/dist/plugin/transformer/index.d.ts.map +1 -0
  186. package/dist/plugin/transformer/index.js +1 -0
  187. package/dist/{transformer/index.d.ts → plugin/transformer/plugin.d.ts} +3 -4
  188. package/dist/plugin/transformer/plugin.d.ts.map +1 -0
  189. package/dist/plugin/transformer/plugin.js +94 -0
  190. package/dist/plugin/transformer/plugin.js.map +1 -0
  191. package/dist/plugin/transformer/transformer-client-components.d.ts +21 -0
  192. package/dist/plugin/transformer/transformer-client-components.d.ts.map +1 -0
  193. package/dist/plugin/transformer/transformer-client-components.js +61 -0
  194. package/dist/plugin/transformer/transformer-client-components.js.map +1 -0
  195. package/dist/{transformer/transformer.d.ts → plugin/transformer/transformer-server-actions.d.ts} +3 -4
  196. package/dist/plugin/transformer/transformer-server-actions.d.ts.map +1 -0
  197. package/dist/plugin/transformer/transformer-server-actions.js +76 -0
  198. package/dist/plugin/transformer/transformer-server-actions.js.map +1 -0
  199. package/dist/plugin/transformer/types.d.ts.map +1 -0
  200. package/dist/plugin/transformer/types.js +1 -0
  201. package/dist/plugin/types.d.ts +225 -0
  202. package/dist/plugin/types.d.ts.map +1 -0
  203. package/dist/plugin/types.js +1 -0
  204. package/dist/plugin/worker/createWorker.d.ts +15 -0
  205. package/dist/plugin/worker/createWorker.d.ts.map +1 -0
  206. package/dist/plugin/worker/createWorker.js +71 -0
  207. package/dist/plugin/worker/createWorker.js.map +1 -0
  208. package/dist/plugin/worker/html/html-worker.d.ts +2 -0
  209. package/dist/plugin/worker/html/html-worker.d.ts.map +1 -0
  210. package/dist/plugin/worker/html/html-worker.development.d.ts +2 -0
  211. package/dist/plugin/worker/html/html-worker.development.d.ts.map +1 -0
  212. package/dist/plugin/worker/html/html-worker.development.js +12 -0
  213. package/dist/plugin/worker/html/html-worker.development.js.map +1 -0
  214. package/dist/plugin/worker/html/html-worker.js +4 -0
  215. package/dist/plugin/worker/html/html-worker.production.d.ts +2 -0
  216. package/dist/plugin/worker/html/html-worker.production.d.ts.map +1 -0
  217. package/dist/plugin/worker/html/html-worker.production.js +12 -0
  218. package/dist/plugin/worker/html/html-worker.production.js.map +1 -0
  219. package/dist/plugin/worker/html/index.d.ts +2 -0
  220. package/dist/plugin/worker/html/index.d.ts.map +1 -0
  221. package/dist/plugin/worker/html/index.js +7 -0
  222. package/dist/plugin/worker/html/index.js.map +1 -0
  223. package/dist/plugin/worker/html/messageHandler.d.ts +3 -0
  224. package/dist/plugin/worker/html/messageHandler.d.ts.map +1 -0
  225. package/dist/plugin/worker/html/messageHandler.js +98 -0
  226. package/dist/plugin/worker/html/messageHandler.js.map +1 -0
  227. package/dist/plugin/worker/html/plugin.d.ts +4 -0
  228. package/dist/plugin/worker/html/plugin.d.ts.map +1 -0
  229. package/dist/plugin/worker/html/plugin.js +89 -0
  230. package/dist/plugin/worker/html/renderPages.d.ts +38 -0
  231. package/dist/plugin/worker/html/renderPages.d.ts.map +1 -0
  232. package/dist/plugin/worker/html/renderPages.js +123 -0
  233. package/dist/plugin/worker/html/renderPages.js.map +1 -0
  234. package/dist/plugin/worker/loader.d.ts.map +1 -0
  235. package/dist/plugin/worker/loader.js +11 -0
  236. package/dist/plugin/worker/loader.js.map +1 -0
  237. package/dist/plugin/worker/plugin.d.ts +10 -0
  238. package/dist/plugin/worker/plugin.d.ts.map +1 -0
  239. package/dist/plugin/worker/plugin.js +14 -0
  240. package/dist/plugin/worker/rsc/createRscStream.d.ts +5 -0
  241. package/dist/plugin/worker/rsc/createRscStream.d.ts.map +1 -0
  242. package/dist/plugin/worker/rsc/createRscStream.js +39 -0
  243. package/dist/plugin/worker/rsc/createRscStream.js.map +1 -0
  244. package/dist/plugin/worker/rsc/development.d.ts +5 -0
  245. package/dist/plugin/worker/rsc/development.d.ts.map +1 -0
  246. package/dist/plugin/worker/rsc/development.js +13 -0
  247. package/dist/plugin/worker/rsc/development.js.map +1 -0
  248. package/dist/plugin/worker/rsc/index.d.ts +4 -0
  249. package/dist/plugin/worker/rsc/index.d.ts.map +1 -0
  250. package/dist/plugin/worker/rsc/index.js +15 -0
  251. package/dist/plugin/worker/rsc/index.js.map +1 -0
  252. package/dist/plugin/worker/rsc/plugin.d.ts +4 -0
  253. package/dist/plugin/worker/rsc/plugin.d.ts.map +1 -0
  254. package/dist/plugin/worker/rsc/plugin.js +82 -0
  255. package/dist/plugin/worker/rsc/plugin.js.map +1 -0
  256. package/dist/plugin/worker/rsc/production.d.ts +5 -0
  257. package/dist/plugin/worker/rsc/production.d.ts.map +1 -0
  258. package/dist/plugin/worker/rsc/production.js +13 -0
  259. package/dist/plugin/worker/rsc/production.js.map +1 -0
  260. package/dist/plugin/worker/rsc/rsc-worker.d.ts +2 -0
  261. package/dist/plugin/worker/rsc/rsc-worker.d.ts.map +1 -0
  262. package/dist/plugin/worker/rsc/rsc-worker.js +106 -0
  263. package/dist/plugin/worker/types.d.ts +99 -0
  264. package/dist/plugin/worker/types.d.ts.map +1 -0
  265. package/dist/plugin/worker/types.js +1 -0
  266. package/dist/server.d.ts +4 -1
  267. package/dist/server.d.ts.map +1 -1
  268. package/dist/server.js +9 -0
  269. package/dist/server.js.map +1 -0
  270. package/dist/tsconfig.tsbuildinfo +1 -0
  271. package/dist/types.d.ts +1 -201
  272. package/dist/types.d.ts.map +1 -1
  273. package/dist/types.js +1 -0
  274. package/package.json +42 -25
  275. package/plugin/build/createClientBuildConfig.ts +21 -0
  276. package/plugin/build/createServerBuildConfig.ts +66 -0
  277. package/plugin/build/createSharedBuildConfig.ts +35 -0
  278. package/plugin/build/mergeInputs.ts +58 -0
  279. package/plugin/checkFilesExist.ts +82 -0
  280. package/{src → plugin}/collect-css-manifest.ts +4 -2
  281. package/plugin/config/defaults.ts +44 -0
  282. package/plugin/config/getCondition.ts +3 -0
  283. package/plugin/config/getPaths.ts +25 -0
  284. package/plugin/config/getWorkerPath.ts +5 -0
  285. package/plugin/config/index.ts +8 -0
  286. package/plugin/config/moduleIdDefault.ts +23 -0
  287. package/plugin/config/resolveOptions.ts +53 -0
  288. package/plugin/config/resolvePages.ts +43 -0
  289. package/plugin/config/resolveUserConfig.ts +97 -0
  290. package/{src → plugin}/getEnv.ts +1 -1
  291. package/plugin/helpers/createClientInputNormalizer.ts +48 -0
  292. package/plugin/helpers/createServerInputNormalizer.ts +52 -0
  293. package/plugin/helpers/createStaticInputNormalizer.ts +26 -0
  294. package/plugin/helpers/getModuleManifest.ts +31 -0
  295. package/plugin/helpers/inputNormalizer.ts +39 -0
  296. package/plugin/helpers/inputNormalizerWorker.ts +47 -0
  297. package/plugin/helpers/resolveFilePath.ts +108 -0
  298. package/plugin/helpers/resolveWorkerModule.ts +41 -0
  299. package/plugin/helpers/validateModuleBase.ts +30 -0
  300. package/plugin/helpers/validateResolvedConfig.ts +21 -0
  301. package/plugin/index.ts +2 -0
  302. package/plugin/loader/createBuildLoader.ts +44 -0
  303. package/plugin/loader/createCssLoader.ts +73 -0
  304. package/{src/html → plugin/loader}/createPageLoader.ts +35 -27
  305. package/plugin/plugin.ts +8 -0
  306. package/plugin/preserver/index.ts +1 -0
  307. package/plugin/preserver/plugin.ts +109 -0
  308. package/plugin/react-client/index.ts +1 -0
  309. package/plugin/react-client/plugin.ts +33 -0
  310. package/{src → plugin}/react-server/createDevMiddleware.ts +17 -1
  311. package/{src → plugin}/react-server/createDevServer.ts +0 -1
  312. package/{src → plugin}/react-server/createHandler.ts +31 -10
  313. package/{src → plugin}/react-server/createReactNodeStreamer.ts +1 -0
  314. package/{src → plugin}/react-server/createRscStream.ts +8 -12
  315. package/{src → plugin}/react-server/createSsrHandler.ts +16 -19
  316. package/plugin/react-server/index.ts +12 -0
  317. package/plugin/react-server/plugin.ts +464 -0
  318. package/{src → plugin}/resolvePage.ts +1 -1
  319. package/{src → plugin}/resolveProps.ts +15 -6
  320. package/plugin/server.tsx +0 -0
  321. package/plugin/transformer/index.ts +1 -0
  322. package/plugin/transformer/plugin.ts +141 -0
  323. package/plugin/transformer/transformer-client-components.ts +94 -0
  324. package/{src/transformer/transformer.ts → plugin/transformer/transformer-server-actions.ts} +14 -27
  325. package/{src → plugin}/types.ts +97 -55
  326. package/plugin/worker/createWorker.ts +84 -0
  327. package/plugin/worker/html/html-worker.development.tsx +8 -0
  328. package/plugin/worker/html/html-worker.production.tsx +8 -0
  329. package/plugin/worker/html/html-worker.ts +5 -0
  330. package/plugin/worker/html/index.ts +5 -0
  331. package/plugin/worker/html/messageHandler.ts +116 -0
  332. package/plugin/worker/html/plugin.ts +97 -0
  333. package/plugin/worker/html/renderPages.ts +188 -0
  334. package/plugin/worker/plugin.ts +17 -0
  335. package/plugin/worker/rsc/createRscStream.ts +42 -0
  336. package/plugin/worker/rsc/development.ts +6 -0
  337. package/plugin/worker/rsc/index.ts +14 -0
  338. package/plugin/worker/rsc/plugin.ts +83 -0
  339. package/plugin/worker/rsc/production.ts +6 -0
  340. package/plugin/worker/rsc/rsc-worker.tsx +128 -0
  341. package/plugin/worker/types.ts +124 -0
  342. package/scripts/check-react-version.mjs +48 -30
  343. package/scripts/react+0.0.0-experimental-b3a95caf-20250113.patch +4291 -0
  344. package/scripts/react-dom+0.0.0-experimental-b3a95caf-20250113.patch +108798 -0
  345. package/scripts/react-server-dom-esm+0.0.0-experimental-b3a95caf-20250113.patch +24775 -0
  346. package/{patches → scripts}/react-server-dom-esm+0.0.1.patch +11354 -3
  347. package/tsconfig.json +15 -13
  348. package/dist/assertServerCondition.d.ts.map +0 -1
  349. package/dist/bin/patch.js +0 -51
  350. package/dist/bin/patch.js.map +0 -1
  351. package/dist/build/createBuildConfig.d.ts +0 -13
  352. package/dist/build/createBuildConfig.d.ts.map +0 -1
  353. package/dist/build/mergeInputs.d.ts +0 -5
  354. package/dist/build/mergeInputs.d.ts.map +0 -1
  355. package/dist/checkFilesExist.d.ts +0 -8
  356. package/dist/checkFilesExist.d.ts.map +0 -1
  357. package/dist/collect-css-manifest.d.ts.map +0 -1
  358. package/dist/components.d.ts.map +0 -1
  359. package/dist/copy-dir.d.ts.map +0 -1
  360. package/dist/getEnv.d.ts.map +0 -1
  361. package/dist/helpers/inputNormalizer.d.ts +0 -6
  362. package/dist/helpers/inputNormalizer.d.ts.map +0 -1
  363. package/dist/helpers/normalizedRelativePath.d.ts.map +0 -1
  364. package/dist/helpers/tryManifest.d.ts.map +0 -1
  365. package/dist/html/createPageLoader.d.ts.map +0 -1
  366. package/dist/manifest.d.ts.map +0 -1
  367. package/dist/module-graph.d.ts.map +0 -1
  368. package/dist/options.d.ts +0 -91
  369. package/dist/options.d.ts.map +0 -1
  370. package/dist/plugin.d.ts +0 -3
  371. package/dist/plugin.d.ts.map +0 -1
  372. package/dist/react-client/plugin.d.ts +0 -4
  373. package/dist/react-client/plugin.d.ts.map +0 -1
  374. package/dist/react-client/plugin.js +0 -28
  375. package/dist/react-client/plugin.js.map +0 -1
  376. package/dist/react-server/createDevMiddleware.d.ts.map +0 -1
  377. package/dist/react-server/createDevServer.d.ts.map +0 -1
  378. package/dist/react-server/createHandler.d.ts +0 -23
  379. package/dist/react-server/createHandler.d.ts.map +0 -1
  380. package/dist/react-server/createReactNodeStreamer.d.ts.map +0 -1
  381. package/dist/react-server/createRscStream.d.ts.map +0 -1
  382. package/dist/react-server/createSsrHandler.d.ts.map +0 -1
  383. package/dist/react-server/plugin.d.ts +0 -8
  384. package/dist/react-server/plugin.d.ts.map +0 -1
  385. package/dist/react-server/plugin.js +0 -345
  386. package/dist/react-server/plugin.js.map +0 -1
  387. package/dist/resolvePage.d.ts.map +0 -1
  388. package/dist/resolveProps.d.ts.map +0 -1
  389. package/dist/scripts/check-react-version.js +0 -34
  390. package/dist/scripts/check-react-version.js.map +0 -1
  391. package/dist/src/build/createBuildConfig.js +0 -44
  392. package/dist/src/build/createBuildConfig.js.map +0 -1
  393. package/dist/src/build/mergeInputs.js +0 -16
  394. package/dist/src/build/mergeInputs.js.map +0 -1
  395. package/dist/src/checkFilesExist.js +0 -61
  396. package/dist/src/checkFilesExist.js.map +0 -1
  397. package/dist/src/collect-css-manifest.js.map +0 -1
  398. package/dist/src/components.js.map +0 -1
  399. package/dist/src/getEnv.js +0 -76
  400. package/dist/src/getEnv.js.map +0 -1
  401. package/dist/src/helpers/inputNormalizer.js +0 -11
  402. package/dist/src/helpers/inputNormalizer.js.map +0 -1
  403. package/dist/src/helpers/normalizedRelativePath.js +0 -34
  404. package/dist/src/helpers/normalizedRelativePath.js.map +0 -1
  405. package/dist/src/helpers/tryManifest.js.map +0 -1
  406. package/dist/src/html/createPageLoader.js +0 -72
  407. package/dist/src/html/createPageLoader.js.map +0 -1
  408. package/dist/src/options.js +0 -307
  409. package/dist/src/options.js.map +0 -1
  410. package/dist/src/react-server/createHandler.js.map +0 -1
  411. package/dist/src/react-server/createRscStream.js.map +0 -1
  412. package/dist/src/resolvePage.js.map +0 -1
  413. package/dist/src/resolveProps.js.map +0 -1
  414. package/dist/src/worker/createHtmlStream.js +0 -62
  415. package/dist/src/worker/createHtmlStream.js.map +0 -1
  416. package/dist/src/worker/createWorker.js +0 -34
  417. package/dist/src/worker/createWorker.js.map +0 -1
  418. package/dist/src/worker/renderPages.js +0 -99
  419. package/dist/src/worker/renderPages.js.map +0 -1
  420. package/dist/transformer/index.d.ts.map +0 -1
  421. package/dist/transformer/preserveDirectives.d.ts +0 -4
  422. package/dist/transformer/preserveDirectives.d.ts.map +0 -1
  423. package/dist/transformer/preserver.d.ts +0 -2
  424. package/dist/transformer/preserver.d.ts.map +0 -1
  425. package/dist/transformer/transformer.d.ts.map +0 -1
  426. package/dist/transformer/types.d.ts.map +0 -1
  427. package/dist/worker/createHtmlStream.d.ts +0 -7
  428. package/dist/worker/createHtmlStream.d.ts.map +0 -1
  429. package/dist/worker/createWorker.d.ts +0 -10
  430. package/dist/worker/createWorker.d.ts.map +0 -1
  431. package/dist/worker/loader.d.ts.map +0 -1
  432. package/dist/worker/loader.js +0 -7
  433. package/dist/worker/loader.js.map +0 -1
  434. package/dist/worker/renderPages.d.ts +0 -18
  435. package/dist/worker/renderPages.d.ts.map +0 -1
  436. package/dist/worker/types.d.ts +0 -31
  437. package/dist/worker/types.d.ts.map +0 -1
  438. package/dist/worker/worker.d.ts +0 -7
  439. package/dist/worker/worker.d.ts.map +0 -1
  440. package/dist/worker/worker.js +0 -112
  441. package/dist/worker/worker.js.map +0 -1
  442. package/src/build/createBuildConfig.ts +0 -57
  443. package/src/build/mergeInputs.ts +0 -42
  444. package/src/checkFilesExist.ts +0 -67
  445. package/src/helpers/inputNormalizer.ts +0 -22
  446. package/src/index.ts +0 -4
  447. package/src/options.ts +0 -423
  448. package/src/plugin.ts +0 -5
  449. package/src/react-client/plugin.ts +0 -34
  450. package/src/react-server/plugin.ts +0 -409
  451. package/src/transformer/index.ts +0 -112
  452. package/src/transformer/preserveDirectives.ts +0 -100
  453. package/src/transformer/preserver.ts +0 -47
  454. package/src/worker/createHtmlStream.ts +0 -76
  455. package/src/worker/createWorker.ts +0 -44
  456. package/src/worker/renderPages.ts +0 -144
  457. package/src/worker/types.ts +0 -38
  458. package/src/worker/worker.tsx +0 -136
  459. /package/dist/{assertServerCondition.d.ts → plugin/assertServerCondition.d.ts} +0 -0
  460. /package/dist/{components.d.ts → plugin/components.d.ts} +0 -0
  461. /package/dist/{copy-dir.d.ts → plugin/copy-dir.d.ts} +0 -0
  462. /package/dist/{getEnv.d.ts → plugin/getEnv.d.ts} +0 -0
  463. /package/dist/{helpers → plugin/helpers}/normalizedRelativePath.d.ts +0 -0
  464. /package/dist/{helpers → plugin/helpers}/tryManifest.d.ts +0 -0
  465. /package/dist/{manifest.d.ts → plugin/manifest.d.ts} +0 -0
  466. /package/dist/{module-graph.d.ts → plugin/module-graph.d.ts} +0 -0
  467. /package/dist/{react-server → plugin/react-server}/createDevMiddleware.d.ts +0 -0
  468. /package/dist/{react-server → plugin/react-server}/createDevServer.d.ts +0 -0
  469. /package/dist/{react-server → plugin/react-server}/createReactNodeStreamer.d.ts +0 -0
  470. /package/dist/{resolvePage.d.ts → plugin/resolvePage.d.ts} +0 -0
  471. /package/dist/{resolveProps.d.ts → plugin/resolveProps.d.ts} +0 -0
  472. /package/dist/{transformer → plugin/transformer}/types.d.ts +0 -0
  473. /package/dist/{worker → plugin/worker}/loader.d.ts +0 -0
  474. /package/{src → plugin}/assertServerCondition.ts +0 -0
  475. /package/{src → plugin}/components.tsx +0 -0
  476. /package/{src → plugin}/copy-dir.ts +0 -0
  477. /package/{src/server.tsx → plugin/getCondition.ts} +0 -0
  478. /package/{src → plugin}/helpers/normalizedRelativePath.ts +0 -0
  479. /package/{src → plugin}/helpers/tryManifest.ts +0 -0
  480. /package/{src → plugin}/manifest.ts +0 -0
  481. /package/{src → plugin}/module-graph.ts +0 -0
  482. /package/{src → plugin}/transformer/README.md +0 -0
  483. /package/{src → plugin}/transformer/types.ts +0 -0
  484. /package/{src → plugin}/worker/loader.ts +0 -0
@@ -3,11 +3,22 @@ import {
3
3
  collectManifestCss,
4
4
  collectModuleGraphCss,
5
5
  } from "../collect-css-manifest.js";
6
- import { DEFAULT_CONFIG } from "../options.js";
6
+ import { DEFAULT_CONFIG } from "../config/defaults.js";
7
7
  import { resolvePage } from "../resolvePage.js";
8
8
  import { resolveProps } from "../resolveProps.js";
9
9
  import type { CreateHandlerOptions, StreamPluginOptions } from "../types.js";
10
10
  import { createRscStream } from "./createRscStream.js";
11
+ import type { PipeableStream } from "react-dom/server";
12
+
13
+ type CreateHandlerResult =
14
+ | { type: "success"; controller: AbortController; stream: PipeableStream; assets: any; clientPath: string }
15
+ | { type: "error"; error: Error }
16
+ | { type: "skip" };
17
+
18
+ interface HandlerAssets {
19
+ css: Set<string>;
20
+ clientPath: string;
21
+ }
11
22
 
12
23
  export async function createHandler<T>(
13
24
  url: string,
@@ -19,7 +30,7 @@ export async function createHandler<T>(
19
30
  Pick<StreamPluginOptions, "moduleBase" | "moduleBasePath" | "projectRoot">
20
31
  >,
21
32
  streamOptions: CreateHandlerOptions<T>
22
- ) {
33
+ ): Promise<CreateHandlerResult> {
23
34
  const root = pluginOptions.projectRoot ?? process.cwd();
24
35
 
25
36
  const Html = pluginOptions.Html ?? DEFAULT_CONFIG.HTML;
@@ -55,8 +66,7 @@ export async function createHandler<T>(
55
66
  : (id: string) => collectModuleGraphCss(streamOptions.moduleGraph!, id);
56
67
 
57
68
  const loadWithCss = async (id: string) => {
58
- if (!id) return {};
59
-
69
+ console.log('[RSC] Loading module:', id);
60
70
  try {
61
71
  const mod = await streamOptions.loader(id);
62
72
  const pageCss = await Promise.resolve(getCss(id));
@@ -110,35 +120,46 @@ export async function createHandler<T>(
110
120
  error: new Error("Invalid props: " + propsPath, {
111
121
  cause: props,
112
122
  }),
113
- };
123
+ }
114
124
  }
115
125
 
116
126
  // Add any additional CSS files
117
127
  if (streamOptions.cssFiles) {
118
128
  streamOptions.cssFiles.forEach((css) => cssModules.add(css));
119
129
  }
130
+
120
131
  const stream = createRscStream({
121
132
  Html: Html,
122
133
  Page: Page,
123
134
  props: props,
124
- moduleBasePath: pluginOptions.moduleBasePath, // eg /src
135
+ moduleBasePath: pluginOptions.moduleBasePath,
125
136
  logger: streamOptions.logger ?? createLogger(),
126
137
  cssFiles: Array.from(cssModules),
127
138
  route: url,
128
139
  url,
129
140
  pipableStreamOptions: streamOptions.pipableStreamOptions,
141
+ htmlProps: {
142
+ pageProps: props,
143
+ route: url,
144
+ url: url,
145
+ },
130
146
  });
131
147
 
132
148
  if (!stream) {
149
+ console.log("[createHandler] No stream created for route:", url);
133
150
  return { type: "skip" as const };
134
151
  }
135
152
 
153
+ const assets: HandlerAssets = {
154
+ css: new Set(cssFiles ?? []),
155
+ clientPath: pagePath ?? ''
156
+ };
157
+
136
158
  return {
137
- type: "success" as const,
159
+ type: "success",
138
160
  controller,
139
161
  stream,
140
- assets: {
141
- css: cssFiles,
142
- },
162
+ assets,
163
+ clientPath: assets.clientPath,
143
164
  };
144
165
  }
@@ -2,6 +2,7 @@ import React from "react";
2
2
  import {
3
3
  createFromNodeStream,
4
4
  type CreateFromNodeStreamOptions,
5
+ // @ts-ignore
5
6
  } from "react-server-dom-esm/client.node";
6
7
  import type { Readable } from "stream";
7
8
 
@@ -1,10 +1,9 @@
1
- import React from "react";
2
- import {
3
- renderToPipeableStream,
4
- type PipeableStream,
5
- } from "react-server-dom-esm/server.node";
1
+ import * as React from "react";
2
+ // @ts-ignore
3
+ import { renderToPipeableStream } from "react-server-dom-esm/server.node";
6
4
  import { CssCollector } from "../components.js";
7
5
  import type { RscStreamOptions } from "../types.js";
6
+ import type { PipeableStream } from "react-dom/server";
8
7
 
9
8
  export function createRscStream(
10
9
  streamOptions: RscStreamOptions
@@ -15,11 +14,11 @@ export function createRscStream(
15
14
  props,
16
15
  logger,
17
16
  cssFiles,
18
- route,
19
- url,
20
17
  moduleBasePath,
21
18
  pipableStreamOptions,
19
+ htmlProps,
22
20
  } = streamOptions;
21
+
23
22
  const css = Array.isArray(cssFiles)
24
23
  ? cssFiles.map((css, index) =>
25
24
  React.createElement(CssCollector, {
@@ -33,10 +32,7 @@ export function createRscStream(
33
32
  Html,
34
33
  {
35
34
  key: "html",
36
- pageProps: props,
37
- moduleBasePath: moduleBasePath,
38
- route,
39
- url,
35
+ ...htmlProps
40
36
  },
41
37
  React.createElement(Page, { key: "page", ...props }),
42
38
  ...css
@@ -46,7 +42,7 @@ export function createRscStream(
46
42
  onError: logger?.error ?? console.error,
47
43
  onPostpone: logger?.info ?? console.info,
48
44
  environmentName: "Server",
49
- ...pipableStreamOptions,
45
+ ...pipableStreamOptions
50
46
  }
51
47
  );
52
48
  }
@@ -1,14 +1,12 @@
1
- import { dirname, join, resolve } from "node:path";
1
+ import { join, resolve } from "node:path";
2
2
  import { Writable } from "node:stream";
3
- import { fileURLToPath } from "node:url";
4
3
  import { Worker } from "node:worker_threads";
5
4
  import { type ViteDevServer } from "vite";
6
- import { DEFAULT_CONFIG } from "../options.js";
5
+ import { DEFAULT_CONFIG } from "../config/defaults.js";
7
6
  import type { RequestHandler, StreamPluginOptions } from "../types.js";
8
7
  import type { WorkerRscChunkMessage } from "../worker/types.js";
9
8
  import { createHandler } from "./createHandler.js";
10
9
 
11
- const __dirname = dirname(fileURLToPath(import.meta.url));
12
10
 
13
11
  export function createSsrHandler(
14
12
  options: Required<
@@ -20,17 +18,16 @@ export function createSsrHandler(
20
18
  Required<
21
19
  Pick<
22
20
  StreamPluginOptions,
23
- "moduleBase" | "moduleBasePath" | "moduleBaseURL" | "projectRoot"
21
+ "moduleBase" | "moduleBasePath" | "moduleBaseURL" | "projectRoot" | "htmlWorkerPath"
24
22
  >
25
- > &
26
- Pick<StreamPluginOptions, "workerPath">,
23
+ >,
27
24
  server: ViteDevServer,
28
- clientComponents: Map<string, string>
25
+ _clientComponents: Map<string, string>
29
26
  ): RequestHandler {
30
27
  const worker = new Worker(
31
- options?.workerPath
32
- ? resolve(server.config.root, options?.workerPath)
33
- : DEFAULT_CONFIG.WORKER_PATH,
28
+ options?.htmlWorkerPath
29
+ ? resolve(server.config.root, options?.htmlWorkerPath)
30
+ : DEFAULT_CONFIG.HTML_WORKER_PATH,
34
31
  {
35
32
  env: {
36
33
  NODE_OPTIONS: "--conditions ''",
@@ -70,16 +67,16 @@ export function createSsrHandler(
70
67
  moduleGraph: server.moduleGraph,
71
68
  }
72
69
  );
73
- const moduleBasePath = join(
70
+ const moduleRootPath = join(
74
71
  server.config.cacheDir,
75
72
  options.moduleBasePath
76
73
  );
77
- const htmlOutputPath = join(
78
- server.config.cacheDir,
79
- server.config.build.outDir,
80
- req.url,
81
- "index.html"
82
- );
74
+ // const htmlOutputPath = join(
75
+ // server.config.cacheDir,
76
+ // server.config.build.outDir,
77
+ // req.url,
78
+ // "index.html"
79
+ // );
83
80
  if (result.type !== "success") {
84
81
  throw new Error(
85
82
  result.type === "error" ? String(result.error) : "Skipped"
@@ -114,7 +111,7 @@ export function createSsrHandler(
114
111
  type: "RSC_CHUNK",
115
112
  id: req.url ?? "/",
116
113
  chunk: rscData,
117
- moduleBasePath,
114
+ moduleRootPath: moduleRootPath,
118
115
  moduleBaseURL: options.moduleBaseURL,
119
116
  // Don't need file paths in dev mode
120
117
  outDir: "",
@@ -0,0 +1,12 @@
1
+ import { reactPreservePlugin } from "../preserver/plugin.js";
2
+ import { reactTransformPlugin } from "../transformer/plugin.js";
3
+ import type { StreamPluginOptions } from "../types.js";
4
+ import { reactServerPlugin } from "./plugin.js";
5
+
6
+ export function vitePluginReactServer(options = {} as StreamPluginOptions): import("vite").Plugin[] {
7
+ return [
8
+ reactTransformPlugin(options),
9
+ reactServerPlugin(options),
10
+ reactPreservePlugin(options),
11
+ ];
12
+ }
@@ -0,0 +1,464 @@
1
+ import { readdirSync } from "fs";
2
+ import type { ServerResponse } from "node:http";
3
+ import { join, resolve } from "node:path";
4
+ import { performance } from "node:perf_hooks";
5
+ import { Worker } from "node:worker_threads";
6
+ import React from "react";
7
+ import {
8
+ createLogger,
9
+ type ResolvedConfig,
10
+ type UserConfig,
11
+ type ViteDevServer,
12
+ } from "vite";
13
+ import { checkFilesExist } from "../checkFilesExist.js";
14
+ import { DEFAULT_CONFIG } from "../config/defaults.js";
15
+ import { getPluginRoot } from "../config/getPaths.js";
16
+ import { resolveOptions } from "../config/resolveOptions.js";
17
+ import { resolvePages } from "../config/resolvePages.js";
18
+ import { resolveUserConfig } from "../config/resolveUserConfig.js";
19
+ import { getModuleManifest } from "../helpers/getModuleManifest.js";
20
+ import { tryManifest } from "../helpers/tryManifest.js";
21
+ import { createBuildLoader } from '../loader/createBuildLoader.js';
22
+ import type {
23
+ BuildTiming,
24
+ CheckFilesExistReturn,
25
+ ReactStreamPluginMeta,
26
+ ResolvedUserConfig,
27
+ ResolvedUserOptions,
28
+ } from "../types.js";
29
+ import { type StreamPluginOptions } from "../types.js";
30
+ import { createWorker } from "../worker/createWorker.js";
31
+ import { renderPages } from "../worker/html/renderPages.js";
32
+ import { createHandler } from "./createHandler.js";
33
+
34
+ export function reactServerPlugin(
35
+ options: StreamPluginOptions
36
+ ): import("vite").Plugin<{ meta: ReactStreamPluginMeta, addCssFile: (path: string) => void } > {
37
+ const timing: BuildTiming = {
38
+ start: performance.now(),
39
+ };
40
+
41
+ let files: CheckFilesExistReturn;
42
+ // let env: Awaited<ReturnType<typeof getEnv>>;
43
+ let worker: Worker;
44
+ let finalConfig: ResolvedConfig;
45
+ let cssModules = new Set<string>();
46
+ let clientComponents = new Map<string, string>();
47
+ // let define: Record<string, string>;
48
+ let buildCssFiles = new Set<string>();
49
+ let root: string = process.cwd();
50
+ let userConfig: ResolvedUserConfig;
51
+ let userOptions: ResolvedUserOptions;
52
+ let moduleGraph: Record<
53
+ string,
54
+ {
55
+ file: string;
56
+ src: string;
57
+ name: string;
58
+ isEntry: boolean;
59
+ imports: string[];
60
+ dynamicImports: string[];
61
+ }
62
+ > = {};
63
+
64
+ interface BuildStats {
65
+ htmlFiles: number;
66
+ clientComponents: number;
67
+ cssFiles: number;
68
+ totalRoutes: number;
69
+ timing: {
70
+ config: number;
71
+ build: number;
72
+ render: number;
73
+ total: number;
74
+ };
75
+ }
76
+
77
+ const resolvedOptions = resolveOptions(options);
78
+ if (resolvedOptions.type === "error") {
79
+ throw resolvedOptions.error;
80
+ }
81
+ userOptions = resolvedOptions.userOptions;
82
+ root = userOptions.projectRoot;
83
+ return {
84
+ name: "vite:react-stream-server",
85
+ enforce: "post",
86
+ api: {
87
+ meta: { timing },
88
+ addCssFile(path: string) {
89
+ buildCssFiles.add(path);
90
+ },
91
+ },
92
+ configResolved(resolvedConfig) {
93
+ finalConfig = resolvedConfig;
94
+ timing.configResolved = performance.now();
95
+ if (finalConfig.command === "build") {
96
+ finalConfig = {
97
+ ...finalConfig,
98
+ mode: "production",
99
+ };
100
+ }
101
+
102
+ // Verify transformer runs first, preserver runs last
103
+ const plugins = finalConfig.plugins;
104
+ const transformerIndex = plugins.findIndex(
105
+ (p) => p.name === "vite:react-stream-transformer"
106
+ );
107
+ const preserverIndex = plugins.findIndex(
108
+ (p) => p.name === "vite-plugin-react-server:preserve-directives"
109
+ );
110
+
111
+ if (transformerIndex === -1) {
112
+ throw new Error("Transformer plugin not installed");
113
+ }
114
+ if (preserverIndex < transformerIndex) {
115
+ throw new Error(
116
+ "Transformer plugin isn't installed or isn't running before preserver"
117
+ );
118
+ }
119
+ },
120
+ async configureServer(server: ViteDevServer) {
121
+ if (server.config.root !== root) {
122
+ console.log(
123
+ "[vite-plugin-react-server] Root dir changed",
124
+ server.config.root,
125
+ root
126
+ );
127
+ root = server.config.root;
128
+ }
129
+
130
+ const activeStreams = new Set<ServerResponse>();
131
+
132
+ // Handle Vite server restarts
133
+ server.ws.on("restart", (path) => {
134
+ console.log(
135
+ "[vite-plugin-react-server] 🔧 Plugin changed, preparing for restart:",
136
+ path
137
+ );
138
+
139
+ // Close streams with restart message
140
+ for (const res of activeStreams) {
141
+ res.writeHead(503, {
142
+ "Content-Type": "text/x-component",
143
+ "Retry-After": "1",
144
+ });
145
+ res.end('{"error":"Server restarting..."}');
146
+ }
147
+ activeStreams.clear();
148
+ });
149
+
150
+ server.ws.on("connection", (_socket, _req) => {
151
+ console.log("[vite-plugin-react-server] hooking up ws connection");
152
+ });
153
+
154
+ server.ws.on("listening", () => {
155
+ console.log("[vite-plugin-react-server] hooking up ws listening");
156
+ });
157
+
158
+ server.middlewares.use(async (req, res, next) => {
159
+ if (req.headers.accept !== "text/x-component") return next();
160
+ console.log("[vite-plugin-react-server] middleware called");
161
+ try {
162
+ const handler = await createHandler(
163
+ req.url ?? "",
164
+ {
165
+ Page: userOptions.Page,
166
+ props: userOptions.props,
167
+ build: userOptions.build,
168
+ Html: React.Fragment,
169
+ pageExportName: userOptions.pageExportName,
170
+ propsExportName: userOptions.propsExportName,
171
+ moduleBase: userOptions.moduleBase,
172
+ moduleBasePath: userOptions.moduleBasePath,
173
+ projectRoot: root,
174
+ },
175
+ {
176
+ cssFiles: Array.from(cssModules),
177
+ logger: createLogger(),
178
+ loader: server.ssrLoadModule,
179
+ moduleGraph: server.moduleGraph,
180
+ }
181
+ );
182
+ if (handler.type === "success") {
183
+ handler.stream?.pipe(res);
184
+ }
185
+ activeStreams.add(res);
186
+ } finally {
187
+ res.on("close", () => {
188
+ console.log(
189
+ "[vite-plugin-react-server] ➖ Stream closed for:",
190
+ req.url
191
+ );
192
+ activeStreams.delete(res);
193
+ });
194
+ }
195
+ });
196
+ },
197
+ async config(config, configEnv): Promise<UserConfig> {
198
+ if (typeof config.root === "string" && config.root !== root) {
199
+ console.log(
200
+ "[vite-plugin-react-server] Root dir changed",
201
+ config.root,
202
+ root
203
+ );
204
+ root = config.root;
205
+ }
206
+ const resolvedPages = await resolvePages(userOptions.build.pages);
207
+ if (resolvedPages.type === "error") {
208
+ throw resolvedPages.error;
209
+ }
210
+
211
+ files = await checkFilesExist(resolvedPages.pages, userOptions, root);
212
+
213
+ const resolvedConfig = resolveUserConfig({
214
+ condition: "react-server",
215
+ config,
216
+ configEnv,
217
+ userOptions,
218
+ files,
219
+ });
220
+
221
+ if (resolvedConfig.type === "error") {
222
+ throw resolvedConfig.error;
223
+ }
224
+
225
+ userConfig = resolvedConfig.userConfig;
226
+
227
+ return resolvedConfig.userConfig;
228
+ },
229
+ async buildStart() {
230
+ if (!timing.buildStart) {
231
+ timing.buildStart = performance.now();
232
+ } else {
233
+ console.log("Build already started");
234
+ }
235
+ },
236
+ async closeBundle() {
237
+ if (!userConfig || finalConfig.command !== "build") return;
238
+ try {
239
+ timing.renderStart = performance.now();
240
+
241
+ const resolvedServerManifest = tryManifest({
242
+ root,
243
+ outDir: userOptions.build.server,
244
+ ssrManifest: false,
245
+ });
246
+
247
+ const serverManifest =
248
+ resolvedServerManifest.type === "error"
249
+ ? getModuleManifest.bind(this)()
250
+ : resolvedServerManifest.manifest;
251
+ // Get worker path from manifest
252
+ // Look for the html-worker entry directly
253
+ let htmlWorkerPath = serverManifest["html-worker"]?.file;
254
+
255
+ if (options.htmlWorkerPath) {
256
+ htmlWorkerPath = join(root, options.htmlWorkerPath);
257
+ } else {
258
+ htmlWorkerPath = join(
259
+ getPluginRoot(),
260
+ DEFAULT_CONFIG.HTML_WORKER_PATH
261
+ );
262
+ }
263
+
264
+ // Initialize worker with path from server directory
265
+ const workerPath = resolve(
266
+ root,
267
+ userOptions.build.server,
268
+ htmlWorkerPath
269
+ );
270
+
271
+ worker = await createWorker({
272
+ projectRoot: userOptions.projectRoot,
273
+ workerPath,
274
+ condition: "react-server",
275
+ reverseCondition: true,
276
+ mode: finalConfig.mode as "production" | "development",
277
+ nodeOptions: "--conditions=react-client",
278
+ });
279
+
280
+ // Get routes directly from pages
281
+ const resolvedPages = await resolvePages(userOptions.build.pages);
282
+ if (resolvedPages.type === "error") {
283
+ throw resolvedPages.error;
284
+ }
285
+ const routes = resolvedPages.pages;
286
+ console.log("[plugin] Routes to render:", routes);
287
+
288
+ const entries = Object.values(serverManifest).filter(
289
+ (entry) => entry.isEntry
290
+ );
291
+ const css = entries.flatMap((entry) => entry.css);
292
+ const loader = createBuildLoader({
293
+ root,
294
+ userConfig,
295
+ pluginContext: this,
296
+ });
297
+
298
+ const { failedRoutes, completedRoutes } = await renderPages(routes, {
299
+ pipableStreamOptions: {
300
+ bootstrapModules: entries.map((entry) => "/" + entry.file),
301
+ },
302
+ moduleBasePath: userOptions.moduleBasePath,
303
+ moduleBaseURL: userOptions.moduleBaseURL,
304
+ clientCss: css?.map((css) => "/" + css) ?? [],
305
+ userConfig,
306
+ pluginOptions: userOptions,
307
+ worker: worker,
308
+ manifest: serverManifest,
309
+ loader,
310
+ onCssFile: (path: string) => {
311
+ if (buildCssFiles && path.endsWith(".css")) {
312
+ buildCssFiles.add(path);
313
+ }
314
+ },
315
+ });
316
+
317
+ // Debug render results
318
+ console.log("[vite-plugin-react-server] Render results:", {
319
+ failedRoutes,
320
+ completedRoutes,
321
+ });
322
+
323
+ if (failedRoutes.size) {
324
+ console.error(
325
+ "[vite-plugin-react-server] Failed to render routes:",
326
+ failedRoutes
327
+ );
328
+ }
329
+ console.log("[vite-plugin-react-server] Render complete");
330
+ console.log("[vite-plugin-react-server] Terminating worker");
331
+ if (worker) await worker.terminate();
332
+
333
+ timing.renderEnd = performance.now();
334
+ timing.total = (timing.renderEnd - timing.start) / 1000;
335
+
336
+ // Collect stats
337
+ const stats: BuildStats = {
338
+ htmlFiles: routes.length,
339
+ clientComponents: clientComponents.size,
340
+ cssFiles: cssModules.size,
341
+ totalRoutes: routes.length,
342
+ timing: {
343
+ config: ((timing.configResolved ?? 0) - timing.start) / 1000,
344
+ build:
345
+ ((timing.buildStart ?? 0) - (timing.configResolved ?? 0)) / 1000,
346
+ render:
347
+ ((timing.renderEnd ?? 0) - (timing.renderStart ?? 0)) / 1000,
348
+ total: (timing.renderEnd ?? 0 - timing.start) / 1000,
349
+ },
350
+ };
351
+
352
+ // Format duration helper
353
+ const formatDuration = (seconds: number) => {
354
+ if (seconds < 0.001) {
355
+ return `${(seconds * 1000000).toFixed(0)}μs`;
356
+ }
357
+ if (seconds < 1) {
358
+ return `${(seconds * 1000).toFixed(0)}ms`;
359
+ }
360
+ return `${seconds.toFixed(2)}s`;
361
+ };
362
+
363
+ console.log("\n[vite-plugin-react-server] Build Summary:");
364
+ console.log("─".repeat(50));
365
+ console.log(`�� Generated ${stats.htmlFiles} HTML files`);
366
+ console.log(`🎯 Processed ${stats.clientComponents} client components`);
367
+ console.log(`🎨 Included ${stats.cssFiles} CSS files`);
368
+ console.log(`🛣️ Total routes: ${stats.totalRoutes}`);
369
+ console.log("─".repeat(50));
370
+ console.log("⏱️ Timing:");
371
+ console.log(` Config: ${formatDuration(stats.timing.config)}`);
372
+ console.log(` Build: ${formatDuration(stats.timing.build)}`);
373
+ console.log(` Render: ${formatDuration(stats.timing.render)}`);
374
+ console.log(" ".repeat(12));
375
+ console.log(` Total: ${formatDuration(stats.timing.total)}`);
376
+ console.log("─".repeat(50));
377
+
378
+ // Ensure worker is terminated
379
+ if (worker) {
380
+ console.log("[vite-plugin-react-server] Terminating worker...");
381
+ await worker.terminate();
382
+ worker = null as any;
383
+ }
384
+ } catch (error) {
385
+ console.error("[vite-plugin-react-server] Build failed:", error);
386
+ // Make sure to terminate worker even on error
387
+ if (worker) await worker.terminate();
388
+ throw error;
389
+ }
390
+ },
391
+ async buildEnd(error) {
392
+ if (error) {
393
+ console.error("[vite-plugin-react-server] Build error:", error);
394
+ }
395
+ if (worker) await worker.terminate();
396
+ },
397
+ handleHotUpdate({ file }) {
398
+ if (file.endsWith(".css")) {
399
+ cssModules.add(file);
400
+ }
401
+ },
402
+ async renderChunk(_code: string, chunk: any, _options: any) {
403
+ const moduleIds = this.getModuleIds();
404
+ const fallbackManifestEntries = Object.fromEntries(
405
+ Array.from(moduleIds)
406
+ .map((module) => {
407
+ const moduleInfo = this.getModuleInfo(module);
408
+ if (!moduleInfo) return [module, null];
409
+ const {
410
+ code,
411
+ id,
412
+ isEntry,
413
+ importedIds,
414
+ dynamicallyImportedIds,
415
+ ...rest
416
+ } = moduleInfo;
417
+ return [
418
+ chunk.name ?? id,
419
+ {
420
+ file: chunk.fileName,
421
+ src: module,
422
+ name: chunk.name ?? id,
423
+ isEntry: chunk.isEntry ?? isEntry,
424
+ imports: chunk.imports ?? importedIds,
425
+ dynamicImports: chunk.dynamicImports ?? dynamicallyImportedIds,
426
+ directive: rest.meta["directive"],
427
+ css: chunk.viteMetadata?.importedCss ?? [],
428
+ assets: chunk.viteMetadata?.importedAssets ?? [],
429
+ },
430
+ ];
431
+ })
432
+ .filter(Boolean)
433
+ );
434
+
435
+ moduleGraph = {
436
+ ...moduleGraph,
437
+ ...fallbackManifestEntries,
438
+ };
439
+
440
+ if (chunk.fileName.includes("html-worker")) {
441
+ const workerPath = resolve(
442
+ root,
443
+ userOptions.build.server,
444
+ chunk.fileName
445
+ );
446
+ console.log("Files in output dir:", {
447
+ files: readdirSync(resolve(root, userOptions.build.server)),
448
+ workerPath,
449
+ });
450
+
451
+ // Don't initialize worker during build phase
452
+ if (finalConfig.command === "build") {
453
+ return null;
454
+ }
455
+
456
+ worker = await createWorker({
457
+ projectRoot: userOptions.projectRoot,
458
+ workerPath,
459
+ });
460
+ }
461
+ return null;
462
+ },
463
+ };
464
+ }