rwsdk
Version:
Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime
64 lines (63 loc) • 2.47 kB
JavaScript
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
import { getManifest } from "../lib/manifest.js";
// context(justinvdm, 2026-03-15): Vite's client manifest uses keys without
// a leading slash (e.g. "src/app/pages/Welcome.tsx"), but our module IDs in
// scriptsToBeLoaded use Vite-style leading-slash paths (e.g.
// "/src/app/pages/Welcome.tsx") from normalizeModulePath. We strip the
// leading slash here so the lookup succeeds.
const toManifestKey = (id) => (id.startsWith("/") ? id.slice(1) : id);
const findCssForModule = (scriptId, manifest) => {
const css = new Set();
const visited = new Set();
const inner = (id) => {
if (visited.has(id)) {
return;
}
visited.add(id);
const entry = manifest[toManifestKey(id)];
if (!entry) {
return;
}
if (entry.css) {
for (const href of entry.css) {
css.add(href);
}
}
// context(justinvdm, 2026-04-23): Rolldown associates CSS with the
// chunk that imports the CSS file, which may be a dynamic import target
// rather than the entry chunk. Walk both static and dynamic imports to
// find CSS from transitive dependencies.
if (entry.imports) {
for (const importId of entry.imports) {
inner(importId);
}
}
if (entry.dynamicImports) {
for (const importId of entry.dynamicImports) {
inner(importId);
}
}
};
inner(scriptId);
return Array.from(css);
};
import { toAbsoluteHref } from "./assetPaths.js";
export const Stylesheets = async ({ requestInfo, }) => {
const manifest = await getManifest();
const allStylesheets = new Set();
for (const scriptId of requestInfo.rw.scriptsToBeLoaded) {
const css = findCssForModule(scriptId, manifest);
for (const entry of css) {
allStylesheets.add(toAbsoluteHref(entry));
}
}
for (const [, entry] of Object.entries(manifest)) {
if (entry.isEntry) {
const css = findCssForModule(entry.src ?? "", manifest);
for (const href of css) {
allStylesheets.add(toAbsoluteHref(href));
}
}
}
return (_jsx(_Fragment, { children: Array.from(allStylesheets).map((href) => (_jsx("link", { rel: "stylesheet", href: href, precedence: "first" }, href))) }));
};