rwsdk 0.1.15 → 0.1.17

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 (51) hide show
  1. package/dist/lib/compileTsModule.d.mts +1 -0
  2. package/dist/lib/compileTsModule.mjs +27 -0
  3. package/dist/runtime/entries/navigation.d.ts +1 -0
  4. package/dist/runtime/entries/navigation.js +1 -0
  5. package/dist/runtime/imports/client.js +1 -1
  6. package/dist/runtime/imports/ssr.js +1 -1
  7. package/dist/runtime/imports/worker.js +1 -1
  8. package/dist/runtime/register/ssr.js +1 -1
  9. package/dist/runtime/render/injectRSCPayload.d.ts +3 -0
  10. package/dist/runtime/render/injectRSCPayload.js +79 -0
  11. package/dist/scripts/build-vendor-bundles.d.mts +1 -0
  12. package/dist/scripts/build-vendor-bundles.mjs +92 -0
  13. package/dist/vite/aliasByEnvPlugin.d.mts +2 -0
  14. package/dist/vite/aliasByEnvPlugin.mjs +11 -0
  15. package/dist/vite/asyncSetupPlugin.d.mts +6 -0
  16. package/dist/vite/asyncSetupPlugin.mjs +23 -0
  17. package/dist/vite/copyPrismaWasmPlugin.d.mts +4 -0
  18. package/dist/vite/copyPrismaWasmPlugin.mjs +32 -0
  19. package/dist/vite/createDirectiveLookupPlugin.mjs +4 -6
  20. package/dist/vite/customReactBuildPlugin.d.mts +4 -0
  21. package/dist/vite/customReactBuildPlugin.mjs +61 -0
  22. package/dist/vite/findSsrSpecifiers.mjs +16 -0
  23. package/dist/vite/injectHmrPreambleJsxPlugin.d.mts +2 -0
  24. package/dist/vite/injectHmrPreambleJsxPlugin.mjs +22 -0
  25. package/dist/vite/miniflareHMRPlugin.mjs +59 -21
  26. package/dist/vite/miniflarePlugin.d.mts +9 -0
  27. package/dist/vite/miniflarePlugin.mjs +135 -0
  28. package/dist/vite/requestUtils.d.mts +6 -0
  29. package/dist/vite/requestUtils.mjs +35 -0
  30. package/dist/vite/setupEnvFiles.d.mts +4 -0
  31. package/dist/vite/setupEnvFiles.mjs +31 -0
  32. package/dist/vite/ssrBridgePlugin.mjs +18 -5
  33. package/dist/vite/useClientPlugin.d.mts +8 -0
  34. package/dist/vite/useClientPlugin.mjs +295 -0
  35. package/dist/vite/useClientPlugin.test.d.mts +1 -0
  36. package/dist/vite/useClientPlugin.test.mjs +1204 -0
  37. package/dist/worker/__ssr_bridge.js +8947 -0
  38. package/dist/worker/__ssr_bridge.js.map +1 -0
  39. package/package.json +1 -1
  40. package/dist/vite/invalidateClientModule.d.mts +0 -2
  41. package/dist/vite/invalidateClientModule.mjs +0 -8
  42. package/dist/vite/invalidateModule copy.d.mts +0 -2
  43. package/dist/vite/invalidateModule copy.mjs +0 -14
  44. package/dist/vite/invalidateSSRModule.d.mts +0 -2
  45. package/dist/vite/invalidateSSRModule.mjs +0 -7
  46. package/dist/vite/isJsFile.d.ts +0 -1
  47. package/dist/vite/isJsFile.js +0 -3
  48. package/dist/vite/mode.d.mts +0 -5
  49. package/dist/vite/mode.mjs +0 -25
  50. package/dist/vite/modePlugin.d.mts +0 -2
  51. package/dist/vite/modePlugin.mjs +0 -10
