rwsdk 0.2.0-alpha.0 → 0.2.0-alpha.10

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.
@@ -1,6 +1,7 @@
1
1
  import debug from "debug";
2
2
  import { SSR_BRIDGE_PATH } from "../lib/constants.mjs";
3
- import { findSsrImportSpecifiers } from "./findSsrSpecifiers.mjs";
3
+ import { findSsrImportCallSites } from "./findSsrSpecifiers.mjs";
4
+ import MagicString from "magic-string";
4
5
  const log = debug("rwsdk:vite:ssr-bridge-plugin");
5
6
  export const VIRTUAL_SSR_PREFIX = "virtual:rwsdk:ssr:";
6
7
  export const ssrBridgePlugin = ({ clientFiles, serverFiles, }) => {
@@ -108,35 +109,27 @@ export const ssrBridgePlugin = ({ clientFiles, serverFiles, }) => {
108
109
  return "export default {};";
109
110
  }
110
111
  log("Fetched SSR module code length: %d", code?.length || 0);
111
- const { imports, dynamicImports } = findSsrImportSpecifiers(idForFetch, code || "", log);
112
- const allSpecifiers = [
113
- ...new Set([...imports, ...dynamicImports]),
114
- ].map((id) => id.startsWith("/@id/") ? id.slice("/@id/".length) : id);
115
- const switchCases = allSpecifiers
116
- .map((specifier) => ` case "${specifier}": void import("${VIRTUAL_SSR_PREFIX}${specifier}");`)
117
- .join("\n");
118
- const transformedCode = `
119
- await (async function(__vite_ssr_import__, __vite_ssr_dynamic_import__) {${code}})(
120
- __ssrImport.bind(null, false),
121
- __ssrImport.bind(null, true)
122
- );
123
-
124
- function __ssrImport(isDynamic, id, ...args) {
125
- id = id.startsWith('/@id/') ? id.slice('/@id/'.length) : id;
126
-
127
- switch (id) {
128
- ${switchCases}
129
- }
130
-
131
- return isDynamic
132
- ? __vite_ssr_dynamic_import__("/@id/${VIRTUAL_SSR_PREFIX}" + id, ...args)
133
- : __vite_ssr_import__("/@id/${VIRTUAL_SSR_PREFIX}" + id, ...args);
134
- }
135
- `;
136
- log("Transformed SSR module code length: %d", transformedCode.length);
112
+ const s = new MagicString(code || "");
113
+ const callsites = findSsrImportCallSites(idForFetch, code || "", log);
114
+ for (const site of callsites) {
115
+ const normalized = site.specifier.startsWith("/@id/")
116
+ ? site.specifier.slice("/@id/".length)
117
+ : site.specifier;
118
+ // context(justinvdm, 11 Aug 2025):
119
+ // - We replace __vite_ssr_import__ and __vite_ssr_dynamic_import__
120
+ // with import() calls so that the module graph can be built
121
+ // correctly (vite looks for imports and import()s to build module
122
+ // graph)
123
+ // - We prepend /@id/$VIRTUAL_SSR_PREFIX to the specifier so that we
124
+ // can stay within the SSR subgraph of the worker module graph
125
+ const replacement = `import("/@id/${VIRTUAL_SSR_PREFIX}${normalized}")`;
126
+ s.overwrite(site.start, site.end, replacement);
127
+ }
128
+ const out = s.toString();
129
+ log("Transformed SSR module code length: %d", out.length);
137
130
  process.env.VERBOSE &&
138
- log("Transformed SSR module code for realId=%s: %s", realId, transformedCode);
139
- return transformedCode;
131
+ log("Transformed SSR module code for realId=%s: %s", realId, out);
132
+ return out;
140
133
  }
141
134
  }
142
135
  process.env.VERBOSE && log("No load handling for id=%s", id);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rwsdk",
3
- "version": "0.2.0-alpha.0",
3
+ "version": "0.2.0-alpha.10",
4
4
  "description": "Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime",
5
5
  "type": "module",
6
6
  "bin": {