rwsdk 0.3.3 → 0.3.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.
@@ -13,7 +13,7 @@ export async function buildApp({ builder, clientEntryPoints, clientFiles, server
13
13
  const workerEnv = builder.environments.worker;
14
14
  await runDirectivesScan({
15
15
  rootConfig: builder.config,
16
- environment: workerEnv,
16
+ environments: builder.environments,
17
17
  clientFiles,
18
18
  serverFiles,
19
19
  });
@@ -1,4 +1,5 @@
1
1
  import resolve, { ResolveOptions } from "enhanced-resolve";
2
2
  import { ResolvedConfig } from "vite";
3
+ import { Environment } from "vite";
3
4
  export declare const mapViteResolveToEnhancedResolveOptions: (viteConfig: ResolvedConfig, envName: string) => ResolveOptions;
4
- export declare const createViteAwareResolver: (viteConfig: ResolvedConfig, envName: string, environment?: any) => resolve.ResolveFunctionAsync;
5
+ export declare const createViteAwareResolver: (viteConfig: ResolvedConfig, environment: Environment) => resolve.ResolveFunctionAsync;
@@ -238,8 +238,8 @@ export const mapViteResolveToEnhancedResolveOptions = (viteConfig, envName) => {
238
238
  };
239
239
  return baseOptions;
240
240
  };
241
- export const createViteAwareResolver = (viteConfig, envName, environment) => {
242
- const baseOptions = mapViteResolveToEnhancedResolveOptions(viteConfig, envName);
241
+ export const createViteAwareResolver = (viteConfig, environment) => {
242
+ const baseOptions = mapViteResolveToEnhancedResolveOptions(viteConfig, environment.name);
243
243
  // Add Vite plugin resolver if environment is provided
244
244
  const plugins = environment
245
245
  ? [new VitePluginResolverPlugin(environment)]
@@ -34,7 +34,7 @@ export const directiveModulesDevPlugin = ({ clientFiles, serverFiles, projectRoo
34
34
  // We don't await it here, allowing Vite to continue its startup.
35
35
  scanPromise = runDirectivesScan({
36
36
  rootConfig: server.config,
37
- environment: server.environments.worker,
37
+ environments: server.environments,
38
38
  clientFiles,
39
39
  serverFiles,
40
40
  }).then(() => {
@@ -1,7 +1,7 @@
1
1
  import { Environment, ResolvedConfig } from "vite";
2
- export declare const runDirectivesScan: ({ rootConfig, environment, clientFiles, serverFiles, }: {
2
+ export declare const runDirectivesScan: ({ rootConfig, environments, clientFiles, serverFiles, }: {
3
3
  rootConfig: ResolvedConfig;
4
- environment: Environment;
4
+ environments: Record<string, Environment>;
5
5
  clientFiles: Set<string>;
6
6
  serverFiles: Set<string>;
7
7
  }) => Promise<void>;
@@ -5,8 +5,7 @@ import debug from "debug";
5
5
  import { getViteEsbuild } from "./getViteEsbuild.mjs";
6
6
  import { normalizeModulePath } from "../lib/normalizeModulePath.mjs";
7
7
  import { externalModules } from "./constants.mjs";
8
- import { createViteAwareResolver, mapViteResolveToEnhancedResolveOptions, } from "./createViteAwareResolver.mjs";
9
- import resolve from "enhanced-resolve";
8
+ import { createViteAwareResolver, } from "./createViteAwareResolver.mjs";
10
9
  const log = debug("rwsdk:vite:run-directives-scan");
11
10
  // Copied from Vite's source code.
12
11
  // https://github.com/vitejs/vite/blob/main/packages/vite/src/shared/utils.ts
@@ -15,13 +14,13 @@ const isObject = (value) => Object.prototype.toString.call(value) === "[object O
15
14
  // https://github.com/vitejs/vite/blob/main/packages/vite/src/node/utils.ts
16
15
  const externalRE = /^(https?:)?\/\//;
17
16
  const isExternalUrl = (url) => externalRE.test(url);
18
- export const runDirectivesScan = async ({ rootConfig, environment, clientFiles, serverFiles, }) => {
17
+ export const runDirectivesScan = async ({ rootConfig, environments, clientFiles, serverFiles, }) => {
19
18
  console.log("\nšŸ” Scanning for 'use client' and 'use server' directives...");
20
19
  // Set environment variable to indicate scanning is in progress
21
20
  process.env.RWSDK_DIRECTIVE_SCAN_ACTIVE = "true";
22
21
  try {
23
22
  const esbuild = await getViteEsbuild(rootConfig.root);
24
- const input = environment.config.build.rollupOptions?.input;
23
+ const input = environments.worker.config.build.rollupOptions?.input;
25
24
  let entries;
26
25
  if (Array.isArray(input)) {
27
26
  entries = input;
@@ -36,20 +35,13 @@ export const runDirectivesScan = async ({ rootConfig, environment, clientFiles,
36
35
  entries = [];
37
36
  }
38
37
  if (entries.length === 0) {
39
- log("No entries found for directives scan in environment '%s', skipping.", environment.name);
38
+ log("No entries found for directives scan in worker environment, skipping.");
40
39
  return;
41
40
  }
42
41
  const absoluteEntries = entries.map((entry) => path.resolve(rootConfig.root, entry));
43
- log("Starting directives scan for environment '%s' with entries:", environment.name, absoluteEntries);
44
- // Use enhanced-resolve with Vite plugin integration for full compatibility
45
- const workerResolver = createViteAwareResolver(rootConfig, environment.name, environment);
46
- // Create a client resolver with browser conditions
47
- // We'll use the mapViteResolveToEnhancedResolveOptions function directly
48
- // to create a resolver with browser conditions
49
- const clientResolveOptions = mapViteResolveToEnhancedResolveOptions(rootConfig, environment.name);
50
- // Override the conditions to use browser conditions for client resolution
51
- clientResolveOptions.conditionNames = ["browser", "module"];
52
- const clientResolver = resolve.create(clientResolveOptions);
42
+ log("Starting directives scan for worker environment with entries:", absoluteEntries);
43
+ const workerResolver = createViteAwareResolver(rootConfig, environments.worker);
44
+ const clientResolver = createViteAwareResolver(rootConfig, environments.client);
53
45
  const moduleEnvironments = new Map();
54
46
  const fileContentCache = new Map();
55
47
  const readFileWithCache = async (path) => {
@@ -153,8 +145,6 @@ export const runDirectivesScan = async ({ rootConfig, environment, clientFiles,
153
145
  });
154
146
  build.onLoad({ filter: /\.(m|c)?[jt]sx?$/ }, async (args) => {
155
147
  log("onLoad called for:", args.path);
156
- const inheritedEnv = args.pluginData?.inheritedEnv || "worker";
157
- log("Inherited environment for", args.path, "is", inheritedEnv);
158
148
  if (!args.path.startsWith("/") ||
159
149
  args.path.includes("virtual:") ||
160
150
  isExternalUrl(args.path)) {
@@ -167,12 +157,28 @@ export const runDirectivesScan = async ({ rootConfig, environment, clientFiles,
167
157
  }
168
158
  try {
169
159
  const contents = await readFileWithCache(args.path);
170
- // The environment is determined in onResolve. Here we just populate the output sets.
171
- if (hasDirective(contents, "use client")) {
160
+ 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
+ }
173
+ // 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);
176
+ // Finally, populate the output sets if the file has a directive.
177
+ if (isClient) {
172
178
  log("Discovered 'use client' in:", args.path);
173
179
  clientFiles.add(normalizeModulePath(args.path, rootConfig.root));
174
180
  }
175
- if (hasDirective(contents, "use server")) {
181
+ if (isServer) {
176
182
  log("Discovered 'use server' in:", args.path);
177
183
  serverFiles.add(normalizeModulePath(args.path, rootConfig.root));
178
184
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rwsdk",
3
- "version": "0.3.3",
3
+ "version": "0.3.5",
4
4
  "description": "Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime",
5
5
  "type": "module",
6
6
  "bin": {