@@ -0,0 +1 @@
1
+ export declare const compileTsModule: (tsCode: string) => string;
@@ -0,0 +1,27 @@
1
+ import ts from "typescript";
2
+ import path from "path";
3
+ export const compileTsModule = (tsCode) => {
4
+ const tsConfigPath = "./tsconfig.json";
5
+ // Find the nearest tsconfig.json
6
+ const configPath = ts.findConfigFile(path.dirname(tsConfigPath), ts.sys.fileExists, path.basename(tsConfigPath));
7
+ if (!configPath) {
8
+ throw new Error(`Could not find a valid tsconfig.json at path: ${tsConfigPath}`);
9
+ }
10
+ // Read and parse tsconfig.json
11
+ const configFile = ts.readConfigFile(configPath, ts.sys.readFile);
12
+ if (configFile.error) {
13
+ throw new Error(`Error reading tsconfig.json: ${ts.formatDiagnostic(configFile.error, ts.createCompilerHost({}))}`);
14
+ }
15
+ const parsedConfig = ts.parseJsonConfigFileContent(configFile.config, ts.sys, path.dirname(configPath));
16
+ const compilerOptions = parsedConfig.options;
17
+ // Transpile the TypeScript code using the compiler options
18
+ const output = ts.transpileModule(tsCode, {
19
+ compilerOptions,
20
+ reportDiagnostics: true,
21
+ });
22
+ if (output.diagnostics && output.diagnostics.length) {
23
+ const diagnosticMessages = output.diagnostics.map((diagnostic) => ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"));
24
+ throw new Error(`TypeScript Compilation Errors:\n${diagnosticMessages.join("\n")}`);
25
+ }
26
+ return output.outputText; // Compiled JavaScript code
27
+ };
@@ -0,0 +1 @@
1
+ export * from "../clientNavigation";
@@ -0,0 +1 @@
1
+ export * from "../clientNavigation";
@@ -5,7 +5,7 @@ export const loadModule = memoize(async (id) => {
5
5
  return await import(/* @vite-ignore */ id);
6
6
  }
7
7
  else {
8
- const { useClientLookup } = await import("virtual:use-client-lookup");
8
+ const { useClientLookup } = await import("virtual:use-client-lookup.js");
9
9
  const moduleFn = useClientLookup[id];
10
10
  if (!moduleFn) {
11
11
  throw new Error(`(client) No module found for '${id}' in module lookup for "use client" directive`);
@@ -1,6 +1,6 @@
1
1
  import memoize from "lodash/memoize";
2
2
  export const ssrLoadModule = memoize(async (id) => {
3
- const { useClientLookup } = await import("virtual:use-client-lookup");
3
+ const { useClientLookup } = await import("virtual:use-client-lookup.js");
4
4
  const moduleFn = useClientLookup[id];
5
5
  if (!moduleFn) {
6
6
  throw new Error(`(ssr) No module found for '${id}' in module lookup for "use client" directive`);
@@ -2,7 +2,7 @@ import memoize from "lodash/memoize";
2
2
  import { requestInfo } from "../requestInfo/worker";
3
3
  import { ssrWebpackRequire as baseSsrWebpackRequire } from "rwsdk/__ssr_bridge";
4
4
  export const loadServerModule = memoize(async (id) => {
5
- const { useServerLookup } = await import("virtual:use-server-lookup");
5
+ const { useServerLookup } = await import("virtual:use-server-lookup.js");
6
6
  const moduleFn = useServerLookup[id];
7
7
  if (!moduleFn) {
8
8
  throw new Error(`(worker) No module found for '${id}' in module lookup for "use server" directive`);
@@ -1,7 +1,7 @@
1
1
  import memoize from "lodash/memoize";
2
2
  import { createServerReference as baseCreateServerReference } from "react-server-dom-webpack/client.edge";
3
3
  export const loadServerModule = memoize(async (id) => {
4
- const { useServerLookup } = await import("virtual:use-server-lookup");
4
+ const { useServerLookup } = await import("virtual:use-server-lookup.js");
5
5
  const moduleFn = useServerLookup[id];
6
6
  if (!moduleFn) {
7
7
  throw new Error(`(worker) No module found for '${id}' in module lookup for "use server" directive`);
@@ -0,0 +1,3 @@
1
+ export declare function injectRSCPayload(rscStream: ReadableStream, { nonce }: {
2
+ nonce: string;
3
+ }): TransformStream<any, any>;
@@ -0,0 +1,79 @@
1
+ // port(justinvdm, 10 Mar 2025): This is a modified version of https://github.com/devongovett/rsc-html-stream/blob/main/server.js
2
+ // Modification: We needed to add a nonce attribute to the script tag for CSP
3
+ const encoder = new TextEncoder();
4
+ const trailer = "</body></html>";
5
+ export function injectRSCPayload(rscStream, { nonce }) {
6
+ let decoder = new TextDecoder();
7
+ let resolveFlightDataPromise;
8
+ let flightDataPromise = new Promise((resolve) => (resolveFlightDataPromise = resolve));
9
+ let startedRSC = false;
10
+ // Buffer all HTML chunks enqueued during the current tick of the event loop (roughly)
11
+ // and write them to the output stream all at once. This ensures that we don't generate
12
+ // invalid HTML by injecting RSC in between two partial chunks of HTML.
13
+ let buffered = [];
14
+ let timeout = null;
15
+ function flushBufferedChunks(controller) {
16
+ for (let chunk of buffered) {
17
+ let buf = decoder.decode(chunk);
18
+ if (buf.endsWith(trailer)) {
19
+ buf = buf.slice(0, -trailer.length);
20
+ }
21
+ controller.enqueue(encoder.encode(buf));
22
+ }
23
+ buffered.length = 0;
24
+ timeout = null;
25
+ }
26
+ return new TransformStream({
27
+ transform(chunk, controller) {
28
+ buffered.push(chunk);
29
+ if (timeout) {
30
+ return;
31
+ }
32
+ timeout = setTimeout(async () => {
33
+ flushBufferedChunks(controller);
34
+ if (!startedRSC) {
35
+ startedRSC = true;
36
+ writeRSCStream(rscStream, controller, { nonce })
37
+ .catch((err) => controller.error(err))
38
+ .then(resolveFlightDataPromise);
39
+ }
40
+ }, 0);
41
+ },
42
+ async flush(controller) {
43
+ await flightDataPromise;
44
+ if (timeout) {
45
+ clearTimeout(timeout);
46
+ flushBufferedChunks(controller);
47
+ }
48
+ controller.enqueue(encoder.encode("</body></html>"));
49
+ },
50
+ });
51
+ }
52
+ async function writeRSCStream(rscStream, controller, { nonce }) {
53
+ let decoder = new TextDecoder("utf-8", { fatal: true });
54
+ for await (let chunk of rscStream) {
55
+ // Try decoding the chunk to send as a string.
56
+ // If that fails (e.g. binary data that is invalid unicode), write as base64.
57
+ try {
58
+ writeChunk(JSON.stringify(decoder.decode(chunk, { stream: true })), controller, { nonce });
59
+ }
60
+ catch (err) {
61
+ let base64 = JSON.stringify(btoa(String.fromCodePoint(...chunk)));
62
+ writeChunk(`Uint8Array.from(atob(${base64}), m => m.codePointAt(0))`, controller, { nonce });
63
+ }
64
+ }
65
+ let remaining = decoder.decode();
66
+ if (remaining.length) {
67
+ writeChunk(JSON.stringify(remaining), controller, { nonce });
68
+ }
69
+ }
70
+ function writeChunk(chunk, controller, { nonce }) {
71
+ controller.enqueue(encoder.encode(`<script nonce="${nonce}">${escapeScript(`(self.__FLIGHT_DATA||=[]).push(${chunk})`)}</script>`));
72
+ }
73
+ // Escape closing script tags and HTML comments in JS content.
74
+ // https://www.w3.org/TR/html52/semantics-scripting.html#restrictions-for-contents-of-script-elements
75
+ // Avoid replacing </script with <\/script as it would break the following valid JS: 0</script/ (i.e. regexp literal).
76
+ // Instead, escape the s character.
77
+ function escapeScript(script) {
78
+ return script.replace(/<!--/g, "<\\!--").replace(/<\/(script)/gi, "</\\$1");
79
+ }
@@ -0,0 +1 @@
1
+ export declare const buildVendorBundles: () => Promise<void>;
@@ -0,0 +1,92 @@
1
+ import { resolve } from "node:path";
2
+ import { build, mergeConfig } from "vite";
3
+ import { $ } from "../lib/$.mjs";
4
+ import { VENDOR_DIST_DIR, VENDOR_SRC_DIR } from "../lib/constants.mjs";
5
+ import { unlink } from "fs/promises";
6
+ import { pathExists } from "fs-extra";
7
+ const createConfig = (mode) => ({
8
+ mode,
9
+ plugins: [],
10
+ logLevel: process.env.VERBOSE ? "info" : "error",
11
+ build: {
12
+ emptyOutDir: false,
13
+ sourcemap: true,
14
+ minify: mode === "production",
15
+ },
16
+ define: {
17
+ "process.env.NODE_ENV": JSON.stringify(mode),
18
+ },
19
+ });
20
+ const configs = {
21
+ // Build react internals with server conditions
22
+ reactServerInternals: (mode) => mergeConfig(createConfig(mode), {
23
+ build: {
24
+ outDir: VENDOR_DIST_DIR,
25
+ lib: {
26
+ entry: resolve(VENDOR_SRC_DIR, "react-server-internals.js"),
27
+ name: "react-server-internals",
28
+ formats: ["es"],
29
+ fileName: () => `react-server-internals.${mode}.js`,
30
+ },
31
+ },
32
+ resolve: {
33
+ conditions: ["react-server"],
34
+ },
35
+ }),
36
+ // Build custom React implementation (for both development and production)
37
+ react: (mode) => mergeConfig(createConfig(mode), {
38
+ build: {
39
+ outDir: VENDOR_DIST_DIR,
40
+ lib: {
41
+ entry: resolve(VENDOR_SRC_DIR, "react.js"),
42
+ name: "react",
43
+ formats: ["es"],
44
+ fileName: () => `react.${mode}.js`,
45
+ },
46
+ },
47
+ resolve: {
48
+ alias: {
49
+ "react-server-internals": resolve(VENDOR_DIST_DIR, `react-server-internals.${mode}.js`),
50
+ },
51
+ },
52
+ }),
53
+ };
54
+ export const buildVendorBundles = async () => {
55
+ console.log("Cleaning vendor directory...");
56
+ await $ `pnpm clean:vendor`;
57
+ // Build for both development and production modes
58
+ const modes = ["development", "production"];
59
+ // Build the temporary server internals files first
60
+ console.log("Building react-server-internals...");
61
+ for (const mode of modes) {
62
+ await build(configs.reactServerInternals(mode));
63
+ }
64
+ // Build React using the server internals
65
+ console.log("Building React custom builds...");
66
+ for (const mode of modes) {
67
+ await build(configs.react(mode));
68
+ }
69
+ // Clean up temporary server internals files
70
+ console.log("Cleaning up temporary server internals files...");
71
+ for (const mode of modes) {
72
+ const serverInternalsFile = resolve(VENDOR_DIST_DIR, `react-server-internals.${mode}.js`);
73
+ const serverInternalsMapFile = `${serverInternalsFile}.map`;
74
+ try {
75
+ // Delete the server internals JavaScript file
76
+ if (await pathExists(serverInternalsFile)) {
77
+ await unlink(serverInternalsFile);
78
+ }
79
+ // Delete the source map file if it exists
80
+ if (await pathExists(serverInternalsMapFile)) {
81
+ await unlink(serverInternalsMapFile);
82
+ }
83
+ }
84
+ catch (error) {
85
+ console.warn(`Warning: Failed to delete ${serverInternalsFile}`, error);
86
+ }
87
+ }
88
+ console.log("Done building vendor bundles");
89
+ };
90
+ if (import.meta.url === new URL(process.argv[1], import.meta.url).href) {
91
+ buildVendorBundles();
92
+ }
@@ -0,0 +1,2 @@
1
+ import { Plugin } from "vite";
2
+ export declare const aliasByEnvPlugin: (aliasesByEnv: Record<string, Record<string, string>>) => Plugin;
@@ -0,0 +1,11 @@
1
+ export const aliasByEnvPlugin = (aliasesByEnv) => ({
2
+ name: "rw-sdk-env-alias",
3
+ enforce: "pre",
4
+ resolveId(id, importer) {
5
+ const aliases = aliasesByEnv[this.environment.name] ?? {};
6
+ const alias = aliases[id];
7
+ if (alias) {
8
+ return alias;
9
+ }
10
+ },
11
+ });
@@ -0,0 +1,6 @@
1
+ import { Plugin } from "vite";
2
+ export declare const asyncSetupPlugin: ({ setup, }: {
3
+ setup: ({ command }: {
4
+ command: "serve" | "build";
5
+ }) => Promise<unknown>;
6
+ }) => Plugin[];
@@ -0,0 +1,23 @@
1
+ export const asyncSetupPlugin = ({ setup, }) => {
2
+ let taskPromise = Promise.resolve(null);
3
+ return [
4
+ {
5
+ name: "rw-sdk-async-setup:serve",
6
+ apply: "serve",
7
+ configureServer(server) {
8
+ taskPromise = setup({ command: "serve" });
9
+ server.middlewares.use(async (_req, _res, next) => {
10
+ await taskPromise;
11
+ next();
12
+ });
13
+ },
14
+ },
15
+ {
16
+ name: "rw-sdk-async-setup:build",
17
+ apply: "build",
18
+ async buildStart() {
19
+ await setup({ command: "build" });
20
+ },
21
+ },
22
+ ];
23
+ };
@@ -0,0 +1,4 @@
1
+ import { Plugin } from "vite";
2
+ export declare const copyPrismaWasmPlugin: ({ rootDir, }: {
3
+ rootDir: string;
4
+ }) => Plugin;
@@ -0,0 +1,32 @@
1
+ import { copy, pathExists } from "fs-extra";
2
+ import { resolve } from "node:path";
3
+ import MagicString from "magic-string";
4
+ import path from "path";
5
+ export const copyPrismaWasmPlugin = ({ rootDir, }) => ({
6
+ name: "rwsdk:copy-prisma-wasm",
7
+ enforce: "post",
8
+ apply: "build",
9
+ async writeBundle() {
10
+ const wasmFilePath = resolve(rootDir, "node_modules/.prisma/client/query_engine_bg.wasm");
11
+ const fileName = path.basename(wasmFilePath);
12
+ const outputPath = path.resolve(rootDir, "dist", "worker", fileName);
13
+ if (await pathExists(wasmFilePath)) {
14
+ await copy(wasmFilePath, outputPath);
15
+ console.log(`✅ Copied ${fileName} from ${wasmFilePath} to ${outputPath}`);
16
+ }
17
+ },
18
+ renderChunk(code) {
19
+ if (!code.includes(".wasm")) {
20
+ return;
21
+ }
22
+ const s = new MagicString(code);
23
+ s.replace(/import\(["'](.+?\.wasm)["']\)/g, (_, filePath) => {
24
+ const fileName = path.basename(filePath);
25
+ return `import("./${fileName}")`;
26
+ });
27
+ return {
28
+ code: s.toString(),
29
+ map: s.generateMap(),
30
+ };
31
+ },
32
+ });
@@ -150,12 +150,12 @@ export const createDirectiveLookupPlugin = async ({ projectRootDir, files, confi
150
150
  const escapedVirtualModuleName = config.virtualModuleName.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
151
151
  const escapedPrefixedModuleName = `/@id/${config.virtualModuleName}`.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
152
152
  build.onResolve({
153
- filter: new RegExp(`^(${escapedVirtualModuleName}|${escapedPrefixedModuleName})$`),
153
+ filter: new RegExp(`^(${escapedVirtualModuleName}|${escapedPrefixedModuleName})\.js$`),
154
154
  }, () => {
155
155
  process.env.VERBOSE &&
156
156
  log("Esbuild onResolve: marking %s as external", config.virtualModuleName);
157
157
  return {
158
- path: config.virtualModuleName,
158
+ path: `${config.virtualModuleName}.js`,
159
159
  external: true,
160
160
  };
161
161
  });
@@ -184,13 +184,11 @@ export const createDirectiveLookupPlugin = async ({ projectRootDir, files, confi
184
184
  },
185
185
  resolveId(source) {
186
186
  process.env.VERBOSE && log("Resolving id=%s", source);
187
- if (source === config.virtualModuleName ||
188
- source === `/@id/${config.virtualModuleName}` ||
189
- source === `/@id/${config.virtualModuleName}.js`) {
187
+ if (source === `${config.virtualModuleName}.js`) {
190
188
  log("Resolving %s module", config.virtualModuleName);
191
189
  // context(justinvdm, 16 Jun 2025): Include .js extension
192
190
  // so it goes through vite processing chain
193
- return `${config.virtualModuleName}.js`;
191
+ return source;
194
192
  }
195
193
  process.env.VERBOSE && log("No resolution for id=%s", source);
196
194
  },
@@ -0,0 +1,4 @@
1
+ import { Plugin } from "vite";
2
+ export declare const customReactBuildPlugin: ({ projectRootDir, }: {
3
+ projectRootDir: string;
4
+ }) => Promise<Plugin>;
@@ -0,0 +1,61 @@
1
+ import { resolve } from "path";
2
+ import { mkdirp, copy } from "fs-extra";
3
+ import { VENDOR_DIST_DIR } from "../lib/constants.mjs";
4
+ const copyReactFiles = async (viteDistDir) => {
5
+ await mkdirp(viteDistDir);
6
+ await copy(resolve(VENDOR_DIST_DIR, "react.js"), resolve(viteDistDir, "react.js"));
7
+ await copy(resolve(VENDOR_DIST_DIR, "react.js.map"), resolve(viteDistDir, "react.js.map"));
8
+ await copy(resolve(VENDOR_DIST_DIR, "react-dom-server-edge.js"), resolve(viteDistDir, "react-dom-server-edge.js"));
9
+ await copy(resolve(VENDOR_DIST_DIR, "react-dom-server-edge.js.map"), resolve(viteDistDir, "react-dom-server-edge.js.map"));
10
+ };
11
+ export const customReactBuildPlugin = async ({ projectRootDir, }) => {
12
+ const viteDistDir = resolve(projectRootDir, "node_modules", ".vite_@redwoodjs/sdk");
13
+ await copyReactFiles(viteDistDir);
14
+ return {
15
+ name: "custom-react-build-plugin",
16
+ enforce: "pre",
17
+ applyToEnvironment: (environment) => {
18
+ return environment.name === "worker";
19
+ },
20
+ async configureServer() {
21
+ await mkdirp(viteDistDir);
22
+ await copy(resolve(VENDOR_DIST_DIR, "react.js"), resolve(viteDistDir, "react.js"));
23
+ await copy(resolve(VENDOR_DIST_DIR, "react.js.map"), resolve(viteDistDir, "react.js.map"));
24
+ await copy(resolve(VENDOR_DIST_DIR, "react-dom-server-edge.js"), resolve(viteDistDir, "react-dom-server-edge.js"));
25
+ await copy(resolve(VENDOR_DIST_DIR, "react-dom-server-edge.js.map"), resolve(viteDistDir, "react-dom-server-edge.js.map"));
26
+ },
27
+ resolveId(id) {
28
+ if (id === "react") {
29
+ return resolve(viteDistDir, "react.js");
30
+ }
31
+ if (id === "react-dom/server.edge" || id === "react-dom/server") {
32
+ return resolve(viteDistDir, "react-dom-server-edge.js");
33
+ }
34
+ },
35
+ config: () => ({
36
+ environments: {
37
+ worker: {
38
+ optimizeDeps: {
39
+ esbuildOptions: {
40
+ plugins: [
41
+ {
42
+ name: "rewrite-react-imports",
43
+ setup(build) {
44
+ build.onResolve({ filter: /^react$/ }, (args) => {
45
+ return { path: resolve(viteDistDir, "react.js") };
46
+ });
47
+ build.onResolve({ filter: /^react-dom\/server\.edge$/ }, (args) => {
48
+ return {
49
+ path: resolve(viteDistDir, "react-dom-server-edge.js"),
50
+ };
51
+ });
52
+ },
53
+ },
54
+ ],
55
+ },
56
+ },
57
+ },
58
+ },
59
+ }),
60
+ };
61
+ };
@@ -32,6 +32,22 @@ export function findSsrImportSpecifiers(id, code, log) {
32
32
  pattern: `__vite_ssr_dynamic_import__('$SPECIFIER')`,
33
33
  list: dynamicImports,
34
34
  },
35
+ {
36
+ pattern: `__vite_ssr_import__("$SPECIFIER", $$$REST)`,
37
+ list: imports,
38
+ },
39
+ {
40
+ pattern: `__vite_ssr_import__('$SPECIFIER', $$$REST)`,
41
+ list: imports,
42
+ },
43
+ {
44
+ pattern: `__vite_ssr_dynamic_import__("$SPECIFIER", $$$REST)`,
45
+ list: dynamicImports,
46
+ },
47
+ {
48
+ pattern: `__vite_ssr_dynamic_import__('$SPECIFIER', $$$REST)`,
49
+ list: dynamicImports,
50
+ },
35
51
  ];
36
52
  for (const { pattern, list } of patterns) {
37
53
  const matches = root.root().findAll(pattern);
@@ -0,0 +1,2 @@
1
+ import { type Plugin } from "vite";
2
+ export declare const injectHmrPreambleJsxPlugin: () => Plugin;
@@ -0,0 +1,22 @@
1
+ import MagicString from "magic-string";
2
+ export const injectHmrPreambleJsxPlugin = () => ({
3
+ name: "rw-sdk-inject-hmr-preamble",
4
+ apply: "serve",
5
+ async transform(code, id) {
6
+ const htmlHeadRE = /jsxDEV\("html",[^]*?jsxDEV\("head",[^]*?\[(.*?)\]/s;
7
+ const match = code.match(htmlHeadRE);
8
+ if (!match) {
9
+ return;
10
+ }
11
+ const s = new MagicString(code);
12
+ const headContentStart = match.index + match[0].lastIndexOf("[");
13
+ s.appendLeft(headContentStart + 1, `jsxDEV("script", {
14
+ type: "module",
15
+ src: "/__vite_preamble__",
16
+ }),`);
17
+ return {
18
+ code: s.toString(),
19
+ map: s.generateMap(),
20
+ };
21
+ },
22
+ });
@@ -32,13 +32,50 @@ const hasEntryAsAncestor = (module, entryFile, seen = new Set()) => {
32
32
  }
33
33
  return false;
34
34
  };
35
+ const isInUseClientGraph = ({ file, clientFiles, server, }) => {
36
+ const id = normalizeModulePath(server.config.root, file);
37
+ if (clientFiles.has(id)) {
38
+ return true;
39
+ }
40
+ const modules = server.environments.client.moduleGraph.getModulesByFile(file);
41
+ if (!modules) {
42
+ return false;
43
+ }
44
+ for (const m of modules) {
45
+ for (const importer of m.importers) {
46
+ if (importer.file &&
47
+ isInUseClientGraph({ file: importer.file, clientFiles, server })) {
48
+ return true;
49
+ }
50
+ }
51
+ }
52
+ return false;
53
+ };
35
54
  export const miniflareHMRPlugin = (givenOptions) => [
36
55
  {
37
56
  name: "rwsdk:miniflare-hmr",
38
57
  async hotUpdate(ctx) {
39
58
  const { clientFiles, serverFiles, viteEnvironment: { name: environment }, workerEntryPathname: entry, } = givenOptions;
40
59
  if (process.env.VERBOSE) {
41
- this.environment.logger.info(`Hot update: (env=${this.environment.name}) ${ctx.file}\nModule graph:\n\n${dumpFullModuleGraph(ctx.server, this.environment.name)}`);
60
+ log(`Hot update: (env=${this.environment.name}) ${ctx.file}\nModule graph:\n\n${dumpFullModuleGraph(ctx.server, this.environment.name)}`);
61
+ }
62
+ if (!isJsFile(ctx.file) && !ctx.file.endsWith(".css")) {
63
+ return;
64
+ }
65
+ if (this.environment.name === "ssr") {
66
+ log("SSR update, invalidating recursively", ctx.file);
67
+ const isUseClientUpdate = isInUseClientGraph({
68
+ file: ctx.file,
69
+ clientFiles,
70
+ server: ctx.server,
71
+ });
72
+ if (!isUseClientUpdate) {
73
+ return [];
74
+ }
75
+ invalidateModule(ctx.server, "ssr", ctx.file);
76
+ invalidateModule(ctx.server, environment, VIRTUAL_SSR_PREFIX +
77
+ normalizeModulePath(givenOptions.rootDir, ctx.file));
78
+ return [];
42
79
  }
43
80
  if (!["client", environment].includes(this.environment.name)) {
44
81
  return [];
@@ -77,33 +114,34 @@ export const miniflareHMRPlugin = (givenOptions) => [
77
114
  }
78
115
  // todo(justinvdm, 12 Dec 2024): Skip client references
79
116
  const modules = Array.from(ctx.server.environments[environment].moduleGraph.getModulesByFile(ctx.file) ?? []);
80
- const isWorkerUpdate = ctx.file === entry ||
81
- modules.some((module) => hasEntryAsAncestor(module, entry));
82
- // The worker doesnt need an update
83
- // => Short circuit HMR
84
- if (!isWorkerUpdate) {
85
- return [];
86
- }
117
+ const isWorkerUpdate = Boolean(modules);
87
118
  // The worker needs an update, but this is the client environment
88
119
  // => Notify for HMR update of any css files imported by in worker, that are also in the client module graph
89
120
  // Why: There may have been changes to css classes referenced, which might css modules to change
90
121
  if (this.environment.name === "client") {
91
- for (const [_, module] of ctx.server.environments[environment]
92
- .moduleGraph.idToModuleMap) {
93
- // todo(justinvdm, 13 Dec 2024): We check+update _all_ css files in worker module graph,
94
- // but it could just be a subset of css files that are actually affected, depending
95
- // on the importers and imports of the changed file. We should be smarter about this.
96
- if (module.file && module.file.endsWith(".css")) {
97
- const clientModules = ctx.server.environments.client.moduleGraph.getModulesByFile(module.file);
98
- for (const clientModule of clientModules ?? []) {
99
- invalidateModule(ctx.server, "client", clientModule);
122
+ if (isWorkerUpdate) {
123
+ for (const [_, module] of ctx.server.environments[environment]
124
+ .moduleGraph.idToModuleMap) {
125
+ // todo(justinvdm, 13 Dec 2024): We check+update _all_ css files in worker module graph,
126
+ // but it could just be a subset of css files that are actually affected, depending
127
+ // on the importers and imports of the changed file. We should be smarter about this.
128
+ if (module.file && module.file.endsWith(".css")) {
129
+ const clientModules = ctx.server.environments.client.moduleGraph.getModulesByFile(module.file);
130
+ for (const clientModule of clientModules ?? []) {
131
+ invalidateModule(ctx.server, "client", clientModule);
132
+ }
100
133
  }
101
134
  }
102
135
  }
103
- // context(justinvdm, 10 Jul 2025): If this isn't a file with a client
104
- // directive or a css file, we shouldn't invalidate anything else to
105
- // avoid full page reload
106
- return hasClientDirective || ctx.file.endsWith(".css") ? undefined : [];
136
+ const isUseClientUpdate = isInUseClientGraph({
137
+ file: ctx.file,
138
+ clientFiles,
139
+ server: ctx.server,
140
+ });
141
+ if (!isUseClientUpdate && !ctx.file.endsWith(".css")) {
142
+ return [];
143
+ }
144
+ return ctx.modules;
107
145
  }
108
146
  // The worker needs an update, and the hot check is for the worker environment
109
147
  // => Notify for custom RSC-based HMR update, then short circuit HMR
@@ -0,0 +1,9 @@
1
+ import { Plugin } from "vite";
2
+ import { cloudflare } from "@cloudflare/vite-plugin";
3
+ type BasePluginOptions = Parameters<typeof cloudflare>[0];
4
+ type MiniflarePluginOptions = BasePluginOptions & {};
5
+ export declare const miniflarePlugin: (givenOptions: MiniflarePluginOptions & {
6
+ rootDir: string;
7
+ workerEntryPathname: string;
8
+ }) => (Plugin | Plugin[])[];
9
+ export {};