rwsdk 1.0.0-beta.9 → 1.0.0-beta.9-test.20251006190822

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
  // @ts-ignore
2
2
  import { compile } from "@mdx-js/mdx";
3
3
  import debug from "debug";
4
+ import { glob } from "glob";
4
5
  import fsp from "node:fs/promises";
5
6
  import path from "node:path";
6
7
  import { INTERMEDIATES_OUTPUT_DIR } from "../lib/constants.mjs";
@@ -17,6 +18,40 @@ const isObject = (value) => Object.prototype.toString.call(value) === "[object O
17
18
  // https://github.com/vitejs/vite/blob/main/packages/vite/src/node/utils.ts
18
19
  const externalRE = /^(https?:)?\/\//;
19
20
  const isExternalUrl = (url) => externalRE.test(url);
21
+ async function findDirectiveRoots({ root, readFileWithCache, directiveCheckCache, }) {
22
+ const srcDir = path.resolve(root, "src");
23
+ console.log("########", srcDir);
24
+ const files = await glob("**/*.{ts,tsx,js,jsx,mjs,mts,cjs,cts,mdx}", {
25
+ cwd: srcDir,
26
+ absolute: true,
27
+ });
28
+ const directiveFiles = new Set();
29
+ for (const file of files) {
30
+ if (directiveCheckCache.has(file)) {
31
+ if (directiveCheckCache.get(file)) {
32
+ directiveFiles.add(file);
33
+ }
34
+ continue;
35
+ }
36
+ try {
37
+ const content = await readFileWithCache(file);
38
+ const hasClient = hasDirective(content, "use client");
39
+ const hasServer = hasDirective(content, "use server");
40
+ const hasAnyDirective = hasClient || hasServer;
41
+ directiveCheckCache.set(file, hasAnyDirective);
42
+ if (hasAnyDirective) {
43
+ directiveFiles.add(file);
44
+ }
45
+ }
46
+ catch (e) {
47
+ log("Could not read file during pre-scan, skipping:", file);
48
+ // Cache the failure to avoid re-reading a problematic file
49
+ directiveCheckCache.set(file, false);
50
+ }
51
+ }
52
+ log("Pre-scan found directive files:", Array.from(directiveFiles));
53
+ return directiveFiles;
54
+ }
20
55
  export async function resolveModuleWithEnvironment({ path, importer, importerEnv, clientResolver, workerResolver, }) {
21
56
  const resolver = importerEnv === "client" ? clientResolver : workerResolver;
22
57
  return new Promise((resolvePromise) => {
@@ -52,10 +87,21 @@ export function classifyModule({ contents, inheritedEnv, }) {
52
87
  return { moduleEnv, isClient, isServer };
53
88
  }
54
89
  export const runDirectivesScan = async ({ rootConfig, environments, clientFiles, serverFiles, entries: initialEntries, }) => {
90
+ console.log("######## entries", initialEntries);
55
91
  deferredLog("\n… (rwsdk) Scanning for 'use client' and 'use server' directives...");
56
92
  // Set environment variable to indicate scanning is in progress
57
93
  process.env.RWSDK_DIRECTIVE_SCAN_ACTIVE = "true";
58
94
  try {
95
+ const fileContentCache = new Map();
96
+ const directiveCheckCache = new Map();
97
+ const readFileWithCache = async (path) => {
98
+ if (fileContentCache.has(path)) {
99
+ return fileContentCache.get(path);
100
+ }
101
+ const contents = await fsp.readFile(path, "utf-8");
102
+ fileContentCache.set(path, contents);
103
+ return contents;
104
+ };
59
105
  const esbuild = await getViteEsbuild(rootConfig.root);
60
106
  const input = initialEntries ?? environments.worker.config.build.rollupOptions?.input;
61
107
  let entries;
@@ -78,19 +124,19 @@ export const runDirectivesScan = async ({ rootConfig, environments, clientFiles,
78
124
  // Filter out virtual modules since they can't be scanned by esbuild
79
125
  const realEntries = entries.filter((entry) => !entry.includes("virtual:"));
80
126
  const absoluteEntries = realEntries.map((entry) => path.resolve(rootConfig.root, entry));
81
- log("Starting directives scan for worker environment with entries:", absoluteEntries);
127
+ const applicationDirectiveFiles = await findDirectiveRoots({
128
+ root: rootConfig.root,
129
+ readFileWithCache,
130
+ directiveCheckCache,
131
+ });
132
+ const combinedEntries = new Set([
133
+ ...absoluteEntries,
134
+ ...applicationDirectiveFiles,
135
+ ]);
136
+ log("Starting directives scan with combined entries:", Array.from(combinedEntries));
82
137
  const workerResolver = createViteAwareResolver(rootConfig, environments.worker);
83
138
  const clientResolver = createViteAwareResolver(rootConfig, environments.client);
84
139
  const moduleEnvironments = new Map();
85
- const fileContentCache = new Map();
86
- const readFileWithCache = async (path) => {
87
- if (fileContentCache.has(path)) {
88
- return fileContentCache.get(path);
89
- }
90
- const contents = await fsp.readFile(path, "utf-8");
91
- fileContentCache.set(path, contents);
92
- return contents;
93
- };
94
140
  const esbuildScanPlugin = {
95
141
  name: "rwsdk:esbuild-scan-plugin",
96
142
  setup(build) {
@@ -232,7 +278,7 @@ export const runDirectivesScan = async ({ rootConfig, environments, clientFiles,
232
278
  },
233
279
  };
234
280
  await esbuild.build({
235
- entryPoints: absoluteEntries,
281
+ entryPoints: Array.from(combinedEntries),
236
282
  bundle: true,
237
283
  write: false,
238
284
  outdir: path.join(INTERMEDIATES_OUTPUT_DIR, "directive-scan"),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rwsdk",
3
- "version": "1.0.0-beta.9",
3
+ "version": "1.0.0-beta.9-test.20251006190822",
4
4
  "description": "Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime",
5
5
  "type": "module",
6
6
  "bin": {
@@ -168,7 +168,8 @@
168
168
  "ts-morph": "~27.0.0",
169
169
  "unique-names-generator": "~4.7.1",
170
170
  "vibe-rules": "~0.3.0",
171
- "vite-tsconfig-paths": "~5.1.4"
171
+ "vite-tsconfig-paths": "~5.1.4",
172
+ "@types/glob": "^8.1.0"
172
173
  },
173
174
  "peerDependencies": {
174
175
  "@cloudflare/vite-plugin": "^1.12.4",
@@ -190,7 +191,7 @@
190
191
  "semver": "~7.7.1",
191
192
  "tsx": "~4.20.0",
192
193
  "typescript": "~5.9.0",
193
- "vite": "7.1.6",
194
+ "vite": "~7.1.9",
194
195
  "vitest": "~3.2.0"
195
196
  }
196
197
  }