rwsdk 0.1.0-alpha.7 → 0.1.0-alpha.9
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 +2 -3
- package/dist/runtime/lib/realtime/worker.d.ts +1 -1
- package/dist/runtime/lib/router.d.ts +25 -25
- package/dist/runtime/worker.d.ts +2 -1
- package/dist/runtime/worker.js +4 -4
- package/dist/vite/createDirectiveLookupPlugin.mjs +8 -0
- package/dist/vite/directivesPlugin.mjs +25 -5
- package/package.json +1 -1
package/dist/runtime/client.js
CHANGED
|
@@ -43,8 +43,7 @@ export const initClient = async ({ transport = fetchTransport, } = {}) => {
|
|
|
43
43
|
}
|
|
44
44
|
const React = await import("react");
|
|
45
45
|
const { hydrateRoot } = await import("react-dom/client");
|
|
46
|
-
|
|
47
|
-
const { createFromReadableStream, createFromFetch, encodeReply } = await import("react-server-dom-webpack/client.browser");
|
|
46
|
+
const { createFromReadableStream } = await import("react-server-dom-webpack/client.browser");
|
|
48
47
|
const { rscStream } = await import("rsc-html-stream/client");
|
|
49
48
|
let rscPayload;
|
|
50
49
|
rscPayload ??= createFromReadableStream(rscStream, {
|
|
@@ -59,7 +58,7 @@ export const initClient = async ({ transport = fetchTransport, } = {}) => {
|
|
|
59
58
|
hydrateRoot(rootEl, _jsx(Content, {}));
|
|
60
59
|
if (import.meta.hot) {
|
|
61
60
|
import.meta.hot.on("rsc:update", (e) => {
|
|
62
|
-
console.log("[
|
|
61
|
+
console.log("[rwsdk] hot update", e.file);
|
|
63
62
|
callServer("__rsc_hot_update", [e.file]);
|
|
64
63
|
});
|
|
65
64
|
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { RealtimeDurableObject } from "./durableObject";
|
|
2
2
|
export { renderRealtimeClients } from "./renderRealtimeClients";
|
|
3
|
-
export declare const realtimeRoute: (getDurableObjectNamespace: (env: Cloudflare.Env) => DurableObjectNamespace<RealtimeDurableObject>) => import("../router").RouteDefinition
|
|
3
|
+
export declare const realtimeRoute: (getDurableObjectNamespace: (env: Cloudflare.Env) => DurableObjectNamespace<RealtimeDurableObject>) => import("../router").RouteDefinition<import("../../requestInfo/types").RequestInfo<any, import("../../requestInfo/types").DefaultAppContext>>;
|
|
@@ -1,51 +1,51 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
import { RequestInfo } from "../requestInfo/types";
|
|
3
|
-
export type DocumentProps = RequestInfo & {
|
|
3
|
+
export type DocumentProps<T extends RequestInfo = RequestInfo> = T & {
|
|
4
4
|
children: React.ReactNode;
|
|
5
5
|
};
|
|
6
|
-
export type LayoutProps = {
|
|
6
|
+
export type LayoutProps<T extends RequestInfo = RequestInfo> = {
|
|
7
7
|
children?: React.ReactNode;
|
|
8
|
-
requestInfo?:
|
|
8
|
+
requestInfo?: T;
|
|
9
9
|
};
|
|
10
10
|
export type RwContext = {
|
|
11
11
|
nonce: string;
|
|
12
|
-
Document: React.FC<DocumentProps
|
|
12
|
+
Document: React.FC<DocumentProps<any>>;
|
|
13
13
|
rscPayload: boolean;
|
|
14
|
-
layouts?: React.FC<LayoutProps
|
|
14
|
+
layouts?: React.FC<LayoutProps<any>>[];
|
|
15
15
|
};
|
|
16
|
-
export type RouteMiddleware = (requestInfo:
|
|
17
|
-
type RouteFunction = (requestInfo:
|
|
16
|
+
export type RouteMiddleware<T extends RequestInfo = RequestInfo> = (requestInfo: T) => Response | Promise<Response> | void | Promise<void> | Promise<Response | void>;
|
|
17
|
+
type RouteFunction<T extends RequestInfo = RequestInfo> = (requestInfo: T) => Response | Promise<Response>;
|
|
18
18
|
type MaybePromise<T> = T | Promise<T>;
|
|
19
|
-
type RouteComponent = (requestInfo:
|
|
20
|
-
type RouteHandler = RouteFunction | RouteComponent | [...RouteMiddleware[], RouteFunction | RouteComponent];
|
|
21
|
-
export type Route = RouteMiddleware | RouteDefinition | Array<Route
|
|
22
|
-
export type RouteDefinition = {
|
|
19
|
+
type RouteComponent<T extends RequestInfo = RequestInfo> = (requestInfo: T) => MaybePromise<React.JSX.Element | Response>;
|
|
20
|
+
type RouteHandler<T extends RequestInfo = RequestInfo> = RouteFunction<T> | RouteComponent<T> | [...RouteMiddleware<T>[], RouteFunction<T> | RouteComponent<T>];
|
|
21
|
+
export type Route<T extends RequestInfo = RequestInfo> = RouteMiddleware<T> | RouteDefinition<T> | Array<Route<T>>;
|
|
22
|
+
export type RouteDefinition<T extends RequestInfo = RequestInfo> = {
|
|
23
23
|
path: string;
|
|
24
|
-
handler: RouteHandler
|
|
25
|
-
layouts?: React.FC<LayoutProps
|
|
24
|
+
handler: RouteHandler<T>;
|
|
25
|
+
layouts?: React.FC<LayoutProps<T>>[];
|
|
26
26
|
};
|
|
27
|
-
export declare function matchPath(routePath: string, requestPath: string):
|
|
28
|
-
export declare function defineRoutes(routes: Route[]): {
|
|
29
|
-
routes: Route[];
|
|
27
|
+
export declare function matchPath<T extends RequestInfo = RequestInfo>(routePath: string, requestPath: string): T["params"] | null;
|
|
28
|
+
export declare function defineRoutes<T extends RequestInfo = RequestInfo>(routes: Route<T>[]): {
|
|
29
|
+
routes: Route<T>[];
|
|
30
30
|
handle: ({ request, renderPage, getRequestInfo, onError, runWithRequestInfoOverrides, }: {
|
|
31
31
|
request: Request;
|
|
32
|
-
renderPage: (requestInfo:
|
|
33
|
-
getRequestInfo: () =>
|
|
32
|
+
renderPage: (requestInfo: T, Page: React.FC, onError: (error: unknown) => void) => Promise<Response>;
|
|
33
|
+
getRequestInfo: () => T;
|
|
34
34
|
onError: (error: unknown) => void;
|
|
35
|
-
runWithRequestInfoOverrides: <Result>(overrides: Partial<
|
|
35
|
+
runWithRequestInfoOverrides: <Result>(overrides: Partial<T>, fn: () => Promise<Result>) => Promise<Result>;
|
|
36
36
|
}) => Response | Promise<Response>;
|
|
37
37
|
};
|
|
38
|
-
export declare function route(path: string, handler: RouteHandler): RouteDefinition
|
|
39
|
-
export declare function index(handler: RouteHandler): RouteDefinition
|
|
40
|
-
export declare function prefix(prefixPath: string, routes: Route[]): Route[];
|
|
41
|
-
export declare function layout(LayoutComponent: React.FC<LayoutProps
|
|
42
|
-
export declare function render(Document: React.FC<DocumentProps
|
|
38
|
+
export declare function route<T extends RequestInfo = RequestInfo>(path: string, handler: RouteHandler<T>): RouteDefinition<T>;
|
|
39
|
+
export declare function index<T extends RequestInfo = RequestInfo>(handler: RouteHandler<T>): RouteDefinition<T>;
|
|
40
|
+
export declare function prefix<T extends RequestInfo = RequestInfo>(prefixPath: string, routes: Route<T>[]): Route<T>[];
|
|
41
|
+
export declare function layout<T extends RequestInfo = RequestInfo>(LayoutComponent: React.FC<LayoutProps<T>>, routes: Route<T>[]): Route<T>[];
|
|
42
|
+
export declare function render<T extends RequestInfo = RequestInfo>(Document: React.FC<DocumentProps<T>>, routes: Route<T>[],
|
|
43
43
|
/**
|
|
44
44
|
* @param options - Configuration options for rendering.
|
|
45
45
|
* @param options.rscPayload - Toggle the RSC payload that's appended to the Document. Disabling this will mean that interactivity no longer works.
|
|
46
46
|
*/
|
|
47
47
|
options?: {
|
|
48
48
|
rscPayload: boolean;
|
|
49
|
-
}): Route[];
|
|
49
|
+
}): Route<T>[];
|
|
50
50
|
export declare const isClientReference: (Component: React.FC<any>) => boolean;
|
|
51
51
|
export {};
|
package/dist/runtime/worker.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import React from "react";
|
|
2
|
+
import { RequestInfo, DefaultAppContext } from "./requestInfo/types";
|
|
2
3
|
import { Route } from "./lib/router";
|
|
3
4
|
declare global {
|
|
4
5
|
type Env = {
|
|
@@ -6,7 +7,7 @@ declare global {
|
|
|
6
7
|
DB: D1Database;
|
|
7
8
|
};
|
|
8
9
|
}
|
|
9
|
-
export declare const defineApp: (routes: Route[]) => {
|
|
10
|
+
export declare const defineApp: <T extends RequestInfo = RequestInfo<any, DefaultAppContext>>(routes: Route<T>[]) => {
|
|
10
11
|
fetch: (request: Request, env: Env, cf: ExecutionContext) => Promise<Response>;
|
|
11
12
|
};
|
|
12
13
|
export declare const SmokeTestWrapper: React.FC<{
|
package/dist/runtime/worker.js
CHANGED
|
@@ -31,7 +31,8 @@ export const defineApp = (routes) => {
|
|
|
31
31
|
}
|
|
32
32
|
try {
|
|
33
33
|
const url = new URL(request.url);
|
|
34
|
-
const isRSCRequest = url.searchParams.has("__rsc")
|
|
34
|
+
const isRSCRequest = url.searchParams.has("__rsc") ||
|
|
35
|
+
request.headers.get("accept")?.includes("text/x-component");
|
|
35
36
|
const isSmokeTest = url.searchParams.has("__smoke_test");
|
|
36
37
|
const userHeaders = new Headers();
|
|
37
38
|
const rw = {
|
|
@@ -50,8 +51,7 @@ export const defineApp = (routes) => {
|
|
|
50
51
|
const createPageElement = (requestInfo, Page) => {
|
|
51
52
|
let pageElement;
|
|
52
53
|
if (isClientReference(Page)) {
|
|
53
|
-
const { ctx, params } = requestInfo;
|
|
54
|
-
// context(justinvdm, 25 Feb 2025): If the page is a client reference, we need to avoid passing
|
|
54
|
+
const { ctx, params } = requestInfo; // context(justinvdm, 25 Feb 2025): If the page is a client reference, we need to avoid passing
|
|
55
55
|
// down props the client shouldn't get (e.g. env). For safety, we pick the allowed props explicitly.
|
|
56
56
|
pageElement = _jsx(Page, { ctx: ctx, params: params });
|
|
57
57
|
}
|
|
@@ -122,7 +122,7 @@ export const defineApp = (routes) => {
|
|
|
122
122
|
resolve(await router.handle({
|
|
123
123
|
request,
|
|
124
124
|
renderPage,
|
|
125
|
-
getRequestInfo,
|
|
125
|
+
getRequestInfo: getRequestInfo,
|
|
126
126
|
runWithRequestInfoOverrides,
|
|
127
127
|
onError: reject,
|
|
128
128
|
}));
|
|
@@ -135,6 +135,14 @@ export const createDirectiveLookupPlugin = async ({ projectRootDir, files, confi
|
|
|
135
135
|
aliases.push({ find: findRegex, replacement: actualFilePath });
|
|
136
136
|
verboseLog("Added alias for `node_modules` module matching directive in env=%s: %s -> %s", env, file, actualFilePath);
|
|
137
137
|
}
|
|
138
|
+
else {
|
|
139
|
+
verboseLog("Adding to optimizeDeps.entries: %s", actualFilePath);
|
|
140
|
+
const entries = Array.isArray(viteConfig.optimizeDeps.entries)
|
|
141
|
+
? viteConfig.optimizeDeps.entries
|
|
142
|
+
: [].concat(viteConfig.optimizeDeps.entries ?? []);
|
|
143
|
+
viteConfig.optimizeDeps.entries = entries;
|
|
144
|
+
entries.push(actualFilePath);
|
|
145
|
+
}
|
|
138
146
|
}
|
|
139
147
|
log("Environment configuration complete for env=%s with %d optimizeDeps includes and %d aliases", env, Array.from(files).filter((f) => f.includes("/node_modules/")).length, files.size);
|
|
140
148
|
}
|
|
@@ -1,9 +1,31 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
1
3
|
import debug from "debug";
|
|
2
4
|
import { transformClientComponents } from "./transformClientComponents.mjs";
|
|
3
5
|
import { transformServerFunctions } from "./transformServerFunctions.mjs";
|
|
4
6
|
import { normalizeModulePath } from "./normalizeModulePath.mjs";
|
|
5
7
|
const log = debug("rwsdk:vite:rsc-directives-plugin");
|
|
6
8
|
const verboseLog = debug("verbose:rwsdk:vite:rsc-directives-plugin");
|
|
9
|
+
const getLoader = (filePath) => {
|
|
10
|
+
const ext = path.extname(filePath).slice(1);
|
|
11
|
+
switch (ext) {
|
|
12
|
+
case "mjs":
|
|
13
|
+
case "cjs":
|
|
14
|
+
return "js";
|
|
15
|
+
case "mts":
|
|
16
|
+
case "cts":
|
|
17
|
+
return "ts";
|
|
18
|
+
case "jsx":
|
|
19
|
+
return "jsx";
|
|
20
|
+
case "tsx":
|
|
21
|
+
return "tsx";
|
|
22
|
+
case "ts":
|
|
23
|
+
return "ts";
|
|
24
|
+
case "js":
|
|
25
|
+
default:
|
|
26
|
+
return "js";
|
|
27
|
+
}
|
|
28
|
+
};
|
|
7
29
|
export const directivesPlugin = ({ projectRootDir, clientFiles, serverFiles, }) => ({
|
|
8
30
|
name: "rwsdk:rsc-directives",
|
|
9
31
|
async transform(code, id) {
|
|
@@ -39,10 +61,8 @@ export const directivesPlugin = ({ projectRootDir, clientFiles, serverFiles, })
|
|
|
39
61
|
name: "rsc-directives-esbuild-transform",
|
|
40
62
|
setup(build) {
|
|
41
63
|
log("Setting up esbuild plugin for environment: %s", env);
|
|
42
|
-
build.onLoad({ filter:
|
|
64
|
+
build.onLoad({ filter: /\.(js|ts|jsx|tsx|mts|mjs|cjs)$/ }, async (args) => {
|
|
43
65
|
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
66
|
let code;
|
|
47
67
|
try {
|
|
48
68
|
code = await fs.readFile(args.path, "utf-8");
|
|
@@ -60,7 +80,7 @@ export const directivesPlugin = ({ projectRootDir, clientFiles, serverFiles, })
|
|
|
60
80
|
log("Esbuild client component transformation successful for path=%s", args.path);
|
|
61
81
|
return {
|
|
62
82
|
contents: clientResult.code,
|
|
63
|
-
loader:
|
|
83
|
+
loader: getLoader(args.path),
|
|
64
84
|
};
|
|
65
85
|
}
|
|
66
86
|
const serverResult = transformServerFunctions(code, normalizeModulePath(projectRootDir, args.path), env, serverFiles);
|
|
@@ -68,7 +88,7 @@ export const directivesPlugin = ({ projectRootDir, clientFiles, serverFiles, })
|
|
|
68
88
|
log("Esbuild server function transformation successful for path=%s", args.path);
|
|
69
89
|
return {
|
|
70
90
|
contents: serverResult.code,
|
|
71
|
-
loader:
|
|
91
|
+
loader: getLoader(args.path),
|
|
72
92
|
};
|
|
73
93
|
}
|
|
74
94
|
verboseLog("Esbuild no transformation applied for path=%s", args.path);
|