vite-plugin-react-server 0.3.9 → 0.3.11

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 (197) hide show
  1. package/dist/client.d.ts +1 -1
  2. package/dist/client.d.ts.map +1 -1
  3. package/dist/client.js +1 -1
  4. package/dist/index.d.ts +3 -6
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +2 -2
  7. package/dist/index.js.map +1 -1
  8. package/dist/package.json +2 -4
  9. package/dist/plugin/assertServerCondition.d.ts +5 -1
  10. package/dist/plugin/assertServerCondition.d.ts.map +1 -1
  11. package/dist/plugin/assertServerCondition.js +1 -3
  12. package/dist/plugin/checkFilesExist.js +10 -7
  13. package/dist/plugin/checkFilesExist.js.map +1 -1
  14. package/dist/plugin/components.js +1 -1
  15. package/dist/plugin/components.js.map +1 -1
  16. package/dist/plugin/config/createModuleIdGenerator.d.ts +11 -0
  17. package/dist/plugin/config/createModuleIdGenerator.d.ts.map +1 -0
  18. package/dist/plugin/config/createModuleIdGenerator.js +44 -0
  19. package/dist/plugin/config/createModuleIdGenerator.js.map +1 -0
  20. package/dist/plugin/config/defaults.d.ts +31 -19
  21. package/dist/plugin/config/defaults.d.ts.map +1 -1
  22. package/dist/plugin/config/defaults.js +22 -27
  23. package/dist/plugin/config/defaults.js.map +1 -1
  24. package/dist/plugin/config/resolveOptions.d.ts +1 -1
  25. package/dist/plugin/config/resolveOptions.d.ts.map +1 -1
  26. package/dist/plugin/config/resolveOptions.js +202 -16
  27. package/dist/plugin/config/resolveOptions.js.map +1 -1
  28. package/dist/plugin/config/resolvePages.d.ts +2 -0
  29. package/dist/plugin/config/resolvePages.d.ts.map +1 -1
  30. package/dist/plugin/config/resolvePages.js.map +1 -1
  31. package/dist/plugin/config/resolveUserConfig.d.ts +2 -2
  32. package/dist/plugin/config/resolveUserConfig.d.ts.map +1 -1
  33. package/dist/plugin/config/resolveUserConfig.js +161 -50
  34. package/dist/plugin/config/resolveUserConfig.js.map +1 -1
  35. package/dist/plugin/helpers/getBundleManifest.d.ts +20 -0
  36. package/dist/plugin/helpers/getBundleManifest.d.ts.map +1 -0
  37. package/dist/plugin/helpers/getBundleManifest.js +43 -0
  38. package/dist/plugin/helpers/getBundleManifest.js.map +1 -0
  39. package/dist/plugin/helpers/getModuleManifest.d.ts +5 -0
  40. package/dist/plugin/helpers/getModuleManifest.d.ts.map +1 -1
  41. package/dist/plugin/helpers/getModuleManifest.js +20 -21
  42. package/dist/plugin/helpers/inputNormalizer.d.ts +15 -1
  43. package/dist/plugin/helpers/inputNormalizer.d.ts.map +1 -1
  44. package/dist/plugin/helpers/inputNormalizer.js +122 -16
  45. package/dist/plugin/helpers/inputNormalizer.js.map +1 -1
  46. package/dist/plugin/helpers/inputNormalizerWorker.d.ts +2 -1
  47. package/dist/plugin/helpers/inputNormalizerWorker.d.ts.map +1 -1
  48. package/dist/plugin/helpers/inputNormalizerWorker.js +10 -7
  49. package/dist/plugin/helpers/tryManifest.d.ts +2 -0
  50. package/dist/plugin/helpers/tryManifest.d.ts.map +1 -1
  51. package/dist/plugin/helpers/tryManifest.js +1 -1
  52. package/dist/plugin/helpers/tryManifest.js.map +1 -1
  53. package/dist/plugin/loader/createBuildLoader.d.ts +6 -2
  54. package/dist/plugin/loader/createBuildLoader.d.ts.map +1 -1
  55. package/dist/plugin/loader/createBuildLoader.js +35 -10
  56. package/dist/plugin/loader/createBuildLoader.js.map +1 -1
  57. package/dist/plugin/loader/createPageLoader.d.ts.map +1 -1
  58. package/dist/plugin/loader/createPageLoader.js +0 -7
  59. package/dist/plugin/plugin.d.ts +0 -1
  60. package/dist/plugin/plugin.d.ts.map +1 -1
  61. package/dist/plugin/plugin.js +2 -1
  62. package/dist/plugin/preserver/plugin.d.ts.map +1 -1
  63. package/dist/plugin/preserver/plugin.js +3 -2
  64. package/dist/plugin/preserver/plugin.js.map +1 -1
  65. package/dist/plugin/react-client/index.d.ts +2 -1
  66. package/dist/plugin/react-client/index.d.ts.map +1 -1
  67. package/dist/plugin/react-client/index.js +19 -1
  68. package/dist/plugin/react-client/index.js.map +1 -0
  69. package/dist/plugin/react-client/plugin.d.ts +2 -2
  70. package/dist/plugin/react-client/plugin.d.ts.map +1 -1
  71. package/dist/plugin/react-client/plugin.js +106 -10
  72. package/dist/plugin/react-client/plugin.js.map +1 -1
  73. package/dist/plugin/react-server/createHandler.d.ts +2 -2
  74. package/dist/plugin/react-server/createHandler.d.ts.map +1 -1
  75. package/dist/plugin/react-server/createHandler.js +5 -5
  76. package/dist/plugin/react-server/createHandler.js.map +1 -1
  77. package/dist/plugin/react-server/createRscStream.d.ts.map +1 -1
  78. package/dist/plugin/react-server/createRscStream.js +15 -1
  79. package/dist/plugin/react-server/createRscStream.js.map +1 -1
  80. package/dist/plugin/react-server/createSsrHandler.d.ts +2 -2
  81. package/dist/plugin/react-server/createSsrHandler.d.ts.map +1 -1
  82. package/dist/plugin/react-server/createSsrHandler.js +5 -12
  83. package/dist/plugin/react-server/index.js +18 -9
  84. package/dist/plugin/react-server/index.js.map +1 -0
  85. package/dist/plugin/react-server/plugin.d.ts.map +1 -1
  86. package/dist/plugin/react-server/plugin.js +133 -127
  87. package/dist/plugin/react-server/plugin.js.map +1 -1
  88. package/dist/plugin/transformer/plugin.d.ts +2 -1
  89. package/dist/plugin/transformer/plugin.d.ts.map +1 -1
  90. package/dist/plugin/transformer/plugin.js +52 -72
  91. package/dist/plugin/transformer/plugin.js.map +1 -1
  92. package/dist/plugin/transformer/transformer-client-components.d.ts +12 -3
  93. package/dist/plugin/transformer/transformer-client-components.d.ts.map +1 -1
  94. package/dist/plugin/transformer/transformer-client-components.js +71 -10
  95. package/dist/plugin/transformer/transformer-client-components.js.map +1 -1
  96. package/dist/plugin/transformer/transformer-server-actions.d.ts +3 -3
  97. package/dist/plugin/transformer/transformer-server-actions.d.ts.map +1 -1
  98. package/dist/plugin/transformer/transformer-server-actions.js +80 -66
  99. package/dist/plugin/transformer/types.d.ts +4 -0
  100. package/dist/plugin/transformer/types.d.ts.map +1 -1
  101. package/dist/plugin/types.d.ts +51 -20
  102. package/dist/plugin/types.d.ts.map +1 -1
  103. package/dist/plugin/worker/html/messageHandler.d.ts.map +1 -1
  104. package/dist/plugin/worker/html/messageHandler.js +37 -23
  105. package/dist/plugin/worker/html/messageHandler.js.map +1 -1
  106. package/dist/plugin/worker/html/plugin.d.ts.map +1 -1
  107. package/dist/plugin/worker/html/plugin.js +10 -5
  108. package/dist/plugin/worker/html/renderPages.d.ts +7 -6
  109. package/dist/plugin/worker/html/renderPages.d.ts.map +1 -1
  110. package/dist/plugin/worker/html/renderPages.js +112 -57
  111. package/dist/plugin/worker/html/renderPages.js.map +1 -1
  112. package/dist/plugin/worker/loader.d.ts +1 -11
  113. package/dist/plugin/worker/loader.d.ts.map +1 -1
  114. package/dist/plugin/worker/loader.js +2 -2
  115. package/dist/plugin/worker/loader.js.map +1 -1
  116. package/dist/plugin/worker/plugin.d.ts +10 -1
  117. package/dist/plugin/worker/plugin.d.ts.map +1 -1
  118. package/dist/plugin/worker/plugin.js +10 -1
  119. package/dist/plugin/worker/types.d.ts +17 -4
  120. package/dist/plugin/worker/types.d.ts.map +1 -1
  121. package/dist/server.d.ts +1 -3
  122. package/dist/server.d.ts.map +1 -1
  123. package/dist/server.js +1 -3
  124. package/dist/server.js.map +1 -1
  125. package/dist/tsconfig.tsbuildinfo +1 -1
  126. package/package.json +2 -4
  127. package/plugin/assertServerCondition.ts +2 -3
  128. package/plugin/checkFilesExist.ts +10 -10
  129. package/plugin/components.tsx +1 -1
  130. package/plugin/config/createModuleIdGenerator.ts +52 -0
  131. package/plugin/config/defaults.ts +24 -15
  132. package/plugin/config/resolveOptions.ts +253 -25
  133. package/plugin/config/resolvePages.ts +1 -1
  134. package/plugin/config/resolveUserConfig.ts +197 -62
  135. package/plugin/helpers/getBundleManifest.ts +75 -0
  136. package/plugin/helpers/getModuleManifest.ts +5 -0
  137. package/plugin/helpers/inputNormalizer.ts +175 -26
  138. package/plugin/helpers/inputNormalizerWorker.ts +13 -8
  139. package/plugin/helpers/tryManifest.ts +2 -0
  140. package/plugin/loader/createBuildLoader.ts +54 -10
  141. package/plugin/loader/createPageLoader.ts +1 -7
  142. package/plugin/plugin.ts +2 -1
  143. package/plugin/preserver/plugin.ts +2 -1
  144. package/plugin/react-client/index.ts +12 -1
  145. package/plugin/react-client/plugin.ts +127 -12
  146. package/plugin/react-server/createHandler.ts +7 -13
  147. package/plugin/react-server/createRscStream.ts +14 -2
  148. package/plugin/react-server/createSsrHandler.ts +7 -26
  149. package/plugin/react-server/plugin.ts +176 -144
  150. package/plugin/transformer/plugin.ts +69 -94
  151. package/plugin/transformer/transformer-client-components.ts +98 -24
  152. package/plugin/transformer/transformer-server-actions.ts +22 -7
  153. package/plugin/transformer/types.ts +4 -0
  154. package/plugin/types.ts +118 -64
  155. package/plugin/worker/html/messageHandler.ts +46 -31
  156. package/plugin/worker/html/plugin.ts +15 -11
  157. package/plugin/worker/html/renderPages.ts +175 -88
  158. package/plugin/worker/loader.ts +4 -13
  159. package/plugin/worker/plugin.ts +10 -1
  160. package/plugin/worker/types.ts +24 -3
  161. package/dist/node_modules/magic-string/dist/magic-string.es.js +0 -1283
  162. package/dist/node_modules/magic-string/dist/magic-string.es.js.map +0 -1
  163. package/dist/plugin/build/createClientBuildConfig.d.ts +0 -3
  164. package/dist/plugin/build/createClientBuildConfig.d.ts.map +0 -1
  165. package/dist/plugin/build/createClientBuildConfig.js +0 -14
  166. package/dist/plugin/build/createServerBuildConfig.d.ts +0 -12
  167. package/dist/plugin/build/createServerBuildConfig.d.ts.map +0 -1
  168. package/dist/plugin/build/createServerBuildConfig.js +0 -40
  169. package/dist/plugin/build/createSharedBuildConfig.d.ts +0 -5
  170. package/dist/plugin/build/createSharedBuildConfig.d.ts.map +0 -1
  171. package/dist/plugin/build/createSharedBuildConfig.js +0 -28
  172. package/dist/plugin/build/mergeInputs.d.ts +0 -9
  173. package/dist/plugin/build/mergeInputs.d.ts.map +0 -1
  174. package/dist/plugin/build/mergeInputs.js +0 -56
  175. package/dist/plugin/config/moduleIdDefault.d.ts +0 -8
  176. package/dist/plugin/config/moduleIdDefault.d.ts.map +0 -1
  177. package/dist/plugin/config/moduleIdDefault.js +0 -23
  178. package/dist/plugin/config/moduleIdDefault.js.map +0 -1
  179. package/dist/plugin/helpers/getModuleManifest.js.map +0 -1
  180. package/dist/plugin/react-server/createDevMiddleware.d.ts +0 -8
  181. package/dist/plugin/react-server/createDevMiddleware.d.ts.map +0 -1
  182. package/dist/plugin/react-server/createDevMiddleware.js +0 -68
  183. package/dist/plugin/react-server/createDevServer.d.ts +0 -4
  184. package/dist/plugin/react-server/createDevServer.d.ts.map +0 -1
  185. package/dist/plugin/react-server/createDevServer.js +0 -4
  186. package/dist/plugin/react-server/createReactNodeStreamer.d.ts +0 -10
  187. package/dist/plugin/react-server/createReactNodeStreamer.d.ts.map +0 -1
  188. package/dist/plugin/react-server/createReactNodeStreamer.js +0 -7
  189. package/dist/plugin/transformer/transformer-server-actions.js.map +0 -1
  190. package/plugin/build/createClientBuildConfig.ts +0 -21
  191. package/plugin/build/createServerBuildConfig.ts +0 -66
  192. package/plugin/build/createSharedBuildConfig.ts +0 -35
  193. package/plugin/build/mergeInputs.ts +0 -58
  194. package/plugin/config/moduleIdDefault.ts +0 -23
  195. package/plugin/react-server/createDevMiddleware.ts +0 -91
  196. package/plugin/react-server/createDevServer.ts +0 -9
  197. package/plugin/react-server/createReactNodeStreamer.ts +0 -26
