@cedarjs/vite 0.0.4

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 (213) hide show
  1. package/LICENSE +21 -0
  2. package/bins/rw-vite-build.mjs +61 -0
  3. package/bins/rw-vite-dev.mjs +57 -0
  4. package/bins/vite.mjs +12 -0
  5. package/dist/build/build.d.ts +21 -0
  6. package/dist/build/build.d.ts.map +1 -0
  7. package/dist/build/build.js +78 -0
  8. package/dist/buildFeServer.d.ts +6 -0
  9. package/dist/buildFeServer.d.ts.map +1 -0
  10. package/dist/buildFeServer.js +39 -0
  11. package/dist/buildRouteHooks.d.ts +3 -0
  12. package/dist/buildRouteHooks.d.ts.map +1 -0
  13. package/dist/buildRouteHooks.js +45 -0
  14. package/dist/buildRouteManifest.d.ts +6 -0
  15. package/dist/buildRouteManifest.d.ts.map +1 -0
  16. package/dist/buildRouteManifest.js +58 -0
  17. package/dist/buildRscClientAndServer.d.ts +4 -0
  18. package/dist/buildRscClientAndServer.d.ts.map +1 -0
  19. package/dist/buildRscClientAndServer.js +30 -0
  20. package/dist/bundled/react-server-dom-webpack.server.d.ts +2 -0
  21. package/dist/bundled/react-server-dom-webpack.server.d.ts.map +1 -0
  22. package/dist/bundled/react-server-dom-webpack.server.js +33529 -0
  23. package/dist/cjs/ClientRouter.d.ts +1 -0
  24. package/dist/cjs/build/build.d.ts +1 -0
  25. package/dist/cjs/build/build.js +116 -0
  26. package/dist/cjs/buildFeServer.d.ts +1 -0
  27. package/dist/cjs/buildFeServer.js +63 -0
  28. package/dist/cjs/buildRouteHooks.js +66 -0
  29. package/dist/cjs/buildRouteManifest.js +92 -0
  30. package/dist/cjs/buildRscClientAndServer.js +54 -0
  31. package/dist/cjs/client.d.ts +1 -0
  32. package/dist/cjs/clientSsr.d.ts +1 -0
  33. package/dist/cjs/devFeServer.js +153 -0
  34. package/dist/cjs/index.d.ts +1 -0
  35. package/dist/cjs/index.js +184 -0
  36. package/dist/cjs/lib/StatusError.js +36 -0
  37. package/dist/cjs/lib/entries.js +53 -0
  38. package/dist/cjs/lib/envVarDefinitions.js +87 -0
  39. package/dist/cjs/lib/getMergedConfig.js +187 -0
  40. package/dist/cjs/lib/onWarn.js +36 -0
  41. package/dist/cjs/lib/registerFwGlobalsAndShims.js +117 -0
  42. package/dist/cjs/middleware/index.js +22 -0
  43. package/dist/cjs/middleware/invokeMiddleware.js +52 -0
  44. package/dist/cjs/middleware/register.js +106 -0
  45. package/dist/cjs/middleware/types.js +16 -0
  46. package/dist/cjs/package.json +1 -0
  47. package/dist/cjs/plugins/vite-plugin-jsx-loader.js +42 -0
  48. package/dist/cjs/plugins/vite-plugin-remove-from-bundle.js +56 -0
  49. package/dist/cjs/plugins/vite-plugin-rsc-analyze.js +104 -0
  50. package/dist/cjs/plugins/vite-plugin-rsc-reload.js +75 -0
  51. package/dist/cjs/plugins/vite-plugin-rsc-routes-auto-loader.js +159 -0
  52. package/dist/cjs/plugins/vite-plugin-rsc-routes-imports.js +125 -0
  53. package/dist/cjs/plugins/vite-plugin-rsc-ssr-router-import.js +75 -0
  54. package/dist/cjs/plugins/vite-plugin-rsc-transform-client.js +229 -0
  55. package/dist/cjs/plugins/vite-plugin-rsc-transform-server.js +398 -0
  56. package/dist/cjs/plugins/vite-plugin-swap-apollo-provider.js +41 -0
  57. package/dist/cjs/rsc/rscBuildAnalyze.js +116 -0
  58. package/dist/cjs/rsc/rscBuildClient.js +91 -0
  59. package/dist/cjs/rsc/rscBuildCopyCssAssets.js +56 -0
  60. package/dist/cjs/rsc/rscBuildEntriesFile.js +115 -0
  61. package/dist/cjs/rsc/rscBuildForServer.js +157 -0
  62. package/dist/cjs/rsc/rscBuildForSsr.js +172 -0
  63. package/dist/cjs/rsc/rscBuildRwEnvVars.js +56 -0
  64. package/dist/cjs/rsc/rscRenderer.js +165 -0
  65. package/dist/cjs/rsc/rscRequestHandler.js +162 -0
  66. package/dist/cjs/rsc/rscStudioHandlers.js +154 -0
  67. package/dist/cjs/runFeServer.js +166 -0
  68. package/dist/cjs/streaming/buildForStreamingServer.js +65 -0
  69. package/dist/cjs/streaming/collectCss.js +52 -0
  70. package/dist/cjs/streaming/createReactStreamingHandler.js +173 -0
  71. package/dist/cjs/streaming/streamHelpers.js +228 -0
  72. package/dist/cjs/streaming/transforms/bufferedTransform.js +61 -0
  73. package/dist/cjs/streaming/transforms/cancelTimeoutTransform.js +34 -0
  74. package/dist/cjs/streaming/transforms/encode-decode.js +36 -0
  75. package/dist/cjs/streaming/transforms/serverInjectionTransform.js +84 -0
  76. package/dist/cjs/streaming/triggerRouteHooks.js +96 -0
  77. package/dist/cjs/types.js +16 -0
  78. package/dist/cjs/utils.js +91 -0
  79. package/dist/devFeServer.d.ts +2 -0
  80. package/dist/devFeServer.d.ts.map +1 -0
  81. package/dist/devFeServer.js +134 -0
  82. package/dist/index.d.ts +6 -0
  83. package/dist/index.d.ts.map +1 -0
  84. package/dist/index.js +154 -0
  85. package/dist/lib/StatusError.d.ts +8 -0
  86. package/dist/lib/StatusError.d.ts.map +1 -0
  87. package/dist/lib/StatusError.js +11 -0
  88. package/dist/lib/entries.d.ts +3 -0
  89. package/dist/lib/entries.d.ts.map +1 -0
  90. package/dist/lib/entries.js +32 -0
  91. package/dist/lib/envVarDefinitions.d.ts +14 -0
  92. package/dist/lib/envVarDefinitions.d.ts.map +1 -0
  93. package/dist/lib/envVarDefinitions.js +53 -0
  94. package/dist/lib/getMergedConfig.d.ts +11 -0
  95. package/dist/lib/getMergedConfig.d.ts.map +1 -0
  96. package/dist/lib/getMergedConfig.js +153 -0
  97. package/dist/lib/onWarn.d.ts +3 -0
  98. package/dist/lib/onWarn.d.ts.map +1 -0
  99. package/dist/lib/onWarn.js +12 -0
  100. package/dist/lib/registerFwGlobalsAndShims.d.ts +12 -0
  101. package/dist/lib/registerFwGlobalsAndShims.d.ts.map +1 -0
  102. package/dist/lib/registerFwGlobalsAndShims.js +83 -0
  103. package/dist/middleware/index.d.ts +2 -0
  104. package/dist/middleware/index.d.ts.map +1 -0
  105. package/dist/middleware/index.js +1 -0
  106. package/dist/middleware/invokeMiddleware.d.ts +14 -0
  107. package/dist/middleware/invokeMiddleware.d.ts.map +1 -0
  108. package/dist/middleware/invokeMiddleware.js +32 -0
  109. package/dist/middleware/register.d.ts +23 -0
  110. package/dist/middleware/register.d.ts.map +1 -0
  111. package/dist/middleware/register.js +69 -0
  112. package/dist/middleware/types.d.ts +3 -0
  113. package/dist/middleware/types.d.ts.map +1 -0
  114. package/dist/middleware/types.js +0 -0
  115. package/dist/package.json +1 -0
  116. package/dist/plugins/vite-plugin-jsx-loader.d.ts +8 -0
  117. package/dist/plugins/vite-plugin-jsx-loader.d.ts.map +1 -0
  118. package/dist/plugins/vite-plugin-jsx-loader.js +18 -0
  119. package/dist/plugins/vite-plugin-remove-from-bundle.d.ts +17 -0
  120. package/dist/plugins/vite-plugin-remove-from-bundle.d.ts.map +1 -0
  121. package/dist/plugins/vite-plugin-remove-from-bundle.js +31 -0
  122. package/dist/plugins/vite-plugin-rsc-analyze.d.ts +3 -0
  123. package/dist/plugins/vite-plugin-rsc-analyze.d.ts.map +1 -0
  124. package/dist/plugins/vite-plugin-rsc-analyze.js +70 -0
  125. package/dist/plugins/vite-plugin-rsc-reload.d.ts +3 -0
  126. package/dist/plugins/vite-plugin-rsc-reload.d.ts.map +1 -0
  127. package/dist/plugins/vite-plugin-rsc-reload.js +41 -0
  128. package/dist/plugins/vite-plugin-rsc-routes-auto-loader.d.ts +3 -0
  129. package/dist/plugins/vite-plugin-rsc-routes-auto-loader.d.ts.map +1 -0
  130. package/dist/plugins/vite-plugin-rsc-routes-auto-loader.js +130 -0
  131. package/dist/plugins/vite-plugin-rsc-routes-imports.d.ts +3 -0
  132. package/dist/plugins/vite-plugin-rsc-routes-imports.d.ts.map +1 -0
  133. package/dist/plugins/vite-plugin-rsc-routes-imports.js +96 -0
  134. package/dist/plugins/vite-plugin-rsc-ssr-router-import.d.ts +7 -0
  135. package/dist/plugins/vite-plugin-rsc-ssr-router-import.d.ts.map +1 -0
  136. package/dist/plugins/vite-plugin-rsc-ssr-router-import.js +41 -0
  137. package/dist/plugins/vite-plugin-rsc-transform-client.d.ts +3 -0
  138. package/dist/plugins/vite-plugin-rsc-transform-client.d.ts.map +1 -0
  139. package/dist/plugins/vite-plugin-rsc-transform-client.js +195 -0
  140. package/dist/plugins/vite-plugin-rsc-transform-server.d.ts +3 -0
  141. package/dist/plugins/vite-plugin-rsc-transform-server.d.ts.map +1 -0
  142. package/dist/plugins/vite-plugin-rsc-transform-server.js +364 -0
  143. package/dist/plugins/vite-plugin-swap-apollo-provider.d.ts +12 -0
  144. package/dist/plugins/vite-plugin-swap-apollo-provider.d.ts.map +1 -0
  145. package/dist/plugins/vite-plugin-swap-apollo-provider.js +17 -0
  146. package/dist/rsc/rscBuildAnalyze.d.ts +16 -0
  147. package/dist/rsc/rscBuildAnalyze.d.ts.map +1 -0
  148. package/dist/rsc/rscBuildAnalyze.js +92 -0
  149. package/dist/rsc/rscBuildClient.d.ts +7 -0
  150. package/dist/rsc/rscBuildClient.d.ts.map +1 -0
  151. package/dist/rsc/rscBuildClient.js +67 -0
  152. package/dist/rsc/rscBuildCopyCssAssets.d.ts +7 -0
  153. package/dist/rsc/rscBuildCopyCssAssets.d.ts.map +1 -0
  154. package/dist/rsc/rscBuildCopyCssAssets.js +22 -0
  155. package/dist/rsc/rscBuildEntriesFile.d.ts +10 -0
  156. package/dist/rsc/rscBuildEntriesFile.d.ts.map +1 -0
  157. package/dist/rsc/rscBuildEntriesFile.js +81 -0
  158. package/dist/rsc/rscBuildForServer.d.ts +7 -0
  159. package/dist/rsc/rscBuildForServer.d.ts.map +1 -0
  160. package/dist/rsc/rscBuildForServer.js +133 -0
  161. package/dist/rsc/rscBuildForSsr.d.ts +9 -0
  162. package/dist/rsc/rscBuildForSsr.d.ts.map +1 -0
  163. package/dist/rsc/rscBuildForSsr.js +138 -0
  164. package/dist/rsc/rscBuildRwEnvVars.d.ts +10 -0
  165. package/dist/rsc/rscBuildRwEnvVars.d.ts.map +1 -0
  166. package/dist/rsc/rscBuildRwEnvVars.js +22 -0
  167. package/dist/rsc/rscRenderer.d.ts +9 -0
  168. package/dist/rsc/rscRenderer.d.ts.map +1 -0
  169. package/dist/rsc/rscRenderer.js +130 -0
  170. package/dist/rsc/rscRequestHandler.d.ts +10 -0
  171. package/dist/rsc/rscRequestHandler.d.ts.map +1 -0
  172. package/dist/rsc/rscRequestHandler.js +131 -0
  173. package/dist/rsc/rscStudioHandlers.d.ts +10 -0
  174. package/dist/rsc/rscStudioHandlers.d.ts.map +1 -0
  175. package/dist/rsc/rscStudioHandlers.js +120 -0
  176. package/dist/runFeServer.d.ts +2 -0
  177. package/dist/runFeServer.d.ts.map +1 -0
  178. package/dist/runFeServer.js +132 -0
  179. package/dist/streaming/buildForStreamingServer.d.ts +4 -0
  180. package/dist/streaming/buildForStreamingServer.d.ts.map +1 -0
  181. package/dist/streaming/buildForStreamingServer.js +41 -0
  182. package/dist/streaming/collectCss.d.ts +7 -0
  183. package/dist/streaming/collectCss.d.ts.map +1 -0
  184. package/dist/streaming/collectCss.js +27 -0
  185. package/dist/streaming/createReactStreamingHandler.d.ts +12 -0
  186. package/dist/streaming/createReactStreamingHandler.d.ts.map +1 -0
  187. package/dist/streaming/createReactStreamingHandler.js +139 -0
  188. package/dist/streaming/streamHelpers.d.ts +23 -0
  189. package/dist/streaming/streamHelpers.d.ts.map +1 -0
  190. package/dist/streaming/streamHelpers.js +192 -0
  191. package/dist/streaming/transforms/bufferedTransform.d.ts +2 -0
  192. package/dist/streaming/transforms/bufferedTransform.d.ts.map +1 -0
  193. package/dist/streaming/transforms/bufferedTransform.js +37 -0
  194. package/dist/streaming/transforms/cancelTimeoutTransform.d.ts +2 -0
  195. package/dist/streaming/transforms/cancelTimeoutTransform.d.ts.map +1 -0
  196. package/dist/streaming/transforms/cancelTimeoutTransform.js +10 -0
  197. package/dist/streaming/transforms/encode-decode.d.ts +3 -0
  198. package/dist/streaming/transforms/encode-decode.d.ts.map +1 -0
  199. package/dist/streaming/transforms/encode-decode.js +11 -0
  200. package/dist/streaming/transforms/serverInjectionTransform.d.ts +16 -0
  201. package/dist/streaming/transforms/serverInjectionTransform.d.ts.map +1 -0
  202. package/dist/streaming/transforms/serverInjectionTransform.js +60 -0
  203. package/dist/streaming/triggerRouteHooks.d.ts +26 -0
  204. package/dist/streaming/triggerRouteHooks.d.ts.map +1 -0
  205. package/dist/streaming/triggerRouteHooks.js +71 -0
  206. package/dist/types.d.ts +31 -0
  207. package/dist/types.d.ts.map +1 -0
  208. package/dist/types.js +0 -0
  209. package/dist/utils.d.ts +14 -0
  210. package/dist/utils.d.ts.map +1 -0
  211. package/dist/utils.js +62 -0
  212. package/inject/reactRefresh.js +7 -0
  213. package/package.json +116 -0
