@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,70 @@
1
+ import path from "node:path";
2
+ import * as swc from "@swc/core";
3
+ function rscAnalyzePlugin(clientEntryCallback, serverEntryCallback) {
4
+ return {
5
+ name: "redwood-rsc-analyze-plugin",
6
+ transform(code, id) {
7
+ const ext = path.extname(id);
8
+ if ([".ts", ".tsx", ".js", ".jsx"].includes(ext)) {
9
+ const mod = swc.parseSync(code, {
10
+ syntax: ext === ".ts" || ext === ".tsx" ? "typescript" : "ecmascript",
11
+ tsx: ext === ".tsx"
12
+ });
13
+ let directiveFound = false;
14
+ for (const item of mod.body) {
15
+ if (item.type === "ExpressionStatement" && item.expression.type === "StringLiteral") {
16
+ if (item.expression.value === "use client") {
17
+ clientEntryCallback(id);
18
+ directiveFound = true;
19
+ } else if (item.expression.value === "use server") {
20
+ serverEntryCallback(id);
21
+ directiveFound = true;
22
+ }
23
+ }
24
+ }
25
+ if (!directiveFound && code.includes("use server") && containsServerAction(mod)) {
26
+ serverEntryCallback(id);
27
+ }
28
+ }
29
+ return code;
30
+ }
31
+ };
32
+ }
33
+ function isServerAction(node) {
34
+ return node.body?.type === "BlockStatement" && node.body.stmts.some(
35
+ (s) => s.type === "ExpressionStatement" && s.expression.type === "StringLiteral" && s.expression.value === "use server"
36
+ );
37
+ }
38
+ function isFunctionDeclaration(node) {
39
+ return node.type === "FunctionDeclaration";
40
+ }
41
+ function isFunctionExpression(node) {
42
+ return node.type === "FunctionExpression";
43
+ }
44
+ function isArrowFunctionExpression(node) {
45
+ return node.type === "ArrowFunctionExpression";
46
+ }
47
+ function containsServerAction(mod) {
48
+ function walk(node) {
49
+ if (isFunctionDeclaration(node) || isFunctionExpression(node) || isArrowFunctionExpression(node)) {
50
+ if (isServerAction(node)) {
51
+ return true;
52
+ }
53
+ }
54
+ return Object.values(node).some(
55
+ (value) => (Array.isArray(value) ? value : [value]).some((v) => {
56
+ if (typeof v?.type === "string") {
57
+ return walk(v);
58
+ }
59
+ if (typeof v?.expression?.type === "string") {
60
+ return walk(v.expression);
61
+ }
62
+ return false;
63
+ })
64
+ );
65
+ }
66
+ return walk(mod);
67
+ }
68
+ export {
69
+ rscAnalyzePlugin
70
+ };
@@ -0,0 +1,3 @@
1
+ import type { Plugin } from 'vite';
2
+ export declare function rscReloadPlugin(fn: (type: 'full-reload') => void): Plugin;
3
+ //# sourceMappingURL=vite-plugin-rsc-reload.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vite-plugin-rsc-reload.d.ts","sourceRoot":"","sources":["../../src/plugins/vite-plugin-rsc-reload.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAA;AAElC,wBAAgB,eAAe,CAAC,EAAE,EAAE,CAAC,IAAI,EAAE,aAAa,KAAK,IAAI,GAAG,MAAM,CAgDzE"}
@@ -0,0 +1,41 @@
1
+ import path from "node:path";
2
+ import * as swc from "@swc/core";
3
+ function rscReloadPlugin(fn) {
4
+ let enabled = false;
5
+ const isClientEntry = (id, code) => {
6
+ const ext = path.extname(id);
7
+ if ([".ts", ".tsx", ".js", ".jsx"].includes(ext)) {
8
+ const mod = swc.parseSync(code, {
9
+ syntax: ext === ".ts" || ext === ".tsx" ? "typescript" : "ecmascript",
10
+ tsx: ext === ".tsx"
11
+ });
12
+ for (const item of mod.body) {
13
+ if (item.type === "ExpressionStatement" && item.expression.type === "StringLiteral" && item.expression.value === "use client") {
14
+ return true;
15
+ }
16
+ }
17
+ }
18
+ return false;
19
+ };
20
+ return {
21
+ name: "reload-plugin",
22
+ configResolved(config) {
23
+ if (config.mode === "development") {
24
+ enabled = true;
25
+ }
26
+ },
27
+ async handleHotUpdate(ctx) {
28
+ if (!enabled) {
29
+ return [];
30
+ }
31
+ if (ctx.modules.length && !isClientEntry(ctx.file, await ctx.read())) {
32
+ return fn("full-reload");
33
+ } else {
34
+ return [];
35
+ }
36
+ }
37
+ };
38
+ }
39
+ export {
40
+ rscReloadPlugin
41
+ };
@@ -0,0 +1,3 @@
1
+ import type { Plugin } from 'vite';
2
+ export declare function rscRoutesAutoLoader(): Plugin;
3
+ //# sourceMappingURL=vite-plugin-rsc-routes-auto-loader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vite-plugin-rsc-routes-auto-loader.d.ts","sourceRoot":"","sources":["../../src/plugins/vite-plugin-rsc-routes-auto-loader.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAA;AA6BlC,wBAAgB,mBAAmB,IAAI,MAAM,CAgK5C"}
@@ -0,0 +1,130 @@
1
+ import path from "path";
2
+ import babelGenerator from "@babel/generator";
3
+ const generate = babelGenerator.default;
4
+ import { parse as babelParse } from "@babel/parser/index.cjs";
5
+ import babelTraverse from "@babel/traverse";
6
+ const traverse = babelTraverse.default;
7
+ import * as t from "@babel/types";
8
+ import { normalizePath } from "vite";
9
+ import {
10
+ ensurePosixPath,
11
+ getPaths,
12
+ importStatementPath,
13
+ processPagesDir
14
+ } from "@cedarjs/project-config";
15
+ const getPathRelativeToSrc = (maybeAbsolutePath) => {
16
+ if (!path.isAbsolute(maybeAbsolutePath)) {
17
+ return maybeAbsolutePath;
18
+ }
19
+ return `./${path.relative(getPaths().web.src, maybeAbsolutePath)}`;
20
+ };
21
+ const withRelativeImports = (page) => {
22
+ return {
23
+ ...page,
24
+ relativeImport: ensurePosixPath(getPathRelativeToSrc(page.importPath))
25
+ };
26
+ };
27
+ function rscRoutesAutoLoader() {
28
+ const routesFileId = normalizePath(getPaths().web.routes);
29
+ const pages = processPagesDir().map(withRelativeImports);
30
+ const duplicatePageImportNames = /* @__PURE__ */ new Set();
31
+ const sortedPageImportNames = pages.map((page) => page.importName).sort();
32
+ for (let i = 0; i < sortedPageImportNames.length - 1; i++) {
33
+ if (sortedPageImportNames[i + 1] === sortedPageImportNames[i]) {
34
+ duplicatePageImportNames.add(sortedPageImportNames[i]);
35
+ }
36
+ }
37
+ if (duplicatePageImportNames.size > 0) {
38
+ const pageNames = Array.from(duplicatePageImportNames).map((name) => `'${name}'`).join(", ");
39
+ throw new Error(
40
+ `Unable to find only a single file ending in 'Page.{js,jsx,ts,tsx}' in the following page directories: ${pageNames}`
41
+ );
42
+ }
43
+ return {
44
+ name: "rsc-routes-auto-loader-dev",
45
+ transform: async function(code, id) {
46
+ if (id !== routesFileId) {
47
+ return null;
48
+ }
49
+ if (pages.length === 0) {
50
+ return null;
51
+ }
52
+ const ext = path.extname(id);
53
+ const plugins = [];
54
+ if (ext === ".jsx") {
55
+ plugins.push("jsx");
56
+ }
57
+ const ast = babelParse(code, {
58
+ sourceType: "unambiguous",
59
+ plugins
60
+ });
61
+ const importedNames = /* @__PURE__ */ new Set();
62
+ const allImports = /* @__PURE__ */ new Map();
63
+ const wrappers = /* @__PURE__ */ new Set();
64
+ traverse(ast, {
65
+ ImportDeclaration(path2) {
66
+ const importPath = path2.node.source.value;
67
+ if (importPath === null) {
68
+ return;
69
+ }
70
+ const userImportRelativePath = getPathRelativeToSrc(
71
+ importStatementPath(importPath)
72
+ );
73
+ const defaultSpecifier = path2.node.specifiers.filter(
74
+ (specifier) => t.isImportDefaultSpecifier(specifier)
75
+ )[0];
76
+ if (userImportRelativePath && defaultSpecifier) {
77
+ importedNames.add(defaultSpecifier.local.name);
78
+ }
79
+ path2.node.specifiers.forEach((specifier) => {
80
+ allImports.set(specifier.local.name, path2.node);
81
+ });
82
+ },
83
+ JSXElement() {
84
+ },
85
+ CallExpression(path2) {
86
+ if ((t.isIdentifier(path2.node.callee, { name: "jsxs" }) || t.isIdentifier(path2.node.callee, { name: "jsx" })) && t.isIdentifier(path2.node.arguments[0]) && path2.node.arguments[0].name === "Set") {
87
+ const jsxArgs = path2.node.arguments;
88
+ if (t.isObjectExpression(jsxArgs[1])) {
89
+ const wrapProp = jsxArgs[1].properties.find(
90
+ (prop) => t.isObjectProperty(prop) && t.isIdentifier(prop.key, { name: "wrap" })
91
+ );
92
+ if (t.isArrayExpression(wrapProp?.value)) {
93
+ wrapProp.value.elements.forEach((element) => {
94
+ if (t.isIdentifier(element)) {
95
+ wrappers.add(element.name);
96
+ }
97
+ });
98
+ } else if (t.isIdentifier(wrapProp?.value)) {
99
+ wrappers.add(wrapProp.value.name);
100
+ }
101
+ }
102
+ }
103
+ }
104
+ });
105
+ const nonImportedPages = pages.filter(
106
+ (page) => !importedNames.has(page.importName)
107
+ );
108
+ wrappers.forEach((wrapper) => {
109
+ const wrapperImport = allImports.get(wrapper);
110
+ if (wrapperImport) {
111
+ wrapperImport.source.value = "@cedarjs/router/dist/dummyComponent";
112
+ }
113
+ });
114
+ for (const page of nonImportedPages) {
115
+ ast.program.body.unshift(
116
+ t.variableDeclaration("const", [
117
+ t.variableDeclarator(
118
+ t.identifier(page.constName),
119
+ t.arrowFunctionExpression([], t.nullLiteral())
120
+ )
121
+ ])
122
+ );
123
+ }
124
+ return generate(ast).code;
125
+ }
126
+ };
127
+ }
128
+ export {
129
+ rscRoutesAutoLoader
130
+ };
@@ -0,0 +1,3 @@
1
+ import type { Plugin } from 'vite';
2
+ export declare function rscRoutesImports(): Plugin;
3
+ //# sourceMappingURL=vite-plugin-rsc-routes-imports.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vite-plugin-rsc-routes-imports.d.ts","sourceRoot":"","sources":["../../src/plugins/vite-plugin-rsc-routes-imports.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAA;AA2BlC,wBAAgB,gBAAgB,IAAI,MAAM,CAiGzC"}
@@ -0,0 +1,96 @@
1
+ import path from "path";
2
+ import babelGenerator from "@babel/generator";
3
+ const { default: generate } = babelGenerator;
4
+ import { parse as babelParse } from "@babel/parser/index.cjs";
5
+ import babelTraverse from "@babel/traverse";
6
+ const { default: traverse } = babelTraverse;
7
+ import * as t from "@babel/types";
8
+ import { normalizePath } from "vite";
9
+ import {
10
+ ensurePosixPath,
11
+ getPaths,
12
+ importStatementPath,
13
+ processPagesDir
14
+ } from "@cedarjs/project-config";
15
+ const getPathRelativeToSrc = (maybeAbsolutePath) => {
16
+ if (!path.isAbsolute(maybeAbsolutePath)) {
17
+ return maybeAbsolutePath;
18
+ }
19
+ return `./${path.relative(getPaths().web.src, maybeAbsolutePath)}`;
20
+ };
21
+ const withRelativeImports = (page) => {
22
+ return {
23
+ ...page,
24
+ relativeImport: ensurePosixPath(getPathRelativeToSrc(page.importPath))
25
+ };
26
+ };
27
+ function rscRoutesImports() {
28
+ const routesFileId = normalizePath(getPaths().web.routes);
29
+ const pages = processPagesDir().map(withRelativeImports);
30
+ const duplicatePageImportNames = /* @__PURE__ */ new Set();
31
+ const sortedPageImportNames = pages.map((page) => page.importName).sort();
32
+ for (let i = 0; i < sortedPageImportNames.length - 1; i++) {
33
+ if (sortedPageImportNames[i + 1] === sortedPageImportNames[i]) {
34
+ duplicatePageImportNames.add(sortedPageImportNames[i]);
35
+ }
36
+ }
37
+ if (duplicatePageImportNames.size > 0) {
38
+ const pageNames = Array.from(duplicatePageImportNames).map((name) => `'${name}'`).join(", ");
39
+ throw new Error(
40
+ `Unable to find only a single file ending in 'Page.{js,jsx,ts,tsx}' in the following page directories: ${pageNames}`
41
+ );
42
+ }
43
+ return {
44
+ name: "rsc-routes-imports",
45
+ transform: async function(code, id) {
46
+ if (id !== routesFileId) {
47
+ return null;
48
+ }
49
+ if (pages.length === 0) {
50
+ return null;
51
+ }
52
+ const ext = path.extname(id);
53
+ const plugins = [];
54
+ if (ext === ".jsx") {
55
+ plugins.push("jsx");
56
+ }
57
+ const ast = babelParse(code, {
58
+ sourceType: "unambiguous",
59
+ plugins
60
+ });
61
+ const importedNames = /* @__PURE__ */ new Set();
62
+ traverse(ast, {
63
+ ImportDeclaration(path2) {
64
+ const importPath = path2.node.source.value;
65
+ if (importPath === null) {
66
+ return;
67
+ }
68
+ const userImportRelativePath = getPathRelativeToSrc(
69
+ importStatementPath(path2.node.source?.value)
70
+ );
71
+ const defaultSpecifier = path2.node.specifiers.filter(
72
+ (specifier) => t.isImportDefaultSpecifier(specifier)
73
+ )[0];
74
+ if (userImportRelativePath && defaultSpecifier) {
75
+ importedNames.add(defaultSpecifier.local.name);
76
+ }
77
+ }
78
+ });
79
+ const nonImportedPages = pages.filter((page) => {
80
+ return !importedNames.has(page.importName);
81
+ });
82
+ for (const page of nonImportedPages) {
83
+ ast.program.body.unshift(
84
+ t.importDeclaration(
85
+ [t.importDefaultSpecifier(t.identifier(page.importName))],
86
+ t.stringLiteral(page.importPath)
87
+ )
88
+ );
89
+ }
90
+ return generate(ast).code;
91
+ }
92
+ };
93
+ }
94
+ export {
95
+ rscRoutesImports
96
+ };
@@ -0,0 +1,7 @@
1
+ import type { Plugin } from 'vite';
2
+ /**
3
+ * Transform `import { Router } from '@cedarjs/router/RscRouter'` to
4
+ * `import { Router } from '@cedarjs/router/SsrRouter'`
5
+ */
6
+ export declare function rscSsrRouterImport(): Plugin;
7
+ //# sourceMappingURL=vite-plugin-rsc-ssr-router-import.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vite-plugin-rsc-ssr-router-import.d.ts","sourceRoot":"","sources":["../../src/plugins/vite-plugin-rsc-ssr-router-import.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAA;AAKlC;;;GAGG;AACH,wBAAgB,kBAAkB,IAAI,MAAM,CAqC3C"}
@@ -0,0 +1,41 @@
1
+ import path from "node:path";
2
+ import babelGenerator from "@babel/generator";
3
+ const { default: generate } = babelGenerator;
4
+ import { parse as babelParse } from "@babel/parser/index.cjs";
5
+ import babelTraverse from "@babel/traverse";
6
+ const { default: traverse } = babelTraverse;
7
+ import * as t from "@babel/types";
8
+ import { normalizePath } from "vite";
9
+ import { getPaths } from "@cedarjs/project-config";
10
+ function rscSsrRouterImport() {
11
+ const routesFileId = normalizePath(getPaths().web.routes);
12
+ return {
13
+ name: "rsc-ssr-router-import",
14
+ transform: async function(code, id) {
15
+ if (id !== routesFileId) {
16
+ return null;
17
+ }
18
+ const ext = path.extname(id);
19
+ const plugins = [];
20
+ if (ext === ".jsx") {
21
+ plugins.push("jsx");
22
+ }
23
+ const ast = babelParse(code, {
24
+ sourceType: "unambiguous",
25
+ plugins
26
+ });
27
+ traverse(ast, {
28
+ ImportDeclaration(path2) {
29
+ const source = path2.node.source.value;
30
+ if (source === "@cedarjs/router/RscRouter") {
31
+ path2.node.source = t.stringLiteral("@cedarjs/router/SsrRouter");
32
+ }
33
+ }
34
+ });
35
+ return generate(ast).code;
36
+ }
37
+ };
38
+ }
39
+ export {
40
+ rscSsrRouterImport
41
+ };
@@ -0,0 +1,3 @@
1
+ import { type Plugin } from 'vite';
2
+ export declare function rscTransformUseClientPlugin(clientEntryFiles: Record<string, string>): Plugin;
3
+ //# sourceMappingURL=vite-plugin-rsc-transform-client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vite-plugin-rsc-transform-client.d.ts","sourceRoot":"","sources":["../../src/plugins/vite-plugin-rsc-transform-client.ts"],"names":[],"mappings":"AAIA,OAAO,EAAiB,KAAK,MAAM,EAAE,MAAM,MAAM,CAAA;AAIjD,wBAAgB,2BAA2B,CACzC,gBAAgB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GACvC,MAAM,CAoER"}
@@ -0,0 +1,195 @@
1
+ import path from "node:path";
2
+ import * as acorn from "acorn-loose";
3
+ import { normalizePath } from "vite";
4
+ import { getPaths } from "@cedarjs/project-config";
5
+ function rscTransformUseClientPlugin(clientEntryFiles) {
6
+ return {
7
+ name: "rsc-transform-use-client-plugin",
8
+ transform: async function(code, id) {
9
+ if (!code.includes("use client")) {
10
+ return code;
11
+ }
12
+ if (code.includes("$$id")) {
13
+ return code;
14
+ }
15
+ let body;
16
+ try {
17
+ body = acorn.parse(code, {
18
+ ecmaVersion: 2024,
19
+ sourceType: "module"
20
+ }).body;
21
+ } catch (x) {
22
+ console.error("Error parsing %s %s", id, x.message);
23
+ return code;
24
+ }
25
+ let useClient = false;
26
+ let useServer = false;
27
+ for (const node of body) {
28
+ if (node.type !== "ExpressionStatement" || !node.directive) {
29
+ break;
30
+ }
31
+ if (node.directive === "use client") {
32
+ useClient = true;
33
+ }
34
+ if (node.directive === "use server") {
35
+ useServer = true;
36
+ }
37
+ }
38
+ if (!useClient) {
39
+ return code;
40
+ }
41
+ if (useClient && useServer) {
42
+ throw new Error(
43
+ 'Cannot have both "use client" and "use server" directives in the same file.'
44
+ );
45
+ }
46
+ const transformedCode = await transformClientModule(
47
+ code,
48
+ body,
49
+ id,
50
+ clientEntryFiles
51
+ );
52
+ return transformedCode;
53
+ }
54
+ };
55
+ }
56
+ function addExportNames(names, node) {
57
+ switch (node.type) {
58
+ case "Identifier":
59
+ names.push(node.name);
60
+ return;
61
+ case "ObjectPattern":
62
+ for (const property of node.properties) {
63
+ addExportNames(names, property);
64
+ }
65
+ return;
66
+ case "ArrayPattern":
67
+ for (const element of node.elements) {
68
+ if (element) {
69
+ addExportNames(names, element);
70
+ }
71
+ }
72
+ return;
73
+ case "Property":
74
+ addExportNames(names, node.value);
75
+ return;
76
+ case "AssignmentPattern":
77
+ addExportNames(names, node.left);
78
+ return;
79
+ case "RestElement":
80
+ addExportNames(names, node.argument);
81
+ return;
82
+ case "ParenthesizedExpression":
83
+ addExportNames(names, node.expression);
84
+ return;
85
+ }
86
+ }
87
+ async function parseExportNamesIntoNames(code, body, names) {
88
+ for (const node of body) {
89
+ switch (node.type) {
90
+ case "ExportAllDeclaration":
91
+ if (node.exported) {
92
+ addExportNames(names, node.exported);
93
+ continue;
94
+ } else {
95
+ let childBody;
96
+ try {
97
+ childBody = acorn.parse(code, {
98
+ ecmaVersion: 2024,
99
+ sourceType: "module"
100
+ }).body;
101
+ } catch (x) {
102
+ console.error("Error parsing %s %s", "", x.message);
103
+ continue;
104
+ }
105
+ await parseExportNamesIntoNames(code, childBody, names);
106
+ continue;
107
+ }
108
+ case "ExportDefaultDeclaration":
109
+ names.push("default");
110
+ continue;
111
+ case "ExportNamedDeclaration":
112
+ if (node.declaration) {
113
+ if (node.declaration.type === "VariableDeclaration") {
114
+ const declarations = node.declaration.declarations;
115
+ for (const declaration of declarations) {
116
+ addExportNames(names, declaration.id);
117
+ }
118
+ } else {
119
+ addExportNames(names, node.declaration.id);
120
+ }
121
+ }
122
+ if (node.specifiers) {
123
+ const specifiers = node.specifiers;
124
+ for (const specifier of specifiers) {
125
+ addExportNames(names, specifier.exported);
126
+ }
127
+ }
128
+ continue;
129
+ // For CJS support
130
+ case "ExpressionStatement": {
131
+ let assignmentExpression = null;
132
+ if (node.expression.type === "AssignmentExpression") {
133
+ assignmentExpression = node.expression;
134
+ } else if (node.expression.type === "LogicalExpression" && node.expression.right.type === "AssignmentExpression") {
135
+ assignmentExpression = node.expression.right;
136
+ }
137
+ if (!assignmentExpression) {
138
+ continue;
139
+ }
140
+ if (assignmentExpression.left.type !== "MemberExpression") {
141
+ continue;
142
+ }
143
+ if (assignmentExpression.left.object.type !== "Identifier") {
144
+ continue;
145
+ }
146
+ if (assignmentExpression.left.object.name === "exports" && assignmentExpression.left.property.type === "Identifier") {
147
+ if (!names.includes(assignmentExpression.left.property.name)) {
148
+ names.push(assignmentExpression.left.property.name);
149
+ }
150
+ } else if (assignmentExpression.left.object.name === "module" && assignmentExpression.left.property.type === "Identifier" && assignmentExpression.left.property.name === "exports" && assignmentExpression.right.type === "ObjectExpression") {
151
+ assignmentExpression.right.properties.forEach((property) => {
152
+ if (property.type === "Property" && property.key.type === "Identifier") {
153
+ if (!names.includes(property.key.name)) {
154
+ names.push(property.key.name);
155
+ }
156
+ }
157
+ });
158
+ }
159
+ continue;
160
+ }
161
+ }
162
+ }
163
+ }
164
+ async function transformClientModule(code, body, url, clientEntryFiles) {
165
+ const names = [];
166
+ await parseExportNamesIntoNames(code, body, names);
167
+ console.log("transformClientModule names", names);
168
+ const entryRecord = Object.entries(clientEntryFiles).find(
169
+ ([_key, value]) => value === url
170
+ );
171
+ const loadId = normalizePath(
172
+ entryRecord ? path.join(getPaths().web.distRsc, "assets", `${entryRecord[0]}.mjs`) : url
173
+ );
174
+ let newSrc = 'import {registerClientReference} from "react-server-dom-webpack/server";\n';
175
+ for (const name of names) {
176
+ if (name === "default") {
177
+ newSrc += "export default registerClientReference(function() {";
178
+ newSrc += "throw new Error(" + JSON.stringify(
179
+ "Attempted to call the default export of " + url + " from the server but it's on the client. It's not possible to invoke a client function from the server, it can only be rendered as a Component or passed to props of a Client Component."
180
+ ) + ");";
181
+ } else {
182
+ newSrc += "export const " + name + " = ";
183
+ newSrc += "registerClientReference(function() {";
184
+ newSrc += "throw new Error(" + JSON.stringify(
185
+ "Attempted to call " + name + "() from the server but " + name + " is on the client. It's not possible to invoke a client function from the server, it can only be rendered as a Component or passed to props of a Client Component."
186
+ ) + ");";
187
+ }
188
+ newSrc += `},${JSON.stringify(loadId)},${JSON.stringify(name)})
189
+ ;`;
190
+ }
191
+ return newSrc;
192
+ }
193
+ export {
194
+ rscTransformUseClientPlugin
195
+ };
@@ -0,0 +1,3 @@
1
+ import type { Plugin } from 'vite';
2
+ export declare function rscTransformUseServerPlugin(outDir: string, serverEntryFiles: Record<string, string>): Plugin;
3
+ //# sourceMappingURL=vite-plugin-rsc-transform-server.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vite-plugin-rsc-transform-server.d.ts","sourceRoot":"","sources":["../../src/plugins/vite-plugin-rsc-transform-server.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAA;AAElC,wBAAgB,2BAA2B,CACzC,MAAM,EAAE,MAAM,EACd,gBAAgB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GACvC,MAAM,CA8GR"}