package/plugin/types.ts CHANGED
@@ -1,40 +1,57 @@
1
- import type { ComponentType } from 'react';
2
- import type {
1
+ import type { ComponentType } from "react";
2
+ import type { RenderToPipeableStreamOptions } from "react-dom/server";
3
+ import type { PreRenderedChunk } from "rollup";
4
+ import type { PreRenderedAsset } from "rollup";
5
+ import type {
3
6
  UserConfig,
4
7
  BuildOptions,
5
8
  InlineConfig,
6
9
  AliasOptions,
7
10
  Connect,
8
- } from 'vite';
11
+ ResolveOptions,
12
+ } from "vite";
13
+ import type { PipeableStreamOptions } from "./worker/types.js";
9
14
 
10
15
  // Input can be a string path, React component, tuple, or array
11
- export type NormalizerInput = string | ComponentType<any> | [string, string] | string[];
16
+ export type NormalizerInput = unknown;
12
17
 
13
18
  export type InputNormalizer = (input: NormalizerInput) => [string, string];
14
19
 
15
- export type InputNormalizerWorker = (input: NormalizerInput) => Promise<[string, string]>;
20
+ export type InputNormalizerWorker = (
21
+ input: NormalizerInput
22
+ ) => Promise<[string, string]>;
16
23
 
