vite-plugin-react-server 0.3.18 → 1.0.0

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 (246) hide show
  1. package/README.md +220 -141
  2. package/bin/patch.mjs +8 -2
  3. package/dist/package.json +15 -8
  4. package/dist/plugin/checkFilesExist.d.ts.map +1 -1
  5. package/dist/plugin/checkFilesExist.js +6 -2
  6. package/dist/plugin/checkFilesExist.js.map +1 -1
  7. package/dist/plugin/collect-manifest-client-files.d.ts +23 -0
  8. package/dist/plugin/collect-manifest-client-files.d.ts.map +1 -0
  9. package/dist/plugin/collect-manifest-client-files.js +131 -0
  10. package/dist/plugin/collect-manifest-client-files.js.map +1 -0
  11. package/dist/plugin/components.d.ts +3 -13
  12. package/dist/plugin/components.d.ts.map +1 -1
  13. package/dist/plugin/components.js +3 -13
  14. package/dist/plugin/config/defaults.d.ts +14 -6
  15. package/dist/plugin/config/defaults.d.ts.map +1 -1
  16. package/dist/plugin/config/defaults.js +9 -5
  17. package/dist/plugin/config/defaults.js.map +1 -1
  18. package/dist/plugin/config/getPaths.d.ts +0 -1
  19. package/dist/plugin/config/getPaths.d.ts.map +1 -1
  20. package/dist/plugin/config/getPaths.js +2 -7
  21. package/dist/plugin/config/getPaths.js.map +1 -1
  22. package/dist/plugin/config/mimeTypes.d.ts +2 -0
  23. package/dist/plugin/config/mimeTypes.d.ts.map +1 -0
  24. package/dist/plugin/config/mimeTypes.js +24 -0
  25. package/dist/plugin/config/mimeTypes.js.map +1 -0
  26. package/dist/plugin/config/resolveOptions.d.ts +2 -2
  27. package/dist/plugin/config/resolveOptions.d.ts.map +1 -1
  28. package/dist/plugin/config/resolveOptions.js +47 -28
  29. package/dist/plugin/config/resolveOptions.js.map +1 -1
  30. package/dist/plugin/config/resolvePages.d.ts +1 -0
  31. package/dist/plugin/config/resolvePages.d.ts.map +1 -1
  32. package/dist/plugin/config/resolvePages.js +9 -5
  33. package/dist/plugin/config/resolvePages.js.map +1 -1
  34. package/dist/plugin/config/resolveUserConfig.d.ts +2 -1
  35. package/dist/plugin/config/resolveUserConfig.d.ts.map +1 -1
  36. package/dist/plugin/config/resolveUserConfig.js +181 -147
  37. package/dist/plugin/config/resolveUserConfig.js.map +1 -1
  38. package/dist/plugin/copy-dir.js +23 -18
  39. package/dist/plugin/copy-dir.js.map +1 -0
  40. package/dist/plugin/css-collector-inline.d.ts +10 -0
  41. package/dist/plugin/css-collector-inline.d.ts.map +1 -0
  42. package/dist/plugin/css-collector-inline.js +55 -0
  43. package/dist/plugin/css-collector-inline.js.map +1 -0
  44. package/dist/plugin/css-collector.d.ts +14 -0
  45. package/dist/plugin/css-collector.d.ts.map +1 -0
  46. package/dist/plugin/css-collector.js +49 -0
  47. package/dist/plugin/css-collector.js.map +1 -0
  48. package/dist/plugin/helpers/createHandler.d.ts +17 -0
  49. package/dist/plugin/helpers/createHandler.d.ts.map +1 -0
  50. package/dist/plugin/helpers/createHandler.js +111 -0
  51. package/dist/plugin/helpers/createHandler.js.map +1 -0
  52. package/dist/plugin/helpers/createRscStream.d.ts +27 -0
  53. package/dist/plugin/helpers/createRscStream.d.ts.map +1 -0
  54. package/dist/plugin/helpers/createRscStream.js +80 -0
  55. package/dist/plugin/helpers/createRscStream.js.map +1 -0
  56. package/dist/plugin/helpers/getBundleManifest.d.ts.map +1 -1
  57. package/dist/plugin/helpers/getBundleManifest.js +12 -4
  58. package/dist/plugin/helpers/getBundleManifest.js.map +1 -1
  59. package/dist/plugin/html.d.ts +5 -0
  60. package/dist/plugin/html.d.ts.map +1 -0
  61. package/dist/plugin/html.js +11 -0
  62. package/dist/plugin/html.js.map +1 -0
  63. package/dist/plugin/loader/createBuildLoader.d.ts +1 -1
  64. package/dist/plugin/loader/createBuildLoader.d.ts.map +1 -1
  65. package/dist/plugin/loader/createBuildLoader.js +8 -5
  66. package/dist/plugin/loader/createBuildLoader.js.map +1 -1
  67. package/dist/plugin/loader/css-loader.d.ts.map +1 -1
  68. package/dist/plugin/loader/css-loader.js.map +1 -1
  69. package/dist/plugin/loader/react-loader.js +2 -2
  70. package/dist/plugin/loader/react-loader.js.map +1 -1
  71. package/dist/plugin/preserver/plugin.d.ts.map +1 -1
  72. package/dist/plugin/preserver/plugin.js +49 -14
  73. package/dist/plugin/preserver/plugin.js.map +1 -1
  74. package/dist/plugin/react-client/plugin.d.ts.map +1 -1
  75. package/dist/plugin/react-client/plugin.js +21 -78
  76. package/dist/plugin/react-client/plugin.js.map +1 -1
  77. package/dist/plugin/react-server/index.d.ts.map +1 -1
  78. package/dist/plugin/react-server/index.js +2 -0
  79. package/dist/plugin/react-server/index.js.map +1 -1
  80. package/dist/plugin/react-server/plugin.d.ts +2 -1
  81. package/dist/plugin/react-server/plugin.d.ts.map +1 -1
  82. package/dist/plugin/react-server/plugin.js +92 -225
  83. package/dist/plugin/react-server/plugin.js.map +1 -1
  84. package/dist/plugin/react-static/index.d.ts +2 -0
  85. package/dist/plugin/react-static/index.d.ts.map +1 -0
  86. package/dist/plugin/react-static/index.js +1 -0
  87. package/dist/plugin/react-static/plugin.d.ts +7 -0
  88. package/dist/plugin/react-static/plugin.d.ts.map +1 -0
  89. package/dist/plugin/react-static/plugin.js +207 -0
  90. package/dist/plugin/react-static/plugin.js.map +1 -0
  91. package/dist/plugin/react-static/types.d.ts +2 -0
  92. package/dist/plugin/react-static/types.d.ts.map +1 -0
  93. package/dist/plugin/resolvePage.d.ts.map +1 -1
  94. package/dist/plugin/resolvePage.js +9 -0
  95. package/dist/plugin/resolvePage.js.map +1 -1
  96. package/dist/plugin/root.d.ts +2 -0
  97. package/dist/plugin/root.d.ts.map +1 -0
  98. package/dist/plugin/root.js +12 -0
  99. package/dist/plugin/root.js.map +1 -0
  100. package/dist/plugin/transformer/plugin.d.ts.map +1 -1
  101. package/dist/plugin/transformer/plugin.js +32 -23
  102. package/dist/plugin/transformer/plugin.js.map +1 -1
  103. package/dist/plugin/transformer/types.d.ts +1 -18
  104. package/dist/plugin/transformer/types.d.ts.map +1 -1
  105. package/dist/plugin/types.d.ts +70 -15
  106. package/dist/plugin/types.d.ts.map +1 -1
  107. package/dist/plugin/worker/createWorker.js +0 -1
  108. package/dist/plugin/worker/createWorker.js.map +1 -1
  109. package/dist/plugin/worker/html/html-worker.development.d.ts +30 -0
  110. package/dist/plugin/worker/html/html-worker.development.d.ts.map +1 -1
  111. package/dist/plugin/worker/html/html-worker.development.js +30 -2
  112. package/dist/plugin/worker/html/html-worker.development.js.map +1 -1
  113. package/dist/plugin/worker/html/html-worker.production.js +3 -5
  114. package/dist/plugin/worker/html/html-worker.production.js.map +1 -1
  115. package/dist/plugin/worker/html/messageHandler.d.ts.map +1 -1
  116. package/dist/plugin/worker/html/messageHandler.js +12 -3
  117. package/dist/plugin/worker/html/messageHandler.js.map +1 -1
  118. package/dist/plugin/worker/html/renderPages.d.ts +13 -26
  119. package/dist/plugin/worker/html/renderPages.d.ts.map +1 -1
  120. package/dist/plugin/worker/html/renderPages.js +138 -86
  121. package/dist/plugin/worker/html/renderPages.js.map +1 -1
  122. package/dist/plugin/worker/rsc/messageHandler.d.ts.map +1 -1
  123. package/dist/plugin/worker/rsc/messageHandler.js +104 -84
  124. package/dist/plugin/worker/rsc/messageHandler.js.map +1 -1
  125. package/dist/plugin/worker/rsc/rsc-worker.development.js +13 -18
  126. package/dist/plugin/worker/rsc/rsc-worker.development.js.map +1 -1
  127. package/dist/plugin/worker/rsc/rsc-worker.production.js +4 -1
  128. package/dist/plugin/worker/rsc/rsc-worker.production.js.map +1 -1
  129. package/dist/plugin/worker/rsc/state.d.ts.map +1 -1
  130. package/dist/plugin/worker/rsc/state.js.map +1 -1
  131. package/dist/plugin/worker/types.d.ts +3 -0
  132. package/dist/plugin/worker/types.d.ts.map +1 -1
  133. package/dist/tsconfig.tsbuildinfo +1 -1
  134. package/package.json +14 -6
  135. package/plugin/checkFilesExist.ts +7 -3
  136. package/plugin/collect-manifest-client-files.ts +167 -0
  137. package/plugin/components.ts +3 -0
  138. package/plugin/config/defaults.tsx +70 -0
  139. package/plugin/config/getPaths.ts +1 -7
  140. package/plugin/config/mimeTypes.ts +17 -0
  141. package/plugin/config/resolveOptions.ts +58 -45
  142. package/plugin/config/resolvePages.ts +8 -4
  143. package/plugin/config/resolveUserConfig.ts +220 -176
  144. package/plugin/css-collector-inline.tsx +60 -0
  145. package/plugin/css-collector.tsx +62 -0
  146. package/plugin/helpers/createHandler.ts +135 -0
  147. package/plugin/helpers/createRscStream.ts +109 -0
  148. package/plugin/helpers/getBundleManifest.ts +14 -5
  149. package/plugin/html.tsx +9 -0
  150. package/plugin/loader/createBuildLoader.ts +9 -6
  151. package/plugin/loader/css-loader.ts +0 -2
  152. package/plugin/loader/react-loader.ts +2 -2
  153. package/plugin/preserver/plugin.ts +64 -17
  154. package/plugin/react-client/plugin.ts +23 -93
  155. package/plugin/react-server/index.ts +2 -0
  156. package/plugin/react-server/plugin.ts +111 -302
  157. package/plugin/react-static/index.ts +1 -0
  158. package/plugin/react-static/plugin.ts +256 -0
  159. package/plugin/react-static/types.ts +3 -0
  160. package/plugin/resolvePage.ts +9 -0
  161. package/plugin/root.ts +4 -0
  162. package/plugin/transformer/plugin.ts +40 -31
  163. package/plugin/transformer/types.ts +0 -19
  164. package/plugin/types.ts +77 -16
  165. package/plugin/worker/createWorker.ts +1 -1
  166. package/plugin/worker/html/README.md +63 -0
  167. package/plugin/worker/html/html-worker.development.tsx +89 -2
  168. package/plugin/worker/html/html-worker.production.tsx +8 -10
  169. package/plugin/worker/html/messageHandler.ts +12 -3
  170. package/plugin/worker/html/renderPages.ts +178 -138
  171. package/plugin/worker/rsc/README.md +58 -0
  172. package/plugin/worker/rsc/messageHandler.tsx +112 -113
  173. package/plugin/worker/rsc/rsc-worker.development.ts +12 -22
  174. package/plugin/worker/rsc/rsc-worker.production.ts +5 -1
  175. package/plugin/worker/rsc/state.ts +0 -3
  176. package/plugin/worker/types.ts +3 -0
  177. package/scripts/react+0.0.0-experimental-eda36a1c-20250228.patch +114 -12
  178. package/scripts/react-dom+0.0.0-experimental-eda36a1c-20250228.patch +10571 -121
  179. package/tsconfig.json +2 -2
  180. package/dist/plugin/collect-css-manifest.d.ts +0 -4
  181. package/dist/plugin/collect-css-manifest.d.ts.map +0 -1
  182. package/dist/plugin/collect-css-manifest.js +0 -65
  183. package/dist/plugin/collect-css-manifest.js.map +0 -1
  184. package/dist/plugin/config/createModuleIdGenerator.d.ts +0 -11
  185. package/dist/plugin/config/createModuleIdGenerator.d.ts.map +0 -1
  186. package/dist/plugin/config/createModuleIdGenerator.js +0 -44
  187. package/dist/plugin/config/createModuleIdGenerator.js.map +0 -1
  188. package/dist/plugin/getEnv.d.ts +0 -19
  189. package/dist/plugin/getEnv.d.ts.map +0 -1
  190. package/dist/plugin/getEnv.js +0 -107
  191. package/dist/plugin/loader/createCssLoader.d.ts +0 -30
  192. package/dist/plugin/loader/createCssLoader.d.ts.map +0 -1
  193. package/dist/plugin/loader/createCssLoader.js +0 -35
  194. package/dist/plugin/loader/createPageLoader.d.ts +0 -24
  195. package/dist/plugin/loader/createPageLoader.d.ts.map +0 -1
  196. package/dist/plugin/loader/createPageLoader.js +0 -50
  197. package/dist/plugin/loader/rsc/messageHandler.d.ts +0 -2
  198. package/dist/plugin/loader/rsc/messageHandler.d.ts.map +0 -1
  199. package/dist/plugin/loader/rsc/rsc-worker.development.d.ts +0 -2
  200. package/dist/plugin/loader/rsc/rsc-worker.development.d.ts.map +0 -1
  201. package/dist/plugin/loader/rsc/rsc-worker.development.js +0 -1
  202. package/dist/plugin/module-graph.d.ts +0 -10
  203. package/dist/plugin/module-graph.d.ts.map +0 -1
  204. package/dist/plugin/module-graph.js +0 -35
  205. package/dist/plugin/react-server/createHandler.d.ts +0 -17
  206. package/dist/plugin/react-server/createHandler.d.ts.map +0 -1
  207. package/dist/plugin/react-server/createHandler.js +0 -126
  208. package/dist/plugin/react-server/createHandler.js.map +0 -1
  209. package/dist/plugin/react-server/createRscStream.d.ts +0 -16
  210. package/dist/plugin/react-server/createRscStream.d.ts.map +0 -1
  211. package/dist/plugin/react-server/createRscStream.js +0 -70
  212. package/dist/plugin/react-server/createRscStream.js.map +0 -1
  213. package/dist/plugin/react-server/createSsrHandler.d.ts +0 -4
  214. package/dist/plugin/react-server/createSsrHandler.d.ts.map +0 -1
  215. package/dist/plugin/react-server/createSsrHandler.js +0 -95
  216. package/dist/plugin/utils/logger.d.ts +0 -9
  217. package/dist/plugin/utils/logger.d.ts.map +0 -1
  218. package/dist/plugin/utils/logger.js +0 -68
  219. package/dist/plugin/utils/logger.js.map +0 -1
  220. package/dist/plugin/worker/html/plugin.d.ts +0 -4
  221. package/dist/plugin/worker/html/plugin.d.ts.map +0 -1
  222. package/dist/plugin/worker/html/plugin.js +0 -94
  223. package/dist/plugin/worker/plugin.d.ts +0 -19
  224. package/dist/plugin/worker/plugin.d.ts.map +0 -1
  225. package/dist/plugin/worker/plugin.js +0 -23
  226. package/dist/plugin/worker/rsc/plugin.d.ts +0 -4
  227. package/dist/plugin/worker/rsc/plugin.d.ts.map +0 -1
  228. package/dist/plugin/worker/rsc/plugin.js +0 -76
  229. package/plugin/collect-css-manifest.ts +0 -82
  230. package/plugin/components.tsx +0 -14
  231. package/plugin/config/createModuleIdGenerator.ts +0 -52
  232. package/plugin/config/defaults.ts +0 -51
  233. package/plugin/getEnv.ts +0 -135
  234. package/plugin/loader/createCssLoader.ts +0 -73
  235. package/plugin/loader/createPageLoader.ts +0 -103
  236. package/plugin/loader/rsc/messageHandler.tsx +0 -1
  237. package/plugin/loader/rsc/rsc-worker.development.ts +0 -1
  238. package/plugin/module-graph.ts +0 -48
  239. package/plugin/react-server/createHandler.ts +0 -162
  240. package/plugin/react-server/createRscStream.ts +0 -86
  241. package/plugin/react-server/createSsrHandler.ts +0 -125
  242. package/plugin/utils/logger.ts +0 -52
  243. package/plugin/worker/html/plugin.ts +0 -101
  244. package/plugin/worker/plugin.ts +0 -26
  245. package/plugin/worker/rsc/plugin.ts +0 -84
  246. /package/dist/plugin/{loader/rsc/messageHandler.js → react-static/types.js} +0 -0