@@ -0,0 +1,364 @@
1
+ import path from "node:path";
2
+ import * as babel from "@babel/core";
3
+ import * as swc from "@swc/core";
4
+ function rscTransformUseServerPlugin(outDir, serverEntryFiles) {
5
+ return {
6
+ name: "rsc-transform-use-server-plugin",
7
+ transform: async function(code, id) {
8
+ if (!code.includes("use server")) {
9
+ return code;
10
+ }
11
+ let mod;
12
+ const isTypescript = id.endsWith(".ts") || id.endsWith(".tsx");
13
+ try {
14
+ mod = swc.parseSync(code, {
15
+ target: "es2022",
16
+ syntax: isTypescript ? "typescript" : "ecmascript",
17
+ tsx: id.endsWith(".tsx"),
18
+ jsx: id.endsWith(".jsx")
19
+ });
20
+ } catch (e) {
21
+ console.error("Error parsing", id, e.message);
22
+ return code;
23
+ }
24
+ let useClient = false;
25
+ let moduleScopedUseServer = false;
26
+ for (const node of mod.body) {
27
+ if (node.type !== "ExpressionStatement" || node.expression.type !== "StringLiteral") {
28
+ continue;
29
+ }
30
+ if (node.expression.value === "use client") {
31
+ useClient = true;
32
+ }
33
+ if (node.expression.value === "use server") {
34
+ moduleScopedUseServer = true;
35
+ }
36
+ }
37
+ if (useClient && moduleScopedUseServer) {
38
+ throw new Error(
39
+ 'Cannot have both "use client" and "use server" directives in the same file.'
40
+ );
41
+ }
42
+ let builtFileName = id;
43
+ const serverEntryKey = Object.entries(serverEntryFiles).find(
44
+ ([_key, value]) => value === id
45
+ )?.[0];
46
+ if (serverEntryKey) {
47
+ builtFileName = path.join(outDir, "assets", serverEntryKey + ".mjs");
48
+ if (process.platform === "win32") {
49
+ builtFileName = builtFileName.replaceAll("\\", "/");
50
+ }
51
+ }
52
+ if (!builtFileName) {
53
+ throw new Error(
54
+ `Could not find ${id} in serverEntryFiles: ` + JSON.stringify(serverEntryFiles)
55
+ );
56
+ }
57
+ let transformedCode = code;
58
+ if (moduleScopedUseServer) {
59
+ transformedCode = transformServerModule(mod, builtFileName, code);
60
+ } else {
61
+ const result = babel.transformSync(code, {
62
+ filename: id,
63
+ presets: ["@babel/preset-typescript"],
64
+ plugins: [[babelPluginTransformServerAction, { url: builtFileName }]]
65
+ });
66
+ if (!result) {
67
+ console.error("Failed to transform code", id, code);
68
+ throw new Error("Failed to transform code");
69
+ }
70
+ if (!result.code) {
71
+ console.error("Failed to transform code", id, code);
72
+ throw new Error("Transform didn't return any code");
73
+ }
74
+ transformedCode = result.code;
75
+ }
76
+ return transformedCode;
77
+ }
78
+ };
79
+ }
80
+ function transformServerModule(mod, url, code) {
81
+ const localNames = /* @__PURE__ */ new Map();
82
+ const localTypes = /* @__PURE__ */ new Map();
83
+ for (const node of mod.body) {
84
+ switch (node.type) {
85
+ // TODO (RSC): Add code comments with examples of each type of node
86
+ case "ExportDeclaration":
87
+ if (node.declaration.type === "FunctionDeclaration") {
88
+ const name = node.declaration.identifier.value;
89
+ localNames.set(name, name);
90
+ localTypes.set(name, "function");
91
+ } else if (node.declaration.type === "VariableDeclaration") {
92
+ for (const declaration of node.declaration.declarations) {
93
+ if (declaration.id.type === "Identifier") {
94
+ const name = declaration.id.value;
95
+ localNames.set(name, name);
96
+ }
97
+ }
98
+ }
99
+ break;
100
+ case "ExportDefaultDeclaration":
101
+ if (node.decl.type === "FunctionExpression") {
102
+ const identifier = node.decl.identifier;
103
+ if (identifier) {
104
+ localNames.set(identifier.value, "default");
105
+ localTypes.set(identifier.value, "function");
106
+ }
107
+ }
108
+ break;
109
+ case "ExportNamedDeclaration":
110
+ for (const specifier of node.specifiers) {
111
+ if (specifier.type === "ExportSpecifier") {
112
+ const name = specifier.orig.value;
113
+ if (specifier.exported?.type === "Identifier") {
114
+ const exportedName = specifier.exported.value;
115
+ localNames.set(name, exportedName);
116
+ } else if (specifier.orig.type === "Identifier") {
117
+ localNames.set(name, name);
118
+ }
119
+ }
120
+ }
121
+ break;
122
+ case "ExportDefaultExpression":
123
+ if (node.expression.type === "Identifier") {
124
+ localNames.set(node.expression.value, "default");
125
+ }
126
+ break;
127
+ }
128
+ }
129
+ let newSrc = code + '\n\nimport {registerServerReference} from "react-server-dom-webpack/server";\n';
130
+ localNames.forEach(function(exported, local) {
131
+ if (localTypes.get(local) !== "function") {
132
+ newSrc += "if (typeof " + local + ' === "function") ';
133
+ }
134
+ const urlStr = JSON.stringify(url);
135
+ const exportedStr = JSON.stringify(exported);
136
+ newSrc += `registerServerReference(${local},${urlStr},${exportedStr});
137
+ `;
138
+ });
139
+ return newSrc;
140
+ }
141
+ function babelPluginTransformServerAction({
142
+ types: t
143
+ }) {
144
+ const localNames = /* @__PURE__ */ new Map();
145
+ const localTypes = /* @__PURE__ */ new Map();
146
+ const serverActionNodes = [];
147
+ const topLevelFunctions = [];
148
+ return {
149
+ name: "babel-plugin-redwood-transform-server-action",
150
+ visitor: {
151
+ Program: {
152
+ enter(path2) {
153
+ path2.node.body.forEach((statement) => {
154
+ if (t.isFunctionDeclaration(statement)) {
155
+ if (hasUseServerDirective(statement)) {
156
+ const name = statement.id?.name;
157
+ if (!name) {
158
+ throw new Error("Function declaration must have a name");
159
+ }
160
+ topLevelFunctions.push(name);
161
+ localTypes.set(name, "function");
162
+ }
163
+ }
164
+ if (t.isVariableDeclaration(statement)) {
165
+ statement.declarations.forEach((declarator) => {
166
+ if (t.isFunctionExpression(declarator.init)) {
167
+ if (hasUseServerDirective(declarator.init)) {
168
+ const name = declarator.id.type === "Identifier" ? declarator.id.name : void 0;
169
+ if (!name) {
170
+ throw new Error("Function declaration must have a name");
171
+ }
172
+ topLevelFunctions.push(name);
173
+ localTypes.set(name, "function");
174
+ }
175
+ } else if (t.isArrowFunctionExpression(declarator.init)) {
176
+ if (hasUseServerDirective(declarator.init)) {
177
+ const name = declarator.id.type === "Identifier" ? declarator.id.name : void 0;
178
+ if (!name) {
179
+ throw new Error("Function declaration must have a name");
180
+ }
181
+ topLevelFunctions.push(name);
182
+ localTypes.set(name, "function");
183
+ }
184
+ }
185
+ });
186
+ }
187
+ });
188
+ },
189
+ exit(path2, state) {
190
+ if (serverActionNodes.length === 0 && localTypes.size === 0) {
191
+ return;
192
+ }
193
+ const body = path2.node.body;
194
+ body.push(
195
+ t.importDeclaration(
196
+ [
197
+ t.importSpecifier(
198
+ t.identifier("registerServerReference"),
199
+ t.identifier("registerServerReference")
200
+ )
201
+ ],
202
+ t.stringLiteral("react-server-dom-webpack/server")
203
+ )
204
+ );
205
+ serverActionNodes.forEach((functionDeclaration) => {
206
+ body.push(t.exportNamedDeclaration(functionDeclaration));
207
+ const name = functionDeclaration.id?.name || "";
208
+ body.push(registerServerRef(name, state.opts.url, name));
209
+ });
210
+ localNames.forEach((exportedName, localName) => {
211
+ if (!localTypes.get(localName)) {
212
+ return;
213
+ }
214
+ const localType = localTypes.get(localName);
215
+ if (localType === "function") {
216
+ body.push(
217
+ registerServerRef(localName, state.opts.url, exportedName)
218
+ );
219
+ } else {
220
+ body.push(
221
+ t.ifStatement(
222
+ t.binaryExpression(
223
+ "===",
224
+ t.unaryExpression("typeof", t.identifier(localName)),
225
+ t.stringLiteral("function")
226
+ ),
227
+ registerServerRef(localName, state.opts.url, exportedName)
228
+ )
229
+ );
230
+ }
231
+ });
232
+ }
233
+ },
234
+ ExportNamedDeclaration(path2) {
235
+ const declaration = path2.node.declaration;
236
+ const specifiers = path2.node.specifiers;
237
+ if (t.isFunctionDeclaration(declaration)) {
238
+ if (hasUseServerDirective(declaration)) {
239
+ const identifier = declaration.id?.name;
240
+ if (identifier) {
241
+ localNames.set(identifier, identifier);
242
+ localTypes.set(identifier, "function");
243
+ }
244
+ } else {
245
+ const body = declaration.body;
246
+ const serverActionNodeIndex = indexOfServerActionNode(body);
247
+ if (serverActionNodeIndex >= 0) {
248
+ const serverActionNode = body.body[serverActionNodeIndex];
249
+ if (serverActionNode && t.isFunctionDeclaration(serverActionNode)) {
250
+ const name = serverActionNode.id?.name;
251
+ if (!name) {
252
+ }
253
+ serverActionNodes.push(serverActionNode);
254
+ }
255
+ }
256
+ }
257
+ } else if (t.isVariableDeclaration(declaration)) {
258
+ for (const declarator of declaration.declarations) {
259
+ const init = declarator.init;
260
+ if (!t.isArrowFunctionExpression(init) || !t.isBlockStatement(init.body)) {
261
+ continue;
262
+ }
263
+ if (hasUseServerDirective(init)) {
264
+ if (declarator.id.type === "Identifier") {
265
+ localNames.set(declarator.id.name, declarator.id.name);
266
+ localTypes.set(declarator.id.name, "function");
267
+ }
268
+ } else {
269
+ const serverActionNodeIndex = indexOfServerActionNode(init.body);
270
+ if (serverActionNodeIndex >= 0) {
271
+ const serverActionNode = init.body.body[serverActionNodeIndex];
272
+ if (t.isFunctionDeclaration(serverActionNode)) {
273
+ const name = serverActionNode.id?.name;
274
+ if (!name) {
275
+ throw new Error("Server action must have a name");
276
+ }
277
+ const uniqueName = `__rwjs__rsa${serverActionNodes.length}_${name}`;
278
+ serverActionNode.id = t.identifier(uniqueName);
279
+ serverActionNodes.push(serverActionNode);
280
+ init.body.body[serverActionNodeIndex] = t.variableDeclaration(
281
+ "const",
282
+ [
283
+ t.variableDeclarator(
284
+ t.identifier(name),
285
+ t.identifier(uniqueName)
286
+ )
287
+ ]
288
+ );
289
+ }
290
+ }
291
+ }
292
+ }
293
+ } else if (specifiers.length) {
294
+ specifiers.forEach((specifier) => {
295
+ if (t.isExportSpecifier(specifier)) {
296
+ const exportedName = t.isStringLiteral(specifier.exported) ? specifier.exported.value : specifier.exported.name;
297
+ localNames.set(specifier.local.name, exportedName);
298
+ }
299
+ });
300
+ }
301
+ },
302
+ ExportDefaultDeclaration(path2) {
303
+ const declaration = path2.node.declaration;
304
+ if (t.isFunctionDeclaration(declaration)) {
305
+ if (hasUseServerDirective(declaration)) {
306
+ const identifier = declaration.id?.name;
307
+ if (identifier) {
308
+ localNames.set(identifier, "default");
309
+ localTypes.set(identifier, "function");
310
+ }
311
+ } else {
312
+ const body = declaration.body;
313
+ const serverActionNodeIndex = indexOfServerActionNode(body);
314
+ if (serverActionNodeIndex >= 0) {
315
+ const serverActionNode = body.body[serverActionNodeIndex];
316
+ if (serverActionNode && t.isFunctionDeclaration(serverActionNode)) {
317
+ const name = serverActionNode.id?.name;
318
+ if (!name) {
319
+ throw new Error("Server action must have a name");
320
+ }
321
+ const uniqueName = `__rwjs__rsa${serverActionNodes.length}_${name}`;
322
+ serverActionNode.id = t.identifier(uniqueName);
323
+ serverActionNodes.push(serverActionNode);
324
+ body.body[serverActionNodeIndex] = t.variableDeclaration(
325
+ "const",
326
+ [
327
+ t.variableDeclarator(
328
+ t.identifier(name),
329
+ t.identifier(uniqueName)
330
+ )
331
+ ]
332
+ );
333
+ }
334
+ }
335
+ }
336
+ }
337
+ }
338
+ }
339
+ };
340
+ function hasUseServerDirective(statement) {
341
+ return "directives" in statement.body && statement.body.directives.some(
342
+ (directive) => directive.value.value === "use server"
343
+ );
344
+ }
345
+ function indexOfServerActionNode(blockStatement) {
346
+ return blockStatement.body.findIndex(
347
+ (node) => {
348
+ return t.isFunctionDeclaration(node) && hasUseServerDirective(node);
349
+ }
350
+ );
351
+ }
352
+ function registerServerRef(localName, url, exportedName) {
353
+ return t.expressionStatement(
354
+ t.callExpression(t.identifier("registerServerReference"), [
355
+ t.identifier(localName),
356
+ t.stringLiteral(url),
357
+ t.stringLiteral(exportedName)
358
+ ])
359
+ );
360
+ }
361
+ }
362
+ export {
363
+ rscTransformUseServerPlugin
364
+ };
@@ -0,0 +1,12 @@
1
+ import type { PluginOption } from 'vite';
2
+ /**
3
+ *
4
+ * Temporary plugin, that swaps the ApolloProvider import with the Suspense enabled one,
5
+ * until it becomes stable.
6
+ *
7
+ * import { RedwoodApolloProvider } from "@cedarjs/web/apollo" ->
8
+ * import { RedwoodApolloProvider } from "@cedarjs/web/dist/apollo/suspense"
9
+ *
10
+ */
11
+ export declare function swapApolloProvider(): PluginOption;
12
+ //# sourceMappingURL=vite-plugin-swap-apollo-provider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vite-plugin-swap-apollo-provider.d.ts","sourceRoot":"","sources":["../../src/plugins/vite-plugin-swap-apollo-provider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,MAAM,CAAA;AAExC;;;;;;;;GAQG;AACH,wBAAgB,kBAAkB,IAAI,YAAY,CAcjD"}
@@ -0,0 +1,17 @@
1
+ function swapApolloProvider() {
2
+ return {
3
+ name: "redwood-swap-apollo-provider",
4
+ async transform(code, id) {
5
+ if (/web\/src\/App\.(ts|tsx|js|jsx)$/.test(id)) {
6
+ return code.replace(
7
+ "@cedarjs/web/apollo",
8
+ "@cedarjs/web/dist/apollo/suspense"
9
+ );
10
+ }
11
+ return code;
12
+ }
13
+ };
14
+ }
15
+ export {
16
+ swapApolloProvider
17
+ };
@@ -0,0 +1,16 @@
1
+ /**
2
+ * RSC build. Step 1.
3
+ * buildFeServer -> buildRscFeServer -> rscBuildAnalyze
4
+ * Uses rscAnalyzePlugin to collect client and server entry points
5
+ * Starts building the AST in entries.ts
6
+ * Doesn't output any files, only collects a list of RSCs and RSAs
7
+ */
8
+ export declare function rscBuildAnalyze(): Promise<{
9
+ clientEntryFiles: {
10
+ [k: string]: string;
11
+ };
12
+ serverEntryFiles: {
13
+ [k: string]: string;
14
+ };
15
+ }>;
16
+ //# sourceMappingURL=rscBuildAnalyze.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rscBuildAnalyze.d.ts","sourceRoot":"","sources":["../../src/rsc/rscBuildAnalyze.ts"],"names":[],"mappings":"AAQA;;;;;;GAMG;AACH,wBAAsB,eAAe;;;;;;;GAgGpC"}
@@ -0,0 +1,92 @@
1
+ import { build as viteBuild } from "vite";
2
+ import { getPaths } from "@cedarjs/project-config";
3
+ import { getEntries } from "../lib/entries.js";
4
+ import { onWarn } from "../lib/onWarn.js";
5
+ import { rscAnalyzePlugin } from "../plugins/vite-plugin-rsc-analyze.js";
6
+ async function rscBuildAnalyze() {
7
+ console.log("\n");
8
+ console.log("1. rscBuildAnalyze");
9
+ console.log("==================\n");
10
+ const rwPaths = getPaths();
11
+ const clientEntryFileSet = /* @__PURE__ */ new Set();
12
+ const serverEntryFileSet = /* @__PURE__ */ new Set();
13
+ if (!rwPaths.web.viteConfig) {
14
+ throw new Error("Vite config not found");
15
+ }
16
+ await viteBuild({
17
+ configFile: rwPaths.web.viteConfig,
18
+ root: rwPaths.web.src,
19
+ // @MARK: We don't care about the build output from this step. It's just
20
+ // for returning the entry names. Plus, the entire RSC build is chatty
21
+ // enough as it is. You can enable this temporarily if you need to for
22
+ // debugging, but we're keeping it silent by default.
23
+ logLevel: "silent",
24
+ plugins: [
25
+ rscAnalyzePlugin(
26
+ (id) => clientEntryFileSet.add(id),
27
+ (id) => serverEntryFileSet.add(id)
28
+ )
29
+ ],
30
+ ssr: {
31
+ // We can ignore everything that starts with `node:` because it's not
32
+ // going to be RSCs
33
+ // As of vite 5.2 `true` here means "all except node built-ins"
34
+ noExternal: true,
35
+ // Anything we know won't have "use client" or "use server" in it can
36
+ // safely be external. The more we can externalize the better, because
37
+ // it means we can skip analyzing them, which means faster build times.
38
+ external: [
39
+ "@prisma/client",
40
+ "@prisma/fetch-engine",
41
+ "@prisma/internals",
42
+ "@cedarjs/auth-dbauth-api",
43
+ "@cedarjs/cookie-jar",
44
+ "@cedarjs/server-store",
45
+ "@simplewebauthn/server",
46
+ "graphql-scalars",
47
+ "minimatch",
48
+ "playwright",
49
+ "react"
50
+ ],
51
+ resolve: {
52
+ externalConditions: ["react-server"]
53
+ }
54
+ },
55
+ build: {
56
+ // TODO (RSC): Remove `minify: false` when we don't need to debug as often
57
+ minify: false,
58
+ manifest: "rsc-build-manifest.json",
59
+ write: false,
60
+ // We generate the entries from the simple `getEntries` function that analyses
61
+ // the various pages plus the ServerEntry file. This may need revisiting when we
62
+ // spend time on improving dev support or expand the scope of the components
63
+ // that are looked up via the entries mappings.
64
+ ssr: true,
65
+ rollupOptions: {
66
+ onwarn: onWarn,
67
+ input: getEntries()
68
+ }
69
+ }
70
+ });
71
+ const clientEntryFiles = Object.fromEntries(
72
+ Array.from(clientEntryFileSet).map((filename, i) => {
73
+ const rscName = `rsc-${filename.split(/[\/\\]/).at(-1)}-${i}`;
74
+ return [rscName, filename];
75
+ })
76
+ );
77
+ const serverEntryFiles = Object.fromEntries(
78
+ Array.from(serverEntryFileSet).map((filename, i) => {
79
+ const rsaName = `rsa-${filename.split(/[\/\\]/).at(-1)}-${i}`;
80
+ return [rsaName, filename];
81
+ })
82
+ );
83
+ console.log("clientEntryFiles", clientEntryFiles);
84
+ console.log("serverEntryFiles", serverEntryFiles);
85
+ return {
86
+ clientEntryFiles,
87
+ serverEntryFiles
88
+ };
89
+ }
90
+ export {
91
+ rscBuildAnalyze
92
+ };
@@ -0,0 +1,7 @@
1
+ /**
2
+ * RSC build. Step 2.
3
+ * buildFeServer -> buildRscFeServer -> rscBuildClient
4
+ * Generate the client bundle
5
+ */
6
+ export declare function rscBuildClient(clientEntryFiles: Record<string, string>): Promise<[import("rollup").OutputChunk, ...(import("rollup").OutputChunk | import("rollup").OutputAsset)[]]>;
7
+ //# sourceMappingURL=rscBuildClient.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rscBuildClient.d.ts","sourceRoot":"","sources":["../../src/rsc/rscBuildClient.ts"],"names":[],"mappings":"AAQA;;;;GAIG;AACH,wBAAsB,cAAc,CAAC,gBAAgB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,+GAqE5E"}
@@ -0,0 +1,67 @@
1
+ import { build as viteBuild } from "vite";
2
+ import { getPaths } from "@cedarjs/project-config";
3
+ import { onWarn } from "../lib/onWarn.js";
4
+ import { rscRoutesAutoLoader } from "../plugins/vite-plugin-rsc-routes-auto-loader.js";
5
+ import { ensureProcessDirWeb } from "../utils.js";
6
+ async function rscBuildClient(clientEntryFiles) {
7
+ console.log("\n");
8
+ console.log("2. rscBuildClient");
9
+ console.log("=================\n");
10
+ const rwPaths = getPaths();
11
+ ensureProcessDirWeb();
12
+ if (!rwPaths.web.entryClient) {
13
+ throw new Error("Missing web/src/entry.client");
14
+ }
15
+ const clientBuildOutput = await viteBuild({
16
+ envFile: false,
17
+ define: {
18
+ "process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV)
19
+ },
20
+ build: {
21
+ // TODO (RSC): Remove `minify: false` when we don't need to debug as often
22
+ minify: false,
23
+ outDir: rwPaths.web.distBrowser,
24
+ emptyOutDir: true,
25
+ // Needed because `outDir` is not inside `root`
26
+ rollupOptions: {
27
+ onwarn: onWarn,
28
+ input: {
29
+ // @MARK: temporary hack to find the entry client so we can get the
30
+ // index.css bundle but we don't actually want this on an rsc page!
31
+ __rwjs__client_entry: rwPaths.web.entryClient,
32
+ // we need this, so that the output contains rsc-specific bundles
33
+ // for the client-only components. They get loaded once the page is
34
+ // rendered
35
+ ...clientEntryFiles
36
+ },
37
+ preserveEntrySignatures: "exports-only",
38
+ output: {
39
+ // This is not ideal. See
40
+ // https://rollupjs.org/faqs/#why-do-additional-imports-turn-up-in-my-entry-chunks-when-code-splitting
41
+ // But we need it to prevent `import 'client-only'` from being
42
+ // hoisted into App.tsx
43
+ // TODO (RSC): Fix when https://github.com/rollup/rollup/issues/5235
44
+ // is resolved
45
+ hoistTransitiveImports: false,
46
+ entryFileNames: () => {
47
+ return "assets/[name]-[hash].mjs";
48
+ },
49
+ chunkFileNames: `assets/[name]-[hash].mjs`
50
+ }
51
+ },
52
+ manifest: "client-build-manifest.json"
53
+ },
54
+ esbuild: {
55
+ logLevel: "debug"
56
+ },
57
+ logLevel: "info",
58
+ plugins: [rscRoutesAutoLoader()]
59
+ });
60
+ if (!("output" in clientBuildOutput)) {
61
+ throw new Error("Unexpected vite client build output");
62
+ }
63
+ return clientBuildOutput.output;
64
+ }
65
+ export {
66
+ rscBuildClient
67
+ };
@@ -0,0 +1,7 @@
1
+ import type { rscBuildForServer } from './rscBuildForServer.js';
2
+ /**
3
+ * RSC build. Step 5.
4
+ * Copy CSS assets from server to client
5
+ */
6
+ export declare function rscBuildCopyCssAssets(serverBuildOutput: Awaited<ReturnType<typeof rscBuildForServer>>): Promise<void[]>;
7
+ //# sourceMappingURL=rscBuildCopyCssAssets.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rscBuildCopyCssAssets.d.ts","sourceRoot":"","sources":["../../src/rsc/rscBuildCopyCssAssets.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAA;AAE/D;;;GAGG;AACH,wBAAgB,qBAAqB,CACnC,iBAAiB,EAAE,OAAO,CAAC,UAAU,CAAC,OAAO,iBAAiB,CAAC,CAAC,mBAwBjE"}
@@ -0,0 +1,22 @@
1
+ import fs from "fs/promises";
2
+ import path from "path";
3
+ import { getPaths } from "@cedarjs/project-config";
4
+ function rscBuildCopyCssAssets(serverBuildOutput) {
5
+ console.log("\n");
6
+ console.log("5. rscBuildCopyCssAssets");
7
+ console.log("========================\n");
8
+ const rwPaths = getPaths();
9
+ return Promise.all(
10
+ serverBuildOutput.filter((item) => {
11
+ return item.type === "asset" && item.fileName.endsWith(".css");
12
+ }).map((cssAsset) => {
13
+ return fs.copyFile(
14
+ path.join(rwPaths.web.distRsc, cssAsset.fileName),
15
+ path.join(rwPaths.web.distBrowser, cssAsset.fileName)
16
+ );
17
+ })
18
+ );
19
+ }
20
+ export {
21
+ rscBuildCopyCssAssets
22
+ };
@@ -0,0 +1,10 @@
1
+ import type { rscBuildClient } from './rscBuildClient.js';
2
+ import type { rscBuildForServer } from './rscBuildForServer.js';
3
+ /**
4
+ * RSC build. Step 6.
5
+ * Append a mapping of server asset names to client asset names to the
6
+ * `web/dist/rsc/entries.js` file.
7
+ * Only used by the RSC worker.
8
+ */
9
+ export declare function rscBuildEntriesMappings(clientBuildOutput: Awaited<ReturnType<typeof rscBuildClient>>, ssrBuildOutput: Awaited<ReturnType<typeof rscBuildClient>>, serverBuildOutput: Awaited<ReturnType<typeof rscBuildForServer>>, clientEntryFiles: Record<string, string>): Promise<void>;
10
+ //# sourceMappingURL=rscBuildEntriesFile.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rscBuildEntriesFile.d.ts","sourceRoot":"","sources":["../../src/rsc/rscBuildEntriesFile.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AACzD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAA;AAE/D;;;;;GAKG;AAIH,wBAAsB,uBAAuB,CAC3C,iBAAiB,EAAE,OAAO,CAAC,UAAU,CAAC,OAAO,cAAc,CAAC,CAAC,EAC7D,cAAc,EAAE,OAAO,CAAC,UAAU,CAAC,OAAO,cAAc,CAAC,CAAC,EAC1D,iBAAiB,EAAE,OAAO,CAAC,UAAU,CAAC,OAAO,iBAAiB,CAAC,CAAC,EAChE,gBAAgB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,iBAiGzC"}