rwsdk 1.0.0-alpha.9 → 1.0.0-beta.1

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 (159) hide show
  1. package/dist/lib/constants.mjs +1 -2
  2. package/dist/lib/e2e/browser.d.mts +1 -1
  3. package/dist/lib/e2e/browser.mjs +3 -4
  4. package/dist/lib/e2e/dev.mjs +62 -52
  5. package/dist/lib/e2e/environment.d.mts +2 -6
  6. package/dist/lib/e2e/environment.mjs +72 -72
  7. package/dist/lib/e2e/index.d.mts +5 -4
  8. package/dist/lib/e2e/index.mjs +5 -4
  9. package/dist/lib/e2e/poll.d.mts +8 -0
  10. package/dist/lib/e2e/poll.mjs +31 -0
  11. package/dist/lib/e2e/release.mjs +4 -4
  12. package/dist/lib/e2e/retry.d.mts +4 -0
  13. package/dist/lib/e2e/retry.mjs +16 -0
  14. package/dist/lib/e2e/setup.d.mts +2 -0
  15. package/dist/lib/e2e/setup.mjs +1 -0
  16. package/dist/lib/e2e/tarball.d.mts +3 -2
  17. package/dist/lib/e2e/tarball.mjs +30 -5
  18. package/dist/lib/e2e/testHarness.d.mts +59 -50
  19. package/dist/lib/e2e/testHarness.mjs +289 -343
  20. package/dist/lib/getShortName.mjs +1 -2
  21. package/dist/lib/getShortName.test.mjs +2 -2
  22. package/dist/lib/getSrcPaths.js +2 -2
  23. package/dist/lib/hasPkgScript.test.mjs +2 -2
  24. package/dist/lib/jsonUtils.test.mjs +2 -2
  25. package/dist/lib/normalizeModulePath.test.mjs +2 -2
  26. package/dist/lib/setupEnvFiles.mjs +2 -2
  27. package/dist/lib/smokeTests/artifacts.mjs +2 -2
  28. package/dist/lib/smokeTests/browser.d.mts +1 -1
  29. package/dist/lib/smokeTests/browser.mjs +6 -7
  30. package/dist/lib/smokeTests/cleanup.mjs +6 -9
  31. package/dist/lib/smokeTests/codeUpdates.mjs +5 -5
  32. package/dist/lib/smokeTests/development.mjs +2 -2
  33. package/dist/lib/smokeTests/environment.d.mts +2 -3
  34. package/dist/lib/smokeTests/environment.mjs +17 -3
  35. package/dist/lib/smokeTests/release.d.mts +2 -2
  36. package/dist/lib/smokeTests/release.mjs +3 -3
  37. package/dist/lib/smokeTests/reporting.mjs +2 -2
  38. package/dist/lib/smokeTests/runSmokeTests.mjs +4 -4
  39. package/dist/lib/smokeTests/utils.mjs +3 -3
  40. package/dist/lib/testUtils/stubEnvVars.mjs +1 -1
  41. package/dist/llms/rules/middleware.d.ts +1 -1
  42. package/dist/llms/rules/middleware.js +4 -4
  43. package/dist/runtime/client/client.d.ts +2 -2
  44. package/dist/runtime/client/client.js +2 -2
  45. package/dist/runtime/client/navigation.test.js +1 -1
  46. package/dist/runtime/client/types.d.ts +1 -1
  47. package/dist/runtime/entries/client.d.ts +2 -2
  48. package/dist/runtime/entries/client.js +2 -2
  49. package/dist/runtime/entries/router.d.ts +1 -1
  50. package/dist/runtime/entries/router.js +1 -1
  51. package/dist/runtime/entries/worker.d.ts +5 -6
  52. package/dist/runtime/entries/worker.js +5 -6
  53. package/dist/runtime/imports/worker.js +1 -1
  54. package/dist/runtime/lib/auth/session.d.ts +2 -2
  55. package/dist/runtime/lib/auth/session.js +5 -5
  56. package/dist/runtime/lib/db/DOWorkerDialect.d.ts +1 -1
  57. package/dist/runtime/lib/db/DOWorkerDialect.js +1 -1
  58. package/dist/runtime/lib/db/SqliteDurableObject.js +2 -2
  59. package/dist/runtime/lib/db/index.d.ts +2 -2
  60. package/dist/runtime/lib/db/index.js +2 -2
  61. package/dist/runtime/lib/db/migrations.d.ts +1 -1
  62. package/dist/runtime/lib/db/typeInference/builders/alterTable.d.ts +3 -3
  63. package/dist/runtime/lib/db/typeInference/builders/columnDefinition.d.ts +1 -1
  64. package/dist/runtime/lib/db/typeInference/builders/createTable.d.ts +2 -2
  65. package/dist/runtime/lib/db/typeInference/builders/createView.d.ts +1 -1
  66. package/dist/runtime/lib/db/typeInference/builders/dropTable.d.ts +1 -1
  67. package/dist/runtime/lib/db/typeInference/builders/dropView.d.ts +1 -1
  68. package/dist/runtime/lib/db/typeInference/builders/schema.d.ts +3 -3
  69. package/dist/runtime/lib/db/typeInference/database.d.ts +2 -2
  70. package/dist/runtime/lib/memoizeOnId.test.js +1 -1
  71. package/dist/runtime/lib/realtime/client.js +2 -2
  72. package/dist/runtime/lib/realtime/durableObject.js +1 -1
  73. package/dist/runtime/lib/realtime/protocol.test.js +1 -1
  74. package/dist/runtime/lib/realtime/shared.test.js +1 -1
  75. package/dist/runtime/lib/realtime/validateUpgradeRequest.test.js +1 -1
  76. package/dist/runtime/lib/realtime/worker.js +2 -2
  77. package/dist/runtime/lib/router.d.ts +1 -1
  78. package/dist/runtime/lib/router.test.js +2 -3
  79. package/dist/runtime/lib/rwContext.d.ts +1 -1
  80. package/dist/runtime/lib/stitchDocumentAndAppStreams.d.ts +18 -0
  81. package/dist/runtime/lib/stitchDocumentAndAppStreams.js +143 -0
  82. package/dist/runtime/lib/turnstile/useTurnstile.js +1 -1
  83. package/dist/runtime/lib/turnstile/verifyTurnstileToken.test.js +1 -1
  84. package/dist/runtime/register/worker.d.ts +1 -1
  85. package/dist/runtime/register/worker.js +34 -22
  86. package/dist/runtime/render/assembleDocument.d.ts +1 -1
  87. package/dist/runtime/render/createThenableFromReadableStream.js +1 -1
  88. package/dist/runtime/render/preloads.d.ts +2 -2
  89. package/dist/runtime/render/renderDocumentHtmlStream.js +6 -6
  90. package/dist/runtime/render/renderHtmlStream.d.ts +1 -1
  91. package/dist/runtime/render/renderToRscStream.d.ts +4 -1
  92. package/dist/runtime/render/renderToRscStream.js +11 -1
  93. package/dist/runtime/render/renderToStream.d.ts +1 -1
  94. package/dist/runtime/render/renderToStream.js +2 -2
  95. package/dist/runtime/render/stylesheets.d.ts +1 -1
  96. package/dist/runtime/requestInfo/types.d.ts +0 -2
  97. package/dist/runtime/requestInfo/worker.d.ts +1 -1
  98. package/dist/runtime/requestInfo/worker.js +1 -9
  99. package/dist/runtime/script.js +1 -1
  100. package/dist/runtime/ssrBridge.d.ts +2 -2
  101. package/dist/runtime/ssrBridge.js +2 -2
  102. package/dist/runtime/worker.d.ts +1 -1
  103. package/dist/runtime/worker.js +3 -11
  104. package/dist/scripts/addon.d.mts +1 -0
  105. package/dist/scripts/addon.mjs +73 -0
  106. package/dist/scripts/debug-sync.mjs +106 -137
  107. package/dist/scripts/ensure-deploy-env.mjs +6 -6
  108. package/dist/scripts/migrate-new.mjs +3 -4
  109. package/dist/scripts/smoke-test.mjs +2 -9
  110. package/dist/scripts/worker-run.mjs +7 -9
  111. package/dist/vite/buildApp.mjs +1 -1
  112. package/dist/vite/checkIsUsingPrisma.test.mjs +1 -1
  113. package/dist/vite/configPlugin.mjs +33 -6
  114. package/dist/vite/createDirectiveLookupPlugin.mjs +1 -1
  115. package/dist/vite/createDirectiveLookupPlugin.test.mjs +2 -2
  116. package/dist/vite/createViteAwareResolver.d.mts +1 -2
  117. package/dist/vite/createViteAwareResolver.mjs +1 -1
  118. package/dist/vite/directiveModulesDevPlugin.mjs +4 -4
  119. package/dist/vite/directiveModulesDevPlugin.test.mjs +2 -2
  120. package/dist/vite/directivesPlugin.mjs +3 -3
  121. package/dist/vite/directivesPlugin.test.mjs +1 -1
  122. package/dist/vite/ensureAliasArray.test.mjs +1 -1
  123. package/dist/vite/findSpecifiers.mjs +1 -1
  124. package/dist/vite/findSpecifiers.test.mjs +2 -2
  125. package/dist/vite/findSsrSpecifiers.mjs +1 -1
  126. package/dist/vite/findSsrSpecifiers.test.mjs +1 -1
  127. package/dist/vite/getViteEsbuild.mjs +1 -1
  128. package/dist/vite/hasDirective.test.mjs +1 -1
  129. package/dist/vite/index.d.mts +1 -1
  130. package/dist/vite/invalidateCacheIfPrismaClientChanged.mjs +2 -2
  131. package/dist/vite/isJsFile.test.mjs +1 -1
  132. package/dist/vite/{reactConditionsResolverPlugin.d.mts → knownDepsResolverPlugin.d.mts} +3 -3
  133. package/dist/vite/{reactConditionsResolverPlugin.mjs → knownDepsResolverPlugin.mjs} +29 -24
  134. package/dist/vite/linkerPlugin.mjs +2 -2
  135. package/dist/vite/linkerPlugin.test.mjs +1 -1
  136. package/dist/vite/miniflareHMRPlugin.mjs +5 -5
  137. package/dist/vite/miniflareHMRPlugin.test.mjs +1 -1
  138. package/dist/vite/prismaPlugin.mjs +1 -1
  139. package/dist/vite/redwoodPlugin.d.mts +2 -0
  140. package/dist/vite/redwoodPlugin.mjs +36 -17
  141. package/dist/vite/redwoodPlugin.test.mjs +2 -2
  142. package/dist/vite/resolveForcedPaths.d.mts +4 -0
  143. package/dist/vite/resolveForcedPaths.mjs +9 -0
  144. package/dist/vite/runDirectivesScan.d.mts +2 -1
  145. package/dist/vite/runDirectivesScan.mjs +53 -17
  146. package/dist/vite/runDirectivesScan.test.mjs +2 -2
  147. package/dist/vite/ssrBridgePlugin.mjs +10 -3
  148. package/dist/vite/transformClientComponents.mjs +8 -6
  149. package/dist/vite/transformClientComponents.test.mjs +117 -59
  150. package/dist/vite/transformJsxScriptTagsPlugin.mjs +1 -1
  151. package/dist/vite/transformJsxScriptTagsPlugin.test.mjs +2 -2
  152. package/dist/vite/transformServerFunctions.d.mts +1 -1
  153. package/dist/vite/transformServerFunctions.mjs +5 -5
  154. package/dist/vite/transformServerFunctions.test.mjs +3 -3
  155. package/package.json +54 -44
  156. package/dist/runtime/imports/resolveSSRValue.d.ts +0 -1
  157. package/dist/runtime/imports/resolveSSRValue.js +0 -8
  158. package/dist/runtime/lib/injectHtmlAtMarker.d.ts +0 -11
  159. package/dist/runtime/lib/injectHtmlAtMarker.js +0 -90
