rwsdk 0.1.15 → 0.1.17-test.20250715200658

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.
@@ -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`);
@@ -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
  },
@@ -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);
@@ -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
@@ -93,22 +93,35 @@ export const ssrBridgePlugin = ({ clientFiles, serverFiles, }) => {
93
93
  process.env.VERBOSE &&
94
94
  log("Fetch module result: id=%s, result=%O", realId, result);
95
95
  const code = "code" in result ? result.code : undefined;
96
+ if (realId.endsWith(".css")) {
97
+ process.env.VERBOSE &&
98
+ log("Not a JS file, returning code: %s", code);
99
+ return code ?? "";
100
+ }
96
101
  log("Fetched SSR module code length: %d", code?.length || 0);
97
102
  const { imports, dynamicImports } = findSsrImportSpecifiers(realId, code || "", log);
98
- const allSpecifiers = [...new Set([...imports, ...dynamicImports])];
103
+ const allSpecifiers = [
104
+ ...new Set([...imports, ...dynamicImports]),
105
+ ].map((id) => id.startsWith("/@id/") ? id.slice("/@id/".length) : id);
99
106
  const switchCases = allSpecifiers
100
- .map((specifier) => ` case "${specifier}": return import("${VIRTUAL_SSR_PREFIX}${specifier}");`)
107
+ .map((specifier) => ` case "${specifier}": void import("${VIRTUAL_SSR_PREFIX}${specifier}");`)
101
108
  .join("\n");
102
109
  const transformedCode = `
103
110
  await (async function(__vite_ssr_import__, __vite_ssr_dynamic_import__) {${code}})(
104
- (id, ...args) => {ssrImport(id); return __vite_ssr_import__('/@id/${VIRTUAL_SSR_PREFIX}' + id, ...args);},
105
- (id, ...args) => {ssrImport(id); return __vite_ssr_dynamic_import__('/@id/${VIRTUAL_SSR_PREFIX}' + id, ...args);}
111
+ __ssrImport.bind(null, false),
112
+ __ssrImport.bind(null, true)
106
113
  );
107
114
 
