rwsdk 1.0.0-alpha.3 → 1.0.0-alpha.5

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 (72) hide show
  1. package/dist/lib/getShortName.mjs +6 -1
  2. package/dist/lib/getShortName.test.mjs +25 -0
  3. package/dist/lib/hasPkgScript.d.mts +4 -1
  4. package/dist/lib/hasPkgScript.mjs +9 -6
  5. package/dist/lib/hasPkgScript.test.d.mts +1 -0
  6. package/dist/lib/hasPkgScript.test.mjs +33 -0
  7. package/dist/lib/jsonUtils.mjs +3 -0
  8. package/dist/lib/jsonUtils.test.d.mts +1 -0
  9. package/dist/lib/jsonUtils.test.mjs +90 -0
  10. package/dist/lib/normalizeModulePath.d.mts +5 -0
  11. package/dist/lib/normalizeModulePath.mjs +1 -1
  12. package/dist/lib/normalizeModulePath.test.d.mts +1 -0
  13. package/dist/lib/{normalizeModulePath.test.js → normalizeModulePath.test.mjs} +20 -1
  14. package/dist/runtime/lib/memoizeOnId.test.d.ts +1 -0
  15. package/dist/runtime/lib/memoizeOnId.test.js +49 -0
  16. package/dist/runtime/lib/realtime/protocol.test.d.ts +1 -0
  17. package/dist/runtime/lib/realtime/protocol.test.js +107 -0
  18. package/dist/runtime/lib/realtime/shared.test.d.ts +1 -0
  19. package/dist/runtime/lib/realtime/shared.test.js +18 -0
  20. package/dist/runtime/lib/realtime/validateUpgradeRequest.test.d.ts +1 -0
  21. package/dist/runtime/lib/realtime/validateUpgradeRequest.test.js +66 -0
  22. package/dist/runtime/lib/router.test.js +1 -1
  23. package/dist/runtime/lib/turnstile/verifyTurnstileToken.d.ts +2 -1
  24. package/dist/runtime/lib/turnstile/verifyTurnstileToken.js +6 -6
  25. package/dist/runtime/lib/turnstile/verifyTurnstileToken.test.d.ts +1 -0
  26. package/dist/runtime/lib/turnstile/verifyTurnstileToken.test.js +49 -0
  27. package/dist/vite/checkIsUsingPrisma.d.mts +4 -0
  28. package/dist/vite/checkIsUsingPrisma.mjs +2 -2
  29. package/dist/vite/checkIsUsingPrisma.test.d.mts +1 -0
  30. package/dist/vite/checkIsUsingPrisma.test.mjs +30 -0
  31. package/dist/vite/configPlugin.mjs +22 -0
  32. package/dist/vite/createDirectiveLookupPlugin.d.mts +9 -0
  33. package/dist/vite/createDirectiveLookupPlugin.mjs +33 -29
  34. package/dist/vite/createDirectiveLookupPlugin.test.d.mts +1 -0
  35. package/dist/vite/createDirectiveLookupPlugin.test.mjs +40 -0
  36. package/dist/vite/directiveModulesDevPlugin.d.mts +2 -0
  37. package/dist/vite/directiveModulesDevPlugin.mjs +3 -3
  38. package/dist/vite/directiveModulesDevPlugin.test.d.mts +1 -0
  39. package/dist/vite/directiveModulesDevPlugin.test.mjs +59 -0
  40. package/dist/vite/directivesPlugin.d.mts +1 -0
  41. package/dist/vite/directivesPlugin.mjs +1 -1
  42. package/dist/vite/directivesPlugin.test.d.mts +1 -0
  43. package/dist/vite/directivesPlugin.test.mjs +24 -0
  44. package/dist/vite/ensureAliasArray.test.d.mts +1 -0
  45. package/dist/vite/ensureAliasArray.test.mjs +71 -0
  46. package/dist/vite/findSpecifiers.mjs +2 -1
  47. package/dist/vite/findSpecifiers.test.d.mts +1 -0
  48. package/dist/vite/findSpecifiers.test.mjs +202 -0
  49. package/dist/vite/findSsrSpecifiers.test.d.mts +1 -0
  50. package/dist/vite/findSsrSpecifiers.test.mjs +99 -0
  51. package/dist/vite/hasDirective.test.d.mts +1 -0
  52. package/dist/vite/hasDirective.test.mjs +109 -0
  53. package/dist/vite/isJsFile.test.d.mts +1 -0
  54. package/dist/vite/isJsFile.test.mjs +38 -0
  55. package/dist/vite/linkerPlugin.d.mts +8 -0
  56. package/dist/vite/linkerPlugin.mjs +30 -22
  57. package/dist/vite/linkerPlugin.test.d.mts +1 -0
  58. package/dist/vite/linkerPlugin.test.mjs +41 -0
  59. package/dist/vite/miniflareHMRPlugin.d.mts +5 -0
  60. package/dist/vite/miniflareHMRPlugin.mjs +2 -2
  61. package/dist/vite/miniflareHMRPlugin.test.d.mts +1 -0
  62. package/dist/vite/miniflareHMRPlugin.test.mjs +42 -0
  63. package/dist/vite/redwoodPlugin.d.mts +7 -0
  64. package/dist/vite/redwoodPlugin.mjs +7 -3
  65. package/dist/vite/redwoodPlugin.test.d.mts +1 -0
  66. package/dist/vite/redwoodPlugin.test.mjs +34 -0
  67. package/dist/vite/runDirectivesScan.d.mts +19 -0
  68. package/dist/vite/runDirectivesScan.mjs +52 -44
  69. package/dist/vite/runDirectivesScan.test.d.mts +1 -0
  70. package/dist/vite/runDirectivesScan.test.mjs +73 -0
  71. package/package.json +1 -1
  72. /package/dist/lib/{normalizeModulePath.test.d.ts → getShortName.test.d.mts} +0 -0