@@ -0,0 +1,73 @@
1
+ import decompress from "decompress";
2
+ import { findUp } from "find-up";
3
+ import { createWriteStream } from "node:fs";
4
+ import fs from "node:fs/promises";
5
+ import os from "node:os";
6
+ import path from "node:path";
7
+ import { Readable } from "node:stream";
8
+ import { pipeline } from "node:stream/promises";
9
+ async function getRwSdkProjectRootDir(cwd) {
10
+ const packageJsonPath = await findUp("package.json", { cwd });
11
+ if (packageJsonPath) {
12
+ return path.dirname(packageJsonPath);
13
+ }
14
+ return cwd;
15
+ }
16
+ export const addon = async () => {
17
+ const addonName = process.argv[2];
18
+ if (!addonName ||
19
+ process.argv.find((arg) => arg === "-h") ||
20
+ process.argv.find((arg) => arg === "--help")) {
21
+ console.error("Please specify the addon name.");
22
+ console.error(`Usage: npx rwsdk addon <addon-name>`);
23
+ process.exit(1);
24
+ }
25
+ try {
26
+ const projectRootDir = await getRwSdkProjectRootDir(process.cwd());
27
+ const packageJsonPath = path.resolve(projectRootDir, "package.json");
28
+ const packageJsonContent = await fs.readFile(packageJsonPath, "utf-8");
29
+ const { dependencies, devDependencies } = JSON.parse(packageJsonContent);
30
+ let rwsdkVersion = dependencies?.rwsdk || devDependencies?.rwsdk;
31
+ if (rwsdkVersion === "workspace:*") {
32
+ throw new Error('The "rwsdk" dependency is set to "workspace:*". Please use a specific version of the SDK.');
33
+ }
34
+ if (!rwsdkVersion) {
35
+ console.error('Could not find "rwsdk" in your dependencies or devDependencies.');
36
+ process.exit(1);
37
+ }
38
+ if (/^\\d/.test(rwsdkVersion)) {
39
+ rwsdkVersion = `v${rwsdkVersion}`;
40
+ }
41
+ const tmpDirPrefix = path.join(os.tmpdir(), `rwsdk-addon-${addonName}-`);
42
+ const tmpDir = await fs.mkdtemp(tmpDirPrefix);
43
+ const downloadUrl = `https://github.com/redwoodjs/sdk/releases/download/${rwsdkVersion}/${addonName}-${rwsdkVersion}.tar.gz`;
44
+ console.log(`Downloading addon "${addonName}" version ${rwsdkVersion}...`);
45
+ const filePath = path.join(os.tmpdir(), `rwsdk-addon-${addonName}-${rwsdkVersion}.tar.gz`);
46
+ const response = await fetch(downloadUrl);
47
+ if (!response.ok) {
48
+ console.error(`Error downloading addon: ${response.statusText}`);
49
+ process.exit(1);
50
+ }
51
+ if (!response.body) {
52
+ console.error(`\nError: Failed to download addon "${addonName}". The response contained no data.`);
53
+ process.exit(1);
54
+ }
55
+ await pipeline(Readable.fromWeb(response.body), createWriteStream(filePath));
56
+ await decompress(filePath, tmpDir);
57
+ console.log();
58
+ console.log("Download complete!");
59
+ console.log(`The addon files are located in: ${tmpDir}`);
60
+ console.log();
61
+ console.log("To continue, open the step-by-step instructions:");
62
+ const instructionsPath = path.join(tmpDir, "INSTRUCTIONS.md");
63
+ console.log(`code ${instructionsPath}`);
64
+ }
65
+ catch (e) {
66
+ console.error(`Could not download addon "${addonName}". Please check the name and try again.`);
67
+ console.error(e.message);
68
+ process.exit(1);
69
+ }
70
+ };
71
+ if (import.meta.url === new URL(process.argv[1], import.meta.url).href) {
72
+ addon();
73
+ }
@@ -1,32 +1,11 @@
1
- import path from "node:path";
2
- import { fileURLToPath } from "node:url";
1
+ import chokidar from "chokidar";
3
2
  import { $ } from "execa";