108
- function ssrImport(id) {
115
+ function __ssrImport(isDynamic, id, ...args) {
116
+ id = id.startsWith('/@id/') ? id.slice('/@id/'.length) : id;
117
+
109
118
  switch (id) {
110
119
  ${switchCases}
111
120
  }
121
+
122
+ return isDynamic
123
+ ? __vite_ssr_dynamic_import__("/@id/${VIRTUAL_SSR_PREFIX}" + id, ...args)
124
+ : __vite_ssr_import__("/@id/${VIRTUAL_SSR_PREFIX}" + id, ...args);
112
125
  }
113
126
  `;
114
127
  log("Transformed SSR module code length: %d", transformedCode.length);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rwsdk",
3
- "version": "0.1.15",
3
+ "version": "0.1.17-test.20250715200658",
4
4
  "description": "Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime",
5
5
  "type": "module",
6
6
  "bin": {
@@ -1,10 +0,0 @@
1
- import { SqlToTsType, ExecutedBuilder, Prettify } from "../utils";
2
- import { ColumnDefinitionBuilder } from "./columnDefinition";
3
- export interface TableBuilder<TName extends string, TSchema extends Record<string, any> = {}> {
4
- readonly __tableName: TName;
5
- readonly __schema: TSchema;
6
- addColumn<K extends string, T extends string>(name: K, type: T, build?: (col: ColumnDefinitionBuilder<SqlToTsType<T>>) => ColumnDefinitionBuilder<SqlToTsType<T>>): TableBuilder<TName, Prettify<TSchema & Record<K, SqlToTsType<T>>>>;
7
- temporary(): this;
8
- ifNotExists(): this;
9
- execute(): Promise<ExecutedBuilder<this>>;
10
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,2 +0,0 @@
1
- import type { ViteDevServer } from "vite";
2
- export declare const invalidateClientModule: (devServer: ViteDevServer, filePath: string) => void;
@@ -1,8 +0,0 @@
1
- import { invalidateModule } from "./invalidateModule.mjs";
2
- import { normalizeModulePath } from "./normalizeModulePath.mjs";
3
- import { VIRTUAL_SSR_PREFIX } from "./ssrBridgePlugin.mjs";
4
- export const invalidateClientModule = (devServer, filePath) => {
5
- invalidateModule(devServer, "ssr", filePath);
6
- invalidateModule(devServer, "client", filePath);
7
- invalidateModule(devServer, "worker", VIRTUAL_SSR_PREFIX + normalizeModulePath(devServer.config.root, filePath));
8
- };
@@ -1,2 +0,0 @@
1
- import type { ViteDevServer } from "vite";
2
- export declare const invalidateModule: (devServer: ViteDevServer, environment: string, id: string) => void;
@@ -1,14 +0,0 @@
1
- import debug from "debug";
2
- const log = debug("rwsdk:vite:invalidate-module");
3
- const verboseLog = debug("verbose:rwsdk:vite:invalidate-module");
4
- export const invalidateModule = (devServer, environment, id) => {
5
- const [rawId, _query] = id.split("?");
6
- log("Invalidating module: id=%s, environment=%s", id, environment);
7
- const moduleNode = devServer?.environments[environment]?.moduleGraph.idToModuleMap.get(rawId);
8
- if (moduleNode) {
9
- devServer?.environments[environment]?.moduleGraph.invalidateModule(moduleNode);
10
- }
11
- else {
12
- verboseLog("Module not found: id=%s, environment=%s", id, environment);
13
- }
14
- };
@@ -1,2 +0,0 @@
1
- import type { ViteDevServer } from "vite";
2
- export declare const invalidateSSRModule: (devServer: ViteDevServer, filePath: string) => void;
@@ -1,7 +0,0 @@
1
- import { invalidateModule } from "./invalidateModule.mjs";
2
- import { normalizeModulePath } from "./normalizeModulePath.mjs";
3
- import { VIRTUAL_SSR_PREFIX } from "./ssrBridgePlugin.mjs";
4
- export const invalidateSSRModule = (devServer, filePath) => {
5
- invalidateModule(devServer, "ssr", filePath);
6
- invalidateModule(devServer, "worker", VIRTUAL_SSR_PREFIX + normalizeModulePath(devServer.config.root, filePath));
7
- };
@@ -1 +0,0 @@
1
- export declare function isJsFile(filepath: string): boolean;
@@ -1,3 +0,0 @@
1
- export function isJsFile(filepath) {
2
- return /\.(m|c)?(j|t)s(x)?$/.test(filepath);
3
- }
@@ -1,5 +0,0 @@
1
- import { ConfigEnv } from "vite";
2
- type Mode = "development" | "production";
3
- export declare const resolveMode: (env: ConfigEnv) => Mode;
4
- export declare const ensureMode: () => Mode;
5
- export {};
@@ -1,25 +0,0 @@
1
- import debug from "debug";
2
- const log = debug("rwsdk:vite:mode");
3
- const initialNodeEnv = process.env.NODE_ENV;
4
- let mode;
5
- export const resolveMode = (env) => {
6
- if (mode) {
7
- return mode;
8
- }
9
- if (initialNodeEnv) {
10
- mode = initialNodeEnv === "development" ? "development" : "production";
11
- log(`Resolved mode: %s (from initial NODE_ENV=%s)`, mode, initialNodeEnv);
12
- }
13
- else {
14
- mode =
15
- env.command === "serve" || env.isPreview ? "development" : "production";
16
- log(`Resolved mode: %s (env.command=%s, env.isPreview=%s)`, mode, env.isPreview, env.command);
17
- }
18
- return mode;
19
- };
20
- export const ensureMode = () => {
21
- if (mode === undefined) {
22
- throw new Error("RedwoodSDK: mode is not resolved yet");
23
- }
24
- return mode;
25
- };
@@ -1,2 +0,0 @@
1
- import { Plugin } from "vite";
2
- export declare function modePlugin(): Plugin;
@@ -1,10 +0,0 @@
1
- import { resolveMode } from "./mode.mjs";
2
- export function modePlugin() {
3
- return {
4
- name: "rwsdk:mode",
5
- enforce: "pre",
6
- config(_, env) {
7
- resolveMode(env);
8
- },
9
- };
10
- }