rwsdk 1.0.0-beta.3 → 1.0.0-beta.30-test.20251119220440
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.
- package/dist/lib/constants.d.mts +1 -0
- package/dist/lib/constants.mjs +7 -4
- package/dist/lib/e2e/browser.mjs +6 -2
- package/dist/lib/e2e/constants.d.mts +16 -0
- package/dist/lib/e2e/constants.mjs +77 -0
- package/dist/lib/e2e/dev.mjs +37 -49
- package/dist/lib/e2e/environment.d.mts +2 -0
- package/dist/lib/e2e/environment.mjs +202 -65
- package/dist/lib/e2e/index.d.mts +1 -0
- package/dist/lib/e2e/index.mjs +1 -0
- package/dist/lib/e2e/poll.d.mts +1 -1
- package/dist/lib/e2e/release.d.mts +1 -0
- package/dist/lib/e2e/release.mjs +16 -32
- package/dist/lib/e2e/tarball.mjs +2 -34
- package/dist/lib/e2e/testHarness.d.mts +36 -4
- package/dist/lib/e2e/testHarness.mjs +216 -128
- package/dist/lib/e2e/utils.d.mts +1 -0
- package/dist/lib/e2e/utils.mjs +15 -0
- package/dist/runtime/client/client.d.ts +35 -0
- package/dist/runtime/client/client.js +35 -0
- package/dist/runtime/client/navigation.d.ts +49 -0
- package/dist/runtime/client/navigation.js +80 -31
- package/dist/runtime/entries/clientSSR.d.ts +1 -0
- package/dist/runtime/entries/clientSSR.js +3 -0
- package/dist/runtime/entries/router.d.ts +1 -0
- package/dist/runtime/entries/routerClient.d.ts +1 -0
- package/dist/runtime/entries/routerClient.js +1 -0
- package/dist/runtime/entries/worker.d.ts +2 -0
- package/dist/runtime/entries/worker.js +2 -0
- package/dist/runtime/imports/__mocks__/use-client-lookup.d.ts +6 -0
- package/dist/runtime/imports/__mocks__/use-client-lookup.js +6 -0
- package/dist/runtime/lib/db/SqliteDurableObject.d.ts +2 -2
- package/dist/runtime/lib/db/SqliteDurableObject.js +2 -2
- package/dist/runtime/lib/db/createDb.d.ts +1 -2
- package/dist/runtime/lib/db/createDb.js +4 -0
- package/dist/runtime/lib/db/typeInference/builders/alterTable.d.ts +13 -3
- package/dist/runtime/lib/db/typeInference/builders/columnDefinition.d.ts +35 -21
- package/dist/runtime/lib/db/typeInference/builders/createTable.d.ts +9 -2
- package/dist/runtime/lib/db/typeInference/database.d.ts +16 -2
- package/dist/runtime/lib/db/typeInference/typetests/alterTable.typetest.js +80 -5
- package/dist/runtime/lib/db/typeInference/typetests/createTable.typetest.js +104 -2
- package/dist/runtime/lib/db/typeInference/typetests/testUtils.d.ts +1 -0
- package/dist/runtime/lib/db/typeInference/utils.d.ts +59 -9
- package/dist/runtime/lib/links.d.ts +18 -7
- package/dist/runtime/lib/links.js +70 -24
- package/dist/runtime/lib/links.test.js +20 -0
- package/dist/runtime/lib/manifest.d.ts +1 -1
- package/dist/runtime/lib/manifest.js +7 -4
- package/dist/runtime/lib/realtime/client.js +8 -2
- package/dist/runtime/lib/realtime/worker.d.ts +1 -1
- package/dist/runtime/lib/router.d.ts +147 -33
- package/dist/runtime/lib/router.js +169 -20
- package/dist/runtime/lib/router.test.js +241 -0
- package/dist/runtime/lib/stitchDocumentAndAppStreams.d.ts +66 -0
- package/dist/runtime/lib/stitchDocumentAndAppStreams.js +302 -35
- package/dist/runtime/lib/stitchDocumentAndAppStreams.test.d.ts +1 -0
- package/dist/runtime/lib/stitchDocumentAndAppStreams.test.js +418 -0
- package/dist/runtime/lib/{rwContext.d.ts → types.d.ts} +1 -0
- package/dist/runtime/lib/types.js +1 -0
- package/dist/runtime/render/renderDocumentHtmlStream.d.ts +1 -1
- package/dist/runtime/render/renderToStream.d.ts +4 -2
- package/dist/runtime/render/renderToStream.js +53 -24
- package/dist/runtime/render/renderToString.d.ts +3 -1
- package/dist/runtime/requestInfo/types.d.ts +4 -1
- package/dist/runtime/requestInfo/utils.d.ts +9 -0
- package/dist/runtime/requestInfo/utils.js +44 -0
- package/dist/runtime/requestInfo/worker.d.ts +0 -1
- package/dist/runtime/requestInfo/worker.js +3 -10
- package/dist/runtime/script.d.ts +1 -3
- package/dist/runtime/script.js +1 -10
- package/dist/runtime/state.d.ts +3 -0
- package/dist/runtime/state.js +13 -0
- package/dist/runtime/worker.d.ts +3 -1
- package/dist/runtime/worker.js +26 -0
- package/dist/scripts/debug-sync.mjs +18 -20
- package/dist/scripts/worker-run.d.mts +1 -1
- package/dist/scripts/worker-run.mjs +52 -113
- package/dist/use-synced-state/SyncStateServer.d.mts +20 -0
- package/dist/use-synced-state/SyncStateServer.mjs +124 -0
- package/dist/use-synced-state/__tests__/SyncStateServer.test.d.mts +1 -0
- package/dist/use-synced-state/__tests__/SyncStateServer.test.mjs +109 -0
- package/dist/use-synced-state/__tests__/useSyncState.test.d.ts +1 -0
- package/dist/use-synced-state/__tests__/useSyncState.test.js +115 -0
- package/dist/use-synced-state/__tests__/useSyncedState.test.d.ts +1 -0
- package/dist/use-synced-state/__tests__/useSyncedState.test.js +115 -0
- package/dist/use-synced-state/__tests__/worker.test.d.mts +1 -0
- package/dist/use-synced-state/__tests__/worker.test.mjs +69 -0
- package/dist/use-synced-state/client.d.ts +28 -0
- package/dist/use-synced-state/client.js +39 -0
- package/dist/use-synced-state/constants.d.mts +1 -0
- package/dist/use-synced-state/constants.mjs +1 -0
- package/dist/use-synced-state/useSyncState.d.ts +20 -0
- package/dist/use-synced-state/useSyncState.js +58 -0
- package/dist/use-synced-state/useSyncedState.d.ts +20 -0
- package/dist/use-synced-state/useSyncedState.js +58 -0
- package/dist/use-synced-state/worker.d.mts +14 -0
- package/dist/use-synced-state/worker.mjs +73 -0
- package/dist/vite/buildApp.mjs +34 -2
- package/dist/vite/configPlugin.mjs +8 -14
- package/dist/vite/constants.d.mts +1 -0
- package/dist/vite/constants.mjs +1 -0
- package/dist/vite/createDirectiveLookupPlugin.mjs +4 -0
- package/dist/vite/devServerTimingPlugin.mjs +4 -0
- package/dist/vite/directiveModulesDevPlugin.mjs +9 -1
- package/dist/vite/directivesPlugin.mjs +4 -0
- package/dist/vite/envResolvers.d.mts +11 -0
- package/dist/vite/envResolvers.mjs +20 -0
- package/dist/vite/getViteEsbuild.mjs +2 -1
- package/dist/vite/hmrStabilityPlugin.d.mts +2 -0
- package/dist/vite/hmrStabilityPlugin.mjs +73 -0
- package/dist/vite/knownDepsResolverPlugin.d.mts +0 -6
- package/dist/vite/knownDepsResolverPlugin.mjs +32 -14
- package/dist/vite/linkerPlugin.d.mts +2 -1
- package/dist/vite/linkerPlugin.mjs +11 -3
- package/dist/vite/linkerPlugin.test.mjs +15 -0
- package/dist/vite/miniflareHMRPlugin.mjs +6 -38
- package/dist/vite/moveStaticAssetsPlugin.mjs +14 -4
- package/dist/vite/redwoodPlugin.mjs +6 -10
- package/dist/vite/runDirectivesScan.mjs +59 -14
- package/dist/vite/ssrBridgePlugin.mjs +126 -34
- package/dist/vite/ssrBridgeWrapPlugin.d.mts +2 -0
- package/dist/vite/ssrBridgeWrapPlugin.mjs +85 -0
- package/dist/vite/staleDepRetryPlugin.d.mts +2 -0
- package/dist/vite/staleDepRetryPlugin.mjs +74 -0
- package/dist/vite/statePlugin.d.mts +4 -0
- package/dist/vite/statePlugin.mjs +62 -0
- package/package.json +26 -10
- package/dist/vite/manifestPlugin.d.mts +0 -4
- package/dist/vite/manifestPlugin.mjs +0 -63
- /package/dist/runtime/lib/{rwContext.js → links.test.d.ts} +0 -0
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// @ts-ignore
|
|
2
2
|
import { compile } from "@mdx-js/mdx";
|
|
3
3
|
import debug from "debug";
|
|
4
|
+
import { glob } from "glob";
|
|
4
5
|
import fsp from "node:fs/promises";
|
|
5
6
|
import path from "node:path";
|
|
6
7
|
import { INTERMEDIATES_OUTPUT_DIR } from "../lib/constants.mjs";
|
|
@@ -17,6 +18,39 @@ const isObject = (value) => Object.prototype.toString.call(value) === "[object O
|
|
|
17
18
|
// https://github.com/vitejs/vite/blob/main/packages/vite/src/node/utils.ts
|
|
18
19
|
const externalRE = /^(https?:)?\/\//;
|
|
19
20
|
const isExternalUrl = (url) => externalRE.test(url);
|
|
21
|
+
async function findDirectiveRoots({ root, readFileWithCache, directiveCheckCache, }) {
|
|
22
|
+
const srcDir = path.resolve(root, "src");
|
|
23
|
+
const files = await glob("**/*.{ts,tsx,js,jsx,mjs,mts,cjs,cts,mdx}", {
|
|
24
|
+
cwd: srcDir,
|
|
25
|
+
absolute: true,
|
|
26
|
+
});
|
|
27
|
+
const directiveFiles = new Set();
|
|
28
|
+
for (const file of files) {
|
|
29
|
+
if (directiveCheckCache.has(file)) {
|
|
30
|
+
if (directiveCheckCache.get(file)) {
|
|
31
|
+
directiveFiles.add(file);
|
|
32
|
+
}
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
try {
|
|
36
|
+
const content = await readFileWithCache(file);
|
|
37
|
+
const hasClient = hasDirective(content, "use client");
|
|
38
|
+
const hasServer = hasDirective(content, "use server");
|
|
39
|
+
const hasAnyDirective = hasClient || hasServer;
|
|
40
|
+
directiveCheckCache.set(file, hasAnyDirective);
|
|
41
|
+
if (hasAnyDirective) {
|
|
42
|
+
directiveFiles.add(file);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
catch (e) {
|
|
46
|
+
log("Could not read file during pre-scan, skipping:", file);
|
|
47
|
+
// Cache the failure to avoid re-reading a problematic file
|
|
48
|
+
directiveCheckCache.set(file, false);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
log("Pre-scan found directive files:", Array.from(directiveFiles));
|
|
52
|
+
return directiveFiles;
|
|
53
|
+
}
|
|
20
54
|
export async function resolveModuleWithEnvironment({ path, importer, importerEnv, clientResolver, workerResolver, }) {
|
|
21
55
|
const resolver = importerEnv === "client" ? clientResolver : workerResolver;
|
|
22
56
|
return new Promise((resolvePromise) => {
|
|
@@ -56,6 +90,16 @@ export const runDirectivesScan = async ({ rootConfig, environments, clientFiles,
|
|
|
56
90
|
// Set environment variable to indicate scanning is in progress
|
|
57
91
|
process.env.RWSDK_DIRECTIVE_SCAN_ACTIVE = "true";
|
|
58
92
|
try {
|
|
93
|
+
const fileContentCache = new Map();
|
|
94
|
+
const directiveCheckCache = new Map();
|
|
95
|
+
const readFileWithCache = async (path) => {
|
|
96
|
+
if (fileContentCache.has(path)) {
|
|
97
|
+
return fileContentCache.get(path);
|
|
98
|
+
}
|
|
99
|
+
const contents = await fsp.readFile(path, "utf-8");
|
|
100
|
+
fileContentCache.set(path, contents);
|
|
101
|
+
return contents;
|
|
102
|
+
};
|
|
59
103
|
const esbuild = await getViteEsbuild(rootConfig.root);
|
|
60
104
|
const input = initialEntries ?? environments.worker.config.build.rollupOptions?.input;
|
|
61
105
|
let entries;
|
|
@@ -78,19 +122,19 @@ export const runDirectivesScan = async ({ rootConfig, environments, clientFiles,
|
|
|
78
122
|
// Filter out virtual modules since they can't be scanned by esbuild
|
|
79
123
|
const realEntries = entries.filter((entry) => !entry.includes("virtual:"));
|
|
80
124
|
const absoluteEntries = realEntries.map((entry) => path.resolve(rootConfig.root, entry));
|
|
81
|
-
|
|
125
|
+
const applicationDirectiveFiles = await findDirectiveRoots({
|
|
126
|
+
root: rootConfig.root,
|
|
127
|
+
readFileWithCache,
|
|
128
|
+
directiveCheckCache,
|
|
129
|
+
});
|
|
130
|
+
const combinedEntries = new Set([
|
|
131
|
+
...absoluteEntries,
|
|
132
|
+
...applicationDirectiveFiles,
|
|
133
|
+
]);
|
|
134
|
+
log("Starting directives scan with combined entries:", Array.from(combinedEntries));
|
|
82
135
|
const workerResolver = createViteAwareResolver(rootConfig, environments.worker);
|
|
83
136
|
const clientResolver = createViteAwareResolver(rootConfig, environments.client);
|
|
84
137
|
const moduleEnvironments = new Map();
|
|
85
|
-
const fileContentCache = new Map();
|
|
86
|
-
const readFileWithCache = async (path) => {
|
|
87
|
-
if (fileContentCache.has(path)) {
|
|
88
|
-
return fileContentCache.get(path);
|
|
89
|
-
}
|
|
90
|
-
const contents = await fsp.readFile(path, "utf-8");
|
|
91
|
-
fileContentCache.set(path, contents);
|
|
92
|
-
return contents;
|
|
93
|
-
};
|
|
94
138
|
const esbuildScanPlugin = {
|
|
95
139
|
name: "rwsdk:esbuild-scan-plugin",
|
|
96
140
|
setup(build) {
|
|
@@ -166,11 +210,11 @@ export const runDirectivesScan = async ({ rootConfig, environments, clientFiles,
|
|
|
166
210
|
});
|
|
167
211
|
build.onLoad({ filter: /\.(m|c)?[jt]sx?$|\.mdx$/ }, async (args) => {
|
|
168
212
|
log("onLoad called for:", args.path);
|
|
169
|
-
if (!args.path
|
|
213
|
+
if (!path.isAbsolute(args.path) ||
|
|
170
214
|
args.path.includes("virtual:") ||
|
|
171
215
|
isExternalUrl(args.path)) {
|
|
172
216
|
log("Skipping file due to filter:", args.path, {
|
|
173
|
-
|
|
217
|
+
isAbsolute: path.isAbsolute(args.path),
|
|
174
218
|
hasVirtual: args.path.includes("virtual:"),
|
|
175
219
|
isExternal: isExternalUrl(args.path),
|
|
176
220
|
});
|
|
@@ -232,7 +276,7 @@ export const runDirectivesScan = async ({ rootConfig, environments, clientFiles,
|
|
|
232
276
|
},
|
|
233
277
|
};
|
|
234
278
|
await esbuild.build({
|
|
235
|
-
entryPoints:
|
|
279
|
+
entryPoints: Array.from(combinedEntries),
|
|
236
280
|
bundle: true,
|
|
237
281
|
write: false,
|
|
238
282
|
outdir: path.join(INTERMEDIATES_OUTPUT_DIR, "directive-scan"),
|
|
@@ -254,7 +298,8 @@ export const runDirectivesScan = async ({ rootConfig, environments, clientFiles,
|
|
|
254
298
|
}
|
|
255
299
|
};
|
|
256
300
|
const deferredLog = (message) => {
|
|
301
|
+
const doLog = process.env.RWSDK_WORKER_RUN ? log : console.log;
|
|
257
302
|
setTimeout(() => {
|
|
258
|
-
|
|
303
|
+
doLog(message);
|
|
259
304
|
}, 500);
|
|
260
305
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import debug from "debug";
|
|
2
2
|
import MagicString from "magic-string";
|
|
3
3
|
import { INTERMEDIATE_SSR_BRIDGE_PATH } from "../lib/constants.mjs";
|
|
4
|
+
import { externalModulesSet } from "./constants.mjs";
|
|
4
5
|
import { findSsrImportCallSites } from "./findSsrSpecifiers.mjs";
|
|
5
6
|
const log = debug("rwsdk:vite:ssr-bridge-plugin");
|
|
6
7
|
export const VIRTUAL_SSR_PREFIX = "virtual:rwsdk:ssr:";
|
|
@@ -10,9 +11,36 @@ export const ssrBridgePlugin = ({ clientFiles, serverFiles, }) => {
|
|
|
10
11
|
const ssrBridgePlugin = {
|
|
11
12
|
name: "rwsdk:ssr-bridge",
|
|
12
13
|
enforce: "pre",
|
|
13
|
-
|
|
14
|
+
configureServer(server) {
|
|
15
|
+
// context(justinvdm, 19 Nov 2025): This plugin patches the dev server's
|
|
16
|
+
// HMR and optimizer behavior to coordinate the `ssr` and `worker`
|
|
17
|
+
// environments. It runs with `enforce: 'pre'` to ensure these patches
|
|
18
|
+
// are in place before other plugins start interacting with the server.
|
|
14
19
|
devServer = server;
|
|
20
|
+
const ssrHot = server.environments.ssr.hot;
|
|
21
|
+
const originalSsrHotSend = ssrHot.send;
|
|
22
|
+
// Chain the SSR's full reload behaviour to the worker
|
|
23
|
+
ssrHot.send = (...args) => {
|
|
24
|
+
if (typeof args[0] === "object" && args[0].type === "full-reload") {
|
|
25
|
+
for (const envName of ["worker", "ssr"]) {
|
|
26
|
+
const moduleGraph = server.environments[envName].moduleGraph;
|
|
27
|
+
moduleGraph.invalidateAll();
|
|
28
|
+
}
|
|
29
|
+
log("SSR full-reload detected, propagating to worker");
|
|
30
|
+
// context(justinvdm, 21 Oct 2025): By sending the full-reload event
|
|
31
|
+
// to the worker, we ensure that the worker's module runner cache is
|
|
32
|
+
// invalidated, as it would have been if this were a full-reload event
|
|
33
|
+
// from the worker.
|
|
34
|
+
server.environments.worker.hot.send.apply(server.environments.worker.hot, args);
|
|
35
|
+
}
|
|
36
|
+
return originalSsrHotSend.apply(ssrHot, args);
|
|
37
|
+
};
|
|
15
38
|
log("Configured dev server");
|
|
39
|
+
const originalRun = devServer.environments.ssr.depsOptimizer?.run;
|
|
40
|
+
devServer.environments.ssr.depsOptimizer.run = async () => {
|
|
41
|
+
originalRun();
|
|
42
|
+
devServer.environments.worker.depsOptimizer.run();
|
|
43
|
+
};
|
|
16
44
|
},
|
|
17
45
|
config(_, { command, isPreview }) {
|
|
18
46
|
isDev = !isPreview && command === "serve";
|
|
@@ -48,11 +76,20 @@ export const ssrBridgePlugin = ({ clientFiles, serverFiles, }) => {
|
|
|
48
76
|
log("Worker environment esbuild configuration complete");
|
|
49
77
|
}
|
|
50
78
|
},
|
|
51
|
-
async resolveId(id) {
|
|
79
|
+
async resolveId(id, importer) {
|
|
52
80
|
// Skip during directive scanning to avoid performance issues
|
|
53
81
|
if (process.env.RWSDK_DIRECTIVE_SCAN_ACTIVE) {
|
|
54
82
|
return;
|
|
55
83
|
}
|
|
84
|
+
// context(justinvdm, 19 Nov 2025):
|
|
85
|
+
// Ensure platform-specific modules are always treated as external in the
|
|
86
|
+
// SSR environment. This is critical for builds, where we produce a
|
|
87
|
+
// standalone SSR bundle. Without this, Vite might try to bundle these
|
|
88
|
+
// virtual modules or fail to resolve them.
|
|
89
|
+
if (this.environment.name === "ssr" && externalModulesSet.has(id)) {
|
|
90
|
+
log("SSR environment: marking %s as external", id);
|
|
91
|
+
return { id, external: true };
|
|
92
|
+
}
|
|
56
93
|
if (isDev) {
|
|
57
94
|
// context(justinvdm, 27 May 2025): In dev, we need to dynamically load
|
|
58
95
|
// SSR modules, so we return the virtual id so that the dynamic loading
|
|
@@ -107,46 +144,101 @@ export const ssrBridgePlugin = ({ clientFiles, serverFiles, }) => {
|
|
|
107
144
|
if (id.startsWith(VIRTUAL_SSR_PREFIX) &&
|
|
108
145
|
this.environment.name === "worker") {
|
|
109
146
|
const realId = id.slice(VIRTUAL_SSR_PREFIX.length);
|
|
110
|
-
|
|
147
|
+
let idForFetch = realId.endsWith(".css.js")
|
|
111
148
|
? realId.slice(0, -3)
|
|
112
149
|
: realId;
|
|
113
150
|
log("Virtual SSR module load: id=%s, realId=%s, idForFetch=%s", id, realId, idForFetch);
|
|
114
151
|
if (isDev) {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
152
|
+
// from the SSR environment, which is crucial for things like server
|
|
153
|
+
// components.
|
|
154
|
+
try {
|
|
155
|
+
const ssrOptimizer = devServer.environments.ssr.depsOptimizer;
|
|
156
|
+
// context(justinvdm, 20 Oct 2025): This is the fix for the stale
|
|
157
|
+
// dependency issue. The root cause is the "unhashed-to-hashed"
|
|
158
|
+
// transition. Our worker code imports a clean ID
|
|
159
|
+
// (`rwsdk/__ssr_bridge`), but we expect to fetch the hashed,
|
|
160
|
+
// optimized version from the SSR environment. When a re-optimization
|
|
161
|
+
// happens, Vite's `fetchModule` (running in the SSR env) finds a
|
|
162
|
+
// "ghost node" in its module graph for the clean ID and incorrectly
|
|
163
|
+
// re-uses its stale, hashed `id` property.
|
|
164
|
+
//
|
|
165
|
+
// To fix this, we manually resolve the hashed path here, before
|
|
166
|
+
// asking the SSR env to process the module. We look into the SSR
|
|
167
|
+
// optimizer's metadata to find the correct, up-to-date hash and
|
|
168
|
+
// construct the path ourselves. This ensures the SSR env is
|
|
169
|
+
// always working with the correct, versioned ID, bypassing the
|
|
170
|
+
// faulty ghost node lookup.
|
|
171
|
+
if (ssrOptimizer &&
|
|
172
|
+
Object.prototype.hasOwnProperty.call(ssrOptimizer.metadata.optimized, realId)) {
|
|
173
|
+
const depInfo = ssrOptimizer.metadata.optimized[realId];
|
|
174
|
+
idForFetch = ssrOptimizer.getOptimizedDepId(depInfo);
|
|
175
|
+
log("Manually resolved %s to hashed path for fetchModule: %s", realId, idForFetch);
|
|
176
|
+
}
|
|
177
|
+
log("Virtual SSR module load: id=%s, realId=%s, idForFetch=%s", id, realId, idForFetch);
|
|
178
|
+
log("Dev mode: fetching SSR module for realPath=%s", idForFetch);
|
|
179
|
+
// We use `fetchModule` with `cached: false` as a safeguard. Since
|
|
180
|
+
// we're in a `load` hook, we know the worker-side cache for this
|
|
181
|
+
// virtual module is stale. `cached: false` ensures that we also
|
|
182
|
+
// bypass any potentially stale transform result in the SSR
|
|
183
|
+
// environment's cache, guaranteeing we get the freshest possible
|
|
184
|
+
// code.
|
|
185
|
+
const result = await devServer.environments.ssr.fetchModule(idForFetch, undefined, { cached: false });
|
|
186
|
+
if ("code" in result) {
|
|
187
|
+
log("Fetched SSR module code length: %d", result.code?.length || 0);
|
|
188
|
+
const code = result.code;
|
|
189
|
+
if (idForFetch.endsWith(".css") &&
|
|
190
|
+
!idForFetch.endsWith(".module.css")) {
|
|
191
|
+
process.env.VERBOSE &&
|
|
192
|
+
log("Plain CSS file, returning empty module for %s", idForFetch);
|
|
193
|
+
return "export default {};";
|
|
194
|
+
}
|
|
195
|
+
const s = new MagicString(code || "");
|
|
196
|
+
const callsites = findSsrImportCallSites(idForFetch, code || "", log);
|
|
197
|
+
for (const site of callsites) {
|
|
198
|
+
const normalized = site.specifier.startsWith("/@id/")
|
|
199
|
+
? site.specifier.slice("/@id/".length)
|
|
200
|
+
: site.specifier;
|
|
201
|
+
// If the import is for a known external module, we must leave it
|
|
202
|
+
// as a bare specifier. Rewriting it with any prefix (`/@id/` or
|
|
203
|
+
// our virtual one) will break Vite's default externalization.
|
|
204
|
+
if (externalModulesSet.has(normalized)) {
|
|
205
|
+
const replacement = `import("${normalized}")`;
|
|
206
|
+
s.overwrite(site.start, site.end, replacement);
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
// context(justinvdm, 11 Aug 2025):
|
|
210
|
+
// - We replace __vite_ssr_import__ and __vite_ssr_dynamic_import__
|
|
211
|
+
// with import() calls so that the module graph can be built
|
|
212
|
+
// correctly (vite looks for imports and import()s to build module
|
|
213
|
+
// graph)
|
|
214
|
+
// - We prepend /@id/$VIRTUAL_SSR_PREFIX to the specifier so that we
|
|
215
|
+
// can stay within the SSR subgraph of the worker module graph
|
|
216
|
+
const replacement = `import("/@id/${VIRTUAL_SSR_PREFIX}${normalized}")`;
|
|
217
|
+
s.overwrite(site.start, site.end, replacement);
|
|
218
|
+
}
|
|
219
|
+
const out = s.toString();
|
|
220
|
+
process.env.VERBOSE &&
|
|
221
|
+
log("Transformed SSR module code for realId=%s: %s", realId, out);
|
|
222
|
+
return {
|
|
223
|
+
code: out,
|
|
224
|
+
map: null, // Sourcemaps are handled by fetchModule's inlining
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
// This case can be hit if the module is already cached. We may
|
|
229
|
+
// need to handle this more gracefully, but for now we'll just
|
|
230
|
+
// return an empty module.
|
|
231
|
+
log("SSR module %s was already cached. Returning empty.", idForFetch);
|
|
232
|
+
return "export default {}";
|
|
233
|
+
}
|
|
125
234
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
for (const site of callsites) {
|
|
130
|
-
const normalized = site.specifier.startsWith("/@id/")
|
|
131
|
-
? site.specifier.slice("/@id/".length)
|
|
132
|
-
: site.specifier;
|
|
133
|
-
// context(justinvdm, 11 Aug 2025):
|
|
134
|
-
// - We replace __vite_ssr_import__ and __vite_ssr_dynamic_import__
|
|
135
|
-
// with import() calls so that the module graph can be built
|
|
136
|
-
// correctly (vite looks for imports and import()s to build module
|
|
137
|
-
// graph)
|
|
138
|
-
// - We prepend /@id/$VIRTUAL_SSR_PREFIX to the specifier so that we
|
|
139
|
-
// can stay within the SSR subgraph of the worker module graph
|
|
140
|
-
const replacement = `import("/@id/${VIRTUAL_SSR_PREFIX}${normalized}")`;
|
|
141
|
-
s.overwrite(site.start, site.end, replacement);
|
|
235
|
+
catch (e) {
|
|
236
|
+
log("Error fetching SSR module for realPath=%s: %s", id, e);
|
|
237
|
+
throw e;
|
|
142
238
|
}
|
|
143
|
-
const out = s.toString();
|
|
144
|
-
log("Transformed SSR module code length: %d", out.length);
|
|
145
|
-
process.env.VERBOSE &&
|
|
146
|
-
log("Transformed SSR module code for realId=%s: %s", realId, out);
|
|
147
|
-
return out;
|
|
148
239
|
}
|
|
149
240
|
}
|
|
241
|
+
return;
|
|
150
242
|
},
|
|
151
243
|
};
|
|
152
244
|
return ssrBridgePlugin;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { Lang, parse as sgParse } from "@ast-grep/napi";
|
|
2
|
+
import debug from "debug";
|
|
3
|
+
import MagicString from "magic-string";
|
|
4
|
+
const log = debug("rwsdk:vite:ssr-bridge-wrap");
|
|
5
|
+
export const ssrBridgeWrapPlugin = () => {
|
|
6
|
+
return {
|
|
7
|
+
name: "rwsdk:ssr-bridge-wrap",
|
|
8
|
+
apply: "build",
|
|
9
|
+
renderChunk(code, chunk) {
|
|
10
|
+
try {
|
|
11
|
+
if (!chunk.fileName.endsWith("ssr_bridge.js")) {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
const s = new MagicString(code);
|
|
15
|
+
// Use AST parsing to find actual import statements (not in comments)
|
|
16
|
+
const root = sgParse(Lang.JavaScript, code);
|
|
17
|
+
// Find all import statements using AST patterns
|
|
18
|
+
const importPatterns = [
|
|
19
|
+
'import { $$$ } from "$MODULE"',
|
|
20
|
+
"import { $$$ } from '$MODULE'",
|
|
21
|
+
'import $DEFAULT from "$MODULE"',
|
|
22
|
+
"import $DEFAULT from '$MODULE'",
|
|
23
|
+
'import * as $NS from "$MODULE"',
|
|
24
|
+
"import * as $NS from '$MODULE'",
|
|
25
|
+
'import "$MODULE"',
|
|
26
|
+
"import '$MODULE'",
|
|
27
|
+
];
|
|
28
|
+
let lastImportEnd = -1;
|
|
29
|
+
for (const pattern of importPatterns) {
|
|
30
|
+
const matches = root.root().findAll(pattern);
|
|
31
|
+
for (const match of matches) {
|
|
32
|
+
const range = match.range();
|
|
33
|
+
if (range.end.index > lastImportEnd) {
|
|
34
|
+
lastImportEnd = range.end.index;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
// Find the export statement using AST
|
|
39
|
+
const exportPatterns = [
|
|
40
|
+
"export { $$$ }",
|
|
41
|
+
'export { $$$ } from "$MODULE"',
|
|
42
|
+
"export { $$$ } from '$MODULE'",
|
|
43
|
+
];
|
|
44
|
+
let exportStart = -1;
|
|
45
|
+
let exportEnd = -1;
|
|
46
|
+
for (const pattern of exportPatterns) {
|
|
47
|
+
const matches = root.root().findAll(pattern);
|
|
48
|
+
for (const match of matches) {
|
|
49
|
+
const range = match.range();
|
|
50
|
+
// Check if this export contains our target symbols
|
|
51
|
+
const text = match.text();
|
|
52
|
+
if (text.includes("renderHtmlStream") &&
|
|
53
|
+
text.includes("ssrLoadModule") &&
|
|
54
|
+
text.includes("ssrWebpackRequire")) {
|
|
55
|
+
exportStart = range.start.index;
|
|
56
|
+
exportEnd = range.end.index;
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (exportStart !== -1)
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
const banner = `export const { renderHtmlStream, ssrLoadModule, ssrWebpackRequire, ssrGetModuleExport, createThenableFromReadableStream } = (function() {`;
|
|
64
|
+
const footer = `return { renderHtmlStream, ssrLoadModule, ssrWebpackRequire, ssrGetModuleExport, createThenableFromReadableStream };\n})();`;
|
|
65
|
+
// Insert banner after the last import (or at the beginning if no imports)
|
|
66
|
+
const insertIndex = lastImportEnd === -1 ? 0 : lastImportEnd;
|
|
67
|
+
s.appendLeft(insertIndex, banner + "\n");
|
|
68
|
+
// Append footer at the end
|
|
69
|
+
s.append(footer);
|
|
70
|
+
// Remove the original export statement if found
|
|
71
|
+
if (exportStart !== -1 && exportEnd !== -1) {
|
|
72
|
+
s.remove(exportStart, exportEnd);
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
code: s.toString(),
|
|
76
|
+
map: s.generateMap(),
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
catch (e) {
|
|
80
|
+
console.error("Error in ssrBridgeWrapPlugin:", e);
|
|
81
|
+
throw e;
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import debug from "debug";
|
|
2
|
+
const log = debug("rwsdk:vite:stale-dep-retry-plugin");
|
|
3
|
+
let stabilityPromise = null;
|
|
4
|
+
let stabilityResolver = null;
|
|
5
|
+
let debounceTimer = null;
|
|
6
|
+
const DEBOUNCE_MS = 500;
|
|
7
|
+
function startWaitingForStability() {
|
|
8
|
+
if (!stabilityPromise) {
|
|
9
|
+
log("Starting to wait for server stability...");
|
|
10
|
+
stabilityPromise = new Promise((resolve) => {
|
|
11
|
+
stabilityResolver = resolve;
|
|
12
|
+
});
|
|
13
|
+
// Start the timer. If it fires, we're stable.
|
|
14
|
+
debounceTimer = setTimeout(finishWaiting, DEBOUNCE_MS);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
function activityDetected() {
|
|
18
|
+
if (stabilityPromise) {
|
|
19
|
+
// If we're waiting for stability, reset the timer.
|
|
20
|
+
log("Activity detected, resetting stability timer.");
|
|
21
|
+
if (debounceTimer)
|
|
22
|
+
clearTimeout(debounceTimer);
|
|
23
|
+
debounceTimer = setTimeout(finishWaiting, DEBOUNCE_MS);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function finishWaiting() {
|
|
27
|
+
if (stabilityResolver) {
|
|
28
|
+
log("Server appears stable. Resolving promise.");
|
|
29
|
+
stabilityResolver();
|
|
30
|
+
}
|
|
31
|
+
stabilityPromise = null;
|
|
32
|
+
stabilityResolver = null;
|
|
33
|
+
debounceTimer = null;
|
|
34
|
+
}
|
|
35
|
+
export function staleDepRetryPlugin() {
|
|
36
|
+
return {
|
|
37
|
+
name: "rws-vite-plugin:stale-dep-retry",
|
|
38
|
+
apply: "serve",
|
|
39
|
+
// Monitor server activity by tapping into the transform hook. This is a
|
|
40
|
+
// reliable indicator that Vite is busy processing modules.
|
|
41
|
+
transform() {
|
|
42
|
+
activityDetected();
|
|
43
|
+
return null;
|
|
44
|
+
},
|
|
45
|
+
configureServer(server) {
|
|
46
|
+
// context(justinvdm, 19 Nov 2025): This hook adds an error handling
|
|
47
|
+
// middleware for stale dependency errors. It runs in a returned function,
|
|
48
|
+
// which intentionally places it late in the middleware stack. Unlike
|
|
49
|
+
// other plugins that must run before the Cloudflare plugin to prevent
|
|
50
|
+
// startup deadlocks, its timing is not critical, so `enforce: 'pre'`
|
|
51
|
+
// is not needed.
|
|
52
|
+
return () => {
|
|
53
|
+
server.middlewares.use(async function rwsdkStaleBundleErrorHandler(err, req, res, next) {
|
|
54
|
+
if (err &&
|
|
55
|
+
typeof err.message === "string" &&
|
|
56
|
+
err.message.includes("new version of the pre-bundle")) {
|
|
57
|
+
log("Caught stale pre-bundle error. Waiting for server to stabilize...");
|
|
58
|
+
startWaitingForStability();
|
|
59
|
+
await stabilityPromise;
|
|
60
|
+
log("Server stabilized. Sending full-reload and redirecting.");
|
|
61
|
+
// Signal the client to do a full page reload.
|
|
62
|
+
server.environments.client.hot.send({
|
|
63
|
+
type: "full-reload",
|
|
64
|
+
});
|
|
65
|
+
res.writeHead(307, { Location: req.originalUrl || req.url });
|
|
66
|
+
res.end();
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
next(err);
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import debug from "debug";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import { RW_STATE_EXPORT_PATH } from "../lib/constants.mjs";
|
|
4
|
+
import { maybeResolveEnvImport } from "./envResolvers.mjs";
|
|
5
|
+
const log = debug("rwsdk:vite:state-plugin");
|
|
6
|
+
const VIRTUAL_STATE_PREFIX = "virtual:rwsdk:state:";
|
|
7
|
+
export const statePlugin = ({ projectRootDir, }) => {
|
|
8
|
+
let isDev = false;
|
|
9
|
+
const stateModulePath = maybeResolveEnvImport({
|
|
10
|
+
id: RW_STATE_EXPORT_PATH,
|
|
11
|
+
envName: "worker",
|
|
12
|
+
projectRootDir,
|
|
13
|
+
});
|
|
14
|
+
if (!stateModulePath) {
|
|
15
|
+
throw new Error(`[rwsdk] State module path not found for project root: ${projectRootDir}. This is likely a bug in RedwoodSDK. Please report this issue at https://github.com/redwoodjs/sdk/issues/new`);
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
18
|
+
name: "rwsdk:state",
|
|
19
|
+
enforce: "pre",
|
|
20
|
+
config(_, { command, isPreview }) {
|
|
21
|
+
isDev = !isPreview && command === "serve";
|
|
22
|
+
},
|
|
23
|
+
configEnvironment(env, config) {
|
|
24
|
+
if (env === "worker") {
|
|
25
|
+
config.optimizeDeps ??= {};
|
|
26
|
+
config.optimizeDeps.esbuildOptions ??= {};
|
|
27
|
+
config.optimizeDeps.esbuildOptions.plugins ??= [];
|
|
28
|
+
config.optimizeDeps.esbuildOptions.plugins.push({
|
|
29
|
+
name: "rwsdk-state-external",
|
|
30
|
+
setup(build) {
|
|
31
|
+
build.onResolve({
|
|
32
|
+
// context(justinvdm, 13 Oct 2025): Vite dep optimizer slugifies the export path
|
|
33
|
+
filter: new RegExp(`^(${RW_STATE_EXPORT_PATH}|${VIRTUAL_STATE_PREFIX}.*)$`),
|
|
34
|
+
}, (args) => {
|
|
35
|
+
log("Marking as external: %s", args.path);
|
|
36
|
+
return {
|
|
37
|
+
path: args.path,
|
|
38
|
+
external: true,
|
|
39
|
+
};
|
|
40
|
+
});
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
resolveId(id) {
|
|
46
|
+
if (id === RW_STATE_EXPORT_PATH) {
|
|
47
|
+
if (isDev && this.environment.name === "worker") {
|
|
48
|
+
return `${VIRTUAL_STATE_PREFIX}${id}`;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
return stateModulePath;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
async load(id) {
|
|
56
|
+
if (id.startsWith(VIRTUAL_STATE_PREFIX)) {
|
|
57
|
+
log("Loading virtual state module from %s", stateModulePath);
|
|
58
|
+
return await fs.readFile(stateModulePath, "utf-8");
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rwsdk",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.30-test.20251119220440",
|
|
4
4
|
"description": "Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -36,6 +36,10 @@
|
|
|
36
36
|
"types": "./dist/runtime/entries/client.d.ts",
|
|
37
37
|
"default": "./dist/runtime/entries/client.js"
|
|
38
38
|
},
|
|
39
|
+
"./__state": {
|
|
40
|
+
"types": "./dist/runtime/state.d.ts",
|
|
41
|
+
"default": "./dist/runtime/state.js"
|
|
42
|
+
},
|
|
39
43
|
"./__ssr": {
|
|
40
44
|
"react-server": "./dist/runtime/entries/no-react-server.js",
|
|
41
45
|
"types": "./dist/runtime/entries/ssr.d.ts",
|
|
@@ -54,7 +58,8 @@
|
|
|
54
58
|
},
|
|
55
59
|
"./router": {
|
|
56
60
|
"types": "./dist/runtime/entries/router.d.ts",
|
|
57
|
-
"
|
|
61
|
+
"workerd": "./dist/runtime/entries/router.js",
|
|
62
|
+
"default": "./dist/runtime/entries/routerClient.js"
|
|
58
63
|
},
|
|
59
64
|
"./auth": {
|
|
60
65
|
"types": "./dist/runtime/entries/auth.d.ts",
|
|
@@ -99,6 +104,14 @@
|
|
|
99
104
|
"./realtime/durableObject": {
|
|
100
105
|
"types": "./dist/runtime/lib/realtime/durableObject.d.ts",
|
|
101
106
|
"default": "./dist/runtime/lib/realtime/durableObject.js"
|
|
107
|
+
},
|
|
108
|
+
"./use-synced-state/client": {
|
|
109
|
+
"types": "./dist/use-synced-state/client.d.ts",
|
|
110
|
+
"default": "./dist/use-synced-state/client.js"
|
|
111
|
+
},
|
|
112
|
+
"./use-synced-state/worker": {
|
|
113
|
+
"types": "./dist/use-synced-state/worker.d.ts",
|
|
114
|
+
"default": "./dist/use-synced-state/worker.js"
|
|
102
115
|
}
|
|
103
116
|
},
|
|
104
117
|
"keywords": [
|
|
@@ -139,18 +152,22 @@
|
|
|
139
152
|
"@puppeteer/browsers": "~2.10.0",
|
|
140
153
|
"@types/decompress": "~4.2.7",
|
|
141
154
|
"@types/fs-extra": "~11.0.4",
|
|
155
|
+
"@types/glob": "^8.1.0",
|
|
142
156
|
"@types/react": "~19.1.2",
|
|
143
157
|
"@types/react-dom": "~19.1.2",
|
|
144
158
|
"@types/react-is": "~19.0.0",
|
|
145
159
|
"@vitejs/plugin-react": "~5.0.0",
|
|
160
|
+
"capnweb": "~0.2.0",
|
|
146
161
|
"chokidar": "~4.0.0",
|
|
147
162
|
"debug": "~4.4.0",
|
|
163
|
+
"decompress": "~4.2.1",
|
|
148
164
|
"enhanced-resolve": "~5.18.1",
|
|
149
165
|
"eventsource-parser": "~3.0.0",
|
|
150
166
|
"execa": "~9.6.0",
|
|
151
167
|
"find-up": "~8.0.0",
|
|
152
168
|
"fs-extra": "~11.3.0",
|
|
153
|
-
"
|
|
169
|
+
"get-port": "^7.1.0",
|
|
170
|
+
"glob": "~11.1.0",
|
|
154
171
|
"ignore": "~7.0.4",
|
|
155
172
|
"jsonc-parser": "~3.3.1",
|
|
156
173
|
"kysely": "~0.28.2",
|
|
@@ -166,14 +183,13 @@
|
|
|
166
183
|
"ts-morph": "~27.0.0",
|
|
167
184
|
"unique-names-generator": "~4.7.1",
|
|
168
185
|
"vibe-rules": "~0.3.0",
|
|
169
|
-
"vite-tsconfig-paths": "~5.1.4"
|
|
170
|
-
"decompress": "~4.2.1"
|
|
186
|
+
"vite-tsconfig-paths": "~5.1.4"
|
|
171
187
|
},
|
|
172
188
|
"peerDependencies": {
|
|
173
|
-
"@cloudflare/vite-plugin": "^1.
|
|
174
|
-
"react": ">=19.2.0-
|
|
175
|
-
"react-dom": ">=19.2.0-
|
|
176
|
-
"react-server-dom-webpack": ">=19.2.0-
|
|
189
|
+
"@cloudflare/vite-plugin": "^1.13.10",
|
|
190
|
+
"react": ">=19.2.0-0 <19.3.0 || >=19.3.0-0 <20.0.0",
|
|
191
|
+
"react-dom": ">=19.2.0-0 <19.3.0 || >=19.3.0-0 <20.0.0",
|
|
192
|
+
"react-server-dom-webpack": ">=19.2.0-0 <19.3.0 || >=19.3.0-0 <20.0.0",
|
|
177
193
|
"vite": "^6.2.6 || 7.x",
|
|
178
194
|
"wrangler": "^4.35.0"
|
|
179
195
|
},
|
|
@@ -189,7 +205,7 @@
|
|
|
189
205
|
"semver": "~7.7.1",
|
|
190
206
|
"tsx": "~4.20.0",
|
|
191
207
|
"typescript": "~5.9.0",
|
|
192
|
-
"vite": "7.
|
|
208
|
+
"vite": "~7.2.0",
|
|
193
209
|
"vitest": "~3.2.0"
|
|
194
210
|
}
|
|
195
211
|
}
|