4
- import fs from "node:fs/promises";
5
3
  import { existsSync } from "node:fs";
6
- import chokidar from "chokidar";
4
+ import fs from "node:fs/promises";
5
+ import path from "node:path";
6
+ import { fileURLToPath } from "node:url";
7
7
  import { lock } from "proper-lockfile";
8
8
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
9
- const getPackageManagerInfo = (targetDir) => {
10
- if (existsSync(path.join(targetDir, "bun.lock"))) {
11
- return { name: "bun", lockFile: "bun.lock", command: "add" };
12
- }
13
- const pnpmResult = {
14
- name: "pnpm",
15
- lockFile: "pnpm-lock.yaml",
16
- command: "add",
17
- };
18
- if (existsSync(path.join(targetDir, "yarn.lock"))) {
19
- return { name: "yarn", lockFile: "yarn.lock", command: "add" };
20
- }
21
- if (existsSync(path.join(targetDir, "pnpm-lock.yaml")) ||
22
- existsSync(path.join(targetDir, "node_modules", ".pnpm"))) {
23
- return pnpmResult;
24
- }
25
- if (existsSync(path.join(targetDir, "package-lock.json"))) {
26
- return { name: "npm", lockFile: "package-lock.json", command: "install" };
27
- }
28
- return pnpmResult;
29
- };
30
9
  const cleanupViteEntries = async (targetDir) => {
31
10
  const nodeModulesDir = path.join(targetDir, "node_modules");
32
11
  if (!existsSync(nodeModulesDir)) {
@@ -58,91 +37,6 @@ const cleanupViteEntries = async (targetDir) => {
58
37
  console.log(`Failed to cleanup vite cache entries: ${error}`);
59
38
  }
60
39
  };
61
- const performFullSync = async (sdkDir, targetDir) => {
62
- let tarballPath = "";
63
- let tarballName = "";
64
- // Clean up vite cache
65
- await cleanupViteEntries(targetDir);
66
- try {
67
- console.log("📦 Packing SDK...");
68
- const packResult = await $({ cwd: sdkDir }) `npm pack --json`;
69
- const json = JSON.parse(packResult.stdout || "[]");
70
- const packInfo = Array.isArray(json) ? json[0] : undefined;
71
- tarballName = (packInfo && (packInfo.filename || packInfo.name)) || "";
72
- if (!tarballName) {
73
- console.error("❌ Failed to get tarball name from npm pack.");
74
- return;
75
- }
76
- tarballPath = path.resolve(sdkDir, tarballName);
77
- console.log(`💿 Installing ${tarballName} in ${targetDir}...`);
78
- const pm = getPackageManagerInfo(targetDir);
79
- const packageJsonPath = path.join(targetDir, "package.json");
80
- const lockfilePath = path.join(targetDir, pm.lockFile);
81
- const originalPackageJson = await fs
82
- .readFile(packageJsonPath, "utf-8")
83
- .catch(() => null);
84
- const originalLockfile = await fs
85
- .readFile(lockfilePath, "utf-8")
86
- .catch(() => null);
87
- try {
88
- // For bun, we need to remove the existing dependency from package.json
89
- // before adding the tarball to avoid a dependency loop error.
90
- if (pm.name === "bun" && originalPackageJson) {
91
- try {
92
- const targetPackageJson = JSON.parse(originalPackageJson);
93
- let modified = false;
94
- if (targetPackageJson.dependencies?.rwsdk) {
95
- delete targetPackageJson.dependencies.rwsdk;
96
- modified = true;
97
- }
98
- if (targetPackageJson.devDependencies?.rwsdk) {
99
- delete targetPackageJson.devDependencies.rwsdk;
100
- modified = true;
101
- }
102
- if (modified) {
103
- console.log("Temporarily removing rwsdk from target package.json to prevent dependency loop with bun.");
104
- await fs.writeFile(packageJsonPath, JSON.stringify(targetPackageJson, null, 2));
105
- }
106
- }
107
- catch (e) {
108
- console.warn("Could not modify target package.json, proceeding anyway.");
109
- }
110
- }
111
- const cmd = pm.name;
112
- const args = [pm.command];
113
- if (pm.name === "yarn") {
114
- // For modern yarn, disable PnP to avoid resolution issues with local tarballs
115
- process.env.YARN_NODE_LINKER = "node-modules";
116
- args.push(`rwsdk@file:${tarballPath}`);
117
- }
118
- else {
119
- args.push(tarballPath);
120
- }
121
- await $(cmd, args, {
122
- cwd: targetDir,
123
- stdio: "inherit",
124
- });
125
- }
126
- finally {
127
- if (originalPackageJson) {
128
- console.log("Restoring package.json...");
129
- await fs.writeFile(packageJsonPath, originalPackageJson);
130
- }
131
- if (originalLockfile) {
132
- console.log(`Restoring ${pm.lockFile}...`);
133
- await fs.writeFile(lockfilePath, originalLockfile);
134
- }
135
- }
136
- }
137
- finally {
138
- if (tarballPath) {
139
- console.log("Removing tarball...");
140
- await fs.unlink(tarballPath).catch(() => {
141
- // ignore if deletion fails
142
- });
143
- }
144
- }
145
- };
146
40
  const syncFilesWithRsyncOrFs = async (sdkDir, destDir, filesEntries) => {
147
41
  const sources = filesEntries.map((p) => path.join(sdkDir, p));
148
42
  // Always include package.json in sync
@@ -191,45 +85,120 @@ const syncFilesWithRsyncOrFs = async (sdkDir, destDir, filesEntries) => {
191
85
  }
192
86
  }
193
87
  };