17
- export type ResolvedUserConfig = Required<Pick<UserConfig, "root" | "mode" | "build">> &
18
- Omit<UserConfig, "root" | "mode" | "build"> & {
19
- build: NonNullable<Required<Pick<BuildOptions,
20
- "target" |
21
- "outDir" |
22
- "assetsDir" |
23
- "ssr" |
24
- "ssrEmitAssets" |
25
- "ssrManifest" |
26
- "manifest" |
27
- "rollupOptions"
28
- >>> &
29
- Omit<BuildOptions,
30
- "target" |
31
- "outDir" |
32
- "assetsDir" |
33
- "ssr" |
34
- "ssrEmitAssets" |
35
- "ssrManifest" |
36
- "manifest"
37
- >
24
+ export type ResolvedUserConfig = Required<
25
+ Pick<UserConfig, "root" | "mode" | "build" | "resolve">
26
+ > &
27
+ Omit<UserConfig, "root" | "mode" | "build" | "resolve"> & {
28
+ resolve: {alias:AliasOptions} & ResolveOptions;
29
+ } & {
30
+ build: NonNullable<
31
+ Required<
32
+ Pick<
33
+ BuildOptions,
34
+ | "target"
35
+ | "outDir"
36
+ | "assetsDir"
37
+ | "ssr"
38
+ | "ssrEmitAssets"
39
+ | "ssrManifest"
40
+ | "manifest"
41
+ | "rollupOptions"
42
+ >
43
+ >
44
+ > &
45
+ Omit<
46
+ BuildOptions,
47
+ | "target"
48
+ | "outDir"
49
+ | "assetsDir"
50
+ | "ssr"
51
+ | "ssrEmitAssets"
52
+ | "ssrManifest"
53
+ | "manifest"
54
+ >;
38
55
  };
39
56
 
40
57
  // Client plugin options
@@ -65,46 +82,75 @@ export type ResolvedUserOptions = Required<
65
82
  | "propsExportName"
66
83
  | "collectCss"
67
84
  | "collectAssets"
68
- | "assetsDir"
69
85
  | "htmlWorkerPath"
70
86
  | "rscWorkerPath"
71
87
  | "loaderPath"
72
88
  | "clientEntry"
73
89
  | "serverEntry"
74
90
  | "moduleBaseExceptions"
91
+ | "pipableStreamOptions"
92
+ | "moduleId"
75
93
  >
76
94
  > & {
77
95
  build: NonNullable<Required<StreamPluginOptions["build"]>>;
78
- autoDiscover: NonNullable<Required<StreamPluginOptions["autoDiscover"]>>;
96
+ autoDiscover: {
97
+ modulePattern: (path: string) => boolean;
98
+ cssPattern: (path: string) => boolean;
99
+ jsonPattern: (path: string) => boolean;
100
+ clientComponents: (path: string) => boolean;
101
+ propsPattern: (path: string) => boolean;
102
+ pagePattern: (path: string) => boolean;
103
+ serverFunctions: (path: string) => boolean;
104
+ cssModulePattern: (path: string) => boolean;
105
+ vendorPattern: (path: string) => boolean;
106
+ };
79
107
  };
80
108
 
81
- export type createBuildConfigFn<C extends "react-client" | "react-server"> = (input: {
82
- condition: C;
83
- userOptions: ResolvedUserOptions;
84
- userConfig: ResolvedUserConfig;
85
- mode: "production" | "development" | "test";
86
- inputNormalizer: C extends "react-server" ? InputNormalizerWorker : InputNormalizerWorker;
87
- }) => C extends "react-server" ? Promise<InlineConfig> : Promise<InlineConfig>;
109
+ export type createBuildConfigFn<C extends "react-client" | "react-server"> =
110
+ (input: {
111
+ condition: C;
112
+ userOptions: ResolvedUserOptions;
113
+ userConfig: ResolvedUserConfig;
114
+ mode: "production" | "development" | "test";
115
+ inputNormalizer: C extends "react-server"
116
+ ? InputNormalizerWorker
117
+ : InputNormalizerWorker;
118
+ }) => C extends "react-server"
119
+ ? Promise<InlineConfig>
120
+ : Promise<InlineConfig>;
88
121
 
89
122
  export interface StreamPluginOptions {
90
123
  projectRoot?: string;
91
- assetsDir?: string;
92
124
  moduleBase: string;
93
125
  moduleBasePath?: string;
94
126
  moduleBaseURL?: string;
95
127
  clientEntry?: string;
96
128
  serverEntry?: string;
97
129
  // Auto-discovery (zero-config)
98
- autoDiscover?: {
99
- // default: [Pp]age.tsx
100
- pagePattern?: RegExp;
101
- // default: [Pp]rops.ts
102
- propsPattern?: RegExp;
103
- // default: "use client" and .client./\.(m|c)?(j|t)sx?$/
104
- clientComponents?: RegExp;
105
- // default: "use server" and .server./\.(m|c)?(j|t)sx?$/
106
- serverFunctions?: RegExp;
107
- } | undefined;
130
+ autoDiscover?:
131
+ | {
132
+ // default: /\.(m|c)?(j|t)sx?$/
133
+ modulePattern?: string | RegExp | ((path: string) => boolean);
134
+ // default: [Pp]age.tsx
135
+ pagePattern?: string | RegExp | ((path: string) => boolean);
136
+ // default: [Pp]rops.ts
137
+ propsPattern?: string | RegExp | ((path: string) => boolean);
138
+ // default: "use client" and .client./\.(m|c)?(j|t)sx?$/
139
+ clientComponents?: string | RegExp | ((path: string) => boolean);
140
+ // default: "use server" and .server./\.(m|c)?(j|t)sx?$/
141
+ serverFunctions?: string | RegExp | ((path: string) => boolean);
142
+ // default: /\.css$/
143
+ cssPattern?: string | RegExp | ((path: string) => boolean);
144
+ // default: /\.json$/
145
+ jsonPattern?: string | RegExp | ((path: string) => boolean);
146
+ // default: /\.html$/
147
+ htmlPattern?: string | RegExp | ((path: string) => boolean);
148
+ // default: /\.css\.js/
149
+ cssModulePattern?: string | RegExp | ((path: string) => boolean);
150
+ // default: /node_modules|(_virtual)/
151
+ vendorPattern?: string | RegExp | ((path: string) => boolean);
152
+ }
153
+ | undefined;
108
154
  // Manual configuration
109
155
  Page: string | ((url: string) => string);
110
156
  props?: undefined | string | ((url: string) => string);
@@ -125,17 +171,19 @@ export interface StreamPluginOptions {
125
171
  collectAssets?: boolean;
126
172
  build?: BuildConfig;
127
173
  moduleBaseExceptions?: string[];
128
- moduleId?: (id: string) => string;
174
+ pipableStreamOptions?: PipeableStreamOptions;
175
+ moduleId?: (id: string, ssr: boolean) => string;
129
176
  }
130
177
 
131
178
  export interface CreateHandlerOptions<T = any> {
132
179
  loader: (id: string) => Promise<T>;
133
- manifest?: import("vite").Manifest;
180
+ clientManifest?: import("vite").Manifest;
181
+ serverManifest?: import("vite").Manifest;
134
182
  moduleGraph?: import("vite").ModuleGraph;
135
183
  cssFiles?: string[];
136
184
  onCssFile?: (path: string) => void;
137
185
  logger?: import("vite").Logger;
138
- pipableStreamOptions?: any;
186
+ pipableStreamOptions?: PipeableStreamOptions;
139
187
  }
140
188
 
141
189
  export type ModuleLoader = (
@@ -172,7 +220,7 @@ export interface RscStreamOptions {
172
220
  htmlProps: any;
173
221
  route: string;
174
222
  url: string;
175
- pipableStreamOptions?: any;
223
+ pipableStreamOptions?: PipeableStreamOptions;
176
224
  moduleBasePath: string;
177
225
  }
178
226
 
@@ -198,9 +246,17 @@ export interface BuildOutput {
198
246
 
199
247
  export interface BuildConfig {
200
248
  pages: string[] | (() => Promise<string[]> | string[]);
249
+ assetsDir?: string;
201
250
  client?: string; // Output directory for client files
202
251
  server?: string; // Output directory for server files
203
252
  static?: string; // Output directory for static environment - works in both
253
+ api?: string; // Output directory for API files
254
+ outDir?: string;
255
+ hash?: string;
256
+ preserveModulesRoot?: boolean;
257
+ entryFile?: (n: PreRenderedChunk) => string;
258
+ chunkFile?: (n: PreRenderedChunk) => string;
259
+ assetFile?: (n: PreRenderedAsset) => string;
204
260
  }
205
261
 
206
262
  export interface RscResolver {
@@ -276,30 +332,28 @@ export interface BuildTiming {
276
332
  total?: number;
277
333
  }
278
334
 
279
-
280
335
  export type CheckFilesExistReturn = {
281
- propsMap: Map<string,string>,
282
- propsSet: Set<string>
283
- pageMap: Map<string,string>,
284
- pageSet: Set<string>
285
- }
336
+ propsMap: Map<string, string>;
337
+ propsSet: Set<string>;
338
+ pageMap: Map<string, string>;
339
+ pageSet: Set<string>;
340
+ };
286
341
 
287
342
  // Add strict type checking for worker messages
288
- export type WorkerMessage =
289
- | { type: 'READY' }
290
- | { type: 'ERROR'; error: string | Error }
291
- | { type: 'RSC_CHUNK'; id: string; chunk: Buffer }
292
- | { type: 'RSC_END'; id: string }
293
- | { type: 'SHUTDOWN' };
343
+ export type WorkerMessage =
344
+ | { type: "READY" }
345
+ | { type: "ERROR"; error: string | Error }
346
+ | { type: "RSC_CHUNK"; id: string; chunk: Buffer }
347
+ | { type: "RSC_END"; id: string }
348
+ | { type: "SHUTDOWN" };
294
349
 
295
350
  // Add branded types for safety
296
351
  export type ModuleId = string & { readonly __brand: unique symbol };
297
352
  export type PagePath = string & { readonly __brand: unique symbol };
298
353
 
299
-
300
354
  export type HtmlProps = {
301
355
  pageProps: any;
302
356
  route: string;
303
357
  url: string;
304
358
  cssFiles: string[];
305
- }
359
+ };
@@ -1,30 +1,49 @@
1
1
  import { PassThrough } from "node:stream";
2
2
  import { parentPort } from "node:worker_threads";
3
3
  import type { HtmlRenderState, HtmlWorkerMessage } from "../types.js";
4
- import ReactDOMServer from "react-dom/server";
4
+ import * as ReactDOMServer from "react-dom/server";
5
5
  import React from "react";
6
6
  import {
7
7
  createFromNodeStream,
8
8
  // @ts-ignore
9
9
  } from "react-server-dom-esm/client.node";
10
+ import { join } from "path";
11
+ import { readFileSync } from "node:fs";
12
+ import { existsSync } from "node:fs";
10
13
 
11
14
  // Track active renders and streams
12
15
  const activeRenders = new Map<string, HtmlRenderState>();
16
+ const htmlContent = new Map<string, string>();
17
+ const htmlPromises = new Map<string, Promise<string>>();
13
18
 
14
19
  export const messageHandler = async (message: HtmlWorkerMessage) => {
15
20
  try {
16
21
  switch (message.type) {
17
22
  case "RSC_CHUNK": {
18
- const { id, chunk, ...rest } = message;
19
- const render = activeRenders.get(id);
23
+ const {
24
+ id,
25
+ chunk,
26
+ moduleRootPath,
27
+ moduleBaseURL,
28
+ outDir,
29
+ pipableStreamOptions,
30
+ clientManifest = {},
31
+ serverManifest = {},
32
+ htmlOutputPath,
33
+ } = message;
20
34
 
35
+ const render = activeRenders.get(id);
21
36
  if (!render) {
22
37
  activeRenders.set(id, {
23
38
  chunks: [chunk],
24
39
  id,
25
40
  complete: false,
26
41
  rendered: false,
27
- ...rest,
42
+ moduleRootPath,
43
+ moduleBaseURL,
44
+ outDir,
45
+ htmlOutputPath: htmlOutputPath ?? join(outDir, 'index.html'),
46
+ pipableStreamOptions: pipableStreamOptions,
28
47
  });
29
48
  } else {
30
49
  render.chunks = [...render.chunks, chunk];
@@ -39,6 +58,9 @@ export const messageHandler = async (message: HtmlWorkerMessage) => {
39
58
  throw new Error(`No render state found for ${id}`);
40
59
  }
41
60
 
61
+ // Mark this render as complete
62
+ render.complete = true;
63
+
42
64
  // Create a PassThrough stream to handle the chunks
43
65
  const rscStream = new PassThrough();
44
66
 
@@ -47,18 +69,18 @@ export const messageHandler = async (message: HtmlWorkerMessage) => {
47
69
  rscStream.write(chunk);
48
70
  }
49
71
  rscStream.end();
50
-
72
+
51
73
  // Create React elements from stream
52
- const reactElements = createFromNodeStream(
74
+ const reactElements = await createFromNodeStream(
53
75
  rscStream,
54
76
  render.moduleRootPath,
55
- render.moduleBaseURL
77
+ 'localhost'
56
78
  );
57
79
 
58
80
  // Create a promise that resolves when HTML is complete
59
81
  const htmlPromise = new Promise<string>((resolve) => {
60
- let html = "";
61
82
  const collectStream = new PassThrough();
83
+ let html = '';
62
84
 
63
85
  collectStream.on("data", (chunk) => {
64
86
  html += chunk.toString();
@@ -66,8 +88,14 @@ export const messageHandler = async (message: HtmlWorkerMessage) => {
66
88
 
67
89
  collectStream.on("end", () => {
68
90
  resolve(html);
91
+ render.rendered = true;
92
+ parentPort?.postMessage({
93
+ type: "ALL_READY",
94
+ id,
95
+ html,
96
+ outputPath: render.htmlOutputPath,
97
+ });
69
98
  });
70
-
71
99
  // Render to pipeable stream
72
100
  const stream = ReactDOMServer.renderToPipeableStream(
73
101
  reactElements as React.ReactNode,
@@ -75,24 +103,7 @@ export const messageHandler = async (message: HtmlWorkerMessage) => {
75
103
  ...render.pipableStreamOptions,
76
104
  onShellReady() {
77
105
  parentPort?.postMessage({ type: "SHELL_READY", id });
78
- },
79
- async onAllReady() {
80
- const finalHtml = await htmlPromise;
81
- parentPort?.postMessage({
82
- type: "HTML_READY",
83
- id,
84
- html: finalHtml,
85
- outputPath: render.htmlOutputPath,
86
- });
87
- parentPort?.postMessage({ type: "ALL_READY", id });
88
- },
89
- onError(error) {
90
- parentPort?.postMessage({
91
- type: "ERROR",
92
- id,
93
- error: error instanceof Error ? error.message : String(error),
94
- });
95
- },
106
+ }
96
107
  }
97
108
  );
98
109
 
@@ -100,16 +111,20 @@ export const messageHandler = async (message: HtmlWorkerMessage) => {
100
111
  stream.pipe(collectStream);
101
112
  });
102
113
 
114
+ htmlPromises.set(id, htmlPromise);
115
+
116
+ // Clean up resources
117
+ rscStream.destroy();
118
+ activeRenders.delete(id);
119
+ htmlContent.delete(id);
120
+ htmlPromises.delete(id);
103
121
  break;
104
122
  }
105
123
  }
106
124
  } catch (error) {
107
125
  parentPort?.postMessage({
108
126
  type: "ERROR",
109
- id:
110
- message.type === "RSC_CHUNK" || message.type === "RSC_END"
111
- ? message.id
112
- : "",
127
+ id: message.type === "RSC_CHUNK" || message.type === "RSC_END" ? message.id : "",
113
128
  error: error instanceof Error ? error.message : String(error),
114
129
  });
115
130
  }
@@ -1,12 +1,18 @@
1
- import type { Plugin } from "vite";
2
- import { resolve } from "path";
3
- import type { ModuleFormat } from 'rollup';
4
- import type { StreamPluginOptions } from "../../types.js";
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
5
  import { DEFAULT_CONFIG } from "../../config/defaults.js";
6
6
  import { getPluginRoot } from "../../config/getPaths.js";
7
+ import { resolveOptions } from "../../config/resolveOptions.js";
7
8
 
8
-
9
+ let userOptions: ResolvedUserOptions;
9
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;
10
16
  return {
11
17
  name: "vite:react-html-worker",
12
18
  config(config) {
@@ -25,18 +31,16 @@ export function reactHtmlWorkerPlugin(options: StreamPluginOptions): Plugin {
25
31
  },
26
32
  output: {
27
33
  format,
28
- dir: options.build?.server ?? 'dist/server', // Output to server directory
34
+ dir: join(userOptions.build.outDir, userOptions.build.server),
29
35
  entryFileNames: '[name].js',
30
36
  preserveModules: true,
37
+ preserveModulesRoot: userOptions.build.preserveModulesRoot === true ? userOptions.moduleBase : undefined,
31
38
  // Add manifest entry
32
39
  manualChunks: {
33
40
  'html-worker': [htmlWorkerPath]
34
- },
35
- resolve: {
36
- conditions: ['react-server'],
37
41
  }
38
42
  }
39
- };
43
+ } satisfies RollupOptions;
40
44
 
41
45
  return {
42
46
  build: {
@@ -79,7 +83,7 @@ export function reactHtmlWorkerPlugin(options: StreamPluginOptions): Plugin {
79
83
  minify: false,
80
84
  sourcemap: true,
81
85
  }
82
- };
86
+ } satisfies UserConfig;
83
87
  },
84
88
  // Add this to ensure entry is in manifest
85
89
  generateBundle(_, bundle) {