rwsdk 0.1.0-alpha.8 → 0.1.0
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.
- package/dist/runtime/client.d.ts +3 -1
- package/dist/runtime/client.js +16 -13
- package/dist/runtime/clientNavigation.d.ts +3 -0
- package/dist/runtime/clientNavigation.js +43 -0
- package/dist/runtime/entries/client.d.ts +1 -0
- package/dist/runtime/entries/client.js +1 -0
- package/dist/runtime/entries/worker.d.ts +2 -0
- package/dist/runtime/entries/worker.js +2 -0
- package/dist/runtime/imports/ClientOnly.d.ts +3 -0
- package/dist/runtime/imports/ClientOnly.js +8 -0
- package/dist/runtime/imports/NoSSRStub.d.ts +1 -0
- package/dist/runtime/imports/NoSSRStub.js +4 -0
- package/dist/runtime/imports/client.js +15 -2
- package/dist/runtime/imports/worker.d.ts +1 -1
- package/dist/runtime/imports/worker.js +7 -5
- package/dist/runtime/lib/db/DOWorkerDialect.d.ts +29 -0
- package/dist/runtime/lib/db/DOWorkerDialect.js +66 -0
- package/dist/runtime/lib/db/SqliteDurableObject.d.ts +14 -0
- package/dist/runtime/lib/db/SqliteDurableObject.js +42 -0
- package/dist/runtime/lib/db/create.d.ts +3 -0
- package/dist/runtime/lib/db/create.js +36 -0
- package/dist/runtime/lib/db/createDb.d.ts +2 -0
- package/dist/runtime/lib/db/createDb.js +27 -0
- package/dist/runtime/lib/db/index.d.ts +3 -0
- package/dist/runtime/lib/db/index.js +3 -0
- package/dist/runtime/lib/db/logger.d.ts +2 -0
- package/dist/runtime/lib/db/logger.js +41 -0
- package/dist/runtime/lib/db/migrations.d.ts +23 -0
- package/dist/runtime/lib/db/migrations.js +34 -0
- package/dist/runtime/lib/db/types.d.ts +0 -0
- package/dist/runtime/lib/db/types.js +1 -0
- package/dist/runtime/lib/debug.d.ts +2 -0
- package/dist/runtime/lib/debug.js +36 -0
- package/dist/runtime/lib/realtime/worker.d.ts +1 -1
- package/dist/runtime/lib/router.d.ts +31 -26
- package/dist/runtime/lib/router.js +9 -2
- package/dist/runtime/register/ssr.d.ts +2 -0
- package/dist/runtime/register/ssr.js +14 -1
- package/dist/runtime/register/worker.d.ts +1 -1
- package/dist/runtime/register/worker.js +5 -2
- package/dist/runtime/render/renderRscThenableToHtmlStream.d.ts +2 -1
- package/dist/runtime/render/renderRscThenableToHtmlStream.js +17 -3
- package/dist/runtime/render/renderToStream.d.ts +9 -0
- package/dist/runtime/render/renderToStream.js +26 -0
- package/dist/runtime/render/renderToString.d.ts +7 -0
- package/dist/runtime/render/renderToString.js +26 -0
- package/dist/runtime/render/transformRscToHtmlStream.js +1 -0
- package/dist/runtime/worker.d.ts +2 -1
- package/dist/runtime/worker.js +21 -14
- package/dist/scripts/debug-sync.mjs +8 -6
- package/dist/scripts/worker-run.mjs +1 -0
- package/dist/vite/configPlugin.mjs +8 -17
- package/dist/vite/createDirectiveLookupPlugin.d.mts +1 -0
- package/dist/vite/createDirectiveLookupPlugin.mjs +88 -49
- package/dist/vite/devServerTimingPlugin.d.mts +2 -0
- package/dist/vite/devServerTimingPlugin.mjs +24 -0
- package/dist/vite/directivesPlugin.mjs +168 -70
- package/dist/vite/findImportSpecifiers.d.mts +16 -0
- package/dist/vite/findImportSpecifiers.mjs +152 -0
- package/dist/vite/findImportSpecifiers.test.d.mts +1 -0
- package/dist/vite/findImportSpecifiers.test.mjs +73 -0
- package/dist/vite/findSpecifiers.d.mts +31 -0
- package/dist/vite/findSpecifiers.mjs +230 -0
- package/dist/vite/hasDirective.d.mts +7 -0
- package/dist/vite/hasDirective.mjs +54 -0
- package/dist/vite/hasOwnCloudflareVitePlugin.d.mts +3 -0
- package/dist/vite/hasOwnCloudflareVitePlugin.mjs +14 -0
- package/dist/vite/invalidateModule.d.mts +2 -0
- package/dist/vite/invalidateModule.mjs +14 -0
- package/dist/vite/miniflareHMRPlugin.d.mts +8 -0
- package/dist/vite/miniflareHMRPlugin.mjs +133 -0
- package/dist/vite/normalizeModulePath.mjs +12 -1
- package/dist/vite/redwoodPlugin.d.mts +1 -0
- package/dist/vite/redwoodPlugin.mjs +19 -4
- package/dist/vite/resolveModuleId.d.mts +6 -0
- package/dist/vite/resolveModuleId.mjs +14 -0
- package/dist/vite/ssrBridgePlugin.d.mts +5 -1
- package/dist/vite/ssrBridgePlugin.mjs +4 -43
- package/dist/vite/transformClientComponents.d.mts +1 -0
- package/dist/vite/transformClientComponents.mjs +61 -125
- package/dist/vite/transformJsxScriptTagsPlugin.mjs +14 -3
- package/dist/vite/transformServerFunctions.d.mts +11 -3
- package/dist/vite/transformServerFunctions.mjs +256 -171
- package/dist/vite/transformServerFunctions.test.mjs +22 -3
- package/dist/vite/useClientLookupPlugin.mjs +1 -0
- package/dist/vite/useServerLookupPlugin.mjs +1 -0
- package/dist/vite/useServerPlugin.d.mts +1 -1
- package/dist/vite/useServerPlugin.mjs +1 -1
- package/package.json +14 -3
|
@@ -24,6 +24,23 @@ export const IMPORT_PATTERNS = [
|
|
|
24
24
|
"require('$MODULE')",
|
|
25
25
|
"require(`$MODULE`)",
|
|
26
26
|
];
|
|
27
|
+
// These patterns are used to match export statements for client/server component transformations
|
|
28
|
+
export const EXPORT_PATTERNS = [
|
|
29
|
+
// Named exports
|
|
30
|
+
"export const $NAME = $$$",
|
|
31
|
+
"export let $NAME = $$$",
|
|
32
|
+
"export var $NAME = $$$",
|
|
33
|
+
"export function $NAME($$$) { $$$ }",
|
|
34
|
+
"export async function $NAME($$$) { $$$ }",
|
|
35
|
+
// Default exports
|
|
36
|
+
"export default function $NAME($$$) { $$$ }",
|
|
37
|
+
"export default function($$$) { $$$ }",
|
|
38
|
+
"export default $$$",
|
|
39
|
+
// Export declarations
|
|
40
|
+
"export { $$$ }",
|
|
41
|
+
'export { $$$ } from "$MODULE"',
|
|
42
|
+
"export { $$$ } from '$MODULE'",
|
|
43
|
+
];
|
|
27
44
|
/**
|
|
28
45
|
* Finds import specifiers and their positions in the code using the provided patterns.
|
|
29
46
|
* @param code The code to search for import specifiers.
|
|
@@ -74,3 +91,138 @@ export function findImportSpecifiers(id, code, ignoredImportPatterns, log) {
|
|
|
74
91
|
}
|
|
75
92
|
return results;
|
|
76
93
|
}
|
|
94
|
+
/**
|
|
95
|
+
* Finds export information in the code using ast-grep patterns.
|
|
96
|
+
* @param id The file identifier for language detection.
|
|
97
|
+
* @param code The code to search for exports.
|
|
98
|
+
* @param log Optional logger function for debug output.
|
|
99
|
+
* @returns Array of export information objects.
|
|
100
|
+
*/
|
|
101
|
+
export function findExports(id, code, log) {
|
|
102
|
+
const ext = path.extname(id).toLowerCase();
|
|
103
|
+
const lang = ext === ".tsx" || ext === ".jsx" ? Lang.Tsx : SgLang.TypeScript;
|
|
104
|
+
const logger = log ?? (() => { });
|
|
105
|
+
const results = [];
|
|
106
|
+
const seen = new Set(); // Track seen exports to avoid duplicates
|
|
107
|
+
try {
|
|
108
|
+
const root = sgParse(lang, code);
|
|
109
|
+
// Use the existing EXPORT_PATTERNS in a specific order to avoid duplicates
|
|
110
|
+
const orderedPatterns = [
|
|
111
|
+
// Handle re-exports first (most specific)
|
|
112
|
+
...EXPORT_PATTERNS.filter((p) => p.includes('from "$MODULE"') || p.includes("from '$MODULE'")),
|
|
113
|
+
// Then named exports
|
|
114
|
+
...EXPORT_PATTERNS.filter((p) => p.startsWith("export const") ||
|
|
115
|
+
p.startsWith("export let") ||
|
|
116
|
+
p.startsWith("export var") ||
|
|
117
|
+
p.startsWith("export function") ||
|
|
118
|
+
p.startsWith("export async function")),
|
|
119
|
+
// Then default exports
|
|
120
|
+
...EXPORT_PATTERNS.filter((p) => p.startsWith("export default")),
|
|
121
|
+
// Finally export declarations
|
|
122
|
+
...EXPORT_PATTERNS.filter((p) => p === "export { $$$ }"),
|
|
123
|
+
];
|
|
124
|
+
for (const pattern of orderedPatterns) {
|
|
125
|
+
try {
|
|
126
|
+
const matches = root.root().findAll(pattern);
|
|
127
|
+
for (const match of matches) {
|
|
128
|
+
const nameCapture = match.getMatch("NAME");
|
|
129
|
+
const moduleCapture = match.getMatch("MODULE");
|
|
130
|
+
const matchText = match.text();
|
|
131
|
+
if (pattern.includes('from "$MODULE"') ||
|
|
132
|
+
pattern.includes("from '$MODULE'")) {
|
|
133
|
+
// Re-export from module
|
|
134
|
+
const moduleSpecifier = moduleCapture?.text();
|
|
135
|
+
if (!moduleSpecifier)
|
|
136
|
+
continue;
|
|
137
|
+
if (pattern.includes("export *")) {
|
|
138
|
+
// Skip export * for now - too complex
|
|
139
|
+
logger("Skipping export * from %s", moduleSpecifier);
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
// Parse the export list
|
|
143
|
+
const exportListMatch = matchText.match(/export\s*\{\s*([^}]+)\s*\}/);
|
|
144
|
+
if (exportListMatch) {
|
|
145
|
+
const exportList = exportListMatch[1];
|
|
146
|
+
const exports = exportList.split(",").map((e) => e.trim());
|
|
147
|
+
for (const exp of exports) {
|
|
148
|
+
const [originalName, alias] = exp.includes(" as ")
|
|
149
|
+
? exp.split(" as ").map((s) => s.trim())
|
|
150
|
+
: [exp.trim(), undefined];
|
|
151
|
+
const exportName = alias || originalName;
|
|
152
|
+
const key = `${exportName}:${originalName === "default"}:reexport:${moduleSpecifier}`;
|
|
153
|
+
if (seen.has(key))
|
|
154
|
+
continue;
|
|
155
|
+
seen.add(key);
|
|
156
|
+
results.push({
|
|
157
|
+
name: exportName,
|
|
158
|
+
isDefault: originalName === "default",
|
|
159
|
+
alias: alias !== originalName ? alias : undefined,
|
|
160
|
+
isReExport: true,
|
|
161
|
+
moduleSpecifier,
|
|
162
|
+
});
|
|
163
|
+
logger("Found re-export: %s from %s", exportName, moduleSpecifier);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
else if (matchText.startsWith("export default")) {
|
|
168
|
+
// Default export
|
|
169
|
+
const name = nameCapture?.text() || "default";
|
|
170
|
+
const key = `${name}:true:default`;
|
|
171
|
+
if (seen.has(key))
|
|
172
|
+
continue;
|
|
173
|
+
seen.add(key);
|
|
174
|
+
results.push({
|
|
175
|
+
name,
|
|
176
|
+
isDefault: true,
|
|
177
|
+
});
|
|
178
|
+
logger("Found default export: %s", name);
|
|
179
|
+
}
|
|
180
|
+
else if (matchText.includes("export {")) {
|
|
181
|
+
// Local export declaration
|
|
182
|
+
const exportListMatch = matchText.match(/export\s*\{\s*([^}]+)\s*\}/);
|
|
183
|
+
if (exportListMatch) {
|
|
184
|
+
const exportList = exportListMatch[1];
|
|
185
|
+
const exports = exportList.split(",").map((e) => e.trim());
|
|
186
|
+
for (const exp of exports) {
|
|
187
|
+
const [originalName, alias] = exp.includes(" as ")
|
|
188
|
+
? exp.split(" as ").map((s) => s.trim())
|
|
189
|
+
: [exp.trim(), undefined];
|
|
190
|
+
const exportName = alias || originalName;
|
|
191
|
+
const key = `${exportName}:${originalName === "default"}:local`;
|
|
192
|
+
if (seen.has(key))
|
|
193
|
+
continue;
|
|
194
|
+
seen.add(key);
|
|
195
|
+
results.push({
|
|
196
|
+
name: exportName,
|
|
197
|
+
isDefault: originalName === "default",
|
|
198
|
+
alias: alias !== originalName ? alias : undefined,
|
|
199
|
+
});
|
|
200
|
+
logger("Found local export: %s", exportName);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
else if (nameCapture) {
|
|
205
|
+
// Named export (function, const, etc.)
|
|
206
|
+
const name = nameCapture.text();
|
|
207
|
+
const key = `${name}:false:named`;
|
|
208
|
+
if (seen.has(key))
|
|
209
|
+
continue;
|
|
210
|
+
seen.add(key);
|
|
211
|
+
results.push({
|
|
212
|
+
name,
|
|
213
|
+
isDefault: false,
|
|
214
|
+
});
|
|
215
|
+
logger("Found named export: %s", name);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
catch (err) {
|
|
220
|
+
logger("Error processing export pattern %s: %O", pattern, err);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
catch (err) {
|
|
225
|
+
logger("Error parsing code for exports: %O", err);
|
|
226
|
+
}
|
|
227
|
+
return results;
|
|
228
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { findExports } from "./findImportSpecifiers.mjs";
|
|
3
|
+
describe("findExports", () => {
|
|
4
|
+
it("finds named exports", () => {
|
|
5
|
+
const code = `
|
|
6
|
+
export const Component = () => {};
|
|
7
|
+
export function helper() {}
|
|
8
|
+
export let data = {};
|
|
9
|
+
`;
|
|
10
|
+
const exports = findExports("/test.tsx", code);
|
|
11
|
+
// Just check that we found the exports, order doesn't matter for our use case
|
|
12
|
+
const names = exports.map((e) => e.name).sort();
|
|
13
|
+
expect(names).toEqual(["Component", "data", "helper"]);
|
|
14
|
+
// Check that none are default
|
|
15
|
+
expect(exports.every((e) => !e.isDefault)).toBe(true);
|
|
16
|
+
});
|
|
17
|
+
it("finds default exports", () => {
|
|
18
|
+
const code = `
|
|
19
|
+
export default function Component() {}
|
|
20
|
+
`;
|
|
21
|
+
const exports = findExports("/test.tsx", code);
|
|
22
|
+
// Should find at least one default export
|
|
23
|
+
const defaultExports = exports.filter((e) => e.isDefault);
|
|
24
|
+
expect(defaultExports.length).toBeGreaterThan(0);
|
|
25
|
+
expect(defaultExports.some((e) => e.name === "Component")).toBe(true);
|
|
26
|
+
});
|
|
27
|
+
it("finds export declarations", () => {
|
|
28
|
+
const code = `
|
|
29
|
+
const First = () => {};
|
|
30
|
+
const Second = () => {};
|
|
31
|
+
export { First, Second };
|
|
32
|
+
`;
|
|
33
|
+
const exports = findExports("/test.tsx", code);
|
|
34
|
+
const names = exports.map((e) => e.name).sort();
|
|
35
|
+
expect(names).toEqual(["First", "Second"]);
|
|
36
|
+
});
|
|
37
|
+
it("finds export declarations with aliases", () => {
|
|
38
|
+
const code = `
|
|
39
|
+
const Component = () => {};
|
|
40
|
+
export { Component as MyComponent };
|
|
41
|
+
`;
|
|
42
|
+
const exports = findExports("/test.tsx", code);
|
|
43
|
+
expect(exports.some((e) => e.name === "MyComponent")).toBe(true);
|
|
44
|
+
});
|
|
45
|
+
it("finds re-exports", () => {
|
|
46
|
+
const code = `
|
|
47
|
+
export { sum } from './math';
|
|
48
|
+
export { default as multiply } from './multiply';
|
|
49
|
+
`;
|
|
50
|
+
const exports = findExports("/test.tsx", code);
|
|
51
|
+
// Should find re-exports
|
|
52
|
+
const reExports = exports.filter((e) => e.isReExport);
|
|
53
|
+
expect(reExports.length).toBeGreaterThan(0);
|
|
54
|
+
const names = reExports.map((e) => e.name).sort();
|
|
55
|
+
expect(names).toContain("sum");
|
|
56
|
+
expect(names).toContain("multiply");
|
|
57
|
+
});
|
|
58
|
+
it("handles mixed export styles", () => {
|
|
59
|
+
const code = `
|
|
60
|
+
export const First = () => {};
|
|
61
|
+
const Second = () => {};
|
|
62
|
+
export default function Main() {}
|
|
63
|
+
export { Second };
|
|
64
|
+
`;
|
|
65
|
+
const exports = findExports("/test.tsx", code);
|
|
66
|
+
const names = exports.map((e) => e.name);
|
|
67
|
+
expect(names).toContain("First");
|
|
68
|
+
expect(names).toContain("Second");
|
|
69
|
+
// Should have at least one default export
|
|
70
|
+
const defaultExports = exports.filter((e) => e.isDefault);
|
|
71
|
+
expect(defaultExports.length).toBeGreaterThan(0);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export declare const IMPORT_PATTERNS: string[];
|
|
2
|
+
export declare const EXPORT_PATTERNS: string[];
|
|
3
|
+
export interface ExportInfo {
|
|
4
|
+
name: string;
|
|
5
|
+
isDefault: boolean;
|
|
6
|
+
alias?: string;
|
|
7
|
+
originalName?: string;
|
|
8
|
+
isReExport?: boolean;
|
|
9
|
+
moduleSpecifier?: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Finds import specifiers and their positions in the code using the provided patterns.
|
|
13
|
+
* @param code The code to search for import specifiers.
|
|
14
|
+
* @param lang The language parser to use (TypeScript or Tsx).
|
|
15
|
+
* @param ignoredImportPatterns Array of regex patterns to ignore.
|
|
16
|
+
* @param log Optional logger function for debug output.
|
|
17
|
+
* @returns Array of objects with start, end, and raw import string.
|
|
18
|
+
*/
|
|
19
|
+
export declare function findImportSpecifiers(id: string, code: string, ignoredImportPatterns: RegExp[], log?: (...args: any[]) => void): Array<{
|
|
20
|
+
s: number;
|
|
21
|
+
e: number;
|
|
22
|
+
raw: string;
|
|
23
|
+
}>;
|
|
24
|
+
/**
|
|
25
|
+
* Finds export information in the code using ast-grep patterns.
|
|
26
|
+
* @param id The file identifier for language detection.
|
|
27
|
+
* @param code The code to search for exports.
|
|
28
|
+
* @param log Optional logger function for debug output.
|
|
29
|
+
* @returns Array of export information objects.
|
|
30
|
+
*/
|
|
31
|
+
export declare function findExports(id: string, code: string, log?: (...args: any[]) => void): ExportInfo[];
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { parse as sgParse, Lang as SgLang, Lang } from "@ast-grep/napi";
|
|
2
|
+
import path from "path";
|
|
3
|
+
// These patterns are used to match import statements in code for SSR transformations.
|
|
4
|
+
export const IMPORT_PATTERNS = [
|
|
5
|
+
'import { $$$ } from "$MODULE"',
|
|
6
|
+
"import { $$$ } from '$MODULE'",
|
|
7
|
+
'import $DEFAULT from "$MODULE"',
|
|
8
|
+
"import $DEFAULT from '$MODULE'",
|
|
9
|
+
'import * as $NS from "$MODULE"',
|
|
10
|
+
"import * as $NS from '$MODULE'",
|
|
11
|
+
'import "$MODULE"',
|
|
12
|
+
"import '$MODULE'",
|
|
13
|
+
// Static Re-exports
|
|
14
|
+
'export { $$$ } from "$MODULE"',
|
|
15
|
+
"export { $$$ } from '$MODULE'",
|
|
16
|
+
'export * from "$MODULE"',
|
|
17
|
+
"export * from '$MODULE'",
|
|
18
|
+
// Dynamic Imports
|
|
19
|
+
'import("$MODULE")',
|
|
20
|
+
"import('$MODULE')",
|
|
21
|
+
"import(`$MODULE`)",
|
|
22
|
+
// CommonJS require
|
|
23
|
+
'require("$MODULE")',
|
|
24
|
+
"require('$MODULE')",
|
|
25
|
+
"require(`$MODULE`)",
|
|
26
|
+
];
|
|
27
|
+
// These patterns are used to match export statements for client/server component transformations
|
|
28
|
+
export const EXPORT_PATTERNS = [
|
|
29
|
+
// Named exports
|
|
30
|
+
"export const $NAME = $$$",
|
|
31
|
+
"export let $NAME = $$$",
|
|
32
|
+
"export var $NAME = $$$",
|
|
33
|
+
"export function $NAME($$$) { $$$ }",
|
|
34
|
+
"export async function $NAME($$$) { $$$ }",
|
|
35
|
+
// Default exports
|
|
36
|
+
"export default function $NAME($$$) { $$$ }",
|
|
37
|
+
"export default function($$$) { $$$ }",
|
|
38
|
+
"export default $$$",
|
|
39
|
+
// Export declarations
|
|
40
|
+
"export { $$$ }",
|
|
41
|
+
'export { $$$ } from "$MODULE"',
|
|
42
|
+
"export { $$$ } from '$MODULE'",
|
|
43
|
+
];
|
|
44
|
+
/**
|
|
45
|
+
* Finds import specifiers and their positions in the code using the provided patterns.
|
|
46
|
+
* @param code The code to search for import specifiers.
|
|
47
|
+
* @param lang The language parser to use (TypeScript or Tsx).
|
|
48
|
+
* @param ignoredImportPatterns Array of regex patterns to ignore.
|
|
49
|
+
* @param log Optional logger function for debug output.
|
|
50
|
+
* @returns Array of objects with start, end, and raw import string.
|
|
51
|
+
*/
|
|
52
|
+
export function findImportSpecifiers(id, code, ignoredImportPatterns, log) {
|
|
53
|
+
const ext = path.extname(id).toLowerCase();
|
|
54
|
+
const lang = ext === ".tsx" || ext === ".jsx" ? Lang.Tsx : SgLang.TypeScript;
|
|
55
|
+
const logger = log ?? (() => { });
|
|
56
|
+
const results = [];
|
|
57
|
+
try {
|
|
58
|
+
// sgParse and lang must be provided by the consumer
|
|
59
|
+
const root = sgParse(lang, code);
|
|
60
|
+
for (const pattern of IMPORT_PATTERNS) {
|
|
61
|
+
try {
|
|
62
|
+
const matches = root.root().findAll(pattern);
|
|
63
|
+
for (const match of matches) {
|
|
64
|
+
const moduleCapture = match.getMatch("MODULE");
|
|
65
|
+
if (moduleCapture) {
|
|
66
|
+
const importPath = moduleCapture.text();
|
|
67
|
+
if (importPath.startsWith("virtual:")) {
|
|
68
|
+
logger(":findImportSpecifiersWithPositions: Ignoring import because it starts with 'virtual:': importPath=%s", importPath);
|
|
69
|
+
}
|
|
70
|
+
else if (importPath.includes("__rwsdknossr")) {
|
|
71
|
+
logger(":findImportSpecifiersWithPositions: Ignoring import because it includes '__rwsdknossr': importPath=%s", importPath);
|
|
72
|
+
}
|
|
73
|
+
else if (ignoredImportPatterns.some((pattern) => pattern.test(importPath))) {
|
|
74
|
+
logger(":findImportSpecifiersWithPositions: Ignoring import because it matches IGNORED_IMPORT_PATTERNS: importPath=%s", importPath);
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
const { start, end } = moduleCapture.range();
|
|
78
|
+
results.push({ s: start.index, e: end.index, raw: importPath });
|
|
79
|
+
logger(":findImportSpecifiersWithPositions: Including import specifier: importPath=%s, range=[%d, %d]", importPath, start.index, end.index);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
catch (err) {
|
|
85
|
+
logger(":findImportSpecifiersWithPositions: Error processing pattern: %O", err);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
catch (err) {
|
|
90
|
+
logger(":findImportSpecifiersWithPositions: Error parsing content: %O", err);
|
|
91
|
+
}
|
|
92
|
+
return results;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Finds export information in the code using ast-grep patterns.
|
|
96
|
+
* @param id The file identifier for language detection.
|
|
97
|
+
* @param code The code to search for exports.
|
|
98
|
+
* @param log Optional logger function for debug output.
|
|
99
|
+
* @returns Array of export information objects.
|
|
100
|
+
*/
|
|
101
|
+
export function findExports(id, code, log) {
|
|
102
|
+
const ext = path.extname(id).toLowerCase();
|
|
103
|
+
const lang = ext === ".tsx" || ext === ".jsx" ? Lang.Tsx : SgLang.TypeScript;
|
|
104
|
+
const logger = log ?? (() => { });
|
|
105
|
+
const results = [];
|
|
106
|
+
const seen = new Set(); // Track seen exports to avoid duplicates
|
|
107
|
+
try {
|
|
108
|
+
const root = sgParse(lang, code);
|
|
109
|
+
// Use the existing EXPORT_PATTERNS in a specific order to avoid duplicates
|
|
110
|
+
const orderedPatterns = [
|
|
111
|
+
// Handle re-exports first (most specific)
|
|
112
|
+
...EXPORT_PATTERNS.filter((p) => p.includes('from "$MODULE"') || p.includes("from '$MODULE'")),
|
|
113
|
+
// Then named exports
|
|
114
|
+
...EXPORT_PATTERNS.filter((p) => p.startsWith("export const") ||
|
|
115
|
+
p.startsWith("export let") ||
|
|
116
|
+
p.startsWith("export var") ||
|
|
117
|
+
p.startsWith("export function") ||
|
|
118
|
+
p.startsWith("export async function")),
|
|
119
|
+
// Then default exports
|
|
120
|
+
...EXPORT_PATTERNS.filter((p) => p.startsWith("export default")),
|
|
121
|
+
// Finally export declarations
|
|
122
|
+
...EXPORT_PATTERNS.filter((p) => p === "export { $$$ }"),
|
|
123
|
+
];
|
|
124
|
+
for (const pattern of orderedPatterns) {
|
|
125
|
+
try {
|
|
126
|
+
const matches = root.root().findAll(pattern);
|
|
127
|
+
for (const match of matches) {
|
|
128
|
+
const nameCapture = match.getMatch("NAME");
|
|
129
|
+
const moduleCapture = match.getMatch("MODULE");
|
|
130
|
+
const matchText = match.text();
|
|
131
|
+
if (pattern.includes('from "$MODULE"') ||
|
|
132
|
+
pattern.includes("from '$MODULE'")) {
|
|
133
|
+
// Re-export from module
|
|
134
|
+
const moduleSpecifier = moduleCapture?.text();
|
|
135
|
+
if (!moduleSpecifier)
|
|
136
|
+
continue;
|
|
137
|
+
if (pattern.includes("export *")) {
|
|
138
|
+
// Skip export * for now - too complex
|
|
139
|
+
logger("Skipping export * from %s", moduleSpecifier);
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
// Parse the export list
|
|
143
|
+
const exportListMatch = matchText.match(/export\s*\{\s*([^}]+)\s*\}/);
|
|
144
|
+
if (exportListMatch) {
|
|
145
|
+
const exportList = exportListMatch[1];
|
|
146
|
+
const exports = exportList.split(",").map((e) => e.trim());
|
|
147
|
+
for (const exp of exports) {
|
|
148
|
+
const [originalName, alias] = exp.includes(" as ")
|
|
149
|
+
? exp.split(" as ").map((s) => s.trim())
|
|
150
|
+
: [exp.trim(), undefined];
|
|
151
|
+
const exportName = alias || originalName;
|
|
152
|
+
const key = `${exportName}:${originalName === "default"}:reexport:${moduleSpecifier}`;
|
|
153
|
+
if (seen.has(key))
|
|
154
|
+
continue;
|
|
155
|
+
seen.add(key);
|
|
156
|
+
results.push({
|
|
157
|
+
name: exportName,
|
|
158
|
+
isDefault: originalName === "default",
|
|
159
|
+
alias: alias !== originalName ? alias : undefined,
|
|
160
|
+
originalName: originalName,
|
|
161
|
+
isReExport: true,
|
|
162
|
+
moduleSpecifier,
|
|
163
|
+
});
|
|
164
|
+
logger("Found re-export: %s from %s", exportName, moduleSpecifier);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
else if (matchText.startsWith("export default")) {
|
|
169
|
+
// Default export
|
|
170
|
+
const name = nameCapture?.text() || "default";
|
|
171
|
+
const key = `${name}:true:default`;
|
|
172
|
+
if (seen.has(key))
|
|
173
|
+
continue;
|
|
174
|
+
seen.add(key);
|
|
175
|
+
results.push({
|
|
176
|
+
name,
|
|
177
|
+
isDefault: true,
|
|
178
|
+
});
|
|
179
|
+
logger("Found default export: %s", name);
|
|
180
|
+
}
|
|
181
|
+
else if (matchText.includes("export {")) {
|
|
182
|
+
// Local export declaration
|
|
183
|
+
const exportListMatch = matchText.match(/export\s*\{\s*([^}]+)\s*\}/);
|
|
184
|
+
if (exportListMatch) {
|
|
185
|
+
const exportList = exportListMatch[1];
|
|
186
|
+
const exports = exportList.split(",").map((e) => e.trim());
|
|
187
|
+
for (const exp of exports) {
|
|
188
|
+
const [originalName, alias] = exp.includes(" as ")
|
|
189
|
+
? exp.split(" as ").map((s) => s.trim())
|
|
190
|
+
: [exp.trim(), undefined];
|
|
191
|
+
const exportName = alias || originalName;
|
|
192
|
+
const key = `${exportName}:${originalName === "default"}:local`;
|
|
193
|
+
if (seen.has(key))
|
|
194
|
+
continue;
|
|
195
|
+
seen.add(key);
|
|
196
|
+
results.push({
|
|
197
|
+
name: exportName,
|
|
198
|
+
isDefault: originalName === "default",
|
|
199
|
+
alias: alias !== originalName ? alias : undefined,
|
|
200
|
+
originalName: originalName,
|
|
201
|
+
});
|
|
202
|
+
logger("Found local export: %s", exportName);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
else if (nameCapture) {
|
|
207
|
+
// Named export (function, const, etc.)
|
|
208
|
+
const name = nameCapture.text();
|
|
209
|
+
const key = `${name}:false:named`;
|
|
210
|
+
if (seen.has(key))
|
|
211
|
+
continue;
|
|
212
|
+
seen.add(key);
|
|
213
|
+
results.push({
|
|
214
|
+
name,
|
|
215
|
+
isDefault: false,
|
|
216
|
+
});
|
|
217
|
+
logger("Found named export: %s", name);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
catch (err) {
|
|
222
|
+
logger("Error processing export pattern %s: %O", pattern, err);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
catch (err) {
|
|
227
|
+
logger("Error parsing code for exports: %O", err);
|
|
228
|
+
}
|
|
229
|
+
return results;
|
|
230
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Efficiently checks if a React directive (e.g., "use server", "use client")
|
|
3
|
+
* is present in the code. Optimized for performance with a two-step approach:
|
|
4
|
+
* 1. Quick string search to see if directive exists anywhere
|
|
5
|
+
* 2. Line-by-line check only if the directive might be present
|
|
6
|
+
*/
|
|
7
|
+
export declare function hasDirective(code: string, directive: string): boolean;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Efficiently checks if a React directive (e.g., "use server", "use client")
|
|
3
|
+
* is present in the code. Optimized for performance with a two-step approach:
|
|
4
|
+
* 1. Quick string search to see if directive exists anywhere
|
|
5
|
+
* 2. Line-by-line check only if the directive might be present
|
|
6
|
+
*/
|
|
7
|
+
export function hasDirective(code, directive) {
|
|
8
|
+
// Quick performance check: if directive doesn't exist anywhere, skip line checking
|
|
9
|
+
const singleQuoteDirective = `'${directive}'`;
|
|
10
|
+
const doubleQuoteDirective = `"${directive}"`;
|
|
11
|
+
if (!code.includes(singleQuoteDirective) &&
|
|
12
|
+
!code.includes(doubleQuoteDirective)) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
// Split into lines and check each one
|
|
16
|
+
const lines = code.split("\n");
|
|
17
|
+
let inMultiLineComment = false;
|
|
18
|
+
for (const line of lines) {
|
|
19
|
+
const trimmedLine = line.trim();
|
|
20
|
+
// Skip empty lines
|
|
21
|
+
if (trimmedLine.length === 0) {
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
// Handle multi-line comments
|
|
25
|
+
if (trimmedLine.startsWith("/*")) {
|
|
26
|
+
inMultiLineComment = true;
|
|
27
|
+
// Check if the comment ends on the same line
|
|
28
|
+
if (trimmedLine.includes("*/")) {
|
|
29
|
+
inMultiLineComment = false;
|
|
30
|
+
}
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
if (inMultiLineComment) {
|
|
34
|
+
// Check if this line ends the multi-line comment
|
|
35
|
+
if (trimmedLine.includes("*/")) {
|
|
36
|
+
inMultiLineComment = false;
|
|
37
|
+
}
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
// Skip single-line comments
|
|
41
|
+
if (trimmedLine.startsWith("//")) {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
// Check if this line starts with the directive
|
|
45
|
+
if (trimmedLine.startsWith(doubleQuoteDirective) ||
|
|
46
|
+
trimmedLine.startsWith(singleQuoteDirective)) {
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
// If we hit a non-empty, non-comment line that's not a directive, we can stop
|
|
50
|
+
// (directives must be at the top of the file/scope, after comments)
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { readFile } from "fs/promises";
|
|
2
|
+
import path from "path";
|
|
3
|
+
export async function hasOwnCloudflareVitePlugin({ rootProjectDir, }) {
|
|
4
|
+
const packageJsonPath = path.join(rootProjectDir, "package.json");
|
|
5
|
+
try {
|
|
6
|
+
const packageJson = JSON.parse(await readFile(packageJsonPath, "utf-8"));
|
|
7
|
+
return !!(packageJson.dependencies?.["@cloudflare/vite-plugin"] ||
|
|
8
|
+
packageJson.devDependencies?.["@cloudflare/vite-plugin"]);
|
|
9
|
+
}
|
|
10
|
+
catch (error) {
|
|
11
|
+
console.error("Error reading package.json:", error);
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
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
|
+
};
|