fix(vite): nested island css module styles not working (#3353)

Related https://github.com/denoland/fresh/issues/3325
This commit is contained in:
Marvin Hagemeister
2025-09-08 10:00:14 +02:00
committed by GitHub
parent f869bff685
commit 4ff8e677aa
8 changed files with 82 additions and 7 deletions
@@ -68,6 +68,7 @@ export class RenderState {
// deno-lint-ignore no-explicit-any
islandProps: any[] = [];
islands = new Set<Island>();
islandAssets = new Set<string>();
// deno-lint-ignore no-explicit-any
encounteredPartials = new Set<any>();
owners = new Map<VNode, VNode>();
@@ -194,10 +195,13 @@ options[OptionsType.DIFF] = (vnode) => {
);
}
} else if (
!PATCHED.has(vnode) && !hasIslandOwner(RENDER_STATE, vnode)
!PATCHED.has(vnode)
) {
const island = RENDER_STATE.buildCache.islandRegistry.get(vnode.type);
const insideIsland = hasIslandOwner(RENDER_STATE, vnode);
if (island === undefined) {
if (insideIsland) break patcher;
// Not an island, but we might need to preserve keys
if (vnode.key !== undefined) {
const key = normalizeKey(vnode.key);
@@ -212,7 +216,16 @@ options[OptionsType.DIFF] = (vnode) => {
break patcher;
}
const { islands, islandProps } = RENDER_STATE;
const { islands, islandProps, islandAssets } = RENDER_STATE;
if (insideIsland) {
for (let i = 0; i < island.css.length; i++) {
const css = island.css[i];
islandAssets.add(css);
}
break patcher;
}
islands.add(island);
const originalType = vnode.type;
@@ -419,6 +432,10 @@ function RemainingHead() {
}
});
RENDER_STATE.islandAssets.forEach((css) => {
items.push(h("link", { rel: "stylesheet", href: css }));
});
if (items.length > 0) {
return h(Fragment, null, items);
}
@@ -0,0 +1,10 @@
// @ts-ignore upstream issue https://github.com/denoland/deno/issues/30560
import styles from "./CssModulesNonIsland.module.css";
export function CssModulesNonIsland() {
return (
<div class="green">
<h1 class={styles.root}>green text</h1>
</div>
);
}
@@ -0,0 +1,3 @@
.root {
color: green;
}
@@ -1,6 +1,14 @@
import { CssModulesNonIsland } from "../components/CssModuleNonIsland.tsx";
// @ts-ignore upstream issue https://github.com/denoland/deno/issues/30560
import styles from "./CssModules.module.css";
import { CssModulesOther } from "./CssModulesOther.tsx";
export function CssModules() {
return <h1 class={styles.root}>red text</h1>;
return (
<div class="red">
<h1 class={styles.root}>red text</h1>
<CssModulesOther />
<CssModulesNonIsland />
</div>
);
}
@@ -0,0 +1,3 @@
.root {
color: blue;
}
@@ -0,0 +1,10 @@
// @ts-ignore upstream issue https://github.com/denoland/deno/issues/30560
import styles from "./CssModulesOther.module.css";
export function CssModulesOther() {
return (
<div class="blue">
<h1 class={styles.root}>blue text</h1>
</div>
);
}
+14 -2
View File
@@ -338,11 +338,23 @@ Deno.test({
waitUntil: "networkidle2",
});
const color = await page
.locator("h1")
let color = await page
.locator(".red > h1")
// deno-lint-ignore no-explicit-any
.evaluate((el) => window.getComputedStyle(el as any).color);
expect(color).toEqual("rgb(255, 0, 0)");
color = await page
.locator(".green > h1")
// deno-lint-ignore no-explicit-any
.evaluate((el) => window.getComputedStyle(el as any).color);
expect(color).toEqual("rgb(0, 128, 0)");
color = await page
.locator(".blue > h1")
// deno-lint-ignore no-explicit-any
.evaluate((el) => window.getComputedStyle(el as any).color);
expect(color).toEqual("rgb(0, 0, 255)");
});
},
);
+14 -2
View File
@@ -269,11 +269,23 @@ Deno.test({
waitUntil: "networkidle2",
});
const color = await page
.locator("h1")
let color = await page
.locator(".red > h1")
// deno-lint-ignore no-explicit-any
.evaluate((el) => window.getComputedStyle(el as any).color);
expect(color).toEqual("rgb(255, 0, 0)");
color = await page
.locator(".green > h1")
// deno-lint-ignore no-explicit-any
.evaluate((el) => window.getComputedStyle(el as any).color);
expect(color).toEqual("rgb(0, 128, 0)");
color = await page
.locator(".blue > h1")
// deno-lint-ignore no-explicit-any
.evaluate((el) => window.getComputedStyle(el as any).color);
expect(color).toEqual("rgb(0, 0, 255)");
});
},
sanitizeResources: false,