vite-plugin-react-server
Version:
Vite plugin for React Server Components (RSC)
81 lines (78 loc) • 1.83 kB
JavaScript
/**
* vite-plugin-react-server
* Copyright (c) Nico Brinkkemper
* MIT License
*/
import * as React from 'react';
import { renderToPipeableStream } from 'react-server-dom-esm/server.node';
function createRscStream({
Html,
Page,
props,
loader = (id) => import(id).then((m) => m.default),
moduleRootPath,
moduleBasePath,
moduleBaseURL,
logger,
cssFiles = [],
route,
url,
pipableStreamOptions,
htmlProps,
inlineCss = true,
CssCollector,
root
}) {
const htmlIsFragment = Html == React.Fragment;
if (!htmlIsFragment) {
if (!htmlProps) {
htmlProps = {};
}
if (!("moduleBaseURL" in htmlProps)) {
htmlProps["moduleBaseURL"] = moduleBaseURL;
}
if (!("moduleBasePath" in htmlProps)) {
htmlProps["moduleBasePath"] = moduleBasePath;
}
if (!("moduleRootPath" in htmlProps)) {
htmlProps["moduleRootPath"] = moduleRootPath;
}
if (!("url" in htmlProps)) {
htmlProps["url"] = url;
}
if (!("route" in htmlProps)) {
htmlProps["route"] = route;
}
if (!("pageProps" in htmlProps)) {
htmlProps["pageProps"] = props;
}
}
const withCss = React.createElement(
CssCollector,
inlineCss === true ? {
cssFiles,
route,
moduleBaseURL,
moduleBasePath,
moduleRootPath,
root,
loader
} : { cssFiles, route, moduleBaseURL },
React.createElement(Page, props)
);
const content = htmlIsFragment ? withCss : React.createElement(Html, htmlProps, withCss);
try {
return renderToPipeableStream(
content,
moduleBasePath,
pipableStreamOptions
);
} catch (error) {
logger.error(`Failed to create stream for ${route}.`, {
error
});
return null;
}
}
export { createRscStream };
//# sourceMappingURL=createRscStream.js.map