@@ -1,162 +0,0 @@
1
- import type { PipeableStream } from "react-dom/server";
2
- import { createLogger } from "vite";
3
- import {
4
- collectManifestCss,
5
- collectModuleGraphCss,
6
- } from "../collect-css-manifest.js";
7
- import { DEFAULT_CONFIG } from "../config/defaults.js";
8
- import { resolvePage } from "../resolvePage.js";
9
- import { resolveProps } from "../resolveProps.js";
10
- import type { CreateHandlerOptions, ResolvedUserOptions } from "../types.js";
11
- import { createRscStream } from "./createRscStream.js";
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
- }
22
-
23
- export async function createHandler<T>(
24
- url: string,
25
- pluginOptions: ResolvedUserOptions,
26
- streamOptions: CreateHandlerOptions<T>
27
- ): Promise<CreateHandlerResult> {
28
- const root = pluginOptions.projectRoot ?? process.cwd();
29
-
30
- const Html = pluginOptions.Html ?? DEFAULT_CONFIG.HTML;
31
- const pageExportName =
32
- pluginOptions.pageExportName ?? DEFAULT_CONFIG.PAGE_EXPORT_NAME;
33
- const propsExportName =
34
- pluginOptions.propsExportName ?? DEFAULT_CONFIG.PROPS_EXPORT_NAME;
35
- const controller = new AbortController();
36
-
37
- const cssFiles = streamOptions.cssFiles;
38
- const propsPath =
39
- typeof pluginOptions.props === "function"
40
- ? pluginOptions.props(url)
41
- : pluginOptions.props;
42
- const pagePath =
43
- typeof pluginOptions.Page === "function"
44
- ? pluginOptions.Page(url)
45
- : pluginOptions.Page;
46
-
47
- const cssModules = new Set<string>();
48
-
49
- if (!(streamOptions.serverManifest || streamOptions.moduleGraph))
50
- throw new Error("Missing server manifest or moduleGraph, pass it to options.");
51
-
52
- const getCss = streamOptions.serverManifest
53
- ? (id: string) =>
54
- collectManifestCss(
55
- streamOptions.serverManifest!,
56
- root,
57
- id,
58
- streamOptions.onCssFile
59
- )
60
- : (id: string) => collectModuleGraphCss(streamOptions.moduleGraph!, id);
61
-
62
- const loadWithCss = async (id: string, parentUrl: string) => {
63
- try {
64
- const mod = await streamOptions.loader(id);
65
- const pageCss = await Promise.resolve(getCss(id));
66
- Array.from(pageCss.keys()).forEach((css) => {
67
- cssModules.add(css);
68
- // Notify about new CSS file if callback exists
69
- if (streamOptions.onCssFile) {
70
- streamOptions.onCssFile(css, parentUrl);
71
- }
72
- });
73
- return mod as Record<string, any>;
74
- } catch (e: any) {
75
- if (e.message?.includes("module runner has been closed")) {
76
- return { type: "skip" } as Record<string, any>;
77
- } else {
78
- return { type: "error", error: e } as Record<string, any>;
79
- }
80
- }
81
- };
82
-
83
- const PropsModule = await resolveProps({
84
- propsModule: await loadWithCss(propsPath ?? pagePath, url),
85
- path: String(propsPath ?? pagePath),
86
- exportName: propsExportName,
87
- url,
88
- });
89
- if (PropsModule.type === "error")
90
- return { type: PropsModule.type, error: PropsModule?.error };
91
- if (PropsModule.type === "skip") return { type: PropsModule.type };
92
- const props = PropsModule[propsExportName as keyof typeof PropsModule] as any;
93
- if (props?.type === "error") return { type: props.type, error: props.error };
94
- if (props?.type === "skip") return { type: props.type };
95
-
96
- const PageModule = await resolvePage({
97
- pageModule: await loadWithCss(pagePath, url),
98
- path: pagePath,
99
- exportName: pageExportName,
100
- url,
101
- });
102
- if (PageModule.type === "error")
103
- return { type: PageModule.type, error: PageModule.error };
104
- if (PageModule.type === "skip") return { type: PageModule.type };
105
- const Page = PageModule[pageExportName as keyof typeof PageModule] as any;
106
- if (Page?.type === "error") return { type: Page.type, error: Page.error };
107
- if (Page?.type === "skip") return { type: Page.type };
108
- if (!(typeof Page === "function")) {
109
- return {
110
- type: "error",
111
- error: new Error("Invalid Page component: " + pagePath, {
112
- cause: Page,
113
- }),
114
- };
115
- }
116
- if (!(typeof props === "object")) {
117
- return {
118
- type: "error",
119
- error: new Error("Invalid props: " + propsPath, {
120
- cause: props,
121
- }),
122
- }
123
- }
124
-
125
- // Add any additional CSS files
126
- if (streamOptions.cssFiles) {
127
- streamOptions.cssFiles.forEach((css) => cssModules.add(css));
128
- }
129
- const stream = createRscStream({
130
- Html: Html,
131
- Page: Page,
132
- props: props,
133
- moduleBasePath: '',
134
- logger: streamOptions.logger ?? createLogger(),
135
- cssFiles: Array.from(cssModules),
136
- route: url,
137
- url,
138
- pipableStreamOptions: streamOptions.pipableStreamOptions,
139
- htmlProps: {
140
- pageProps: props,
141
- route: url,
142
- url: url,
143
- },
144
- });
145
-
146
- if (!stream) {
147
- return { type: "skip" as const };
148
- }
149
-
150
- const assets: HandlerAssets = {
151
- css: new Set(cssFiles ?? []),
152
- clientPath: pagePath ?? ''
153
- };
154
-
155
- return {
156
- type: "success",
157
- controller,
158
- stream,
159
- assets,
160
- clientPath: assets.clientPath,
161
- };
162
- }
@@ -1,86 +0,0 @@
1
- import * as React from "react";
2
- // @ts-ignore
3
- import { renderToPipeableStream } from "react-server-dom-esm/server.node";
4
- import type { PipeableStreamOptions } from "../worker/types.js";
5
- import type { Logger } from "vite";
6
-
7
-
8
- // CSS collector component
9
- function CssCollector({
10
- children,
11
- cssFiles,
12
- }: {
13
- children?: React.ReactNode;
14
- cssFiles: string[];
15
- }) {
16
- return React.createElement(
17
- React.Fragment,
18
- null,
19
- ...cssFiles.map((css) => {
20
- const url = css.startsWith('/') || css.startsWith('http') || css.startsWith('./') ? css : '/'+css
21
- return React.createElement('link', {
22
- key: css,
23
- rel: 'stylesheet',
24
- href: url,
25
- precedence: 'high'
26
- })
27
- }),
28
- children
29
- );
30
- }
31
-
32
- export function createRscStream({
33
- Html,
34
- Page,
35
- props,
36
- moduleBasePath,
37
- logger,
38
- cssFiles = [],
39
- route,
40
- url,
41
- pipableStreamOptions,
42
- htmlProps,
43
- }: {
44
- Html: React.ComponentType<any>;
45
- Page: React.ComponentType<any>;
46
- props: any;
47
- moduleBasePath: string;
48
- logger: Logger;
49
- cssFiles?: string[];
50
- route: string;
51
- url: string;
52
- pipableStreamOptions?: PipeableStreamOptions;
53
- htmlProps?: any;
54
- }) {
55
-
56
- const htmlIsFragment = Html == React.Fragment;
57
- const withCss = React.createElement(
58
- CssCollector,
59
- { cssFiles },
60
- React.createElement(Page, props)
61
- )
62
- // Otherwise wrap with Html component
63
- const content = htmlIsFragment
64
- ? withCss
65
- : React.createElement(Html, htmlProps, withCss);
66
- try {
67
- return renderToPipeableStream(
68
- content,
69
- moduleBasePath,
70
- {
71
- onError: (error: Error) => {
72
- if(process.env['NODE_ENV'] === 'development') {
73
- console.trace(error);
74
- }
75
- logger.error(`Stream error at ${route}.`, {error});
76
- },
77
- onPostpone: logger.info ?? console.info,
78
- environmentName: "Server",
79
- ...pipableStreamOptions,
80
- }
81
- );
82
- } catch (error) {
83
- logger.error(`Failed to create stream for ${route}.`, {error: error as Error});
84
- return null;
85
- }
86
- }
@@ -1,125 +0,0 @@
1
- import { join, resolve } from "node:path";
2
- import { Writable } from "node:stream";
3
- import { Worker } from "node:worker_threads";
4
- import { type Manifest, type ViteDevServer } from "vite";
5
- import { DEFAULT_CONFIG } from "../config/defaults.js";
6
- import type { RequestHandler, ResolvedUserOptions } from "../types.js";
7
- import type { WorkerRscChunkMessage } from "../worker/types.js";
8
- import { createHandler } from "./createHandler.js";
9
-
10
-
11
- export function createSsrHandler(
12
- options: ResolvedUserOptions,
13
- server: ViteDevServer,
14
- ): RequestHandler {
15
- const worker = new Worker(
16
- options?.htmlWorkerPath
17
- ? resolve(server.config.root, options?.htmlWorkerPath)
18
- : DEFAULT_CONFIG.HTML_WORKER_PATH,
19
- {
20
- env: {
21
- NODE_OPTIONS: "--conditions ''",
22
- VITE_LOADER_PATH: resolve(
23
- server.config.cacheDir,
24
- "react-stream/worker/loader.js"
25
- ),
26
- },
27
- }
28
- );
29
-
30
- return async function handleSsrRequest(req, res, next) {
31
- if (
32
- !req.url ||
33
- req.url.startsWith("/@") ||
34
- (req.url.includes(".") && !req.url.endsWith(".html"))
35
- ) {
36
- return next();
37
- }
38
-
39
- try {
40
- const result = await createHandler(
41
- req.url ?? "",
42
- {
43
- ...options,
44
- projectRoot: server.config.root,
45
- },
46
- {
47
- loader: server.ssrLoadModule.bind(server),
48
- moduleGraph: server.moduleGraph,
49
- }
50
- );
51
- const moduleRootPath = join(
52
- server.config.cacheDir,
53
- options.moduleBasePath
54
- );
55
- // const htmlOutputPath = join(
56
- // server.config.cacheDir,
57
- // server.config.build.outDir,
58
- // req.url,
59
- // "index.html"
60
- // );
61
- if (result.type !== "success") {
62
- throw new Error(
63
- result.type === "error" ? String(result.error) : "Skipped"
64
- );
65
- }
66
-
67
- // Collect RSC stream data
68
- const rscData = await new Promise<string>((resolve, reject) => {
69
- let data = "";
70
- if (!result.stream) {
71
- resolve(data);
72
- return;
73
- }
74
- const writable = new Writable({
75
- write(chunk, _, callback) {
76
- data += chunk;
77
- callback();
78
- },
79
- final(callback) {
80
- resolve(data);
81
- callback();
82
- },
83
- });
84
-
85
- result.stream.pipe(writable);
86
- writable.on("error", reject);
87
- });
88
-
89
- // Get HTML from worker
90
- const html = await new Promise<string>((resolve, reject) => {
91
- worker.postMessage({
92
- type: "RSC_CHUNK",
93
- id: req.url ?? "/",
94
- chunk: rscData,
95
- moduleRootPath: moduleRootPath,
96
- moduleBaseURL: options.moduleBaseURL,
97
- // Don't need file paths in dev mode
98
- outDir: "",
99
- htmlOutputPath: "",
100
- pipableStreamOptions: options.pipableStreamOptions ?? {},
101
- clientManifest: {},
102
- serverManifest: {},
103
- } satisfies WorkerRscChunkMessage);
104
-
105
- worker.once("message", (msg) => {
106
- if (msg.type === "ERROR") {
107
- const message =
108
- msg.error instanceof Error
109
- ? msg.error.message
110
- : String(msg.error);
111
- reject(new Error(message, { cause: msg }));
112
- } else if (msg.type === "HTML") {
113
- // In dev, content will be the HTML string
114
- resolve(msg.content);
115
- }
116
- });
117
- });
118
-
119
- res.setHeader("Content-Type", "text/html");
120
- res.end(html);
121
- } catch (error) {
122
- next(error);
123
- }
124
- };
125
- }
@@ -1,52 +0,0 @@
1
- import { createLogger as createViteLogger, type LogLevel } from 'vite';
2
-
3
-
4
- const isPrimitive = (value: any) => {
5
- return typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' || typeof value === 'symbol';
6
- }
7
-
8
- const buildMessage = (prefix: string, msg: string, ...args: any[]) => {
9
- let buildMsg = `[${prefix}] ${msg}`
10
- for(const arg of args) {
11
- if(isPrimitive(arg)) {
12
- buildMsg += `\n[${prefix}] ${String(arg)}`;
13
- } else if(arg instanceof Error) {
14
- buildMsg += `\n[${prefix}] ${arg.name}`
15
- buildMsg += `\n[${prefix}] ${arg.message}`
16
- buildMsg += `\n[${prefix}] ${arg.stack}`
17
- buildMsg += `\n[${prefix}] ${arg.cause}`
18
- } else {
19
- buildMsg += `\n[${prefix}] ${JSON.stringify(arg)}`;
20
- }
21
- }
22
- return buildMsg;
23
- }
24
-
25
- export const createLogger = (process.env['NODE_ENV'] === 'development') ? (prefix: string, logLevel: LogLevel = 'info') => {
26
- const logger = createViteLogger(logLevel, {
27
- allowClearScreen: true
28
- });
29
- return {
30
- clear: () => logger.clearScreen('info'),
31
- info: (msg: string, ...args: any[]) => {
32
- console.log(buildMessage(prefix, msg, ...args))
33
- },
34
- warn: (msg: string, ...args: any[]) => {
35
- console.warn(buildMessage(prefix, msg, ...args))
36
- },
37
- error: (msg: string, ...args: any[]) => {
38
- console.error(buildMessage(prefix, msg, ...args))
39
- },
40
- debug: (msg: string, ...args: any[]) => {
41
- console.debug(buildMessage(prefix, msg, ...args))
42
- }
43
- };
44
- } : () => {
45
- return {
46
- clear: () => {},
47
- info: () => {},
48
- warn: () => {},
49
- error: () => {},
50
- debug: () => {},
51
- };
52
- };
@@ -1,101 +0,0 @@
1
- import type { Plugin, UserConfig } from "vite";
2
- import { join, resolve } from "path";
3
- import type { ModuleFormat, RollupOptions } from 'rollup';
4
- import type { ResolvedUserOptions, StreamPluginOptions } from "../../types.js";
5
- import { DEFAULT_CONFIG } from "../../config/defaults.js";
6
- import { getPluginRoot } from "../../config/getPaths.js";
7
- import { resolveOptions } from "../../config/resolveOptions.js";
8
-
9
- let userOptions: ResolvedUserOptions;
10
- export function reactHtmlWorkerPlugin(options: StreamPluginOptions): Plugin {
11
- const resolvedUserOptions = resolveOptions(options);
12
- if(resolvedUserOptions.type === 'error') {
13
- throw resolvedUserOptions.error
14
- }
15
- userOptions = resolvedUserOptions.userOptions;
16
- return {
17
- name: "vite:react-html-worker",
18
- config(config) {
19
- const root = config.root ?? process.cwd();
20
- const pluginRoot = getPluginRoot();
21
- const htmlWorkerPath = typeof options.htmlWorkerPath === 'string'
22
- ? resolve(root, options.htmlWorkerPath)
23
- : resolve(pluginRoot, DEFAULT_CONFIG.HTML_WORKER_PATH);
24
-
25
- const format: ModuleFormat = 'esm';
26
-
27
- // Single worker output for server build
28
- const workerConfig = {
29
- input: {
30
- 'html-worker': htmlWorkerPath,
31
- },
32
- output: {
33
- format,
34
- dir: join(userOptions.build.outDir, userOptions.build.server),
35
- entryFileNames: '[name].js',
36
- preserveModules: true,
37
- preserveModulesRoot: userOptions.build.preserveModulesRoot === true ? userOptions.moduleBase : undefined,
38
- // Add manifest entry
39
- manualChunks: {
40
- 'html-worker': [htmlWorkerPath]
41
- }
42
- }
43
- } satisfies RollupOptions;
44
-
45
- return {
46
- build: {
47
- rollupOptions: {
48
- preserveEntrySignatures: 'strict',
49
- input: {
50
- ...workerConfig.input,
51
- ...(typeof config.build?.rollupOptions?.input === 'object'
52
- ? config.build?.rollupOptions?.input
53
- : {}),
54
- },
55
- external: [
56
- 'vite',
57
- 'rollup',
58
- 'react',
59
- 'react-dom',
60
- 'react-dom/server',
61
- 'react-server-dom-esm',
62
- 'react-server-dom-esm/client.node',
63
- 'react-server-dom-esm/server.node',
64
- 'react-server-dom-esm/node-loader',
65
- 'source-map',
66
- 'acorn-loose',
67
- 'webpack-sources',
68
- 'stream',
69
- 'util',
70
- 'crypto',
71
- 'async_hooks',
72
- 'fs',
73
- 'path',
74
- 'worker_threads',
75
- // if we use node: paths in our code, it should always be catched by below rule.
76
- /^node:.*/,
77
- ],
78
- output: {
79
- ...workerConfig.output,
80
- }
81
- },
82
- manifest: true, // Ensure manifest is generated
83
- minify: false,
84
- sourcemap: true,
85
- }
86
- } satisfies UserConfig;
87
- },
88
- // Add this to ensure entry is in manifest
89
- generateBundle(_, bundle) {
90
- const workerEntry = bundle['html-worker.js'];
91
- if (workerEntry) {
92
- Object.defineProperty(workerEntry, 'isEntry', {
93
- value: true,
94
- writable: false,
95
- enumerable: true,
96
- configurable: false
97
- });
98
- }
99
- }
100
- };
101
- }
@@ -1,26 +0,0 @@
1
- import type { StreamPluginOptions } from "../types.js"
2
- import type { Plugin } from "vite"
3
- import { reactHtmlWorkerPlugin } from "./html/plugin.js"
4
- import { reactRscWorkerPlugin } from "./rsc/plugin.js"
5
-
6
- /**
7
- * This plugin can be used to create your own worker paths. This build should be separated from the main build.
8
- *
9
- *
10
- * ```ts
11
- * @example
12
- *export reactWorkerPluginConfig = {
13
- * htmlWorkerPath: './workers/html.tsx',
14
- * rscWorkerPath: './workers/rsc.tsx',
15
- * }
16
- * ```
17
- *
18
- * @param options
19
- * @returns
20
- */
21
- export function reactWorkerPlugin(options: StreamPluginOptions): Plugin[] {
22
- return [
23
- reactHtmlWorkerPlugin(options),
24
- reactRscWorkerPlugin(options)
25
- ]
26
- }
@@ -1,84 +0,0 @@
1
- import type { Plugin } from "vite";
2
- import { resolve } from "path";
3
- import type { ModuleFormat } from "rollup";
4
- import type { StreamPluginOptions } from "../../types.js";
5
- import { DEFAULT_CONFIG } from "../../config/defaults.js";
6
- import { getPluginRoot } from "../../config/getPaths.js";
7
-
8
- export function reactRscWorkerPlugin(options: StreamPluginOptions): Plugin {
9
- return {
10
- name: "vite:react-rsc-worker",
11
- config(config) {
12
- const root = config.root ?? process.cwd();
13
- const pluginRoot = getPluginRoot();
14
- const rscWorkerPath =
15
- typeof options.rscWorkerPath === "string"
16
- ? resolve(root, options.rscWorkerPath)
17
- : resolve(pluginRoot, DEFAULT_CONFIG.RSC_WORKER_PATH);
18
-
19
- const format: ModuleFormat = "esm";
20
-
21
- // Single worker output for server build
22
- const workerConfig = {
23
- input: {
24
- "rsc-worker": rscWorkerPath,
25
- },
26
- output: {
27
- format,
28
- dir: options.build?.server ?? "dist/server", // Output to server directory
29
- entryFileNames: "[name].js",
30
- preserveModules: true,
31
- manualChunks: {
32
- "rsc-worker": [rscWorkerPath],
33
- },
34
- resolve: {
35
- conditions: ["react-server"],
36
- },
37
- },
38
- };
39
-
40
- return {
41
- build: {
42
- rollupOptions: {
43
- preserveEntrySignatures: "strict",
44
- input: {
45
- ...workerConfig.input,
46
- ...(typeof config.build?.rollupOptions?.input === "object"
47
- ? config.build?.rollupOptions?.input
48
- : {}),
49
- },
50
- external: [
51
- "vite",
52
- "rollup",
53
- "react",
54
- "react-dom",
55
- "react-dom/server",
56
- "react-server-dom-esm",
57
- "react-server-dom-esm/client.node",
58
- "react-server-dom-esm/server.node",
59
- "react-server-dom-esm/node-loader",
60
- "source-map",
61
- "acorn-loose",
62
- "webpack-sources",
63
- "stream",
64
- "util",
65
- "crypto",
66
- "async_hooks",
67
- "fs",
68
- "path",
69
- "worker_threads",
70
- // if we use node: paths in our code, it should always be catched by below rule.
71
- /^node:.*/,
72
- ],
73
- output: {
74
- ...workerConfig.output,
75
- },
76
- },
77
- manifest: true, // Ensure manifest is generated
78
- minify: false,
79
- sourcemap: true,
80
- },
81
- };
82
- },
83
- };
84
- }