import type { LoadHook, ResolveHook } from "node:module"; import { type Logger } from "../directives/options.js"; import type { LoaderConfig } from "../transformer/types.js"; import type { ReferenceGate } from "../references/index.js"; /** * Resolution policy: how the loader turns the URL Node hands it * (typically `file://…/foo.tsx`) into the hosted module ID that ends up * inside the emitted `registerClientReference` / `registerServerReference` * call. There is no universal answer — the policy depends on where the * containing framework will serve / bundle the client chunks. Pass a * function that maps a URL to the ID you want appearing in the client * reference. */ export type ModuleIDResolver = (filePath: string, source: string, isClientByDirective: boolean) => string; /** * Optional callback invoked after a module has been recognised as a * server / client RSC boundary and transformed. The bundler / * worker-orchestration layer above the loader can use it for whatever * cross-cutting concerns it has — emitting build manifest entries, * posting messages back to a parent thread, populating module graphs, etc. * * The loader itself does not look at the return value. */ export type OnTransformCallback = (info: { url: string; filePath: string; transformedId: string; source: string; isServer: boolean; isClient: boolean; /** The boundary module's exported names — the references the build enumerated. */ exportNames: readonly string[]; }) => void; export interface CreateReactLoaderOptions { /** * RSC-contract overrides — the register-reference names, transport * import paths, directive matchers, etc. Defaults to the React-RSC * shape published in `react-server-dom-esm` (see `DEFAULT_LOADER_CONFIG`). */ loader?: Partial; /** * Maps a file URL to the hosted module ID emitted inside the * transformed module's `registerClientReference` / `registerServerReference` * calls. Required — the answer is consumer-specific. */ moduleID: ModuleIDResolver; /** * Optional callback fired for every module the loader identified as * an RSC boundary, after the transform runs. Useful for upstream * orchestration (worker messaging, build manifests). The loader itself * does not inspect the return value. */ onTransform?: OnTransformCallback; /** * Optional reference gate (the manifest-backed allowlist). When provided, * every RSC boundary the loader transforms is registered into it, keyed by * the hosted id `moduleID` emitted, with an importer bound to the module's * real URL. Resolving an incoming server-action / client-reference id through * the gate then becomes a closed lookup instead of importing the id's path * directly — see `createReferenceGate`. Composes with `onTransform`; both run. * * Population is incremental (one entry per boundary as it loads), which suits * dev. For a production trust boundary the host seals the gate after a build * pass has driven every boundary through the loader. */ gate?: ReferenceGate; /** Logger backend. Defaults to a `console`-backed logger; pass * `NULL_LOGGER` to silence. */ logger?: Logger; /** Print per-module trace lines via the logger when true. */ verbose?: boolean; /** * Host policy: returns true when leading content above a file-level * directive is expected (e.g. bundler-injected preamble) and the * misplaced-directive warning should be suppressed. Forwarded to the * directive engine — see `TransformOptions.tolerateLeadingCode`. * Comments alone never need this: they are trivia and tolerated always. */ tolerateLeadingCode?: (source: string) => boolean; } /** * Builds the `load` and `resolve` hooks for a React Server Components * Node ESM loader. The returned hooks plug into `node:module#register` or * `--experimental-loader` and convert `"use client"` / `"use server"` * modules into the contract `react-server-dom-esm` expects at runtime. * * The loader is transport-agnostic — the default `RSC_LOADER_CONFIG` * matches `react-server-dom-esm`, but a consumer can override * `options.loader` to integrate with a different RSC transport. * * @example * ```ts * // register.mjs * import { register } from "node:module"; * import { createReactLoader } from "react-server-loader/loader"; * * const { load, resolve } = createReactLoader({ * moduleID: (filePath) => filePath.replace(process.cwd(), ""), * }); * * register(load, import.meta.url); * ``` */ export declare function createReactLoader(options: CreateReactLoaderOptions): { load: LoadHook; resolve: ResolveHook; }; //# sourceMappingURL=createReactLoader.d.ts.map