rwsdk 0.1.5 → 0.1.6-test.20250702132719
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.js +6 -1
- package/dist/runtime/clientNavigation.d.ts +1 -0
- package/dist/runtime/clientNavigation.js +34 -24
- package/dist/runtime/clientNavigation.test.js +55 -0
- package/dist/runtime/lib/realtime/client.js +6 -0
- package/dist/runtime/lib/realtime/durableObject.js +8 -3
- package/dist/runtime/lib/realtime/worker.js +1 -3
- package/dist/runtime/render/renderRscThenableToHtmlStream.d.ts +2 -1
- package/dist/runtime/render/renderRscThenableToHtmlStream.js +22 -1
- package/dist/runtime/render/renderToStream.js +2 -1
- package/dist/runtime/render/transformRscToHtmlStream.d.ts +2 -1
- package/dist/runtime/render/transformRscToHtmlStream.js +2 -1
- package/dist/runtime/worker.js +1 -0
- package/dist/vite/createDirectiveLookupPlugin.mjs +2 -1
- package/package.json +1 -1
- package/dist/lib/smokeTests/components.d.mts +0 -8
- package/dist/lib/smokeTests/components.mjs +0 -194
- package/dist/lib/smokeTests/templates/SmokeTestInfo.template.d.ts +0 -1
- package/dist/lib/smokeTests/templates/SmokeTestInfo.template.js +0 -82
- package/dist/runtime/components/HealthCheck.d.ts +0 -13
- package/dist/runtime/components/HealthCheck.js +0 -56
- package/dist/runtime/components/HealthCheckClient.d.ts +0 -2
- package/dist/runtime/components/HealthCheckClient.js +0 -78
- package/dist/runtime/imports/NoSSRStub.d.ts +0 -1
- package/dist/runtime/imports/NoSSRStub.js +0 -4
- package/dist/runtime/lib/db/create.d.ts +0 -3
- package/dist/runtime/lib/db/create.js +0 -36
- package/dist/runtime/lib/db/logger.d.ts +0 -2
- package/dist/runtime/lib/db/logger.js +0 -41
- package/dist/runtime/lib/db/types.d.ts +0 -0
- package/dist/runtime/lib/db/types.js +0 -1
- package/dist/runtime/render/__rwsdk_ssr_bridge.d.ts +0 -10
- package/dist/runtime/render/__rwsdk_ssr_bridge.js +0 -9
- package/dist/runtime/render/__rwsdkssr_render.d.ts +0 -9
- package/dist/runtime/render/__rwsdkssr_render.js +0 -13
- package/dist/runtime/render/injectRSCPayload.d.ts +0 -3
- package/dist/runtime/render/injectRSCPayload.js +0 -79
- package/dist/runtime/render/ssrBridge.d.ts +0 -2
- package/dist/runtime/render/ssrBridge.js +0 -2
- package/dist/runtime/render/ssrRenderToReadableStream.d.ts +0 -2
- package/dist/runtime/render/ssrRenderToReadableStream.js +0 -2
- package/dist/runtime/requestInfo/__rwsdknossr_worker.d.ts +0 -5
- package/dist/runtime/requestInfo/__rwsdknossr_worker.js +0 -33
- package/dist/scripts/build-vendor-bundles.d.mts +0 -1
- package/dist/scripts/build-vendor-bundles.mjs +0 -92
- package/dist/vite/aliasedModuleResolver.d.mts +0 -9
- package/dist/vite/aliasedModuleResolver.mjs +0 -62
- package/dist/vite/aliasedSSRResolver.d.mts +0 -5
- package/dist/vite/aliasedSSRResolver.mjs +0 -74
- package/dist/vite/copyPrismaWasmPlugin.d.mts +0 -4
- package/dist/vite/copyPrismaWasmPlugin.mjs +0 -32
- package/dist/vite/ensureConfigArrays.d.mts +0 -1
- package/dist/vite/ensureConfigArrays.mjs +0 -12
- package/dist/vite/findImportSpecifiers.d.mts +0 -30
- package/dist/vite/findImportSpecifiers.mjs +0 -228
- package/dist/vite/findImportSpecifiers.test.mjs +0 -73
- package/dist/vite/isBareImport.d.mts +0 -1
- package/dist/vite/isBareImport.mjs +0 -5
- package/dist/vite/miniflarePlugin.d.mts +0 -9
- package/dist/vite/miniflarePlugin.mjs +0 -135
- package/dist/vite/moduleResolver.d.mts +0 -10
- package/dist/vite/moduleResolver.mjs +0 -74
- package/dist/vite/resolveModuleId.d.mts +0 -6
- package/dist/vite/resolveModuleId.mjs +0 -14
- package/dist/vite/rscDirectivesPlugin.d.mts +0 -6
- package/dist/vite/rscDirectivesPlugin.mjs +0 -80
- package/dist/vite/transformServerReferences.d.mts +0 -11
- package/dist/vite/transformServerReferences.mjs +0 -74
- package/dist/vite/useClientPlugin.d.mts +0 -8
- package/dist/vite/useClientPlugin.mjs +0 -299
- package/dist/vite/useClientPlugin.test.d.mts +0 -1
- package/dist/vite/useClientPlugin.test.mjs +0 -1294
- package/dist/vite/useServerPlugin.test.d.mts +0 -1
- package/dist/vite/useServerPlugin.test.mjs +0 -99
- package/dist/vite/virtualizedSSRPlugin.d.mts +0 -56
- package/dist/vite/virtualizedSSRPlugin.mjs +0 -464
- package/dist/vite/wasmPlugin.d.mts +0 -2
- package/dist/vite/wasmPlugin.mjs +0 -14
- /package/dist/{vite/findImportSpecifiers.test.d.mts → runtime/clientNavigation.test.d.ts} +0 -0
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import path from "path";
|
|
2
|
-
import enhancedResolve from "enhanced-resolve";
|
|
3
|
-
import debug from "debug";
|
|
4
|
-
function applyAlias(request, aliasEntries, name) {
|
|
5
|
-
if (!aliasEntries)
|
|
6
|
-
return request;
|
|
7
|
-
const logPrefix = `:${name}:`;
|
|
8
|
-
// Support both array and object forms
|
|
9
|
-
const entries = Array.isArray(aliasEntries)
|
|
10
|
-
? aliasEntries
|
|
11
|
-
: Object.entries(aliasEntries).map(([find, replacement]) => ({
|
|
12
|
-
find,
|
|
13
|
-
replacement,
|
|
14
|
-
}));
|
|
15
|
-
for (const entry of entries) {
|
|
16
|
-
const { find, replacement } = entry;
|
|
17
|
-
if (typeof find === "string") {
|
|
18
|
-
if (request === find || request.startsWith(find + "/")) {
|
|
19
|
-
debug("rwsdk:vite:aliased-module-resolver")("%s :applyAlias: Matched string alias: '%s' -> '%s' for request '%s'", logPrefix, find, replacement, request);
|
|
20
|
-
return replacement + request.slice(find.length);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
else if (find instanceof RegExp) {
|
|
24
|
-
if (find.test(request)) {
|
|
25
|
-
debug("rwsdk:vite:aliased-module-resolver")("%s :applyAlias: Matched RegExp alias: %O -> '%s' for request '%s'", logPrefix, find, replacement, request);
|
|
26
|
-
return request.replace(find, replacement);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
return request;
|
|
31
|
-
}
|
|
32
|
-
export function createModuleResolver({ getAliases, roots, conditionNames = ["workerd", "edge", "import", "default"], name, }) {
|
|
33
|
-
const log = debug("rwsdk:vite:aliased-module-resolver");
|
|
34
|
-
const logPrefix = `:${name}:`;
|
|
35
|
-
const baseModuleResolver = enhancedResolve.create.sync({
|
|
36
|
-
conditionNames,
|
|
37
|
-
extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx"],
|
|
38
|
-
});
|
|
39
|
-
const attemptResolve = (from, request) => {
|
|
40
|
-
try {
|
|
41
|
-
return baseModuleResolver(from, request);
|
|
42
|
-
}
|
|
43
|
-
catch {
|
|
44
|
-
return false;
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
return function resolveModule(request, importer) {
|
|
48
|
-
log("%s Called with request=%s, importer=%s", logPrefix, request, importer);
|
|
49
|
-
const aliasEntries = getAliases ? getAliases() : [];
|
|
50
|
-
const normalized = applyAlias(request, aliasEntries, name);
|
|
51
|
-
log("%s After aliasing: %s", logPrefix, normalized);
|
|
52
|
-
if (importer != null) {
|
|
53
|
-
const result = attemptResolve(path.dirname(importer), normalized);
|
|
54
|
-
if (result) {
|
|
55
|
-
log("%s Resolved module=%s relative to: importer=%s", logPrefix, result, importer);
|
|
56
|
-
return result;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
for (const root of roots) {
|
|
60
|
-
try {
|
|
61
|
-
log("%s Attempting to resolve module=%s relative to: root=%s", logPrefix, normalized, root);
|
|
62
|
-
const result = attemptResolve(root, normalized);
|
|
63
|
-
if (result) {
|
|
64
|
-
log("%s Resolved module=%s relative to: root=%s", logPrefix, result, importer);
|
|
65
|
-
return result;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
catch (err) {
|
|
69
|
-
log("%s Resolution failed for normalized=%s from root=%s: %O", logPrefix, normalized, root, err);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
return false;
|
|
73
|
-
};
|
|
74
|
-
}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Resolves a module ID consistently across client and server transformations.
|
|
3
|
-
* For node modules, uses the /rwsdk:kind/relativePath format.
|
|
4
|
-
* For regular files, uses the raw ID.
|
|
5
|
-
*/
|
|
6
|
-
export declare function resolveModuleId(id: string, kind: "client" | "server", projectRootDir: string): string;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { normalizeModulePath } from "./normalizeModulePath.mjs";
|
|
2
|
-
/**
|
|
3
|
-
* Resolves a module ID consistently across client and server transformations.
|
|
4
|
-
* For node modules, uses the /rwsdk:kind/relativePath format.
|
|
5
|
-
* For regular files, uses the raw ID.
|
|
6
|
-
*/
|
|
7
|
-
export function resolveModuleId(id, kind, projectRootDir) {
|
|
8
|
-
const modulePath = normalizeModulePath(projectRootDir, id);
|
|
9
|
-
return modulePath.includes("node_modules")
|
|
10
|
-
? modulePath.includes("__rwsdk_kind")
|
|
11
|
-
? modulePath
|
|
12
|
-
: `${modulePath}?__rwsdk_kind=${kind}`
|
|
13
|
-
: modulePath;
|
|
14
|
-
}
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import debug from "debug";
|
|
2
|
-
import { transformClientComponents } from "./transformClientComponents.mjs";
|
|
3
|
-
import { transformServerFunctions } from "./transformServerFunctions.mjs";
|
|
4
|
-
import { normalizeModulePath } from "./normalizeModulePath.mjs";
|
|
5
|
-
const log = debug("rwsdk:vite:rsc-directives-plugin");
|
|
6
|
-
const verboseLog = debug("verbose:rwsdk:vite:rsc-directives-plugin");
|
|
7
|
-
export const rscDirectivesPlugin = ({ projectRootDir, clientFiles, serverFiles, }) => ({
|
|
8
|
-
name: "rwsdk:rsc-directives",
|
|
9
|
-
async transform(code, id) {
|
|
10
|
-
verboseLog("Transform called for id=%s, environment=%s", id, this.environment.name);
|
|
11
|
-
const normalizedId = normalizeModulePath(projectRootDir, id);
|
|
12
|
-
const clientResult = await transformClientComponents(code, normalizedId, {
|
|
13
|
-
environmentName: this.environment.name,
|
|
14
|
-
clientFiles,
|
|
15
|
-
});
|
|
16
|
-
if (clientResult) {
|
|
17
|
-
log("Client component transformation successful for id=%s", id);
|
|
18
|
-
return {
|
|
19
|
-
code: clientResult.code,
|
|
20
|
-
map: clientResult.map,
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
const serverResult = transformServerFunctions(code, normalizedId, this.environment.name, serverFiles);
|
|
24
|
-
if (serverResult) {
|
|
25
|
-
log("Server function transformation successful for id=%s", id);
|
|
26
|
-
return {
|
|
27
|
-
code: serverResult.code,
|
|
28
|
-
map: serverResult.map,
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
verboseLog("No transformation applied for id=%s", id);
|
|
32
|
-
},
|
|
33
|
-
configEnvironment(env, config) {
|
|
34
|
-
log("Configuring environment: env=%s", env);
|
|
35
|
-
config.optimizeDeps ??= {};
|
|
36
|
-
config.optimizeDeps.esbuildOptions ??= {};
|
|
37
|
-
config.optimizeDeps.esbuildOptions.plugins ??= [];
|
|
38
|
-
config.optimizeDeps.esbuildOptions.plugins.push({
|
|
39
|
-
name: "rsc-directives-esbuild-transform",
|
|
40
|
-
setup(build) {
|
|
41
|
-
log("Setting up esbuild plugin for environment: %s", env);
|
|
42
|
-
build.onLoad({ filter: /.*\.js$/ }, async (args) => {
|
|
43
|
-
verboseLog("Esbuild onLoad called for path=%s", args.path);
|
|
44
|
-
const fs = await import("node:fs/promises");
|
|
45
|
-
const path = await import("node:path");
|
|
46
|
-
let code;
|
|
47
|
-
try {
|
|
48
|
-
code = await fs.readFile(args.path, "utf-8");
|
|
49
|
-
}
|
|
50
|
-
catch {
|
|
51
|
-
verboseLog("Failed to read file: %s", args.path);
|
|
52
|
-
return undefined;
|
|
53
|
-
}
|
|
54
|
-
const clientResult = await transformClientComponents(code, normalizeModulePath(projectRootDir, args.path), {
|
|
55
|
-
environmentName: env,
|
|
56
|
-
clientFiles,
|
|
57
|
-
isEsbuild: true,
|
|
58
|
-
});
|
|
59
|
-
if (clientResult) {
|
|
60
|
-
log("Esbuild client component transformation successful for path=%s", args.path);
|
|
61
|
-
return {
|
|
62
|
-
contents: clientResult.code,
|
|
63
|
-
loader: path.extname(args.path).slice(1),
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
const serverResult = transformServerFunctions(code, normalizeModulePath(projectRootDir, args.path), env, serverFiles);
|
|
67
|
-
if (serverResult) {
|
|
68
|
-
log("Esbuild server function transformation successful for path=%s", args.path);
|
|
69
|
-
return {
|
|
70
|
-
contents: serverResult.code,
|
|
71
|
-
loader: path.extname(args.path).slice(1),
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
verboseLog("Esbuild no transformation applied for path=%s", args.path);
|
|
75
|
-
});
|
|
76
|
-
},
|
|
77
|
-
});
|
|
78
|
-
log("Environment configuration complete for env=%s", env);
|
|
79
|
-
},
|
|
80
|
-
});
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export interface TransformServerEnv {
|
|
2
|
-
environmentName: string;
|
|
3
|
-
isEsbuild?: boolean;
|
|
4
|
-
isSSR?: boolean;
|
|
5
|
-
topLevelRoot?: string;
|
|
6
|
-
}
|
|
7
|
-
export interface TransformResult {
|
|
8
|
-
code: string;
|
|
9
|
-
map?: any;
|
|
10
|
-
}
|
|
11
|
-
export declare function transformServerReferences(code: string, id: string, env: TransformServerEnv): Promise<TransformResult | undefined>;
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { relative } from "node:path";
|
|
2
|
-
import MagicString from "magic-string";
|
|
3
|
-
import debug from "debug";
|
|
4
|
-
import { parse } from "es-module-lexer";
|
|
5
|
-
import { ensureNoSSRNamespace, ensureSSRNamespace, } from "./virtualizedSSRPlugin.mjs";
|
|
6
|
-
const logVite = debug("rwsdk:vite:transform-server-references:vite");
|
|
7
|
-
const logEsbuild = debug("rwsdk:vite:transform-server-references:esbuild");
|
|
8
|
-
export async function transformServerReferences(code, id, env) {
|
|
9
|
-
const log = env.isEsbuild ? logEsbuild : logVite;
|
|
10
|
-
log("Called transformServerReferences for id: id=%s, env: %O", id, env);
|
|
11
|
-
// Only transform files that start with 'use server'
|
|
12
|
-
const cleanCode = code.trimStart();
|
|
13
|
-
const hasUseServer = cleanCode.startsWith('"use server"') ||
|
|
14
|
-
cleanCode.startsWith("'use server'");
|
|
15
|
-
if (!hasUseServer) {
|
|
16
|
-
log("Skipping: no 'use server' directive in id=%s", id);
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
log("Processing 'use server' module: id=%s", id);
|
|
20
|
-
// Remove all 'use server' directives
|
|
21
|
-
let s = new MagicString(code);
|
|
22
|
-
s.replaceAll("'use server'", "");
|
|
23
|
-
s.replaceAll('"use server"', "");
|
|
24
|
-
s.trim();
|
|
25
|
-
// Parse exports
|
|
26
|
-
await import("es-module-lexer"); // ensure parse is initialized
|
|
27
|
-
const [_, exports] = parse(code);
|
|
28
|
-
log("Parsed exports for id=%s: %O", id, exports);
|
|
29
|
-
// Compute relativeId for registration
|
|
30
|
-
let relativeId = id;
|
|
31
|
-
if (env.topLevelRoot) {
|
|
32
|
-
try {
|
|
33
|
-
relativeId = `/${relative(env.topLevelRoot, id)}`;
|
|
34
|
-
log("Computed relativeId for id=%s: %s", id, relativeId);
|
|
35
|
-
}
|
|
36
|
-
catch (e) {
|
|
37
|
-
log("Error computing relativeId for id=%s: %O", id, e);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
let importLines = [];
|
|
41
|
-
let exportLines = [];
|
|
42
|
-
if (env.isSSR) {
|
|
43
|
-
log("SSR import detected for id=%s", id);
|
|
44
|
-
// Just re-export everything from the original module in SSR
|
|
45
|
-
importLines.push(`export * from ${JSON.stringify(ensureNoSSRNamespace(id))};`);
|
|
46
|
-
exportLines = [];
|
|
47
|
-
}
|
|
48
|
-
else if (env.environmentName === "worker") {
|
|
49
|
-
log("Worker environment detected for id=%s", id);
|
|
50
|
-
importLines.push('import { registerServerReference } from "rwsdk/worker";');
|
|
51
|
-
importLines.push(`import ${JSON.stringify(ensureSSRNamespace(id))};`);
|
|
52
|
-
for (const e of exports) {
|
|
53
|
-
log("Registering server reference for export: %O in id=%s", e, id);
|
|
54
|
-
exportLines.push(`registerServerReference(${e.ln}, ${JSON.stringify(relativeId)}, ${JSON.stringify(e.ln)});`);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
else if (env.environmentName === "client") {
|
|
58
|
-
log("Client environment detected for id=%s", id);
|
|
59
|
-
importLines.push('import { createServerReference } from "rwsdk/client";');
|
|
60
|
-
for (const e of exports) {
|
|
61
|
-
log("Creating client server reference for export: %O in id=%s", e, id);
|
|
62
|
-
exportLines.push(`export const ${e.ln} = createServerReference(${JSON.stringify(relativeId)}, ${JSON.stringify(e.ln)});`);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
log("Processing complete for id=%s", id);
|
|
66
|
-
const result = [...importLines, ...exportLines].join("\n");
|
|
67
|
-
if (process.env.VERBOSE) {
|
|
68
|
-
log(":VERBOSE: Transformed code for id=%s:\n%s", id, result);
|
|
69
|
-
}
|
|
70
|
-
return {
|
|
71
|
-
code: result + "\n",
|
|
72
|
-
map: s.generateMap({ hires: true }),
|
|
73
|
-
};
|
|
74
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { Plugin } from "vite";
|
|
2
|
-
interface TransformResult {
|
|
3
|
-
code: string;
|
|
4
|
-
map?: any;
|
|
5
|
-
}
|
|
6
|
-
export declare function transformUseClientCode(code: string, relativeId: string): Promise<TransformResult | undefined>;
|
|
7
|
-
export declare const useClientPlugin: () => Plugin;
|
|
8
|
-
export {};
|
|
@@ -1,299 +0,0 @@
|
|
|
1
|
-
import { relative } from "node:path";
|
|
2
|
-
import { Project, Node, SyntaxKind, } from "ts-morph";
|
|
3
|
-
function isJsxFunction(text) {
|
|
4
|
-
return (text.includes("jsx(") || text.includes("jsxs(") || text.includes("jsxDEV("));
|
|
5
|
-
}
|
|
6
|
-
export async function transformUseClientCode(code, relativeId) {
|
|
7
|
-
if (code.indexOf("use client") === -1) {
|
|
8
|
-
return;
|
|
9
|
-
}
|
|
10
|
-
const project = new Project({
|
|
11
|
-
useInMemoryFileSystem: true,
|
|
12
|
-
compilerOptions: {
|
|
13
|
-
sourceMap: true,
|
|
14
|
-
target: 2, // ES6
|
|
15
|
-
module: 1, // CommonJS
|
|
16
|
-
jsx: 2, // React
|
|
17
|
-
},
|
|
18
|
-
});
|
|
19
|
-
const sourceFile = project.createSourceFile("temp.tsx", code);
|
|
20
|
-
const firstString = sourceFile.getFirstDescendantByKind(SyntaxKind.StringLiteral);
|
|
21
|
-
if (firstString?.getText().indexOf("use client") === -1 &&
|
|
22
|
-
firstString?.getStart() !== sourceFile.getStart() // `getStart` does not include the leading comments + whitespace
|
|
23
|
-
) {
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
// Add import declaration properly through the AST
|
|
27
|
-
sourceFile.addImportDeclaration({
|
|
28
|
-
moduleSpecifier: "rwsdk/worker",
|
|
29
|
-
namedImports: [{ name: "registerClientReference" }],
|
|
30
|
-
});
|
|
31
|
-
const components = new Map();
|
|
32
|
-
let anonymousDefaultCount = 0;
|
|
33
|
-
// Pass 1: Collect all component information
|
|
34
|
-
// Handle function declarations
|
|
35
|
-
sourceFile
|
|
36
|
-
.getDescendantsOfKind(SyntaxKind.FunctionDeclaration)
|
|
37
|
-
.forEach((node) => {
|
|
38
|
-
const name = node.getName() || `DefaultComponent${anonymousDefaultCount++}`;
|
|
39
|
-
if (!name)
|
|
40
|
-
return;
|
|
41
|
-
// Only track if it's a component (has JSX return)
|
|
42
|
-
if (isJsxFunction(node.getText())) {
|
|
43
|
-
const ssrName = `${name}SSR`;
|
|
44
|
-
const isInlineExport = node.hasModifier(SyntaxKind.ExportKeyword);
|
|
45
|
-
// Check if this function is used in a default export
|
|
46
|
-
const isDefault = node.hasModifier(SyntaxKind.DefaultKeyword) ||
|
|
47
|
-
sourceFile
|
|
48
|
-
.getDescendantsOfKind(SyntaxKind.ExportAssignment)
|
|
49
|
-
.some((exp) => exp.getExpression().getText() === name);
|
|
50
|
-
components.set(name, {
|
|
51
|
-
name,
|
|
52
|
-
ssrName,
|
|
53
|
-
isDefault,
|
|
54
|
-
isInlineExport,
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
// Handle arrow functions and anonymous default exports
|
|
59
|
-
sourceFile
|
|
60
|
-
.getDescendantsOfKind(SyntaxKind.VariableStatement)
|
|
61
|
-
.forEach((statement) => {
|
|
62
|
-
const declarations = statement.getDeclarationList().getDeclarations();
|
|
63
|
-
declarations.forEach((varDecl) => {
|
|
64
|
-
const arrowFunc = varDecl.getFirstDescendantByKind(SyntaxKind.ArrowFunction);
|
|
65
|
-
if (!arrowFunc)
|
|
66
|
-
return;
|
|
67
|
-
// Only track if it's a component (has JSX return)
|
|
68
|
-
if (isJsxFunction(arrowFunc.getText())) {
|
|
69
|
-
const name = varDecl.getName();
|
|
70
|
-
const isDefault = !!statement.getFirstAncestorByKind(SyntaxKind.ExportAssignment);
|
|
71
|
-
const isInlineExport = statement.hasModifier(SyntaxKind.ExportKeyword);
|
|
72
|
-
if (!name &&
|
|
73
|
-
(isDefault || statement.getText().includes("export default"))) {
|
|
74
|
-
// Handle anonymous default export
|
|
75
|
-
const anonName = `DefaultComponent${anonymousDefaultCount++}`;
|
|
76
|
-
components.set(anonName, {
|
|
77
|
-
name: anonName,
|
|
78
|
-
ssrName: anonName,
|
|
79
|
-
isDefault: true,
|
|
80
|
-
isInlineExport: true,
|
|
81
|
-
isAnonymousDefault: true,
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
else if (name) {
|
|
85
|
-
components.set(name, {
|
|
86
|
-
name,
|
|
87
|
-
ssrName: `${name}SSR`,
|
|
88
|
-
isDefault,
|
|
89
|
-
isInlineExport,
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
// Pass 2: handle exports
|
|
96
|
-
// Remove use client directives
|
|
97
|
-
sourceFile.getDescendantsOfKind(SyntaxKind.StringLiteral).forEach((node) => {
|
|
98
|
-
if (node.getText() === "'use client'" ||
|
|
99
|
-
node.getText() === '"use client"') {
|
|
100
|
-
const parentExpr = node.getFirstAncestorByKind(SyntaxKind.ExpressionStatement);
|
|
101
|
-
if (parentExpr) {
|
|
102
|
-
parentExpr.remove();
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
});
|
|
106
|
-
// Create lists of nodes to modify before making any changes
|
|
107
|
-
const functionsToModify = [];
|
|
108
|
-
const variableStatementsToModify = [];
|
|
109
|
-
const exportDeclarationsToModify = [];
|
|
110
|
-
const exportAssignmentsToModify = [];
|
|
111
|
-
// Collect function declarations to modify
|
|
112
|
-
sourceFile
|
|
113
|
-
.getDescendantsOfKind(SyntaxKind.FunctionDeclaration)
|
|
114
|
-
.forEach((node) => {
|
|
115
|
-
const name = node.getName();
|
|
116
|
-
if (!name || !components.has(name))
|
|
117
|
-
return;
|
|
118
|
-
const component = components.get(name);
|
|
119
|
-
if (component.isInlineExport) {
|
|
120
|
-
functionsToModify.push({
|
|
121
|
-
node,
|
|
122
|
-
nodeText: node.getText(),
|
|
123
|
-
component,
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
// Collect variable statements to modify
|
|
128
|
-
sourceFile
|
|
129
|
-
.getDescendantsOfKind(SyntaxKind.VariableStatement)
|
|
130
|
-
.forEach((statement) => {
|
|
131
|
-
if (!statement.hasModifier(SyntaxKind.ExportKeyword))
|
|
132
|
-
return;
|
|
133
|
-
const declarations = statement.getDeclarationList().getDeclarations();
|
|
134
|
-
let hasComponent = false;
|
|
135
|
-
declarations.forEach((varDecl) => {
|
|
136
|
-
const name = varDecl.getName();
|
|
137
|
-
if (name && components.has(name)) {
|
|
138
|
-
hasComponent = true;
|
|
139
|
-
}
|
|
140
|
-
});
|
|
141
|
-
if (hasComponent) {
|
|
142
|
-
variableStatementsToModify.push({
|
|
143
|
-
node: statement,
|
|
144
|
-
stmtText: statement.getText(),
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
});
|
|
148
|
-
// Collect export declarations to modify
|
|
149
|
-
sourceFile
|
|
150
|
-
.getDescendantsOfKind(SyntaxKind.ExportDeclaration)
|
|
151
|
-
.forEach((node) => {
|
|
152
|
-
const namedExports = node.getNamedExports();
|
|
153
|
-
const nonComponentExports = namedExports.filter((exp) => !components.has(exp.getName()));
|
|
154
|
-
if (nonComponentExports.length !== namedExports.length) {
|
|
155
|
-
exportDeclarationsToModify.push({
|
|
156
|
-
node,
|
|
157
|
-
nonComponentExports,
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
});
|
|
161
|
-
// Collect export assignments to modify
|
|
162
|
-
sourceFile
|
|
163
|
-
.getDescendantsOfKind(SyntaxKind.ExportAssignment)
|
|
164
|
-
.forEach((node) => {
|
|
165
|
-
const expression = node.getExpression();
|
|
166
|
-
if (Node.isArrowFunction(expression)) {
|
|
167
|
-
exportAssignmentsToModify.push({
|
|
168
|
-
node,
|
|
169
|
-
expression,
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
else {
|
|
173
|
-
exportAssignmentsToModify.push({
|
|
174
|
-
node,
|
|
175
|
-
expression: null,
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
});
|
|
179
|
-
// Now apply all modifications in sequence to avoid operating on removed nodes
|
|
180
|
-
// Modify function declarations
|
|
181
|
-
functionsToModify.forEach(({ node, nodeText, component }) => {
|
|
182
|
-
const newText = nodeText.replace(/^export\s+(default\s+)?(async\s+)?function/, "$2function");
|
|
183
|
-
node.replaceWithText(newText);
|
|
184
|
-
});
|
|
185
|
-
// Modify variable statements
|
|
186
|
-
variableStatementsToModify.forEach(({ node, stmtText }) => {
|
|
187
|
-
const newText = stmtText.replace(/^export\s+/, "");
|
|
188
|
-
node.replaceWithText(newText);
|
|
189
|
-
});
|
|
190
|
-
// Modify export declarations
|
|
191
|
-
exportDeclarationsToModify.forEach(({ node, nonComponentExports }) => {
|
|
192
|
-
if (nonComponentExports.length === 0) {
|
|
193
|
-
// If all exports were components, remove the declaration
|
|
194
|
-
node.remove();
|
|
195
|
-
}
|
|
196
|
-
else {
|
|
197
|
-
// If some exports were components, update the export declaration
|
|
198
|
-
const newExports = nonComponentExports
|
|
199
|
-
.map((exp) => exp.getText())
|
|
200
|
-
.join(", ");
|
|
201
|
-
node.replaceWithText(`export { ${newExports} };`);
|
|
202
|
-
}
|
|
203
|
-
});
|
|
204
|
-
// Handle export assignments with arrow functions
|
|
205
|
-
exportAssignmentsToModify.forEach(({ node, expression }) => {
|
|
206
|
-
if (expression && Node.isArrowFunction(expression)) {
|
|
207
|
-
const anonName = `DefaultComponent${anonymousDefaultCount++}`;
|
|
208
|
-
const ssrName = `${anonName}SSR`;
|
|
209
|
-
// First add declarations
|
|
210
|
-
sourceFile.addStatements(`const ${ssrName} = ${expression.getText()}`);
|
|
211
|
-
sourceFile.addStatements(`const ${anonName} = registerClientReference("${relativeId}", "default", ${ssrName});`);
|
|
212
|
-
// Store info for later export
|
|
213
|
-
components.set(anonName, {
|
|
214
|
-
name: anonName,
|
|
215
|
-
ssrName,
|
|
216
|
-
isDefault: true,
|
|
217
|
-
isInlineExport: true,
|
|
218
|
-
isAnonymousDefault: true,
|
|
219
|
-
});
|
|
220
|
-
}
|
|
221
|
-
// Remove the original export default node
|
|
222
|
-
node.remove();
|
|
223
|
-
});
|
|
224
|
-
// Pass 4: rename all identifiers to SSR version - collect first
|
|
225
|
-
const identifiersToRename = [];
|
|
226
|
-
components.forEach(({ name, ssrName, isAnonymousDefault }) => {
|
|
227
|
-
if (isAnonymousDefault)
|
|
228
|
-
return;
|
|
229
|
-
// Find function declarations by name
|
|
230
|
-
const funcDecls = sourceFile.getDescendantsOfKind(SyntaxKind.FunctionDeclaration);
|
|
231
|
-
const funcNode = funcDecls.find((decl) => decl.getName() === name);
|
|
232
|
-
if (funcNode) {
|
|
233
|
-
identifiersToRename.push({ node: funcNode, newName: ssrName });
|
|
234
|
-
return;
|
|
235
|
-
}
|
|
236
|
-
// Find variable declarations by name
|
|
237
|
-
const varDecls = sourceFile.getDescendantsOfKind(SyntaxKind.VariableDeclaration);
|
|
238
|
-
const varNode = varDecls.find((decl) => decl.getName() === name);
|
|
239
|
-
if (varNode) {
|
|
240
|
-
const identifier = varNode.getFirstChildByKind(SyntaxKind.Identifier);
|
|
241
|
-
if (identifier) {
|
|
242
|
-
identifiersToRename.push({ node: identifier, newName: ssrName });
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
});
|
|
246
|
-
// Now apply the renames
|
|
247
|
-
identifiersToRename.forEach(({ node, newName }) => {
|
|
248
|
-
node.rename(newName);
|
|
249
|
-
});
|
|
250
|
-
// Pass 5: Add client reference registrations
|
|
251
|
-
// Add all declarations first
|
|
252
|
-
components.forEach(({ name, ssrName, isDefault, isAnonymousDefault }) => {
|
|
253
|
-
if (!isAnonymousDefault) {
|
|
254
|
-
sourceFile.addStatements(`const ${name} = registerClientReference("${relativeId}", "${isDefault ? "default" : name}", ${ssrName});`);
|
|
255
|
-
}
|
|
256
|
-
});
|
|
257
|
-
// Pass 6: add new exports
|
|
258
|
-
// Then add all exports after declarations
|
|
259
|
-
components.forEach(({ name, ssrName, isDefault }) => {
|
|
260
|
-
if (isDefault) {
|
|
261
|
-
// Export the registerClientReference version as default
|
|
262
|
-
sourceFile.addStatements(`export { ${name} as default, ${ssrName} };`);
|
|
263
|
-
}
|
|
264
|
-
else {
|
|
265
|
-
sourceFile.addStatements(`export { ${ssrName}, ${name} };`);
|
|
266
|
-
}
|
|
267
|
-
});
|
|
268
|
-
// Clean up any remaining export assignments
|
|
269
|
-
sourceFile
|
|
270
|
-
.getDescendantsOfKind(SyntaxKind.ExportAssignment)
|
|
271
|
-
.forEach((node) => {
|
|
272
|
-
// If it's not an arrow function (which we handle separately),
|
|
273
|
-
// just remove the export assignment
|
|
274
|
-
node.remove();
|
|
275
|
-
});
|
|
276
|
-
const emitOutput = sourceFile.getEmitOutput();
|
|
277
|
-
let sourceMap;
|
|
278
|
-
for (const outputFile of emitOutput.getOutputFiles()) {
|
|
279
|
-
if (outputFile.getFilePath().endsWith(".js.map")) {
|
|
280
|
-
sourceMap = JSON.parse(outputFile.getText());
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
return {
|
|
284
|
-
code: sourceFile.getFullText(),
|
|
285
|
-
map: sourceMap,
|
|
286
|
-
};
|
|
287
|
-
}
|
|
288
|
-
export const useClientPlugin = () => ({
|
|
289
|
-
name: "rwsdk:use-client",
|
|
290
|
-
async transform(code, id) {
|
|
291
|
-
if (id.includes(".vite/deps") ||
|
|
292
|
-
id.includes("node_modules") ||
|
|
293
|
-
this.environment.name !== "worker") {
|
|
294
|
-
return;
|
|
295
|
-
}
|
|
296
|
-
const relativeId = `/${relative(this.environment.getTopLevelConfig().root, id)}`;
|
|
297
|
-
return transformUseClientCode(code, relativeId);
|
|
298
|
-
},
|
|
299
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|