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
@@ -9,8 +9,13 @@ import { join } from "path";
9
9
  // @ts-ignore
10
10
  import { globSync } from "fs";
11
11
  import type { OutputOptions } from "rollup";
12
+ import { pluginRoot } from "../root.js";
13
+
14
+ let stashedUserConfig: Record<string, ResolvedUserConfig | null> = {};
15
+
12
16
  export type ResolveUserConfigProps = {
13
17
  isClient?: boolean;
18
+ isStatic?: boolean;
14
19
  config: UserConfig;
15
20
  configEnv: ConfigEnv;
16
21
  userOptions: ResolvedUserOptions;
@@ -23,208 +28,247 @@ export type ResolveUserConfigReturn =
23
28
 
24
29
  export function resolveUserConfig({
25
30
  isClient = false,
31
+ isStatic = false,
26
32
  config,
27
33
  configEnv,
28
34
  userOptions,
29
35
  files,
30
36
  }: ResolveUserConfigProps): ResolveUserConfigReturn {
31
- try {
32
- // Get existing inputs
33
- const root = config.root ?? userOptions.projectRoot ?? process.cwd();
37
+ if(isStatic) {
38
+ const serverConfig = stashedUserConfig[`${userOptions.build.server}-ssr`]
39
+ if(!serverConfig) {
40
+ return {
41
+ type: "error",
42
+ error: new Error("Static plugin should run after the server plugin"),
43
+ }
44
+ }
45
+ return {
46
+ type: "success",
47
+ userConfig: serverConfig,
48
+ }
49
+ }
50
+ const envDir = isStatic
51
+ ? userOptions.build.static
52
+ : isClient
53
+ ? userOptions.build.client
54
+ : userOptions.build.server;
55
+ const ssr =
56
+ typeof config.build?.ssr === "boolean"
57
+ ? config.build?.ssr
58
+ : configEnv.isSsrBuild || (!isClient && !isStatic);
59
+ const envId = `${envDir}${ssr ? "-ssr" : ""}`;
60
+ if (stashedUserConfig[envId]) {
61
+ console.log(`[RSC] Using cached config for ${envId}`);
62
+ return {
63
+ type: "success",
64
+ userConfig: stashedUserConfig[envId],
65
+ };
66
+ }
67
+ // Get existing inputs
68
+ const root = config.root ?? userOptions.projectRoot ?? process.cwd();
34
69
 
35
- const normalizer = createInputNormalizer({
36
- root,
37
- preserveModulesRoot: userOptions.build.preserveModulesRoot
38
- ? userOptions.moduleBase
39
- : undefined,
40
- removeExtension: true,
41
- });
70
+ const normalizer = createInputNormalizer({
71
+ root,
72
+ preserveModulesRoot: userOptions.build.preserveModulesRoot
73
+ ? userOptions.moduleBase
74
+ : undefined,
75
+ removeExtension: true,
76
+ });
42
77
 
43
- const serverEntry = userOptions.serverEntry
44
- ? Object.fromEntries([
45
- normalizer([userOptions.serverEntry, userOptions.serverEntry]),
46
- ])
47
- : null;
48
- const clientEntry = userOptions.clientEntry
49
- ? Object.fromEntries(
50
- [
51
- [userOptions.clientEntry, userOptions.clientEntry],
52
- ["index.html", "index.html"],
53
- ].map(normalizer)
54
- )
55
- : { "index.html": "index.html" };
78
+ const serverEntry = userOptions.serverEntry
79
+ ? Object.fromEntries([
80
+ normalizer([userOptions.serverEntry, userOptions.serverEntry]),
81
+ ])
82
+ : null;
83
+ const clientEntry = userOptions.clientEntry
84
+ ? Object.fromEntries(
85
+ [
86
+ [userOptions.clientEntry, userOptions.clientEntry],
87
+ ["index.html", "index.html"],
88
+ ].map(normalizer)
89
+ )
90
+ : { "index.html": "index.html" };
56
91
 
57
- const autoDiscoveredClientFiles = (inputs: Record<string, string>) => {
58
- const allFiles = globSync(`**/*.client.*`, {
59
- cwd: join(root, userOptions.moduleBase),
60
- });
92
+ const autoDiscoveredClientFiles = (inputs: Record<string, string>) => {
93
+ const allFiles = globSync(`**/*.client.*`, {
94
+ cwd: join(root, userOptions.moduleBase),
95
+ });
61
96
 
62
- for (const file of allFiles) {
63
- const [key, value] = normalizer(join(userOptions.moduleBase, file));
64
- if (!inputs[key]) {
65
- inputs[key] = value;
66
- } else {
67
- console.warn(`[RSC] Client file already exists: ${key}`);
68
- }
97
+ for (const file of allFiles) {
98
+ const [key, value] = normalizer(join(userOptions.moduleBase, file));
99
+ if (!inputs[key]) {
100
+ inputs[key] = value;
101
+ } else {
102
+ console.warn(`[RSC] Client file already exists: ${key}`);
69
103
  }
70
- return inputs;
71
- };
72
- const autoDiscoveredServerFiles = (inputs: Record<string, string>) => {
73
- const allFiles = globSync(`${userOptions.moduleBase}/**/*.server.*`, {
74
- cwd: join(root, userOptions.moduleBase),
75
- });
76
- for (const file of allFiles) {
77
- const [key, value] = normalizer(join(userOptions.moduleBase, file));
78
- if (!inputs[key]) {
79
- inputs[key] = value;
80
- } else {
81
- console.warn(`[RSC] Server file already exists: ${key}`);
82
- }
104
+ }
105
+ return inputs;
106
+ };
107
+ const autoDiscoveredServerFiles = (inputs: Record<string, string>) => {
108
+ const allFiles = globSync(`${userOptions.moduleBase}/**/*.server.*`, {
109
+ cwd: join(root, userOptions.moduleBase),
110
+ });
111
+ for (const file of allFiles) {
112
+ const [key, value] = normalizer(join(userOptions.moduleBase, file));
113
+ if (!inputs[key]) {
114
+ inputs[key] = value;
115
+ } else {
116
+ console.warn(`[RSC] Server file already exists: ${key}`);
83
117
  }
84
- return inputs;
85
- };
86
- const autoDiscoveredFiles = (inputs: Record<string, string>) => {
87
- if (!files) return inputs;
118
+ }
119
+ return inputs;
120
+ };
121
+ const customWorkerFiles = (inputs: Record<string, string>) => {
122
+ const customRscWorker = !userOptions.rscWorkerPath.startsWith(pluginRoot)
123
+ const customHtmlWorker = !userOptions.htmlWorkerPath.startsWith(pluginRoot)
124
+ if(customRscWorker && !inputs['rsc-worker']) {
125
+ inputs['rsc-worker'] = userOptions.rscWorkerPath
126
+ }
127
+ if(customHtmlWorker && !inputs['html-worker']) {
128
+ inputs['html-worker'] = userOptions.htmlWorkerPath
129
+ }
130
+ return inputs
131
+ }
132
+ const autoDiscoveredFiles = (inputs: Record<string, string>) => {
133
+ if (!files) return inputs;
88
134
 
89
- // Add page files without extra prefix
90
- for (const [key, value] of files.pageMap) {
91
- if (!inputs[key]) {
92
- inputs[key] = value;
93
- } else {
94
- console.warn(`[RSC] Page file already exists: ${key}`);
95
- }
135
+ // Add page files without extra prefix
136
+ for (const [key, value] of files.pageMap) {
137
+ if (!inputs[key]) {
138
+ inputs[key] = value;
139
+ } else {
140
+ console.warn(`[RSC] Page file already exists: ${key}`);
96
141
  }
97
- // Add props files without extra prefix
98
- for (const [key, value] of files.propsMap) {
99
- if (!inputs[key]) {
100
- inputs[key] = value;
101
- } else {
102
- console.warn(`[RSC] Props file already exists: ${key}`);
103
- }
142
+ }
143
+ // Add props files without extra prefix
144
+ for (const [key, value] of files.propsMap) {
145
+ if (!inputs[key]) {
146
+ inputs[key] = value;
147
+ } else {
148
+ console.warn(`[RSC] Props file already exists: ${key}`);
104
149
  }
105
- return inputs;
106
- };
150
+ }
151
+ return inputs;
152
+ };
107
153
 
108
- // Add inputs based on condition
109
- let inputs = isClient
110
- ? autoDiscoveredClientFiles(clientEntry)
111
- : autoDiscoveredServerFiles(autoDiscoveredFiles(serverEntry ?? {}));
154
+ // Add inputs based on condition
155
+ let inputs = isClient
156
+ ? autoDiscoveredClientFiles(clientEntry)
157
+ : customWorkerFiles(autoDiscoveredServerFiles(autoDiscoveredFiles(serverEntry ?? {})));
112
158
 
113
- const envDir = isClient
114
- ? userOptions.build.client
115
- : userOptions.build.server;
159
+ const pluginOutput = {
160
+ preserveModules: !isClient,
161
+ preserveModulesRoot: userOptions.build.preserveModulesRoot
162
+ ? userOptions.moduleBase
163
+ : undefined,
164
+ entryFileNames: userOptions.build.entryFile,
165
+ assetFileNames: userOptions.build.assetFile,
166
+ chunkFileNames: userOptions.build.chunkFile,
167
+ format: "esm",
168
+ exports: "named",
169
+ hoistTransitiveImports: false,
170
+ generatedCode: {
171
+ constBindings: true,
172
+ objectShorthand: true,
173
+ },
174
+ interop: "auto",
175
+ } satisfies OutputOptions;
116
176
 
117
- const pluginOutput = {
118
- preserveModules: !isClient,
119
- preserveModulesRoot: userOptions.build.preserveModulesRoot
120
- ? userOptions.moduleBase
121
- : undefined,
122
- entryFileNames: userOptions.build.entryFile,
123
- assetFileNames: userOptions.build.assetFile,
124
- chunkFileNames: userOptions.build.chunkFile,
125
- format: "esm",
126
- exports: "named",
127
- hoistTransitiveImports: false,
128
- generatedCode: {
129
- constBindings: true,
130
- objectShorthand: true,
131
- },
132
- interop: "auto",
133
- } satisfies OutputOptions;
134
-
135
- const newOutput = Array.isArray(config.build?.rollupOptions?.output)
136
- ? [...config.build?.rollupOptions?.output, pluginOutput]
137
- : typeof config.build?.rollupOptions?.output === "object" &&
138
- config.build?.rollupOptions?.output !== null
139
- ? [config.build?.rollupOptions?.output, pluginOutput]
140
- : pluginOutput;
177
+ let newOutput = Array.isArray(config.build?.rollupOptions?.output)
178
+ ? [...config.build?.rollupOptions?.output, pluginOutput]
179
+ : typeof config.build?.rollupOptions?.output === "object" &&
180
+ config.build?.rollupOptions?.output !== null
181
+ ? [config.build?.rollupOptions?.output, pluginOutput]
182
+ : pluginOutput;
141
183
 
142
- if (isClient) {
143
- // client build options
144
- return {
145
- type: "success",
146
- userConfig: {
147
- ...config,
148
- root: root,
149
- mode: configEnv.mode ?? configEnv.command === "build" ? "production" : "development",
150
- resolve: {
151
- external: ["react", "react-dom"],
152
- alias: {},
153
- },
154
- ssr: {
155
- target: "node",
156
- external: [
157
- "react",
158
- "react-dom",
159
- "react-server-dom-esm/client.browser",
160
- ],
161
- resolve: {
162
- externalConditions: ["react-server"],
163
- },
164
- },
165
- // client build options
166
- build: {
167
- ...config.build,
168
- emptyOutDir: config.build?.emptyOutDir ?? true,
169
- outDir: join(userOptions.build.outDir, envDir),
170
- assetsDir: config.build?.assetsDir ?? userOptions.build.assetsDir,
171
- // modern browsers
172
- target: ["esnext"],
173
- minify: true,
174
- ssr:
175
- typeof configEnv.isSsrBuild === "boolean"
176
- ? configEnv.isSsrBuild
177
- : true,
178
- manifest: config.build?.manifest ?? `.vite/manifest.json`,
179
- ssrManifest: config.build?.ssrManifest ?? `.vite/ssr-manifest.json`,
180
- ssrEmitAssets: config.build?.ssrEmitAssets ?? true,
181
- rollupOptions: {
182
- ...config.build?.rollupOptions,
183
- input: inputs,
184
- output: newOutput,
185
- preserveEntrySignatures: "exports-only",
186
- },
187
- },
188
- },
189
- };
190
- }
191
- // server build options
192
- return {
193
- type: "success",
194
- userConfig: {
195
- ...config,
196
- root: root,
197
- mode: configEnv.mode ?? configEnv.command === "build" ? "production" : "development",
184
+ if (isClient) {
185
+ // client plugin build options (client plugin still outputs server files)
186
+ stashedUserConfig[envId] = {
187
+ ...config,
188
+ root: root,
189
+ mode:
190
+ configEnv.mode ?? configEnv.command === "build"
191
+ ? "production"
192
+ : "development",
193
+ resolve: {
194
+ external: ["react", "react-dom"],
195
+ alias: {},
196
+ },
197
+ ssr: {
198
+ target: "node",
199
+ external: ["react", "react-dom", "react-server-dom-esm/client.browser"],
198
200
  resolve: {
199
- alias: {},
200
201
  externalConditions: ["react-server"],
201
202
  },
202
- // server build options
203
- build: {
204
- ...config.build,
205
- emptyOutDir: config.build?.emptyOutDir ?? true,
206
- outDir: join(userOptions.build.outDir, envDir),
207
- target: config.build?.target ?? "node18",
208
- minify: config.build?.minify ?? true,
209
- ssr: config.build?.ssr ?? configEnv.isSsrBuild ?? true,
210
- manifest: config.build?.manifest ?? `.vite/manifest.json`,
211
- ssrManifest: config.build?.ssrManifest ?? `.vite/ssr-manifest.json`,
212
- ssrEmitAssets: config.build?.ssrEmitAssets ?? true,
213
- assetsDir: config.build?.assetsDir ?? userOptions.build.assetsDir,
214
- rollupOptions: {
215
- ...config.build?.rollupOptions,
216
- input: inputs,
217
- preserveEntrySignatures: config.build?.rollupOptions?.preserveEntrySignatures ?? "strict",
218
- output: newOutput,
219
- },
203
+ },
204
+ // client build options
205
+ build: {
206
+ ...config.build,
207
+ emptyOutDir: config.build?.emptyOutDir ?? true,
208
+ outDir: join(userOptions.build.outDir, envDir),
209
+ assetsDir: config.build?.assetsDir ?? userOptions.build.assetsDir,
210
+ copyPublicDir: config.build?.copyPublicDir ?? true,
211
+ // modern browsers
212
+ target: ["esnext"],
213
+ minify: true,
214
+ ssr: ssr,
215
+ manifest: config.build?.manifest ?? `.vite/manifest.json`,
216
+ ssrManifest: config.build?.ssrManifest ?? `.vite/ssr-manifest.json`,
217
+ ssrEmitAssets: config.build?.ssrEmitAssets ?? true,
218
+ rollupOptions: {
219
+ ...config.build?.rollupOptions,
220
+ input: inputs,
221
+ output: newOutput,
222
+ preserveEntrySignatures: "exports-only",
220
223
  },
221
224
  },
222
225
  };
223
- } catch (error) {
226
+ } else {
227
+ // server build options
228
+ if (configEnv.isSsrBuild === false) {
229
+ configEnv.isSsrBuild = true;
230
+ }
231
+ stashedUserConfig[envId] = {
232
+ ...config,
233
+ root: root,
234
+ mode:
235
+ configEnv.mode ?? configEnv.command === "build"
236
+ ? "production"
237
+ : "development",
238
+ resolve: {
239
+ externalConditions: ["react-server"],
240
+ },
241
+ // server build options
242
+ build: {
243
+ ...config.build,
244
+ emptyOutDir: config.build?.emptyOutDir ?? true,
245
+ outDir: join(userOptions.build.outDir, envDir),
246
+ target: config.build?.target ?? "node18",
247
+ minify: config.build?.minify ?? true,
248
+ ssr: ssr,
249
+ manifest: config.build?.manifest ?? `.vite/manifest.json`,
250
+ ssrManifest: config.build?.ssrManifest ?? `.vite/ssr-manifest.json`,
251
+ ssrEmitAssets: config.build?.ssrEmitAssets ?? true,
252
+ copyPublicDir: config.build?.copyPublicDir ?? isStatic,
253
+ assetsDir: config.build?.assetsDir ?? userOptions.build.assetsDir,
254
+ rollupOptions: {
255
+ ...config.build?.rollupOptions,
256
+ input: inputs,
257
+ preserveEntrySignatures:
258
+ config.build?.rollupOptions?.preserveEntrySignatures ?? "strict",
259
+ output: newOutput,
260
+ },
261
+ },
262
+ };
263
+ }
264
+ if (!stashedUserConfig[envId]) {
224
265
  return {
225
266
  type: "error",
226
- error:
227
- error instanceof Error ? error : new Error("Failed to resolve config"),
267
+ error: new Error("Failed to resolve config"),
228
268
  };
229
269
  }
270
+ return {
271
+ type: "success",
272
+ userConfig: stashedUserConfig[envId],
273
+ };
230
274
  }
@@ -0,0 +1,60 @@
1
+ import React from "react";
2
+ import { join } from "node:path";
3
+ import type { InlineCssCollectorProps } from "./types.js";
4
+ import { readFileSync } from "node:fs";
5
+
6
+ interface CssContent {
7
+ type?: string;
8
+ content: string;
9
+ key?: string;
10
+ path: string;
11
+ }
12
+
13
+ /**
14
+ * A component that inlines pre-transformed CSS content.
15
+ * Expects cssFiles to be an array of CssContent objects with the content already loaded.
16
+ */
17
+ export function InlineCssCollector({
18
+ children,
19
+ cssFiles,
20
+ moduleRootPath,
21
+ }: InlineCssCollectorProps) {
22
+ return React.createElement(
23
+ React.Fragment,
24
+ null,
25
+ cssFiles.map((file, index) => {
26
+ if (typeof file === "string") {
27
+ let filePath = file as string;
28
+ if(process.env["NODE_ENV"] === "development") {
29
+ return React.createElement("link", {
30
+ key: file,
31
+ rel: "stylesheet",
32
+ href: filePath,
33
+ });
34
+ }
35
+ return React.createElement(
36
+ "style",
37
+ {
38
+ key: file,
39
+ type: "text/css",
40
+ },
41
+ readFileSync(join(moduleRootPath, filePath), "utf-8")
42
+ );
43
+ }
44
+
45
+ const { type, content, key, path } = file as CssContent;
46
+ return React.createElement(
47
+ "style",
48
+ {
49
+ key: key ?? path ?? index,
50
+ type: type ?? "text/css",
51
+ ...(process.env["NODE_ENV"] === "development" && {
52
+ "data-vite-dev-id": join(moduleRootPath, path),
53
+ }),
54
+ },
55
+ content
56
+ );
57
+ }),
58
+ children
59
+ );
60
+ }
@@ -0,0 +1,62 @@
1
+ import React from "react";
2
+ import { join } from "node:path";
3
+
4
+ /**
5
+ * A component that emits <link> tags for CSS files during streaming.
6
+ * The high precedence ensures they bubble up to the document head.
7
+ */
8
+ export function CssCollector({
9
+ children,
10
+ cssFiles,
11
+ moduleBaseURL,
12
+ route = "/",
13
+ }: {
14
+ children?: React.ReactNode;
15
+ cssFiles: string[];
16
+ moduleBaseURL?: string;
17
+ route?: string;
18
+ }) {
19
+ // Calculate depth and prefix based on route
20
+ const depth = route.split('/').filter(Boolean).length;
21
+ const prefix = depth > 0 ? '../'.repeat(depth) : './';
22
+ const base = typeof moduleBaseURL === 'string' && moduleBaseURL !== '' ? moduleBaseURL : prefix;
23
+
24
+ const elements = cssFiles.map((file) => {
25
+ // Handle pre-processed CSS content - skip in link mode
26
+ if (typeof file === "object" && !React.isValidElement(file)) {
27
+ return null;
28
+ }
29
+ // Handle string paths
30
+ if (typeof file === "string" && file.endsWith('.css')) {
31
+ // Handle different types of paths
32
+ let url = file;
33
+ if (file.startsWith('http') || file.startsWith('data:')) {
34
+ // Keep absolute URLs as is
35
+ url = file;
36
+ } else if (file.startsWith('/')) {
37
+ // Convert absolute paths to relative
38
+ url = base + file.slice(1);
39
+ } else if (!file.startsWith('./') && !file.startsWith('../')) {
40
+ // Add prefix to relative paths that don't start with ./ or ../
41
+ url = join(base, file);
42
+ }
43
+
44
+ return React.createElement("link", {
45
+ key: file,
46
+ rel: "stylesheet",
47
+ href: url,
48
+ precedence: "high",
49
+ });
50
+ }
51
+
52
+ return null;
53
+ }).filter(Boolean);
54
+
55
+ return React.createElement(
56
+ React.Fragment,
57
+ null,
58
+ ...elements,
59
+ children
60
+ );
61
+ }
62
+