@ivogt/rsc-router 0.0.0-experimental.6 → 0.0.0-experimental.8
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/expose-action-id.d.ts +18 -0
- package/dist/expose-handle-id.d.ts +19 -0
- package/dist/expose-loader-id.d.ts +16 -0
- package/dist/expose-location-state-id.d.ts +19 -0
- package/dist/index.d.ts +123 -0
- package/dist/package-resolution.d.ts +42 -0
- package/dist/virtual-entries.d.ts +24 -0
- package/dist/vite/index.js +1237 -0
- package/package.json +5 -2
- package/src/router.ts +42 -0
- package/src/rsc/handler.ts +69 -5
- package/src/vite/index.ts +19 -181
- package/src/vite/package-resolution.ts +125 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Plugin } from "vite";
|
|
2
|
+
/**
|
|
3
|
+
* Vite plugin that exposes action IDs on server reference functions.
|
|
4
|
+
*
|
|
5
|
+
* When React Server Components creates server references via createServerReference(),
|
|
6
|
+
* the action ID (format: "hash#actionName") is passed as the first argument but not
|
|
7
|
+
* exposed on the returned function. This plugin transforms the output to attach
|
|
8
|
+
* the $id property to each server reference function, enabling the router to
|
|
9
|
+
* identify which action was called during revalidation.
|
|
10
|
+
*
|
|
11
|
+
* Server bundles (RSC/SSR) get file paths in $id for filtering (e.g., "src/actions.ts#add").
|
|
12
|
+
* Client bundles keep hashed IDs for security (e.g., "ec387bc704d4#add").
|
|
13
|
+
*
|
|
14
|
+
* Works in:
|
|
15
|
+
* - Build mode: uses renderChunk to transform bundled chunks
|
|
16
|
+
* - Dev mode: uses transform with enforce:"post" to transform after RSC plugin
|
|
17
|
+
*/
|
|
18
|
+
export declare function exposeActionId(): Plugin;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Plugin } from "vite";
|
|
2
|
+
/**
|
|
3
|
+
* Vite plugin that exposes $$id on createHandle calls.
|
|
4
|
+
*
|
|
5
|
+
* When users create handles with createHandle(), this plugin:
|
|
6
|
+
* 1. Injects a $$id as the last parameter (used as the handle name)
|
|
7
|
+
* 2. Sets $$id property on the exported constant for external access
|
|
8
|
+
*
|
|
9
|
+
* This allows handles to be created without explicit names:
|
|
10
|
+
* - Before: export const Breadcrumbs = createHandle<Item>("breadcrumbs")
|
|
11
|
+
* - After: export const Breadcrumbs = createHandle<Item>()
|
|
12
|
+
*
|
|
13
|
+
* The name is auto-generated from file path + export name.
|
|
14
|
+
*
|
|
15
|
+
* Requirements:
|
|
16
|
+
* - Must use direct import: import { createHandle } from "rsc-router"
|
|
17
|
+
* - Must use named export: export const MyHandle = createHandle(...)
|
|
18
|
+
*/
|
|
19
|
+
export declare function exposeHandleId(): Plugin;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Plugin } from "vite";
|
|
2
|
+
/**
|
|
3
|
+
* Vite plugin that exposes $$id on createLoader calls and generates a loader manifest.
|
|
4
|
+
*
|
|
5
|
+
* When users create loaders with createLoader(), this plugin:
|
|
6
|
+
* 1. Injects a $$id property containing the file path and export name
|
|
7
|
+
* 2. Tracks all loaders and generates a virtual manifest module
|
|
8
|
+
*
|
|
9
|
+
* The manifest can be imported by the RSC handler to get all loaders.
|
|
10
|
+
*
|
|
11
|
+
* Requirements:
|
|
12
|
+
* - Must use direct import: import { createLoader } from "rsc-router"
|
|
13
|
+
* - No aliasing support (import { createLoader as cl } won't work)
|
|
14
|
+
* - Must use named export: export const MyLoader = createLoader(...)
|
|
15
|
+
*/
|
|
16
|
+
export declare function exposeLoaderId(): Plugin;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Plugin } from "vite";
|
|
2
|
+
/**
|
|
3
|
+
* Vite plugin that exposes location state keys on createLocationState calls.
|
|
4
|
+
*
|
|
5
|
+
* When users create location states with createLocationState(), this plugin:
|
|
6
|
+
* 1. Injects an auto-generated key as the first parameter
|
|
7
|
+
* 2. Sets __rsc_ls_key property for verification
|
|
8
|
+
*
|
|
9
|
+
* This allows location states to be created without explicit keys:
|
|
10
|
+
* - Before: export const ProductState = createLocationState<Product>("product")
|
|
11
|
+
* - After: export const ProductState = createLocationState<Product>()
|
|
12
|
+
*
|
|
13
|
+
* The key is auto-generated from file path + export name.
|
|
14
|
+
*
|
|
15
|
+
* Requirements:
|
|
16
|
+
* - Must use direct import: import { createLocationState } from "rsc-router"
|
|
17
|
+
* - Must use named export: export const MyState = createLocationState(...)
|
|
18
|
+
*/
|
|
19
|
+
export declare function exposeLocationStateId(): Plugin;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import type { PluginOption } from "vite";
|
|
2
|
+
export { exposeActionId } from "./expose-action-id.ts";
|
|
3
|
+
export { exposeLoaderId } from "./expose-loader-id.ts";
|
|
4
|
+
export { exposeHandleId } from "./expose-handle-id.ts";
|
|
5
|
+
export { exposeLocationStateId } from "./expose-location-state-id.ts";
|
|
6
|
+
/**
|
|
7
|
+
* RSC plugin entry points configuration.
|
|
8
|
+
* All entries use virtual modules by default. Specify a path to use a custom entry file.
|
|
9
|
+
*/
|
|
10
|
+
export interface RscEntries {
|
|
11
|
+
/**
|
|
12
|
+
* Path to a custom browser/client entry file.
|
|
13
|
+
* If not specified, a default virtual entry is used.
|
|
14
|
+
*/
|
|
15
|
+
client?: string;
|
|
16
|
+
/**
|
|
17
|
+
* Path to a custom SSR entry file.
|
|
18
|
+
* If not specified, a default virtual entry is used.
|
|
19
|
+
*/
|
|
20
|
+
ssr?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Path to a custom RSC entry file.
|
|
23
|
+
* If not specified, a default virtual entry is used that imports the router from the `entry` option.
|
|
24
|
+
*/
|
|
25
|
+
rsc?: string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Options for @vitejs/plugin-rsc integration
|
|
29
|
+
*/
|
|
30
|
+
export interface RscPluginOptions {
|
|
31
|
+
/**
|
|
32
|
+
* Entry points for client, ssr, and rsc environments.
|
|
33
|
+
* All entries use virtual modules by default.
|
|
34
|
+
* Specify paths only when you need custom entry files.
|
|
35
|
+
*/
|
|
36
|
+
entries?: RscEntries;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Base options shared by all presets
|
|
40
|
+
*/
|
|
41
|
+
interface RscRouterBaseOptions {
|
|
42
|
+
/**
|
|
43
|
+
* Expose $$id property on server action functions.
|
|
44
|
+
* Required for action-based revalidation to work.
|
|
45
|
+
* @default true
|
|
46
|
+
*/
|
|
47
|
+
exposeActionId?: boolean;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Options for Node.js deployment (default)
|
|
51
|
+
*/
|
|
52
|
+
export interface RscRouterNodeOptions extends RscRouterBaseOptions {
|
|
53
|
+
/**
|
|
54
|
+
* Deployment preset. Defaults to 'node' when not specified.
|
|
55
|
+
*/
|
|
56
|
+
preset?: "node";
|
|
57
|
+
/**
|
|
58
|
+
* Path to your router configuration file that exports the route tree.
|
|
59
|
+
* This file must export a `router` object created with `createRouter()`.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* rscRouter({ router: './src/router.tsx' })
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
router: string;
|
|
67
|
+
/**
|
|
68
|
+
* RSC plugin configuration. By default, rsc-router includes @vitejs/plugin-rsc
|
|
69
|
+
* with sensible defaults.
|
|
70
|
+
*
|
|
71
|
+
* Entry files (browser, ssr, rsc) are optional - if they don't exist,
|
|
72
|
+
* virtual defaults are used.
|
|
73
|
+
*
|
|
74
|
+
* - Omit or pass `true`/`{}` to use defaults (recommended)
|
|
75
|
+
* - Pass `{ entries: {...} }` to customize entry paths
|
|
76
|
+
* - Pass `false` to disable (for manual @vitejs/plugin-rsc configuration)
|
|
77
|
+
*
|
|
78
|
+
* @default true
|
|
79
|
+
*/
|
|
80
|
+
rsc?: boolean | RscPluginOptions;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Options for Cloudflare Workers deployment
|
|
84
|
+
*/
|
|
85
|
+
export interface RscRouterCloudflareOptions extends RscRouterBaseOptions {
|
|
86
|
+
/**
|
|
87
|
+
* Deployment preset for Cloudflare Workers.
|
|
88
|
+
* When using cloudflare preset:
|
|
89
|
+
* - @vitejs/plugin-rsc is NOT added (cloudflare plugin adds it)
|
|
90
|
+
* - Your worker entry (e.g., worker.rsc.tsx) imports the router directly
|
|
91
|
+
* - Browser and SSR use virtual entries
|
|
92
|
+
*/
|
|
93
|
+
preset: "cloudflare";
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Options for rscRouter plugin
|
|
97
|
+
*/
|
|
98
|
+
export type RscRouterOptions = RscRouterNodeOptions | RscRouterCloudflareOptions;
|
|
99
|
+
/**
|
|
100
|
+
* Vite plugin for rsc-router.
|
|
101
|
+
*
|
|
102
|
+
* Includes @vitejs/plugin-rsc and all necessary transforms for the router
|
|
103
|
+
* to function correctly with React Server Components.
|
|
104
|
+
*
|
|
105
|
+
* @example Node.js (default)
|
|
106
|
+
* ```ts
|
|
107
|
+
* export default defineConfig({
|
|
108
|
+
* plugins: [react(), rscRouter({ router: './src/router.tsx' })],
|
|
109
|
+
* });
|
|
110
|
+
* ```
|
|
111
|
+
*
|
|
112
|
+
* @example Cloudflare Workers
|
|
113
|
+
* ```ts
|
|
114
|
+
* export default defineConfig({
|
|
115
|
+
* plugins: [
|
|
116
|
+
* react(),
|
|
117
|
+
* rscRouter({ preset: 'cloudflare' }),
|
|
118
|
+
* cloudflare({ viteEnvironment: { name: 'rsc' } }),
|
|
119
|
+
* ],
|
|
120
|
+
* });
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
export declare function rscRouter(options: RscRouterOptions): Promise<PluginOption[]>;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Package Resolution Utilities
|
|
3
|
+
*
|
|
4
|
+
* Handles detection of workspace vs npm install context and generates
|
|
5
|
+
* appropriate aliases and exclude lists for Vite configuration.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Get the published package name (e.g., "@ivogt/rsc-router")
|
|
9
|
+
*/
|
|
10
|
+
export declare function getPublishedPackageName(): string;
|
|
11
|
+
/**
|
|
12
|
+
* Check if the package is installed from npm (scoped) vs workspace (unscoped)
|
|
13
|
+
*
|
|
14
|
+
* In workspace development:
|
|
15
|
+
* - Package is installed as "rsc-router" via pnpm workspace alias
|
|
16
|
+
* - The scoped name (@ivogt/rsc-router) doesn't exist in node_modules
|
|
17
|
+
*
|
|
18
|
+
* When installed from npm:
|
|
19
|
+
* - Package is installed as "@ivogt/rsc-router"
|
|
20
|
+
* - We need aliases to map "rsc-router/*" to "@ivogt/rsc-router/*"
|
|
21
|
+
*/
|
|
22
|
+
export declare function isInstalledFromNpm(): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Check if we're in a monorepo/workspace development context
|
|
25
|
+
*/
|
|
26
|
+
export declare function isWorkspaceDevelopment(): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Generate the list of modules to exclude from Vite's dependency optimization.
|
|
29
|
+
*
|
|
30
|
+
* We include both the published name and the virtual name because
|
|
31
|
+
* Vite's optimizer runs before alias resolution.
|
|
32
|
+
*/
|
|
33
|
+
export declare function getExcludeDeps(): string[];
|
|
34
|
+
/**
|
|
35
|
+
* Generate aliases to map virtual package paths to the actual published package.
|
|
36
|
+
*
|
|
37
|
+
* Only needed when installed from npm, where the package is under @ivogt/rsc-router
|
|
38
|
+
* but virtual entries import from rsc-router/*.
|
|
39
|
+
*
|
|
40
|
+
* Returns empty object in workspace development where rsc-router resolves directly.
|
|
41
|
+
*/
|
|
42
|
+
export declare function getPackageAliases(): Record<string, string>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default virtual entry file contents for rsc-router.
|
|
3
|
+
* These are used when users don't provide their own entry files.
|
|
4
|
+
*/
|
|
5
|
+
export declare const VIRTUAL_ENTRY_BROWSER: string;
|
|
6
|
+
export declare const VIRTUAL_ENTRY_SSR: string;
|
|
7
|
+
/**
|
|
8
|
+
* Generate the RSC entry content with the specified router path
|
|
9
|
+
*/
|
|
10
|
+
export declare function getVirtualEntryRSC(routerPath: string): string;
|
|
11
|
+
/**
|
|
12
|
+
* Virtual module IDs
|
|
13
|
+
*/
|
|
14
|
+
export declare const VIRTUAL_IDS: {
|
|
15
|
+
readonly browser: "virtual:rsc-router/entry.browser.js";
|
|
16
|
+
readonly ssr: "virtual:rsc-router/entry.ssr.js";
|
|
17
|
+
readonly rsc: "virtual:rsc-router/entry.rsc.js";
|
|
18
|
+
readonly version: "rsc-router:version";
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Virtual module content for version.
|
|
22
|
+
* Exports VERSION - a timestamp that changes on server restart (dev) or at build time (production).
|
|
23
|
+
*/
|
|
24
|
+
export declare function getVirtualVersionContent(version: string): string;
|