194
- const performFastSync = async (sdkDir, targetDir) => {
195
- console.log("⚡️ No dependency changes, performing fast sync...");
196
- // Clean up vite cache
197
- await cleanupViteEntries(targetDir);
198
- const nodeModulesPkgDir = path.join(targetDir, "node_modules", "rwsdk");
199
- // Copy directories/files declared in package.json#files (plus package.json)
200
- const filesToSync = JSON.parse(await fs.readFile(path.join(sdkDir, "package.json"), "utf-8"))
201
- .files || [];
202
- await syncFilesWithRsyncOrFs(sdkDir, nodeModulesPkgDir, filesToSync);
88
+ const findUp = async (names, startDir) => {
89
+ let dir = startDir;
90
+ while (dir !== path.dirname(dir)) {
91
+ for (const name of names) {
92
+ const filePath = path.join(dir, name);
93
+ if (existsSync(filePath)) {
94
+ return dir;
95
+ }
96
+ }
97
+ dir = path.dirname(dir);
98
+ }
99
+ return undefined;
100
+ };
101
+ const getMonorepoRoot = async (startDir) => {
102
+ try {
103
+ // `pnpm root` is the most reliable way to find the workspace root node_modules
104
+ const { stdout } = await $({
105
+ cwd: startDir,
106
+ }) `pnpm root`;
107
+ // pnpm root returns the node_modules path, so we go up one level
108
+ return path.resolve(stdout, "..");
109
+ }
110
+ catch (e) {
111
+ console.warn(`Could not determine pnpm root from ${startDir}. Falling back to file search.`);
112
+ const root = await findUp(["pnpm-workspace.yaml"], startDir);
113
+ if (root) {
114
+ return root;
115
+ }
116
+ }
117
+ console.warn("Could not find pnpm monorepo root. Using parent directory of target as fallback.");
118
+ return path.resolve(startDir, "..");
203
119
  };
204
120
  const areDependenciesEqual = (deps1, deps2) => {
205
121
  // Simple string comparison for this use case is sufficient
206
122
  return JSON.stringify(deps1 ?? {}) === JSON.stringify(deps2 ?? {});
207
123
  };
124
+ const performFullSync = async (sdkDir, targetDir, monorepoRoot) => {
125
+ console.log("📦 Performing full sync with tarball...");
126
+ let tarballPath = "";
127
+ const projectName = path.basename(targetDir);
128
+ const rwsyncDir = path.join(monorepoRoot, "node_modules", `.rwsync_${projectName}`);
129
+ try {
130
+ // 1. Pack the SDK
131
+ const packResult = await $({ cwd: sdkDir }) `npm pack --json`;
132
+ const json = JSON.parse(packResult.stdout || "[]");
133
+ const packInfo = Array.isArray(json) ? json[0] : undefined;
134
+ const tarballName = (packInfo && (packInfo.filename || packInfo.name)) || "";
135
+ if (!tarballName) {
136
+ throw new Error("Failed to get tarball name from npm pack.");
137
+ }
138
+ tarballPath = path.resolve(sdkDir, tarballName);
139
+ // 2. Prepare isolated install directory
140
+ console.log(`Preparing isolated install directory at ${rwsyncDir}`);
141
+ await fs.rm(rwsyncDir, { recursive: true, force: true });
142
+ await fs.mkdir(rwsyncDir, { recursive: true });
143
+ await fs.writeFile(path.join(rwsyncDir, "package.json"), JSON.stringify({ name: `rwsync-env-${projectName}` }, null, 2));
144
+ // 3. Perform isolated install
145
+ console.log(`Installing ${tarballName} in isolation...`);
146
+ await $("pnpm", ["add", tarballPath], {
147
+ cwd: rwsyncDir,
148
+ stdio: "inherit",
149
+ });
150
+ // 4. Create symlink
151
+ const symlinkSource = path.join(rwsyncDir, "node_modules", "rwsdk");
152
+ const symlinkTarget = path.join(targetDir, "node_modules", "rwsdk");
153
+ console.log(`Symlinking ${symlinkTarget} -> ${symlinkSource}`);
154
+ await fs.rm(symlinkTarget, { recursive: true, force: true });
155
+ await fs.mkdir(path.dirname(symlinkTarget), { recursive: true });
156
+ await fs.symlink(symlinkSource, symlinkTarget, "dir");
157
+ }
158
+ finally {
159
+ if (tarballPath) {
160
+ console.log("Removing tarball...");
161
+ await fs.unlink(tarballPath).catch(() => { });
162
+ }
163
+ }
164
+ };
165
+ const performFastSync = async (sdkDir, targetDir, monorepoRoot) => {
166
+ console.log("⚡️ Performing fast sync with rsync...");
167
+ const projectName = path.basename(targetDir);
168
+ const rwsyncDir = path.join(monorepoRoot, "node_modules", `.rwsync_${projectName}`);
169
+ const syncDestDir = path.join(rwsyncDir, "node_modules", "rwsdk");
170
+ // Copy directories/files declared in package.json#files (plus package.json)
171
+ const filesToSync = JSON.parse(await fs.readFile(path.join(sdkDir, "package.json"), "utf-8"))
172
+ .files || [];
173
+ await syncFilesWithRsyncOrFs(sdkDir, syncDestDir, filesToSync);
174
+ };
208
175
  const performSync = async (sdkDir, targetDir) => {
209
176
  console.log("🏗️ Rebuilding SDK...");
210
177
  await $ `pnpm build`;
211
- const forceFullSync = Boolean(process.env.RWSDK_FORCE_FULL_SYNC);
212
- if (forceFullSync) {
213
- console.log("🏃 Force full sync mode is enabled.");
214
- await performFullSync(sdkDir, targetDir);
215
- console.log(" Done syncing");
216
- return;
178
+ // Clean up vite cache in the target project
179
+ await cleanupViteEntries(targetDir);
180
+ const monorepoRoot = await getMonorepoRoot(targetDir);
181
+ const projectName = path.basename(targetDir);
182
+ const installedSdkPackageJsonPath = path.join(monorepoRoot, "node_modules", `.rwsync_${projectName}`, "node_modules", "rwsdk", "package.json");
183
+ let needsFullSync = false;
184
+ if (!existsSync(installedSdkPackageJsonPath)) {
185
+ console.log("No previous sync found, performing full sync.");
186
+ needsFullSync = true;
217
187
  }
218
- const sdkPackageJsonPath = path.join(sdkDir, "package.json");
219
- const installedSdkPackageJsonPath = path.join(targetDir, "node_modules/rwsdk/package.json");
220
- let packageJsonChanged = true;
221
- if (existsSync(installedSdkPackageJsonPath)) {
222
- const sdkPackageJsonContent = await fs.readFile(sdkPackageJsonPath, "utf-8");
223
- const installedSdkPackageJsonContent = await fs.readFile(installedSdkPackageJsonPath, "utf-8");
224
- packageJsonChanged =
225
- sdkPackageJsonContent !== installedSdkPackageJsonContent;
188
+ else {
189
+ const sdkPackageJson = JSON.parse(await fs.readFile(path.join(sdkDir, "package.json"), "utf-8"));
190
+ const installedSdkPackageJson = JSON.parse(await fs.readFile(installedSdkPackageJsonPath, "utf-8"));
191
+ if (!areDependenciesEqual(sdkPackageJson.dependencies, installedSdkPackageJson.dependencies) ||
192
+ !areDependenciesEqual(sdkPackageJson.devDependencies, installedSdkPackageJson.devDependencies)) {
193
+ console.log("Dependency changes detected, performing full sync.");
194
+ needsFullSync = true;
195
+ }
226
196
  }
227
- if (packageJsonChanged) {
228
- console.log("📦 package.json changed, performing full sync...");
229
- await performFullSync(sdkDir, targetDir);
197
+ if (needsFullSync) {
198
+ await performFullSync(sdkDir, targetDir, monorepoRoot);
230
199
  }
231
200
  else {
232
- await performFastSync(sdkDir, targetDir);
201
+ await performFastSync(sdkDir, targetDir, monorepoRoot);
233
202
  }
234
203
  console.log("✅ Done syncing");
235
204
  };
@@ -1,13 +1,13 @@
1
- import { $ } from "../lib/$.mjs";
2
- import { readFile, writeFile } from "fs/promises";
3
- import { resolve, basename, join } from "path";
4
1
  import { randomBytes } from "crypto";
2
+ import { pathExists } from "fs-extra";
3
+ import { readFile, writeFile } from "fs/promises";
5
4
  import { glob } from "glob";
6
5
  import { parse as parseJsonc } from "jsonc-parser";
7
- import { uniqueNamesGenerator, adjectives, animals, } from "unique-names-generator";
6
+ import { basename, join, resolve } from "path";
8
7
  import * as readline from "readline";
9
- import { pathExists } from "fs-extra";
10
- import { parseJson, extractAllJson } from "../lib/jsonUtils.mjs";
8
+ import { adjectives, animals, uniqueNamesGenerator, } from "unique-names-generator";
9
+ import { $ } from "../lib/$.mjs";
10
+ import { extractAllJson, parseJson } from "../lib/jsonUtils.mjs";
11
11
  const promptForDeployment = async () => {
12
12
  const rl = readline.createInterface({
13
13
  input: process.stdin,
@@ -1,9 +1,8 @@
1
+ import { mkdirp } from "fs-extra";
2
+ import { readdir, writeFile } from "fs/promises";
1
3
  import snakeCase from "lodash/snakeCase.js";
2
- import { $ } from "../lib/$.mjs";
3
- import { readdir } from "fs/promises";
4
4
  import { resolve } from "path";
5
- import { mkdirp } from "fs-extra";
6
- import { writeFile } from "fs/promises";
5
+ import { $ } from "../lib/$.mjs";
7
6
  const getNextMigrationNumber = async () => {
8
7
  await mkdirp(resolve(process.cwd(), "./migrations"));
9
8
  const files = await readdir(resolve(process.cwd(), "./migrations"));
@@ -1,6 +1,6 @@
1
- import { fileURLToPath } from "url";
2
- import { join } from "path";
3
1
  import debug from "debug";
2
+ import { join } from "path";
3
+ import { fileURLToPath } from "url";
4
4
  import { runSmokeTests } from "../lib/smokeTests/runSmokeTests.mjs";
5
5
  import { isRunningInCI } from "../lib/smokeTests/utils.mjs";
6
6
  // Set up debug logging
@@ -66,12 +66,6 @@ if (fileURLToPath(import.meta.url) === process.argv[1]) {
66
66
  else if (arg === "--copy-project") {
67
67
  options.copyProject = true;
68
68
  }
69
- else if (arg === "--no-sync") {
70
- syncExplicit = false;
71
- }
72
- else if (arg === "--sync") {
73
- syncExplicit = true;
74
- }
75
69
  else if (arg === "--ci") {
76
70
  // Already handled above, just skip
77
71
  }
@@ -99,7 +93,6 @@ Options:
99
93
  --keep Keep temporary test directory after tests complete
100
94
  --no-headless Run browser tests with GUI (not headless)
101
95
  --sync Force syncing SDK code to test project
102
- --no-sync Disable syncing SDK code to test project
103
96
  --ci Run in CI mode (keeps temp dirs, sets headless)
104
97
  --bail Stop on first test failure
105
98
  --copy-project Copy the project to the artifacts directory
@@ -1,15 +1,13 @@
1
- import path from "path";
2
- import { resolve } from "path";
3
- import { writeFile } from "fs/promises";
4
- import { unstable_readConfig } from "wrangler";
5
- import { createServer as createViteServer } from "vite";
6
- import tmp from "tmp-promise";
1
+ import { Lang, parse } from "@ast-grep/napi";
7
2
  import baseDebug from "debug";
8
3
  import enhancedResolve from "enhanced-resolve";
9
- import { readFile } from "fs/promises";
10
- import { Lang, parse } from "@ast-grep/napi";
11
- import { redwood } from "../vite/index.mjs";
4
+ import { readFile, writeFile } from "fs/promises";
5
+ import path, { resolve } from "path";
6
+ import tmp from "tmp-promise";
7
+ import { createServer as createViteServer } from "vite";
8
+ import { unstable_readConfig } from "wrangler";
12
9
  import { findWranglerConfig } from "../lib/findWranglerConfig.mjs";
10
+ import { redwood } from "../vite/index.mjs";
13
11
  const debug = baseDebug("rwsdk:worker-run");
14
12
  export const runWorkerScript = async (relativeScriptPath) => {
15
13
  if (!relativeScriptPath) {
@@ -1,5 +1,5 @@
1
- import { resolve } from "node:path";
2
1
  import debug from "debug";
2
+ import { resolve } from "node:path";
3
3
  import { runDirectivesScan } from "./runDirectivesScan.mjs";
4
4
  const log = debug("rwsdk:vite:build-app");
5
5
  /**
@@ -1,4 +1,4 @@
1
- import { describe, it, expect } from "vitest";
1
+ import { describe, expect, it } from "vitest";
2
2
  import { isUsingPrisma } from "./checkIsUsingPrisma.mjs";
3
3
  describe("isUsingPrisma", () => {
4
4
  it("should return true if prisma client is resolved", () => {
@@ -1,5 +1,5 @@
1
- import path, { resolve } from "node:path";
2
1
  import enhancedResolve from "enhanced-resolve";
2
+ import path, { resolve } from "node:path";
3
3
  import { INTERMEDIATE_SSR_BRIDGE_PATH } from "../lib/constants.mjs";
4
4
  import { buildApp } from "./buildApp.mjs";
5
5
  import { externalModules } from "./constants.mjs";
@@ -31,7 +31,17 @@ export const configPlugin = ({ silent, projectRootDir, workerEntryPathname, clie
31
31
  },
32
32
  optimizeDeps: {
33
33
  noDiscovery: false,
34
- include: ["rwsdk/worker"],
34
+ include: [
35
+ "rwsdk/__ssr_bridge",
36
+ "rwsdk/auth",
37
+ "rwsdk/constants",
38
+ "rwsdk/db",
39
+ "rwsdk/debug",
40
+ "rwsdk/realtime/durableObject",
41
+ "rwsdk/realtime/worker",
42
+ "rwsdk/router",
43
+ "rwsdk/worker",
44
+ ],
35
45
  exclude: [],
36
46
  entries: [workerEntryPathname],
37
47
  esbuildOptions: {
@@ -78,7 +88,13 @@ export const configPlugin = ({ silent, projectRootDir, workerEntryPathname, clie
78
88
  },
79
89
  optimizeDeps: {
80
90
  noDiscovery: false,
81
- include: ["rwsdk/client"],
91
+ include: [
92
+ "rwsdk/client",
93
+ "rwsdk/constants",
94
+ "rwsdk/debug",
95
+ "rwsdk/realtime/client",
96
+ "rwsdk/turnstile",
97
+ ],
82
98
  entries: [],
83
99
  esbuildOptions: {
84
100
  jsx: "automatic",
@@ -105,7 +121,18 @@ export const configPlugin = ({ silent, projectRootDir, workerEntryPathname, clie
105
121
  noDiscovery: false,
106
122
  entries: [workerEntryPathname],
107
123
  exclude: externalModules,
108
- include: ["rwsdk/__ssr", "rwsdk/__ssr_bridge"],
124
+ include: [
125
+ "rwsdk/__ssr",
126
+ "rwsdk/__ssr_bridge",
127
+ "rwsdk/client",
128
+ "rwsdk/constants",
129
+ "rwsdk/debug",
130
+ "rwsdk/realtime/client",
131
+ "rwsdk/router",
132
+ "rwsdk/worker",
133
+ "rwsdk/realtime/durableObject",
134
+ "rwsdk/realtime/worker",
135
+ ],
109
136
  esbuildOptions: {
110
137
  jsx: "automatic",
111
138
  jsxImportSource: "react",
@@ -138,8 +165,8 @@ export const configPlugin = ({ silent, projectRootDir, workerEntryPathname, clie
138
165
  // original `export` statement from the bundle to prevent syntax
139
166
  // errors.
140
167
  inlineDynamicImports: true,
141
- banner: `export const { renderHtmlStream, ssrWebpackRequire, ssrGetModuleExport, createThenableFromReadableStream } = (function() {`,
142
- footer: `return { renderHtmlStream, ssrWebpackRequire, ssrGetModuleExport, createThenableFromReadableStream };\n})();`,
168
+ banner: `export const { renderHtmlStream, ssrLoadModule, ssrWebpackRequire, ssrGetModuleExport, createThenableFromReadableStream } = (function() {`,
169
+ footer: `return { renderHtmlStream, ssrLoadModule, ssrWebpackRequire, ssrGetModuleExport, createThenableFromReadableStream };\n})();`,
143
170
  },
144
171
  plugins: [
145
172
  {
@@ -1,6 +1,6 @@
1
+ import debug from "debug";
1
2
  import MagicString from "magic-string";
2
3
  import path from "path";
3
- import debug from "debug";
4
4
  import { VENDOR_CLIENT_BARREL_EXPORT_PATH, VENDOR_SERVER_BARREL_EXPORT_PATH, } from "../lib/constants.mjs";
5
5
  export function generateLookupMap({ files, isDev, kind, exportName, }) {
6
6
  const s = new MagicString(`
@@ -1,6 +1,6 @@
1
- import { describe, it, expect } from "vitest";
2
- import { generateLookupMap } from "./createDirectiveLookupPlugin.mjs";
1
+ import { describe, expect, it } from "vitest";
3
2
  import { VENDOR_CLIENT_BARREL_EXPORT_PATH, VENDOR_SERVER_BARREL_EXPORT_PATH, } from "../lib/constants.mjs";
3
+ import { generateLookupMap } from "./createDirectiveLookupPlugin.mjs";
4
4
  describe("generateLookupMap", () => {
5
5
  const files = new Set([
6
6
  "src/app.js",
@@ -1,5 +1,4 @@
1
1
  import resolve, { ResolveOptions } from "enhanced-resolve";
2
- import { ResolvedConfig } from "vite";
3
- import { Environment } from "vite";
2
+ import { Environment, ResolvedConfig } from "vite";
4
3
  export declare const mapViteResolveToEnhancedResolveOptions: (viteConfig: ResolvedConfig, envName: string) => ResolveOptions;
5
4
  export declare const createViteAwareResolver: (viteConfig: ResolvedConfig, environment: Environment) => resolve.ResolveFunctionAsync;
@@ -1,7 +1,7 @@
1
+ import createDebug from "debug";
1
2
  import resolve from "enhanced-resolve";
2
3
  import fs from "fs";
3
4
  import path from "path";
4
- import createDebug from "debug";
5
5
  import { normalizeModulePath } from "../lib/normalizeModulePath.mjs";
6
6
  const debug = createDebug("rwsdk:vite:enhanced-resolve-plugin");
7
7
  // Enhanced-resolve plugin that wraps Vite plugin resolution
@@ -1,8 +1,8 @@
1
- import path from "path";
1
+ import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
2
2
  import os from "os";
3
- import { writeFileSync, mkdirSync, mkdtempSync } from "node:fs";
3
+ import path from "path";
4
+ import { VENDOR_CLIENT_BARREL_EXPORT_PATH, VENDOR_CLIENT_BARREL_PATH, VENDOR_SERVER_BARREL_EXPORT_PATH, VENDOR_SERVER_BARREL_PATH, } from "../lib/constants.mjs";
4
5
  import { normalizeModulePath } from "../lib/normalizeModulePath.mjs";
5
- import { VENDOR_CLIENT_BARREL_PATH, VENDOR_SERVER_BARREL_PATH, VENDOR_CLIENT_BARREL_EXPORT_PATH, VENDOR_SERVER_BARREL_EXPORT_PATH, } from "../lib/constants.mjs";
6
6
  import { runDirectivesScan } from "./runDirectivesScan.mjs";
7
7
  export const generateVendorBarrelContent = (files, projectRootDir) => {
8
8
  const imports = [...files]
@@ -84,7 +84,7 @@ export const directiveModulesDevPlugin = ({ clientFiles, serverFiles, projectRoo
84
84
  env.optimizeDeps.include ??= [];
85
85
  const entries = (env.optimizeDeps.entries = castArray(env.optimizeDeps.entries ?? []));
86
86
  env.optimizeDeps.include.push(VENDOR_CLIENT_BARREL_EXPORT_PATH, VENDOR_SERVER_BARREL_EXPORT_PATH);
87
- if (envName === "client") {
87
+ if (envName === "client" || envName === "ssr") {
88
88
  entries.push(APP_CLIENT_BARREL_PATH);
89
89
  }
90
90
  else if (envName === "worker") {
@@ -1,5 +1,5 @@
1
- import { describe, it, expect } from "vitest";
2
- import { generateVendorBarrelContent, generateAppBarrelContent, } from "./directiveModulesDevPlugin.mjs";
1
+ import { describe, expect, it } from "vitest";
2
+ import { generateAppBarrelContent, generateVendorBarrelContent, } from "./directiveModulesDevPlugin.mjs";
3
3
  describe("directiveModulesDevPlugin helpers", () => {
4
4
  const projectRootDir = "/Users/test/project";
5
5
  describe("generateVendorBarrelContent", () => {
@@ -1,9 +1,9 @@
1
- import path from "node:path";
2
- import fs from "node:fs/promises";
3
1
  import debug from "debug";
2
+ import fs from "node:fs/promises";
3
+ import path from "node:path";
4
+ import { normalizeModulePath } from "../lib/normalizeModulePath.mjs";
4
5
  import { transformClientComponents } from "./transformClientComponents.mjs";
5
6
  import { transformServerFunctions } from "./transformServerFunctions.mjs";
6
- import { normalizeModulePath } from "../lib/normalizeModulePath.mjs";
7
7
  const log = debug("rwsdk:vite:rsc-directives-plugin");
8
8
  export const getLoader = (filePath) => {
9
9
  const ext = path.extname(filePath).slice(1);
@@ -1,4 +1,4 @@
1
- import { describe, it, expect } from "vitest";
1
+ import { describe, expect, it } from "vitest";
2
2
  import { getLoader } from "./directivesPlugin.mjs";
3
3
  describe("getLoader", () => {
4
4
  const testCases = [
@@ -1,4 +1,4 @@
1
- import { describe, it, expect } from "vitest";
1
+ import { describe, expect, it } from "vitest";
2
2
  import { ensureAliasArray } from "./ensureAliasArray.mjs";
3
3
  describe("ensureAliasArray", () => {
4
4
  it("should create resolve and alias array if resolve is undefined", () => {
@@ -1,4 +1,4 @@
1
- import { parse as sgParse, Lang as SgLang, Lang } from "@ast-grep/napi";
1
+ import { Lang, Lang as SgLang, parse as sgParse } from "@ast-grep/napi";
2
2
  import path from "path";
3
3
  // These patterns are used to match import statements in code for SSR transformations.
4
4
  export const IMPORT_PATTERNS = [
@@ -1,5 +1,5 @@
1
- import { describe, it, expect } from "vitest";
2
- import { findImportSpecifiers, findExports } from "./findSpecifiers.mjs";
1
+ import { describe, expect, it } from "vitest";
2
+ import { findExports, findImportSpecifiers } from "./findSpecifiers.mjs";
3
3
  function dedupeImports(imports) {
4
4
  const seen = new Set();
5
5
  return imports.filter((imp) => {
@@ -1,4 +1,4 @@
1
- import { parse as sgParse, Lang as SgLang, Lang } from "@ast-grep/napi";
1
+ import { Lang, Lang as SgLang, parse as sgParse } from "@ast-grep/napi";
2
2
  import path from "path";
3
3
  /**
4
4
  * Finds callsites for __vite_ssr_import__ and __vite_ssr_dynamic_import__ with their ranges.