fully-react
Version:
React Server for Vite
107 lines (104 loc) • 3.14 kB
JavaScript
import path from 'node:path';
import { jsx, Fragment } from 'react/jsx-runtime';
// src/server/dev/find-styles.tsx
async function find_deps(vite, node, deps) {
const branches = [];
async function add(node2) {
if (!deps.has(node2)) {
deps.add(node2);
await find_deps(vite, node2, deps);
}
}
async function add_by_url(url) {
const node2 = await vite.moduleGraph.getModuleByUrl(url);
if (node2) {
await add(node2);
}
}
if (node.ssrTransformResult) {
if (node.ssrTransformResult.deps) {
node.ssrTransformResult.deps.forEach(
(url) => branches.push(add_by_url(url))
);
}
} else {
node.importedModules.forEach((node2) => branches.push(add(node2)));
}
await Promise.all(branches);
}
var style_pattern = /\.(css|less|sass|scss|styl|stylus|pcss|postcss)$/;
async function collectStyles(devServer, match) {
const styles = {};
const deps = /* @__PURE__ */ new Set();
try {
for (const file of match) {
const resolvedId = await devServer.pluginContainer.resolveId(file);
if (!resolvedId) {
console.log("not found");
continue;
}
const id = resolvedId.id;
const normalizedPath = path.resolve(id).replace(/\\/g, "/");
let node = devServer.moduleGraph.getModuleById(normalizedPath);
if (!node) {
const absolutePath = path.resolve(file);
await devServer.ssrLoadModule(absolutePath);
node = await devServer.moduleGraph.getModuleByUrl(absolutePath);
if (!node) {
console.log("not found");
return;
}
}
await find_deps(devServer, node, deps);
}
} catch (e) {
}
for (const dep of deps) {
const parsed = new URL(dep.url, "http://localhost/");
parsed.searchParams;
if (style_pattern.test(dep.file ?? "")) {
try {
const mod = await devServer.ssrLoadModule(dep.url);
styles[dep.url] = mod.default;
} catch {
}
}
}
return styles;
}
var InlineStyles = import.meta.env.DEV ? async function InlineStyles2(props) {
const { default: devServer } = await import('./dev-server-5DV7YDFZ.js');
const styles = await collectStyles(
devServer,
props.entries ?? ["~/root.tsx"]
);
return /* @__PURE__ */ jsx(Fragment, { children: Object.entries(styles ?? {}).map(([url, css]) => /* @__PURE__ */ jsx(
"style",
{
dangerouslySetInnerHTML: { __html: css },
suppressHydrationWarning: true
},
url
)) });
} : () => null;
var ReactRefreshScript = import.meta.env.DEV ? function ReactRefreshScript2() {
if (!import.meta.env.DEV) {
return null;
}
return /* @__PURE__ */ jsx(
"script",
{
type: "module",
dangerouslySetInnerHTML: {
__html: `
import RefreshRuntime from "/@react-refresh"
RefreshRuntime.injectIntoGlobalHook(window)
window.$RefreshReg$ = () => {}
window.$RefreshSig$ = () => (type) => type
window.__vite_plugin_react_preamble_installed__ = true
`
}
}
);
} : () => null;
export { InlineStyles, ReactRefreshScript, collectStyles };