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,4 +1,5 @@
1
1
  import { reactPreservePlugin } from "../preserver/plugin.js";
2
+ import { reactStaticPlugin } from "../react-static/plugin.js";
2
3
  import { reactTransformPlugin } from "../transformer/plugin.js";
3
4
  import type { StreamPluginOptions } from "../types.js";
4
5
  import { reactServerPlugin } from "./plugin.js";
@@ -7,6 +8,7 @@ export function vitePluginReactServer(options = {} as StreamPluginOptions): impo
7
8
  return [
8
9
  reactTransformPlugin(options),
9
10
  reactServerPlugin(options),
11
+ reactStaticPlugin(options),
10
12
  reactPreservePlugin(options),
11
13
  ];
12
14
  }
@@ -1,6 +1,5 @@
1
- import { join, resolve, dirname } from "node:path";
1
+ import { join, dirname } from "node:path";
2
2
  import { performance } from "node:perf_hooks";
3
- import { Worker } from "node:worker_threads";
4
3
  import React from "react";
5
4
  import {
6
5
  createLogger,
@@ -8,43 +7,35 @@ import {
8
7
  type UserConfig,
9
8
  type ViteDevServer,
10
9
  type Manifest,
11
- build,
10
+ type Plugin as VitePlugin,
12
11
  } from "vite";
13
12
  import { checkFilesExist } from "../checkFilesExist.js";
14
- import { DEFAULT_CONFIG } from "../config/defaults.js";
15
- import { getPluginRoot } from "../config/getPaths.js";
16
13
  import { resolveOptions } from "../config/resolveOptions.js";
17
14
  import { resolvePages } from "../config/resolvePages.js";
18
15
  import { resolveUserConfig } from "../config/resolveUserConfig.js";
19
- import { tryManifest } from "../helpers/tryManifest.js";
20
- import { createBuildLoader } from "../loader/createBuildLoader.js";
21
16
  import type {
22
17
  BuildTiming,
23
18
  CheckFilesExistReturn,
19
+ CssContent,
24
20
  ReactStreamPluginMeta,
25
- ResolvedUserConfig,
26
21
  ResolvedUserOptions,
27
22
  } from "../types.js";
28
23
  import { type StreamPluginOptions } from "../types.js";
29
- import { createWorker } from "../worker/createWorker.js";
30
- import { renderPages } from "../worker/html/renderPages.js";
31
- import { createHandler } from "./createHandler.js";
32
- import { mkdir, readFile, writeFile } from "node:fs/promises";
24
+ import { createHandler } from "../helpers/createHandler.js";
25
+ import { mkdir, readFile, stat, writeFile } from "node:fs/promises";
33
26
  import { getBundleManifest } from "../helpers/getBundleManifest.js";
34
27
  import type { ServerResponse } from "node:http";
35
- import { collectManifestCss } from "../collect-css-manifest.js";
36
- import { cssFiles } from "../worker/rsc/state.js";
28
+ import { createInputNormalizer } from "../helpers/inputNormalizer.js";
29
+ import { MIME_TYPES } from "../config/mimeTypes.js";
30
+ import { InlineCssCollector } from "../css-collector-inline.js";
31
+ import { CssCollector } from "../css-collector.js";
32
+ import { collectModuleGraphCss } from "../collect-manifest-client-files.js";
37
33
 
38
34
  let resolvedConfig: ResolvedConfig | null = null;
39
35
  let serverManifestPath: string | null = null;
40
- let clientManifestPath: string | null = null;
41
- let outpuptBundle: any;
42
- let outputOptions: any;
43
36
  let loader: ((id: string) => Promise<Record<string, any>>) | null = null;
44
- let worker: Worker;
45
- export function reactServerPlugin(
46
- options: StreamPluginOptions
47
- ): import("vite").Plugin<{
37
+
38
+ export function reactServerPlugin(options: StreamPluginOptions): VitePlugin<{
48
39
  meta: ReactStreamPluginMeta;
49
40
  addCssFile: (path: string) => void;
50
41
  }> {
@@ -55,39 +46,14 @@ export function reactServerPlugin(
55
46
  let files: CheckFilesExistReturn;
56
47
  // let env: Awaited<ReturnType<typeof getEnv>>;
57
48
  let cssModules = new Set<string>();
58
- let clientComponents = new Map<string, string>();
59
49
  // let define: Record<string, string>;
60
50
  let buildCssFiles = new Set<string>();
61
51
  let root: string = process.cwd();
62
- let userConfig: ResolvedUserConfig;
63
52
  let userOptions: ResolvedUserOptions;
64
- let moduleGraph: Record<
65
- string,
66
- {
67
- file: string;
68
- src: string;
69
- name: string;
70
- isEntry: boolean;
71
- imports: string[];
72
- dynamicImports: string[];
73
- }
74
- > = {};
53
+ let resolvedPages: string[];
75
54
  let serverManifest: Manifest = {};
76
55
 
77
- interface BuildStats {
78
- htmlFiles: number;
79
- clientComponents: number;
80
- cssFiles: number;
81
- totalRoutes: number;
82
- timing: {
83
- config: number;
84
- build: number;
85
- render: number;
86
- total: number;
87
- };
88
- }
89
-
90
- const resolvedOptions = resolveOptions(options);
56
+ const resolvedOptions = resolveOptions(options, false);
91
57
  if (resolvedOptions.type === "error") {
92
58
  throw resolvedOptions.error;
93
59
  }
@@ -105,6 +71,7 @@ export function reactServerPlugin(
105
71
  root
106
72
  );
107
73
  }
74
+
108
75
  return {
109
76
  name: "vite:react-stream-server",
110
77
  enforce: "post",
@@ -122,11 +89,6 @@ export function reactServerPlugin(
122
89
  userOptions.build.server,
123
90
  ".vite/manifest.json"
124
91
  );
125
- clientManifestPath = join(
126
- resolvedConfig.build.outDir,
127
- userOptions.build.client,
128
- ".vite/manifest.json"
129
- );
130
92
  timing.configResolved = performance.now();
131
93
 
132
94
  // Verify transformer runs first, preserver runs last
@@ -147,7 +109,45 @@ export function reactServerPlugin(
147
109
  );
148
110
  }
149
111
  },
150
- async configurePreviewServer(server) {},
112
+
113
+ async configurePreviewServer(server) {
114
+ if (root !== server.config.root) {
115
+ root = server.config.root;
116
+ }
117
+ if (typeof loader !== "function") {
118
+ loader = (id: string) => import(id);
119
+ }
120
+ const normalize = createInputNormalizer({
121
+ root,
122
+ removeExtension: false,
123
+ preserveModulesRoot: userOptions.build.preserveModulesRoot
124
+ ? userOptions.moduleBase
125
+ : undefined,
126
+ });
127
+ server.middlewares.use(async (req, res, next) => {
128
+ const [key, value] = normalize(req.url);
129
+ const fileRoot = key.startsWith("node_modules")
130
+ ? root
131
+ : join(root, userOptions.build.outDir, userOptions.build.static);
132
+ try {
133
+ const filePath = join(fileRoot, value);
134
+ const stats = await stat(filePath);
135
+
136
+ if (stats.isFile()) {
137
+ const ext = value.slice(value.lastIndexOf("."));
138
+ const contentType = MIME_TYPES[ext] || "application/octet-stream";
139
+ res.setHeader("Content-Type", contentType);
140
+ const content = await readFile(filePath);
141
+ res.end(content);
142
+ return;
143
+ }
144
+ next();
145
+ } catch (error) {
146
+ console.log("Error serving static file:", error);
147
+ next();
148
+ }
149
+ });
150
+ },
151
151
  async configureServer(server: ViteDevServer) {
152
152
  if (typeof loader !== "function") {
153
153
  loader = server.ssrLoadModule;
@@ -186,35 +186,63 @@ export function reactServerPlugin(
186
186
  activeStreams.clear();
187
187
  });
188
188
 
189
- // server.ws.on("connection", (_socket, _req) => {
190
- // console.log("[vite-plugin-react-server] hooking up ws connection");
191
- // });
192
-
193
- // server.ws.on("listening", () => {
194
- // console.log("[vite-plugin-react-server] hooking up ws listening");
195
- // });
196
-
197
189
  server.middlewares.use(async (req, res, next) => {
198
190
  if (req.headers.accept !== "text/x-component") return next();
199
191
  if (typeof loader !== "function") {
200
192
  loader = server.ssrLoadModule;
201
193
  }
194
+ let route = req.url?.replace("/index.rsc", "");
195
+ if (!route || route === "") {
196
+ route = "/";
197
+ }
202
198
  try {
203
- const handler = await createHandler(
204
- req.url ?? "",
205
- {
206
- ...userOptions,
207
- // we'll leave the Html generation for later
208
- Html: React.Fragment,
209
- projectRoot: root,
199
+ const handler = await createHandler({
200
+ root: root,
201
+ url:
202
+ typeof userOptions.moduleBaseURL === "string" &&
203
+ userOptions.moduleBaseURL !== ""
204
+ ? new URL(route, userOptions.moduleBaseURL).href
205
+ : route,
206
+ route: route,
207
+ getCss: async (id) => {
208
+ const cssFiles = await collectModuleGraphCss({
209
+ moduleGraph: server.moduleGraph,
210
+ pagePath: id,
211
+ onCss: undefined,
212
+ })
213
+ if (userOptions.inlineCss) {
214
+ const InlineMap = new Map<string, CssContent>();
215
+ await Promise.all(Array.from(cssFiles.entries()).map(async ([file, fileUrl]) => {
216
+ const content = await server.ssrLoadModule(fileUrl + "?inline");
217
+ if (content) {
218
+ InlineMap.set(file, {
219
+ content: content['default'],
220
+ path: file,
221
+ type: "text/css",
222
+ });
223
+ }
224
+ }));
225
+ return InlineMap;
226
+ }
227
+ return cssFiles;
210
228
  },
211
- {
212
- cssFiles: [],
213
- logger: createLogger(),
214
- loader,
215
- moduleGraph: server.moduleGraph,
216
- }
217
- );
229
+ cssFiles: [],
230
+ logger: createLogger(),
231
+ loader,
232
+ moduleBase: userOptions.moduleBase,
233
+ moduleBasePath: userOptions.moduleBasePath,
234
+ moduleBaseURL: userOptions.moduleBaseURL,
235
+ moduleRootPath: root,
236
+ pipableStreamOptions: userOptions.pipableStreamOptions,
237
+ Html: React.Fragment,
238
+ CssCollector: userOptions.inlineCss ? InlineCssCollector as any : CssCollector as any,
239
+ onCssFile: undefined,
240
+ inlineCss: userOptions.inlineCss,
241
+ propsPath: files.urlMap.get(route)?.props ?? route,
242
+ pagePath: files.urlMap.get(route)?.page ?? route,
243
+ pageExportName: userOptions.pageExportName,
244
+ propsExportName: userOptions.propsExportName,
245
+ });
218
246
  if (handler.type === "success") {
219
247
  handler.stream?.pipe(res);
220
248
  }
@@ -240,12 +268,12 @@ export function reactServerPlugin(
240
268
  );
241
269
  root = config.root;
242
270
  }
243
- const resolvedPages = await resolvePages(userOptions.build.pages);
244
- if (resolvedPages.type === "error") {
245
- throw resolvedPages.error;
271
+ const resolvedPagesResult = await resolvePages(userOptions.build.pages);
272
+ if (resolvedPagesResult.type === "error") {
273
+ throw resolvedPagesResult.error;
246
274
  }
247
-
248
- files = await checkFilesExist(resolvedPages.pages, userOptions, root);
275
+ resolvedPages = resolvedPagesResult.pages;
276
+ files = await checkFilesExist(resolvedPages, userOptions, root);
249
277
 
250
278
  const resolvedConfig = resolveUserConfig({
251
279
  isClient: false,
@@ -259,7 +287,6 @@ export function reactServerPlugin(
259
287
  throw resolvedConfig.error;
260
288
  }
261
289
 
262
- userConfig = resolvedConfig.userConfig;
263
290
  return resolvedConfig.userConfig;
264
291
  },
265
292
  async buildStart() {
@@ -269,234 +296,16 @@ export function reactServerPlugin(
269
296
  console.log("Build already started");
270
297
  }
271
298
  },
272
- async closeBundle() {
273
- if (!userConfig || resolvedConfig?.command !== "build") return;
274
- try {
275
- timing.renderStart = performance.now();
276
-
277
- // Get the client manifest
278
- const clientManifestResult = tryManifest({
279
- root: root,
280
- outDir: join(userOptions.build.outDir, userOptions.build.client),
281
- ssrManifest: false,
282
- });
283
-
284
- if (clientManifestResult.type === "error") {
285
- throw clientManifestResult.error;
286
- }
287
-
288
- const clientManifest = clientManifestResult.manifest;
289
-
290
-
291
- worker = await createWorker({
292
- projectRoot: root,
293
- workerPath: userOptions.htmlWorkerPath,
294
- condition: 'react-server',
295
- reverseCondition: true,
296
- mode: (resolvedConfig?.mode ?? "production") as
297
- | "production"
298
- | "development",
299
- nodeOptions: "--conditions=react-client",
300
- });
301
-
302
- // Create the loader
303
- if (typeof loader !== "function") {
304
- if (!Object.keys(serverManifest).length) {
305
- console.warn(
306
- "[vite-plugin-react-server] No server manifest found, the plugin will try to use the plugin context - it may differ from vite's manifest."
307
- );
308
- serverManifest = getBundleManifest({
309
- pluginContext: this,
310
- bundle: outpuptBundle,
311
- moduleBase: userOptions.moduleBase,
312
- preserveModulesRoot: userOptions.build.preserveModulesRoot,
313
- });
314
- if (!Object.keys(serverManifest).length) {
315
- console.warn(
316
- "[vite-plugin-react-server] That didn't work, retrying to read manifest."
317
- );
318
- const resolvedServerManifest = tryManifest({
319
- root: root,
320
- outDir: join(
321
- userOptions.build.outDir,
322
- userOptions.build.server
323
- ),
324
- ssrManifest: false,
325
- });
326
- if (resolvedServerManifest.type === "error") {
327
- // dont build the static files without a server manifest
328
- console.error(
329
- "[vite-plugin-react-server] Failed to read manifest, aborting build."
330
- );
331
- return;
332
- }
333
- serverManifest = resolvedServerManifest.manifest;
334
- }
335
- }
336
- loader = createBuildLoader({
337
- root: root,
338
- userConfig,
339
- userOptions,
340
- pluginContext: this,
341
- serverManifest,
342
- clientManifest,
343
- });
344
- }
345
- const resolvedPages = await resolvePages(userOptions.build.pages);
346
- if (resolvedPages.type === "error") {
347
- throw resolvedPages.error;
348
- }
349
-
350
- const onCssFile = async (path: string, parentUrl: string) => {
351
- if (buildCssFiles && path.endsWith(".css")) {
352
- buildCssFiles.add(path);
353
- if (parentUrl.endsWith(userOptions.build.client)) {
354
- // copy the file to the client build dir
355
- const serverPath = join(
356
- userOptions.build.outDir,
357
- userOptions.build.server,
358
- path
359
- );
360
- await writeFile(
361
- serverPath,
362
- await readFile(
363
- join(
364
- root,
365
- userOptions.build.outDir,
366
- userOptions.build.client,
367
- path
368
- )
369
- )
370
- );
371
- } else {
372
- // copy the file to the client build dir, assume it's in server build dir
373
- const clientPath = join(
374
- userOptions.build.outDir,
375
- userOptions.build.client,
376
- path
377
- );
378
- await mkdir(dirname(clientPath), { recursive: true });
379
- await writeFile(
380
- clientPath,
381
- await readFile(
382
- join(
383
- root,
384
- userOptions.build.outDir,
385
- userOptions.build.server,
386
- path
387
- )
388
- )
389
- );
390
- }
391
- }
392
- };
393
-
394
- const { failedRoutes, completedRoutes } = await renderPages(
395
- this,
396
- resolvedPages.pages,
397
- files,
398
- {
399
- pipableStreamOptions: {
400
- bootstrapModules: clientManifest["index.html"]?.file
401
- ? [clientManifest["index.html"].file.startsWith('/') ? clientManifest["index.html"].file : '/' + clientManifest["index.html"].file]
402
- : [],
403
- },
404
- moduleBasePath: "",
405
- moduleBaseURL: "",
406
- clientCss: clientManifest["index.html"]?.css
407
- ? clientManifest["index.html"].css.map(css => css.startsWith('/') ? css : '/' + css)
408
- : [],
409
- userConfig,
410
- pluginOptions: userOptions,
411
- worker: worker,
412
- clientManifest: clientManifest,
413
- serverManifest: serverManifest,
414
- loader,
415
- onCssFile: onCssFile,
416
- }
417
- );
418
-
419
- if (failedRoutes.size) {
420
- console.error(
421
- "[vite-plugin-react-server] Failed to render routes:",
422
- failedRoutes
423
- );
424
- }
425
- if (worker) await worker.terminate();
426
-
427
- timing.renderEnd = performance.now();
428
- timing.total = (timing.renderEnd - timing.start) / 1000;
429
-
430
- // Update stats to include CSS and client components
431
- const stats: BuildStats = {
432
- htmlFiles: files.urlMap.size,
433
- clientComponents: clientComponents.size,
434
- cssFiles: cssFiles.size,
435
- totalRoutes: files.urlMap.size,
436
- timing: {
437
- config: ((timing.configResolved ?? 0) - timing.start) / 1000,
438
- build:
439
- ((timing.buildStart ?? 0) - (timing.configResolved ?? 0)) / 1000,
440
- render:
441
- ((timing.renderEnd ?? 0) - (timing.renderStart ?? 0)) / 1000,
442
- total: (timing.renderEnd ?? 0 - timing.start) / 1000,
443
- },
444
- };
445
-
446
- // Format duration helper
447
- const formatDuration = (seconds: number) => {
448
- if (seconds < 0.001) {
449
- return `${(seconds * 1000000).toFixed(0)}μs`;
450
- }
451
- if (seconds < 1) {
452
- return `${(seconds * 1000).toFixed(0)}ms`;
453
- }
454
- return `${seconds.toFixed(2)}s`;
455
- };
456
-
457
- console.log("\n[vite-plugin-react-server] Build Summary:");
458
- console.log("─".repeat(50));
459
- console.log(`🎨 Included ${buildCssFiles.size} CSS files`);
460
- console.log(`🛣️ Total routes: ${files.urlMap.size}`);
461
- console.log("─".repeat(50));
462
- console.log("⏱️ Timing:");
463
- console.log(` Config: ${formatDuration(stats.timing.config)}`);
464
- console.log(` Build: ${formatDuration(stats.timing.build)}`);
465
- console.log(` Render: ${formatDuration(stats.timing.render)}`);
466
- console.log(" ".repeat(12));
467
- console.log(` Total: ${formatDuration(stats.timing.total)}`);
468
- console.log("─".repeat(50));
469
-
470
- // Ensure worker is terminated
471
- if (worker) {
472
- await worker.terminate();
473
- worker = null as any;
474
- }
475
- } catch (error) {
476
- console.error("[vite-plugin-react-server] Build failed:", error);
477
- // Make sure to terminate worker even on error
478
- if (worker) await worker.terminate();
479
- worker = null as any;
480
- throw error;
481
- }
482
- },
483
- async buildEnd(error) {
484
- if (error) {
485
- console.error("[vite-plugin-react-server] Build error:", error);
486
- return;
487
- }
488
- },
489
299
  handleHotUpdate({ file }) {
490
300
  if (file.endsWith(".css")) {
491
301
  cssModules.add(file);
492
302
  }
493
303
  },
494
- async generateBundle(options, bundle) {
304
+ async generateBundle(_options, bundle) {
495
305
  if (!resolvedConfig) {
496
306
  throw new Error("Resolved config not found");
497
307
  }
498
- outpuptBundle = bundle;
499
- outputOptions = options;
308
+
500
309
  // Create manifest entries for each chunk
501
310
  serverManifest = getBundleManifest({
502
311
  pluginContext: this,
@@ -0,0 +1 @@
1
+ export { reactStaticPlugin } from "./plugin.js";