@@ -14,6 +14,40 @@ const isObject = (value) => Object.prototype.toString.call(value) === "[object O
14
14
  // https://github.com/vitejs/vite/blob/main/packages/vite/src/node/utils.ts
15
15
  const externalRE = /^(https?:)?\/\//;
16
16
  const isExternalUrl = (url) => externalRE.test(url);
17
+ export async function resolveModuleWithEnvironment({ path, importer, importerEnv, clientResolver, workerResolver, }) {
18
+ const resolver = importerEnv === "client" ? clientResolver : workerResolver;
19
+ return new Promise((resolvePromise) => {
20
+ resolver({}, importer || "", path, {}, (err, result) => {
21
+ if (!err && result) {
22
+ resolvePromise({ id: result });
23
+ }
24
+ else {
25
+ if (err) {
26
+ const errorMessage = err.message || String(err);
27
+ if (errorMessage.includes("Package path . is not exported")) {
28
+ log("Package exports error for %s, marking as external", path);
29
+ }
30
+ else {
31
+ log("Resolution failed for %s: %s", path, errorMessage);
32
+ }
33
+ }
34
+ resolvePromise(null);
35
+ }
36
+ });
37
+ });
38
+ }
39
+ export function classifyModule({ contents, inheritedEnv, }) {
40
+ let moduleEnv = inheritedEnv;
41
+ const isClient = hasDirective(contents, "use client");
42
+ const isServer = hasDirective(contents, "use server");
43
+ if (isClient) {
44
+ moduleEnv = "client";
45
+ }
46
+ else if (isServer) {
47
+ moduleEnv = "worker";
48
+ }
49
+ return { moduleEnv, isClient, isServer };
50
+ }
17
51
  export const runDirectivesScan = async ({ rootConfig, environments, clientFiles, serverFiles, }) => {
18
52
  console.log("\n🔍 Scanning for 'use client' and 'use server' directives...");
19
53
  // Set environment variable to indicate scanning is in progress
@@ -87,17 +121,12 @@ export const runDirectivesScan = async ({ rootConfig, environments, clientFiles,
87
121
  /\.(m|c)?[jt]sx?$/.test(args.importer)) {
88
122
  try {
89
123
  const importerContents = await readFileWithCache(args.importer);
90
- if (hasDirective(importerContents, "use client")) {
91
- importerEnv = "client";
92
- log("Pre-detected importer 'use client' in:", args.importer);
93
- }
94
- else if (hasDirective(importerContents, "use server")) {
95
- importerEnv = "worker";
96
- log("Pre-detected importer 'use server' in:", args.importer);
97
- }
98
- else {
99
- importerEnv = "worker"; // Default for entry points
100
- }
124
+ const classification = classifyModule({
125
+ contents: importerContents,
126
+ inheritedEnv: "worker", // Default for entry points
127
+ });
128
+ importerEnv = classification.moduleEnv;
129
+ log("Pre-detected importer environment in:", args.importer, "as", importerEnv);
101
130
  moduleEnvironments.set(args.importer, importerEnv);
102
131
  }
103
132
  catch (e) {
@@ -109,25 +138,12 @@ export const runDirectivesScan = async ({ rootConfig, environments, clientFiles,
109
138
  importerEnv = "worker"; // Default for entry points or non-script files
110
139
  }
111
140
  log("Importer:", args.importer, "environment:", importerEnv);
112
- const resolver = importerEnv === "client" ? clientResolver : workerResolver;
113
- const resolved = await new Promise((resolve) => {
114
- resolver({}, args.importer || rootConfig.root, args.path, {}, (err, result) => {
115
- if (!err && result) {
116
- resolve({ id: result });
117
- }
118
- else {
119
- if (err) {
120
- const errorMessage = err.message || String(err);
121
- if (errorMessage.includes("Package path . is not exported")) {
122
- log("Package exports error for %s, marking as external", args.path);
123
- }
124
- else {
125
- log("Resolution failed for %s: %s", args.path, errorMessage);
126
- }
127
- }
128
- resolve(null);
129
- }
130
- });
141
+ const resolved = await resolveModuleWithEnvironment({
142
+ path: args.path,
143
+ importer: args.importer,
144
+ importerEnv,
145
+ clientResolver,
146
+ workerResolver,
131
147
  });
132
148
  log("Resolution result:", resolved);
133
149
  const resolvedPath = resolved?.id;
@@ -158,21 +174,13 @@ export const runDirectivesScan = async ({ rootConfig, environments, clientFiles,
158
174
  try {
159
175
  const contents = await readFileWithCache(args.path);
160
176
  const inheritedEnv = args.pluginData?.inheritedEnv || "worker";
161
- let currentEnv = inheritedEnv;
162
- const isClient = hasDirective(contents, "use client");
163
- const isServer = hasDirective(contents, "use server");
164
- // A file's own directive takes precedence over the inherited environment.
165
- if (isClient) {
166
- currentEnv = "client";
167
- }
168
- else if (isServer) {
169
- // `else if` handles cases where a file might have both directives.
170
- // "use client" takes precedence.
171
- currentEnv = "worker";
172
- }
177
+ const { moduleEnv, isClient, isServer } = classifyModule({
178
+ contents,
179
+ inheritedEnv,
180
+ });
173
181
  // Store the definitive environment for this module, so it can be used when it becomes an importer.
174
- moduleEnvironments.set(args.path, currentEnv);
175
- log("Set environment for", args.path, "to", currentEnv);
182
+ moduleEnvironments.set(args.path, moduleEnv);
183
+ log("Set environment for", args.path, "to", moduleEnv);
176
184
  // Finally, populate the output sets if the file has a directive.
177
185
  if (isClient) {
178
186
  log("Discovered 'use client' in:", args.path);
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,73 @@
1
+ import { describe, it, expect, vi } from "vitest";
2
+ import { resolveModuleWithEnvironment, classifyModule, } from "./runDirectivesScan.mjs";
3
+ describe("runDirectivesScan helpers", () => {
4
+ describe("resolveModuleWithEnvironment", () => {
5
+ it("should use the client resolver when importerEnv is 'client'", async () => {
6
+ const clientResolver = vi.fn((_a, _b, _c, _d, cb) => cb(null, "/resolved/client"));
7
+ const workerResolver = vi.fn();
8
+ const result = await resolveModuleWithEnvironment({
9
+ path: "test-path",
10
+ importerEnv: "client",
11
+ clientResolver,
12
+ workerResolver,
13
+ });
14
+ expect(clientResolver).toHaveBeenCalled();
15
+ expect(workerResolver).not.toHaveBeenCalled();
16
+ expect(result).toEqual({ id: "/resolved/client" });
17
+ });
18
+ it("should use the worker resolver when importerEnv is 'worker'", async () => {
19
+ const clientResolver = vi.fn();
20
+ const workerResolver = vi.fn((_a, _b, _c, _d, cb) => cb(null, "/resolved/worker"));
21
+ const result = await resolveModuleWithEnvironment({
22
+ path: "test-path",
23
+ importerEnv: "worker",
24
+ clientResolver,
25
+ workerResolver,
26
+ });
27
+ expect(workerResolver).toHaveBeenCalled();
28
+ expect(clientResolver).not.toHaveBeenCalled();
29
+ expect(result).toEqual({ id: "/resolved/worker" });
30
+ });
31
+ it("should return null on resolution error", async () => {
32
+ const clientResolver = vi.fn((_a, _b, _c, _d, cb) => cb(new Error("Resolution failed")));
33
+ const workerResolver = vi.fn();
34
+ const result = await resolveModuleWithEnvironment({
35
+ path: "test-path",
36
+ importerEnv: "client",
37
+ clientResolver,
38
+ workerResolver,
39
+ });
40
+ expect(result).toBeNull();
41
+ });
42
+ });
43
+ describe("classifyModule", () => {
44
+ it("should return 'client' if 'use client' directive is present", () => {
45
+ const contents = `'use client';\nconsole.log('hello');`;
46
+ const result = classifyModule({ contents, inheritedEnv: "worker" });
47
+ expect(result.moduleEnv).toBe("client");
48
+ expect(result.isClient).toBe(true);
49
+ expect(result.isServer).toBe(false);
50
+ });
51
+ it("should return 'worker' if 'use server' directive is present", () => {
52
+ const contents = `"use server";\nexport default () => {};`;
53
+ const result = classifyModule({ contents, inheritedEnv: "client" });
54
+ expect(result.moduleEnv).toBe("worker");
55
+ expect(result.isClient).toBe(false);
56
+ expect(result.isServer).toBe(true);
57
+ });
58
+ it("should prioritize 'use client' over 'use server'", () => {
59
+ const contents = `'use client';\n'use server';\nconsole.log('hello');`;
60
+ const result = classifyModule({ contents, inheritedEnv: "worker" });
61
+ expect(result.moduleEnv).toBe("client");
62
+ expect(result.isClient).toBe(true);
63
+ expect(result.isServer).toBe(false);
64
+ });
65
+ it("should return the inherited environment if no directive is present", () => {
66
+ const contents = `console.log('no directive');`;
67
+ const result = classifyModule({ contents, inheritedEnv: "worker" });
68
+ expect(result.moduleEnv).toBe("worker");
69
+ expect(result.isClient).toBe(false);
70
+ expect(result.isServer).toBe(false);
71
+ });
72
+ });
73
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rwsdk",
3
- "version": "1.0.0-alpha.3",
3
+ "version": "1.0.0-alpha.5",
4
4
  "description": "Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime",
5
5
  "type": "module",
6
6
  "bin": {