vite-plugin-react-server
Version:
Vite plugin for React Server Components (RSC)
130 lines • 4.6 kB
TypeScript
/**
* # createAbsoluteUrl
*
* This function takes a baseURL and a public origin and returns a function that takes a path and returns the path with the baseURL attached to it.
*
* @example
* ```ts
* const absoluteURL = createAbsoluteURL("/mmc", "https://bidoof.com")
* console.log(absoluteURL("/test")) // "https://bidoof.com/mmc/test"
* ```
*
* @example
* ```ts
* const absoluteURL = createAbsoluteURL("/mmc", "https://bidoof.com")
* console.log(absoluteURL("/test")) // "https://bidoof.com/mmc/test"
* ```
*
* This can replace code like `${process.env.VITE_PUBLIC_ORIGIN}/test` with `absoluteUrl('/test')`, and you can be sure that it will work after
* changing the plugin settings.
*/
export declare const createAbsoluteURL: (withBaseURL: string, withPublicOrigin: string) => (path: string) => string;
/**
* # createBaseURL
*
* This function takes a baseURL and returns a function that takes a path and returns the path with the baseURL attached to it.
*
* @example
* ```ts
* const baseURL = createBaseURL("/mmc")
* console.log(baseURL("/test")) // "/mmc/test"
* ```
*
* @example
* ```ts
* const baseURL = createBaseURL("/mmc/")
* console.log(baseURL("/test")) // "/mmc/test"
* ```
*
* This can replace code like `${import.meta.env.test` with `baseURL(path)`, and you can be sure that it will work after
* changing the plugin settings.
*
* Path handling logic:
* 1. For baseURL ending with "/":
* - If path starts with "/", slice off the leading slash to avoid double slashes
* - If path doesn't start with "/", keep it as is
*
* 2. For baseURL not ending with "/":
* - If path starts with "/", directly concatenate with baseURL
* - If path doesn't start with "/", add a slash between baseURL and path
*
* baseURL "src" + path "src/test" -> should not concatenate to src/src/test
* baseURL "/" + path "https://bidoof.com" -> should not concatenate to /https://bidoof.com"
*/
export declare const createBaseURL: (withBaseURL: string) => (path: string) => string;
/** Remove a single trailing slash from the URL if it ends with one */
export declare const removeTrailingSlash: (url: string) => string;
/** Add a single trailing slash to the URL if it doesn't end with one */
export declare const addTrailingSlash: (url: string) => string;
export declare const removeLeadingSlash: (url: string) => string;
export declare const addLeadingSlash: (url: string) => string;
export declare const isBlankRegex: RegExp;
export declare const isAbsoluteURL: (url: string) => boolean;
export declare const folderName: (path: string, withBaseURL: string) => string;
/**
* # createPageURL
*
* This function takes a baseURL, public origin and a optional normalizer function that mirrors the baseURL's format.
* If baseURL ends with a slash, we continue it using the URL itself (must end with a slash)
*
* - `indexRSC`: The path to the index.rsc file
* - `moduleBaseURL`: The baseURL to use for the module
*
* These can be passed in directly to the createReactFetcher and also determine the defaults when no input are provided.
*
* @example
* ```ts
* import { createFromFetch } from "react-server-dom-esm/client.browser";
* const parsedURL = pageURL(window.location.pathname ?? "/");
* const data = createFromFetch(
* fetch(parsedURL.indexRSC, {
* headers: {
* Accept: "text/x-component",
* }
* }),
* {
* callServer: callServer,
* moduleBaseURL: parsedURL.moduleBaseURL,
* }
* );
* ```
*
* The moduleBasePath being set at the config level as "",
* then we pass it to create a stream `renderToPipeableStream(elements, moduleBasePath)`, and we see
* ```text
* 2:I["src/components/Clickable.client-Dx9diOqr.js","ClientClickable"]
* ```
*
*/
export declare const createPageURL: (withBaseURL: string, withPublicOrigin: string, isDev?: boolean, normalizer?: (url: string) => string) => (to: string, fileName?: string) => {
indexRSC: string;
moduleBaseURL: string;
};
/**
* # moduleBaseURL
*
* This function takes a baseURL, public origin and a optional normalizer function that mirrors the baseURL's format.
*
* @example
* ```ts
* const moduleBaseURL = parseURL("/mmc", "https://bidoof.com")
* if(moduleBaseURL.type === "error") {
* console.error(moduleBaseURL.error)
* } else {
* console.log(moduleBaseURL.url)
* }
* ```
*
**/
export declare const parseURL: (url: string, base: string) => {
type: "success";
url: URL;
error?: never;
base?: never;
} | {
type: "error";
url: string;
base: string;
error: Error;
};
//# sourceMappingURL=urls.d.ts.map