A tool for people curious about the React Server Components protocol
rscexplorer.dev/
rsc
react
1import React from "react";
2
3declare const __webpack_module_cache__: Record<string, { exports: unknown }>;
4
5export function registerClientModule(moduleId: string, moduleExports: unknown): void {
6 if (typeof __webpack_module_cache__ !== "undefined") {
7 __webpack_module_cache__[moduleId] = { exports: moduleExports };
8 }
9}
10
11type ModuleExports = Record<string, unknown>;
12type RequireFn = (id: string) => unknown;
13
14export function evaluateClientModule(compiledCode: string): ModuleExports {
15 const module: { exports: ModuleExports } = { exports: {} };
16 const require: RequireFn = (id: string): unknown => {
17 if (id === "react") return React;
18 throw new Error(`Module "${id}" not found in client context`);
19 };
20 const fn = new Function("module", "exports", "require", "React", compiledCode) as (
21 module: { exports: ModuleExports },
22 exports: ModuleExports,
23 require: RequireFn,
24 ReactLib: typeof React,
25 ) => void;
26 fn(module, module.exports, require, React);
27 return module.exports;
28}