rwsdk 0.1.4 → 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/ssrBridge.js +2 -2
- package/dist/runtime/worker.js +1 -0
- package/dist/scripts/ensure-deploy-env.mjs +8 -8
- 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 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { transformServerFunctions } from "./useServerPlugin.mjs";
|
|
3
|
-
describe("useServerPlugin", () => {
|
|
4
|
-
let COMMENT_CODE = `
|
|
5
|
-
// Comment
|
|
6
|
-
"use server";
|
|
7
|
-
|
|
8
|
-
export function sum() {
|
|
9
|
-
return 1 + 1;
|
|
10
|
-
}
|
|
11
|
-
`;
|
|
12
|
-
let MULTI_LINE_COMMENT_CODE = `
|
|
13
|
-
// Multi-line
|
|
14
|
-
// Comment
|
|
15
|
-
"use server";
|
|
16
|
-
|
|
17
|
-
export function sum() {
|
|
18
|
-
return 1 + 1
|
|
19
|
-
}
|
|
20
|
-
`;
|
|
21
|
-
let COMMENT_BLOCK_CODE = `
|
|
22
|
-
/* Giant
|
|
23
|
-
* Comment
|
|
24
|
-
* Block
|
|
25
|
-
*/
|
|
26
|
-
"use server";
|
|
27
|
-
|
|
28
|
-
export function sum() {
|
|
29
|
-
return 1 + 1
|
|
30
|
-
}
|
|
31
|
-
`;
|
|
32
|
-
let DEFAULT_EXPORT_CODE = `
|
|
33
|
-
"use server";
|
|
34
|
-
|
|
35
|
-
export default function sum() {
|
|
36
|
-
return 1 + 1;
|
|
37
|
-
}
|
|
38
|
-
`;
|
|
39
|
-
let DEFAULT_AND_NAMED_EXPORTS_CODE = `
|
|
40
|
-
"use server";
|
|
41
|
-
|
|
42
|
-
export function sum() {
|
|
43
|
-
return 1 + 1;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export default function sum() {
|
|
47
|
-
return 1 + 2;
|
|
48
|
-
}
|
|
49
|
-
`;
|
|
50
|
-
let NAMED_EXPORT_CODE = `
|
|
51
|
-
"use server";
|
|
52
|
-
|
|
53
|
-
export function sum() {
|
|
54
|
-
return 1 + 1
|
|
55
|
-
}
|
|
56
|
-
`;
|
|
57
|
-
let ARROW_FUNCTION_EXPORT_CODE = `
|
|
58
|
-
"use server";
|
|
59
|
-
|
|
60
|
-
export const sum = () => {
|
|
61
|
-
return 1 + 1
|
|
62
|
-
}
|
|
63
|
-
`;
|
|
64
|
-
let ASYNC_FUNCTION_EXPORT_CODE = `
|
|
65
|
-
"use server";
|
|
66
|
-
|
|
67
|
-
export async function sum() {
|
|
68
|
-
return 1 + 1
|
|
69
|
-
}
|
|
70
|
-
`;
|
|
71
|
-
let IGNORE_NON_FUNCTION_EXPORT_CODE = `
|
|
72
|
-
"use server";
|
|
73
|
-
|
|
74
|
-
export const a = "string";
|
|
75
|
-
`;
|
|
76
|
-
const TEST_CASES = {
|
|
77
|
-
COMMENT_CODE,
|
|
78
|
-
MULTI_LINE_COMMENT_CODE,
|
|
79
|
-
COMMENT_BLOCK_CODE,
|
|
80
|
-
DEFAULT_EXPORT_CODE,
|
|
81
|
-
NAMED_EXPORT_CODE,
|
|
82
|
-
ARROW_FUNCTION_EXPORT_CODE,
|
|
83
|
-
ASYNC_FUNCTION_EXPORT_CODE,
|
|
84
|
-
IGNORE_NON_FUNCTION_EXPORT_CODE,
|
|
85
|
-
DEFAULT_AND_NAMED_EXPORTS_CODE,
|
|
86
|
-
};
|
|
87
|
-
describe("TRANSFORMS", () => {
|
|
88
|
-
for (const [key, CODE] of Object.entries(TEST_CASES)) {
|
|
89
|
-
describe(key, () => {
|
|
90
|
-
it(`CLIENT`, () => {
|
|
91
|
-
expect(transformServerFunctions(CODE, "/test.tsx", "client")).toMatchSnapshot();
|
|
92
|
-
});
|
|
93
|
-
it(`WORKER`, () => {
|
|
94
|
-
expect(transformServerFunctions(CODE, "/test.tsx", "worker")).toMatchSnapshot();
|
|
95
|
-
});
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
});
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* context(justinvdm, 2025-05-14):
|
|
3
|
-
*
|
|
4
|
-
* ## Problem
|
|
5
|
-
* React Server Components (RSC) and traditional SSR require different module
|
|
6
|
-
* resolution:
|
|
7
|
-
* - RSC modules must resolve with the "react-server" export condition
|
|
8
|
-
* - SSR modules must resolve without it
|
|
9
|
-
*
|
|
10
|
-
* This presents a challenge for our framework, where the same modules often
|
|
11
|
-
* need to run in both modes — within a single Cloudflare Worker runtime. We
|
|
12
|
-
* can't split execution contexts or afford duplicated builds.
|
|
13
|
-
*
|
|
14
|
-
* Vite provides an elegant way to manage distinct resolution graphs via its
|
|
15
|
-
* `environments` feature (`client`, `ssr`, `worker`, etc.). Each environment
|
|
16
|
-
* can use different export conditions, plugins, and optimizeDeps configs.
|
|
17
|
-
*
|
|
18
|
-
* However, using separate environments implies separate output bundles. In our
|
|
19
|
-
* case, that would nearly double the final bundle size — which is not viable
|
|
20
|
-
* given Cloudflare Workers' strict 3MB limit.
|
|
21
|
-
*
|
|
22
|
-
* ## Solution
|
|
23
|
-
* We run both RSC and SSR from a single Vite `worker` environment. To simulate
|
|
24
|
-
* distinct resolution graphs, we virtualize SSR imports using a prefix.
|
|
25
|
-
*
|
|
26
|
-
* How it works:
|
|
27
|
-
* - Maintain an SSR subgraph as part of the worker environment's module graph.
|
|
28
|
-
* Any time we see "use client", we enter the subgraph.
|
|
29
|
-
* - We keep the graphs separate by rewriting imports in SSR graph to map to virtual files.
|
|
30
|
-
* - Bare imports to deps get resolved using a custom resolver so that we use
|
|
31
|
-
* import conditions relevant to SSR - note the lack of "react-server"
|
|
32
|
-
* condition: ["workerd", "edge", "import", "default"]
|
|
33
|
-
* - All imports within the subgraph get their path rewritten with the SSR
|
|
34
|
-
* module namespace prefix so that we stay within the subgraph.
|
|
35
|
-
*/
|
|
36
|
-
import { Plugin } from "vite";
|
|
37
|
-
export declare const SSR_NAMESPACE = "virtual:rwsdk:ssr";
|
|
38
|
-
export declare const SSR_NAMESPACE_PREFIX: string;
|
|
39
|
-
export declare const SSR_URL_PREFIX: string;
|
|
40
|
-
export declare const SSR_ESBUILD_NAMESPACE = "__rwsdk_ssr_esbuild_namespace__";
|
|
41
|
-
export declare const SSR_RESOLVER_CONDITION_NAMES: string[];
|
|
42
|
-
export declare const createSSRDepResolver: ({ projectRootDir, }: {
|
|
43
|
-
projectRootDir: string;
|
|
44
|
-
}) => (request: string, importer?: string) => string | false;
|
|
45
|
-
export type VirtualizedSSRContext = {
|
|
46
|
-
projectRootDir: string;
|
|
47
|
-
config: any;
|
|
48
|
-
resolveModule: (request: string, importer: string) => string | false | Promise<string | false>;
|
|
49
|
-
resolveDep: (request: string) => string | false;
|
|
50
|
-
};
|
|
51
|
-
export declare const isSSRPath: (filePath: string) => boolean;
|
|
52
|
-
export declare const ensureNoSSRNamespace: (filePath: string) => string;
|
|
53
|
-
export declare const ensureSSRNamespace: (filePath: string) => string;
|
|
54
|
-
export declare function virtualizedSSRPlugin({ projectRootDir, }: {
|
|
55
|
-
projectRootDir: string;
|
|
56
|
-
}): Plugin;
|
|
@@ -1,464 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* context(justinvdm, 2025-05-14):
|
|
3
|
-
*
|
|
4
|
-
* ## Problem
|
|
5
|
-
* React Server Components (RSC) and traditional SSR require different module
|
|
6
|
-
* resolution:
|
|
7
|
-
* - RSC modules must resolve with the "react-server" export condition
|
|
8
|
-
* - SSR modules must resolve without it
|
|
9
|
-
*
|
|
10
|
-
* This presents a challenge for our framework, where the same modules often
|
|
11
|
-
* need to run in both modes — within a single Cloudflare Worker runtime. We
|
|
12
|
-
* can't split execution contexts or afford duplicated builds.
|
|
13
|
-
*
|
|
14
|
-
* Vite provides an elegant way to manage distinct resolution graphs via its
|
|
15
|
-
* `environments` feature (`client`, `ssr`, `worker`, etc.). Each environment
|
|
16
|
-
* can use different export conditions, plugins, and optimizeDeps configs.
|
|
17
|
-
*
|
|
18
|
-
* However, using separate environments implies separate output bundles. In our
|
|
19
|
-
* case, that would nearly double the final bundle size — which is not viable
|
|
20
|
-
* given Cloudflare Workers' strict 3MB limit.
|
|
21
|
-
*
|
|
22
|
-
* ## Solution
|
|
23
|
-
* We run both RSC and SSR from a single Vite `worker` environment. To simulate
|
|
24
|
-
* distinct resolution graphs, we virtualize SSR imports using a prefix.
|
|
25
|
-
*
|
|
26
|
-
* How it works:
|
|
27
|
-
* - Maintain an SSR subgraph as part of the worker environment's module graph.
|
|
28
|
-
* Any time we see "use client", we enter the subgraph.
|
|
29
|
-
* - We keep the graphs separate by rewriting imports in SSR graph to map to virtual files.
|
|
30
|
-
* - Bare imports to deps get resolved using a custom resolver so that we use
|
|
31
|
-
* import conditions relevant to SSR - note the lack of "react-server"
|
|
32
|
-
* condition: ["workerd", "edge", "import", "default"]
|
|
33
|
-
* - All imports within the subgraph get their path rewritten with the SSR
|
|
34
|
-
* module namespace prefix so that we stay within the subgraph.
|
|
35
|
-
*/
|
|
36
|
-
import path from "path";
|
|
37
|
-
import fs from "fs/promises";
|
|
38
|
-
import { createModuleResolver } from "./moduleResolver.mjs";
|
|
39
|
-
import MagicString from "magic-string";
|
|
40
|
-
import debug from "debug";
|
|
41
|
-
import { ROOT_DIR, DIST_DIR } from "../lib/constants.mjs";
|
|
42
|
-
import { transformClientComponents } from "./transformClientComponents.mjs";
|
|
43
|
-
import { transformServerReferences } from "./transformServerReferences.mjs";
|
|
44
|
-
import { findImportSpecifiers } from "./findImportSpecifiers.mjs";
|
|
45
|
-
import { ensureConfigArrays } from "./ensureConfigArrays.mjs";
|
|
46
|
-
import { isBareImport } from "./isBareImport.mjs";
|
|
47
|
-
export const SSR_NAMESPACE = "virtual:rwsdk:ssr";
|
|
48
|
-
export const SSR_NAMESPACE_PREFIX = SSR_NAMESPACE + ":";
|
|
49
|
-
export const SSR_URL_PREFIX = "/" + SSR_NAMESPACE_PREFIX;
|
|
50
|
-
export const SSR_ESBUILD_NAMESPACE = "__rwsdk_ssr_esbuild_namespace__";
|
|
51
|
-
export const SSR_RESOLVER_CONDITION_NAMES = [
|
|
52
|
-
"workerd",
|
|
53
|
-
"edge",
|
|
54
|
-
"import",
|
|
55
|
-
"default",
|
|
56
|
-
];
|
|
57
|
-
const log = debug("rwsdk:vite:virtualized-ssr");
|
|
58
|
-
const logInfo = log.extend("info");
|
|
59
|
-
const logError = log.extend("error");
|
|
60
|
-
const logResolve = log.extend("resolve");
|
|
61
|
-
const logTransform = log.extend("transform");
|
|
62
|
-
const logEsbuild = debug("rwsdk:vite:virtualized-ssr:esbuild");
|
|
63
|
-
const logEsbuildTransform = logEsbuild.extend("transform");
|
|
64
|
-
const IGNORED_IMPORT_PATTERNS = [
|
|
65
|
-
/^cloudflare:.*/,
|
|
66
|
-
/^react\/jsx-runtime$/,
|
|
67
|
-
/^react\/jsx-dev-runtime$/,
|
|
68
|
-
];
|
|
69
|
-
export const createSSRDepResolver = ({ projectRootDir, }) => createModuleResolver({
|
|
70
|
-
roots: [projectRootDir, ROOT_DIR],
|
|
71
|
-
name: "ssrDep",
|
|
72
|
-
conditionNames: SSR_RESOLVER_CONDITION_NAMES,
|
|
73
|
-
});
|
|
74
|
-
async function resolveSSRPath({ path, importer = ".", context, logFn, }) {
|
|
75
|
-
logFn?.(":resolveSSRPath: called with path=%s, importer=%s", path, importer);
|
|
76
|
-
if (!path.startsWith(SSR_NAMESPACE)) {
|
|
77
|
-
logFn?.(":resolveSSRPath: Skipping non-SSR path: path=%s", path);
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
const raw = ensureNoSSRNamespace(path);
|
|
81
|
-
if (isBareImport(raw)) {
|
|
82
|
-
const ssrResolved = context.resolveDep(raw);
|
|
83
|
-
if (ssrResolved !== false) {
|
|
84
|
-
const resolved = ensureSSRNamespace(ssrResolved);
|
|
85
|
-
logFn?.(":resolveSSRPath: SSR resolver succeeded for bare import import='%s', resolved to resolved='%s'", path, resolved);
|
|
86
|
-
return resolved;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
const moduleResolved = await context.resolveModule(raw, ensureNoSSRNamespace(importer));
|
|
90
|
-
if (moduleResolved) {
|
|
91
|
-
if (moduleResolved.startsWith(DIST_DIR)) {
|
|
92
|
-
if (moduleResolved.includes("__rwsdkssr")) {
|
|
93
|
-
const resolved = ensureSSRNamespace(moduleResolved);
|
|
94
|
-
logFn?.(":resolveSSRPath: Module resolved to an SDK path that contains __rwsdkssr, returning resolved path *with* SSR namespace: moduleResolved=%s", moduleResolved);
|
|
95
|
-
return resolved;
|
|
96
|
-
}
|
|
97
|
-
else {
|
|
98
|
-
logFn?.(":resolveSSRPath: Module resolved to an SDK path, returning resolved path *without* SSR namespace: moduleResolved=%s", moduleResolved);
|
|
99
|
-
return moduleResolved;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
const resolved = ensureSSRNamespace(moduleResolved);
|
|
103
|
-
logFn?.(":resolveSSRPath: Module resolver succeeded for import import='%s' from importer=%s, resolved to moduleResolved='%s'", raw, importer, resolved);
|
|
104
|
-
return resolved;
|
|
105
|
-
}
|
|
106
|
-
else {
|
|
107
|
-
logFn?.(":resolveSSRPath: Module resolver failed for import import='%s' from importer=%s, returning raw path without SSR namespace", raw, importer);
|
|
108
|
-
return raw;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
async function rewriteSSRImports({ code, id, logFn, }) {
|
|
112
|
-
const isSDKPath = ensureNoSSRNamespace(id).startsWith(DIST_DIR);
|
|
113
|
-
logFn?.(":rewriteSSRImports: called for id: id=%s, isSDKPath=%s", id, isSDKPath);
|
|
114
|
-
const imports = findImportSpecifiers(id, code, IGNORED_IMPORT_PATTERNS, logFn);
|
|
115
|
-
logFn?.(":rewriteSSRImports: Found %d imports in id=%s", imports.length, id);
|
|
116
|
-
const ms = new MagicString(code);
|
|
117
|
-
let modified = false;
|
|
118
|
-
for (const i of imports) {
|
|
119
|
-
const raw = i.raw;
|
|
120
|
-
logFn?.(":rewriteSSRImports: Processing import '%s' at [%d, %d] in id=%s", raw, i.s, i.e, id);
|
|
121
|
-
const realPath = ensureNoSSRNamespace(raw);
|
|
122
|
-
if (isSDKPath &&
|
|
123
|
-
!isBareImport(realPath) &&
|
|
124
|
-
!path.isAbsolute(realPath) &&
|
|
125
|
-
!realPath.includes("__rwsdkssr")) {
|
|
126
|
-
logFn?.(":rewriteSSRImports: Skipping import because it is a relative import within the SDK: import='%s', in id=%s", raw, id);
|
|
127
|
-
continue;
|
|
128
|
-
}
|
|
129
|
-
const virtualId = ensureSSRNamespace(raw);
|
|
130
|
-
ms.overwrite(i.s, i.e, virtualId);
|
|
131
|
-
logFn?.(":rewriteSSRImports: Rewrote import import='%s' to virtualId='%s' in id=%s", raw, virtualId, id);
|
|
132
|
-
modified = true;
|
|
133
|
-
}
|
|
134
|
-
if (modified) {
|
|
135
|
-
logFn?.(":rewriteSSRImports: Rewriting complete for id=%s", id);
|
|
136
|
-
return ms;
|
|
137
|
-
}
|
|
138
|
-
else {
|
|
139
|
-
logFn?.(":rewriteSSRImports: No changes made for id=%s", id);
|
|
140
|
-
return null;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
function isSSRModule({ id, code, logFn, esbuild, }) {
|
|
144
|
-
const logger = logFn ?? (() => { });
|
|
145
|
-
if (id.includes("__rwsdkssr")) {
|
|
146
|
-
logger(":isSSRModule: Detected SSR module (includes __rwsdk_ssr): id=%s esbuild=%s", id, !!esbuild);
|
|
147
|
-
return true;
|
|
148
|
-
}
|
|
149
|
-
if (id.startsWith(SSR_NAMESPACE)) {
|
|
150
|
-
logger(":isSSRModule: Detected SSR module (SSR_NAMESPACE): id=%s esbuild=%s", id, !!esbuild);
|
|
151
|
-
return true;
|
|
152
|
-
}
|
|
153
|
-
return false;
|
|
154
|
-
}
|
|
155
|
-
export const isSSRPath = (filePath) => {
|
|
156
|
-
return (filePath.startsWith(SSR_NAMESPACE_PREFIX) ||
|
|
157
|
-
filePath.startsWith(SSR_URL_PREFIX));
|
|
158
|
-
};
|
|
159
|
-
export const ensureNoSSRNamespace = (filePath) => {
|
|
160
|
-
if (filePath.startsWith(SSR_NAMESPACE_PREFIX)) {
|
|
161
|
-
return filePath.slice(SSR_NAMESPACE_PREFIX.length);
|
|
162
|
-
}
|
|
163
|
-
if (filePath.startsWith(SSR_URL_PREFIX)) {
|
|
164
|
-
return filePath.slice(SSR_URL_PREFIX.length);
|
|
165
|
-
}
|
|
166
|
-
return filePath;
|
|
167
|
-
};
|
|
168
|
-
export const ensureSSRNamespace = (filePath) => {
|
|
169
|
-
return SSR_NAMESPACE_PREFIX + ensureNoSSRNamespace(filePath);
|
|
170
|
-
};
|
|
171
|
-
function detectLoader(filePath) {
|
|
172
|
-
const ext = path.extname(filePath).toLowerCase();
|
|
173
|
-
return ext === ".tsx" || ext === ".jsx" ? "tsx" : ext === ".ts" ? "ts" : "js";
|
|
174
|
-
}
|
|
175
|
-
async function esbuildResolveSSRModule({ path, context, importer, }) {
|
|
176
|
-
logEsbuild(":esbuildResolveSSRModule: called with path=%s", path);
|
|
177
|
-
const resolved = await resolveSSRPath({
|
|
178
|
-
path,
|
|
179
|
-
importer,
|
|
180
|
-
context,
|
|
181
|
-
logFn: logEsbuild,
|
|
182
|
-
});
|
|
183
|
-
if (!resolved) {
|
|
184
|
-
logEsbuild(":esbuildResolveSSRModule: path=%s skipped", path);
|
|
185
|
-
return undefined;
|
|
186
|
-
}
|
|
187
|
-
const result = {
|
|
188
|
-
path: resolved,
|
|
189
|
-
};
|
|
190
|
-
if (resolved.startsWith(SSR_NAMESPACE)) {
|
|
191
|
-
result.namespace = SSR_ESBUILD_NAMESPACE;
|
|
192
|
-
}
|
|
193
|
-
else if (isBareImport(resolved)) {
|
|
194
|
-
result.external = true;
|
|
195
|
-
}
|
|
196
|
-
else {
|
|
197
|
-
result.namespace = "file";
|
|
198
|
-
}
|
|
199
|
-
logEsbuild(":esbuildResolveSSRModule: resolved result for path=%s: result=%O", path, result);
|
|
200
|
-
return result;
|
|
201
|
-
}
|
|
202
|
-
async function esbuildLoadAndTransformSSRModule({ filePath, context, logFn, }) {
|
|
203
|
-
const realPath = ensureNoSSRNamespace(filePath);
|
|
204
|
-
if (isBareImport(realPath)) {
|
|
205
|
-
logFn("⏭️ Skipping bare import: %s", realPath);
|
|
206
|
-
return undefined;
|
|
207
|
-
}
|
|
208
|
-
let inputCode;
|
|
209
|
-
try {
|
|
210
|
-
inputCode = await fs.readFile(realPath, "utf-8");
|
|
211
|
-
}
|
|
212
|
-
catch (err) {
|
|
213
|
-
logFn("❌ Failed to read file: %s", realPath);
|
|
214
|
-
return undefined;
|
|
215
|
-
}
|
|
216
|
-
const isSSR = isSSRModule({
|
|
217
|
-
id: filePath,
|
|
218
|
-
code: inputCode,
|
|
219
|
-
logFn,
|
|
220
|
-
esbuild: true,
|
|
221
|
-
});
|
|
222
|
-
let code = inputCode;
|
|
223
|
-
let modified = false;
|
|
224
|
-
const clientResult = await transformClientComponents(code, filePath, {
|
|
225
|
-
environmentName: "worker",
|
|
226
|
-
isEsbuild: true,
|
|
227
|
-
});
|
|
228
|
-
if (clientResult) {
|
|
229
|
-
logFn("🔎 Client component transform complete for %s", filePath);
|
|
230
|
-
code = clientResult.code;
|
|
231
|
-
modified = true;
|
|
232
|
-
}
|
|
233
|
-
else {
|
|
234
|
-
logFn("⏭️ No client component transform needed for %s", filePath);
|
|
235
|
-
}
|
|
236
|
-
const serverResult = await transformServerReferences(code, filePath, {
|
|
237
|
-
environmentName: "worker",
|
|
238
|
-
isEsbuild: true,
|
|
239
|
-
isSSR,
|
|
240
|
-
topLevelRoot: context.projectRootDir,
|
|
241
|
-
});
|
|
242
|
-
if (serverResult) {
|
|
243
|
-
logFn("🔎 Server reference transform complete for %s", filePath);
|
|
244
|
-
code = serverResult.code;
|
|
245
|
-
modified = true;
|
|
246
|
-
}
|
|
247
|
-
else {
|
|
248
|
-
logFn("⏭️ No server reference transform needed for %s", filePath);
|
|
249
|
-
}
|
|
250
|
-
if (isSSR) {
|
|
251
|
-
let rewritten = await rewriteSSRImports({
|
|
252
|
-
code: inputCode,
|
|
253
|
-
id: filePath,
|
|
254
|
-
context,
|
|
255
|
-
logFn,
|
|
256
|
-
});
|
|
257
|
-
if (rewritten) {
|
|
258
|
-
logFn("🔎 Import rewriting complete for %s", filePath);
|
|
259
|
-
code = rewritten.toString();
|
|
260
|
-
modified = true;
|
|
261
|
-
}
|
|
262
|
-
else {
|
|
263
|
-
logFn("⏭️ No import rewriting needed for %s", filePath);
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
if (!modified) {
|
|
267
|
-
if (isSSR) {
|
|
268
|
-
logFn("⏭️ Returning code unmodified for SSR module %s", filePath);
|
|
269
|
-
}
|
|
270
|
-
else {
|
|
271
|
-
logFn("⏭️ Returning code unmodified for non-SSR module %s", filePath);
|
|
272
|
-
return undefined;
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
else {
|
|
276
|
-
logFn("🔎 Returning modified code for %s", filePath);
|
|
277
|
-
if (process.env.VERBOSE) {
|
|
278
|
-
logFn(":VERBOSE: Code for modified %s:\n%s", filePath, code);
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
return {
|
|
282
|
-
contents: code,
|
|
283
|
-
loader: detectLoader(filePath),
|
|
284
|
-
resolveDir: path.dirname(realPath),
|
|
285
|
-
};
|
|
286
|
-
}
|
|
287
|
-
function virtualizedSSREsbuildPlugin(context) {
|
|
288
|
-
return {
|
|
289
|
-
name: "virtualized-ssr-esbuild-plugin",
|
|
290
|
-
setup(build) {
|
|
291
|
-
build.onResolve({ filter: /.*/, namespace: SSR_ESBUILD_NAMESPACE }, async (args) => {
|
|
292
|
-
logEsbuild(":esbuild:onResolve:namespace called with args: %O", args);
|
|
293
|
-
const result = await esbuildResolveSSRModule({
|
|
294
|
-
context,
|
|
295
|
-
path: args.path,
|
|
296
|
-
importer: args.importer,
|
|
297
|
-
});
|
|
298
|
-
logEsbuild(":esbuild:onResolve:namespace resolved result for path=%s: result=%O", args.path, result);
|
|
299
|
-
return result;
|
|
300
|
-
});
|
|
301
|
-
build.onLoad({ filter: /.*/, namespace: SSR_ESBUILD_NAMESPACE }, async (args) => {
|
|
302
|
-
logEsbuild(":esbuild:onLoad:namespace called with args: %O", args);
|
|
303
|
-
return esbuildLoadAndTransformSSRModule({
|
|
304
|
-
filePath: args.path,
|
|
305
|
-
context,
|
|
306
|
-
logFn: logEsbuildTransform,
|
|
307
|
-
});
|
|
308
|
-
});
|
|
309
|
-
build.onResolve({ filter: /^virtual:rwsdk:ssr:/ }, async (args) => {
|
|
310
|
-
logEsbuild(":esbuild:onResolve:prefix called with args: %O", args);
|
|
311
|
-
const result = await esbuildResolveSSRModule({
|
|
312
|
-
context,
|
|
313
|
-
path: args.path,
|
|
314
|
-
importer: args.importer,
|
|
315
|
-
});
|
|
316
|
-
logEsbuild(":esbuild:onResolve:prefix resolved result for path=%s: result=%O", args.path, result);
|
|
317
|
-
return result;
|
|
318
|
-
});
|
|
319
|
-
build.onLoad({ filter: /\.(js|jsx|ts|tsx|mjs|mts)$/ }, async (args) => {
|
|
320
|
-
logEsbuild(":esbuild:onLoad:entry called with args: %O", args);
|
|
321
|
-
const result = await esbuildLoadAndTransformSSRModule({
|
|
322
|
-
filePath: args.path,
|
|
323
|
-
context,
|
|
324
|
-
logFn: logEsbuildTransform,
|
|
325
|
-
});
|
|
326
|
-
if (process.env.VERBOSE) {
|
|
327
|
-
logEsbuild(":esbuild:onLoad:entry result: %O", result);
|
|
328
|
-
}
|
|
329
|
-
return result;
|
|
330
|
-
});
|
|
331
|
-
},
|
|
332
|
-
};
|
|
333
|
-
}
|
|
334
|
-
export function virtualizedSSRPlugin({ projectRootDir, }) {
|
|
335
|
-
logInfo("🚀 Initializing VirtualizedSSR plugin with root: %s", projectRootDir);
|
|
336
|
-
logInfo("📂 Plugin will handle client/server module resolution in a single Vite worker environment");
|
|
337
|
-
const context = {
|
|
338
|
-
projectRootDir,
|
|
339
|
-
config: undefined,
|
|
340
|
-
resolveModule: () => false,
|
|
341
|
-
resolveDep: () => false,
|
|
342
|
-
};
|
|
343
|
-
return {
|
|
344
|
-
name: "rwsdk:virtualized-ssr",
|
|
345
|
-
async configEnvironment(env, config) {
|
|
346
|
-
logInfo(":configEnvironment: Configuring environment: %s", env);
|
|
347
|
-
if (env !== "worker") {
|
|
348
|
-
logInfo(":configEnvironment: Skipping non-worker environment");
|
|
349
|
-
return;
|
|
350
|
-
}
|
|
351
|
-
ensureConfigArrays(config);
|
|
352
|
-
context.resolveModule = createModuleResolver({
|
|
353
|
-
getAliases: () => getAliases(config.resolve ?? {}),
|
|
354
|
-
roots: [projectRootDir],
|
|
355
|
-
name: "resolveModule",
|
|
356
|
-
conditionNames: SSR_RESOLVER_CONDITION_NAMES,
|
|
357
|
-
});
|
|
358
|
-
context.resolveDep = createSSRDepResolver({
|
|
359
|
-
projectRootDir,
|
|
360
|
-
});
|
|
361
|
-
logInfo(":configEnvironment: Setting up aliases for worker environment");
|
|
362
|
-
logInfo(":configEnvironment: Configuration state:");
|
|
363
|
-
logInfo(":configEnvironment: - Project root: %s", projectRootDir);
|
|
364
|
-
logInfo(":configEnvironment: - Virtual SSR namespace: %s", SSR_NAMESPACE);
|
|
365
|
-
config.optimizeDeps.esbuildOptions.plugins = config.optimizeDeps.esbuildOptions.plugins.filter((p) => p?.name !== "virtualized-ssr-esbuild-plugin");
|
|
366
|
-
config.optimizeDeps.esbuildOptions.plugins.unshift(virtualizedSSREsbuildPlugin(context));
|
|
367
|
-
logInfo("✅ Updated Vite config to use only esbuild plugin for SSR virtual deps");
|
|
368
|
-
context.config = config;
|
|
369
|
-
},
|
|
370
|
-
async resolveId(id, importer) {
|
|
371
|
-
if (this.environment.name !== "worker") {
|
|
372
|
-
return;
|
|
373
|
-
}
|
|
374
|
-
logResolve(":plugin:resolveId: called with id: %s", id);
|
|
375
|
-
if (!isSSRPath(id)) {
|
|
376
|
-
logResolve(":plugin:resolveId: Skipping non-SSR namespace: %s", id);
|
|
377
|
-
return id;
|
|
378
|
-
}
|
|
379
|
-
const realPath = ensureNoSSRNamespace(id);
|
|
380
|
-
const result = await resolveSSRPath({
|
|
381
|
-
path: id,
|
|
382
|
-
importer,
|
|
383
|
-
context,
|
|
384
|
-
logFn: logResolve,
|
|
385
|
-
});
|
|
386
|
-
if (!result) {
|
|
387
|
-
logResolve(":plugin:resolveId: path=%s skipped", realPath);
|
|
388
|
-
return;
|
|
389
|
-
}
|
|
390
|
-
logResolve(":plugin:resolveId: resolved result for id=%s: result=%O", id, result);
|
|
391
|
-
return result;
|
|
392
|
-
},
|
|
393
|
-
load(id) {
|
|
394
|
-
if (this.environment.name !== "worker") {
|
|
395
|
-
return;
|
|
396
|
-
}
|
|
397
|
-
logResolve(":plugin:load: called with id: %s", id);
|
|
398
|
-
if (!isSSRPath(id)) {
|
|
399
|
-
logResolve("plugin:load: Skipping non-SSR namespaced id: %s", id);
|
|
400
|
-
return;
|
|
401
|
-
}
|
|
402
|
-
const moduleId = ensureNoSSRNamespace(id);
|
|
403
|
-
try {
|
|
404
|
-
logResolve("📄 load() reading file as separate SSR module: %s", moduleId);
|
|
405
|
-
return fs.readFile(moduleId, "utf-8");
|
|
406
|
-
}
|
|
407
|
-
catch {
|
|
408
|
-
logResolve("❌ load() read failed for: %s", moduleId);
|
|
409
|
-
return null;
|
|
410
|
-
}
|
|
411
|
-
},
|
|
412
|
-
async transform(code, id) {
|
|
413
|
-
logTransform(":plugin:transform: called with id: %s", id);
|
|
414
|
-
if (this.environment.name !== "worker") {
|
|
415
|
-
return null;
|
|
416
|
-
}
|
|
417
|
-
logTransform("📝 Transform: %s", id);
|
|
418
|
-
const isSSR = isSSRModule({
|
|
419
|
-
id,
|
|
420
|
-
code,
|
|
421
|
-
logFn: logTransform,
|
|
422
|
-
esbuild: false,
|
|
423
|
-
});
|
|
424
|
-
if (!isSSR) {
|
|
425
|
-
logTransform("⏭️ Skipping non-SSR module: %s", id);
|
|
426
|
-
return null;
|
|
427
|
-
}
|
|
428
|
-
logTransform("🔎 Processing imports in SSR module: %s", id);
|
|
429
|
-
const rewritten = await rewriteSSRImports({
|
|
430
|
-
code,
|
|
431
|
-
id,
|
|
432
|
-
context,
|
|
433
|
-
logFn: logTransform,
|
|
434
|
-
});
|
|
435
|
-
if (!rewritten) {
|
|
436
|
-
logTransform("⏭️ No changes made for %s", id);
|
|
437
|
-
return null;
|
|
438
|
-
}
|
|
439
|
-
else {
|
|
440
|
-
logTransform("🔎 Rewrote imports for %s", id);
|
|
441
|
-
if (process.env.VERBOSE) {
|
|
442
|
-
logTransform(":VERBOSE: Rewritten code for %s:\n%s", id, rewritten.toString());
|
|
443
|
-
}
|
|
444
|
-
}
|
|
445
|
-
return {
|
|
446
|
-
code: rewritten.toString(),
|
|
447
|
-
map: rewritten.generateMap(),
|
|
448
|
-
};
|
|
449
|
-
},
|
|
450
|
-
};
|
|
451
|
-
}
|
|
452
|
-
function getAliases(resolveConfig) {
|
|
453
|
-
if (!resolveConfig?.alias)
|
|
454
|
-
return [];
|
|
455
|
-
const alias = resolveConfig.alias;
|
|
456
|
-
if (Array.isArray(alias)) {
|
|
457
|
-
return alias;
|
|
458
|
-
}
|
|
459
|
-
// Convert object form to array, ensuring replacement is a string
|
|
460
|
-
return Object.entries(alias).map(([find, replacement]) => ({
|
|
461
|
-
find,
|
|
462
|
-
replacement: String(replacement),
|
|
463
|
-
}));
|
|
464
|
-
}
|
package/dist/vite/wasmPlugin.mjs
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
export const wasmPlugin = () => {
|
|
2
|
-
return {
|
|
3
|
-
name: "rwsdk:wasm",
|
|
4
|
-
resolveId(id) {
|
|
5
|
-
console.log("######", id);
|
|
6
|
-
if (id.startsWith("@id/__CLOUDFLARE_MODULE__CompiledWasm__") &&
|
|
7
|
-
id.endsWith(".wasm__")) {
|
|
8
|
-
const r = id.slice("@id/__CLOUDFLARE_MODULE__CompiledWasm__".length, id.length - "__".length);
|
|
9
|
-
console.log("##################33", r);
|
|
10
|
-
return r;
|
|
11
|
-
}
|
|
12
|
-
},
|
|
13
|
-
};
|
|
14
|
-
};
|
|
File without changes
|