fix(vite): serve css imported in islands (#3272)

Fixes https://github.com/denoland/fresh/issues/3271
This commit is contained in:
Marvin Hagemeister
2025-08-29 14:30:00 +02:00
committed by GitHub
parent b2c4f049bc
commit b41c151de3
14 changed files with 108 additions and 25 deletions
+2
View File
@@ -97,6 +97,7 @@ export class IslandPreparer {
mod: Record<string, unknown>,
chunkName: string,
modName: string,
css: string[],
) {
for (const [name, value] of Object.entries(mod)) {
if (typeof value !== "function") continue;
@@ -110,6 +111,7 @@ export class IslandPreparer {
file: chunkName,
fn,
name: uniqueName,
css,
});
}
}
+1
View File
@@ -33,6 +33,7 @@ export interface Island {
name: string;
exportName: string;
fn: ComponentType;
css: string[];
}
export type ServerIslandRegistry = Map<ComponentType, Island>;
+1
View File
@@ -314,6 +314,7 @@ export class Builder<State = any> {
name,
server: spec,
browser: null,
css: [],
});
}
+4 -2
View File
@@ -27,6 +27,7 @@ export interface IslandModChunk {
name: string;
server: string;
browser: string | null;
css: string[];
}
export type FsRouteFileNoMod<State> = Omit<FsRouteFile<State>, "mod"> & {
@@ -206,7 +207,7 @@ export class MemoryBuildCache<State> implements DevBuildCache<State> {
throw new Error(`Unexpected missing browser chunk`);
}
preparer.prepare(this.islandRegistry, mod, chunk.browser, name);
preparer.prepare(this.islandRegistry, mod, chunk.browser, name, []);
},
),
);
@@ -461,7 +462,8 @@ export async function generateSnapshotServer(
const islandMarkers = islands.map((item) => {
const browser = JSON.stringify(item.browser);
const name = JSON.stringify(item.name);
return `islandPreparer.prepare(islands, ${item.name}, ${browser}, ${name});`;
const css = JSON.stringify(item.css);
return `islandPreparer.prepare(islands, ${item.name}, ${browser}, ${name}, ${css});`;
}).join("\n");
const serializedFsRoutes = fsRoutesFiles
@@ -254,8 +254,9 @@ options[OptionsType.DIFF] = (vnode) => {
RENDER_STATE!.renderedHtmlHead = true;
const entryAssets = RENDER_STATE.buildCache.getEntryAssets();
// deno-lint-ignore no-explicit-any
const items: VNode<any>[] = [];
if (entryAssets.length > 0) {
const items: VNode[] = [];
for (let i = 0; i < entryAssets.length; i++) {
const id = entryAssets[i];
@@ -266,32 +267,22 @@ options[OptionsType.DIFF] = (vnode) => {
);
}
}
if (Array.isArray(vnode.props.children)) {
vnode.props.children.push(...items);
} else if (
vnode.props.children !== null &&
typeof vnode.props.children === "object"
) {
// deno-lint-ignore no-explicit-any
items.push(vnode.props.children as any);
vnode.props.children = items;
} else {
vnode.props.children = items;
}
}
// Append component to render remaining head nodes
const remaining = h(RemainingHead, null);
// deno-lint-ignore no-explicit-any
items.push(h(RemainingHead, null) as VNode<any>);
if (Array.isArray(vnode.props.children)) {
vnode.props.children.push(remaining);
vnode.props.children.push(...items);
} else if (
vnode.props.children !== null &&
typeof vnode.props.children === "object"
) {
vnode.props.children = [vnode.props.children, remaining];
// deno-lint-ignore no-explicit-any
items.unshift(vnode.props.children as any);
vnode.props.children = items;
} else {
vnode.props.children = remaining;
vnode.props.children = items;
}
break;
@@ -412,8 +403,25 @@ options[OptionsType.DIFFED] = (vnode) => {
};
function RemainingHead() {
if (RENDER_STATE !== null && RENDER_STATE.headComponents.size > 0) {
return h(Fragment, null, ...RENDER_STATE.headComponents.values());
if (RENDER_STATE !== null) {
// deno-lint-ignore no-explicit-any
const items: VNode<any>[] = [];
if (RENDER_STATE.headComponents.size > 0) {
items.push(...RENDER_STATE.headComponents.values());
}
RENDER_STATE.islands.forEach((island) => {
if (island.css.length > 0) {
for (let i = 0; i < island.css.length; i++) {
const css = island.css[i];
items.push(h("link", { rel: "stylesheet", href: css }));
}
}
});
if (items.length > 0) {
return h(Fragment, null, items);
}
}
return null;
}