fix: CSS modules not working in _app/_layout/_error and across routes in non-island components (#3781)

This commit is contained in:
Hajime-san
2026-05-27 08:32:41 +02:00
committed by GitHub
parent 072d09297e
commit 3afaaf4f78
30 changed files with 464 additions and 39 deletions
+120
View File
@@ -1,4 +1,5 @@
import { expect } from "@std/expect";
import { walk } from "@std/fs/walk";
import {
waitFor,
waitForText,
@@ -13,8 +14,13 @@ import {
usingEnv,
} from "./test_utils.ts";
import * as path from "@std/path";
import { FRESH_CSS_PLACEHOLDER } from "../src/plugins/server_snapshot.ts";
const viteResult = await buildVite(DEMO_DIR);
const NON_ISLAND_CSS_MODULES_FIXTURE = path.join(
FIXTURE_DIR,
"non_island_css_modules",
);
integrationTest("vite build - launches", async () => {
await launchProd(
@@ -388,6 +394,81 @@ integrationTest(
},
);
integrationTest(
"vite build - css modules in _app/_layout/_error non-island component are injected",
async () => {
await using res = await buildVite(NON_ISLAND_CSS_MODULES_FIXTURE);
await launchProd(
{ cwd: res.tmp },
async (address) => {
await withBrowser(async (page) => {
{
// check _app/_layout
await page.goto(`${address}`, {
waitUntil: "networkidle2",
});
const _app = await page
.locator<HTMLHeadingElement>(".green > h1")
.evaluate((el) => window.getComputedStyle(el).color);
expect(_app).toEqual("rgb(0, 128, 0)");
const _layout = await page
.locator<HTMLHeadingElement>(".red > h1")
.evaluate((el) => window.getComputedStyle(el).color);
expect(_layout).toEqual("rgb(255, 0, 0)");
}
{
// check _app/_layout/_error
await page.goto(`${address}/boom`, {
waitUntil: "networkidle2",
});
const _app = await page
.locator<HTMLHeadingElement>(".green > h1")
.evaluate((el) => window.getComputedStyle(el).color);
expect(_app).toEqual("rgb(0, 128, 0)");
const _layout = await page
.locator<HTMLHeadingElement>(".red > h1")
.evaluate((el) => window.getComputedStyle(el).color);
expect(_layout).toEqual("rgb(255, 0, 0)");
const _error = await page
.locator<HTMLHeadingElement>(".blue > h1")
.evaluate((el) => window.getComputedStyle(el).color);
expect(_error).toEqual("rgb(0, 0, 255)");
}
{
// check _app/_layout/_404
await page.goto(`${address}/non_existent`, {
waitUntil: "networkidle2",
});
const _app = await page
.locator<HTMLHeadingElement>(".green > h1")
.evaluate((el) => window.getComputedStyle(el).color);
expect(_app).toEqual("rgb(0, 128, 0)");
const _layout = await page
.locator<HTMLHeadingElement>(".red > h1")
.evaluate((el) => window.getComputedStyle(el).color);
expect(_layout).toEqual("rgb(255, 0, 0)");
const _404 = await page
.locator<HTMLHeadingElement>(".orange > h1")
.evaluate((el) => window.getComputedStyle(el).color);
expect(_404).toEqual("rgb(255, 165, 0)");
}
});
},
);
},
);
integrationTest("vite build - route css import", async () => {
await launchProd(
{ cwd: viteResult.tmp },
@@ -410,6 +491,45 @@ integrationTest("vite build - route css import", async () => {
);
});
integrationTest(
"vite build - __FRESH_CSS_PLACEHOLDER__ has been replaced in all server chunks",
async () => {
await using res = await buildVite(NON_ISLAND_CSS_MODULES_FIXTURE, {
base: "/my-app/",
});
const serverDir = path.join(res.tmp, "_fresh", "server");
const inspected: string[] = [];
for await (
const entry of walk(serverDir, {
exts: [".mjs"],
includeDirs: false,
})
) {
const content = await Deno.readTextFile(entry.path);
inspected.push(path.relative(serverDir, entry.path));
expect(content).not.toContain(FRESH_CSS_PLACEHOLDER);
}
expect(inspected.length).toBeGreaterThan(0);
},
);
integrationTest(
"vite build - shared server chunk keeps utility css references after replacement",
async () => {
await using res = await buildVite(NON_ISLAND_CSS_MODULES_FIXTURE);
const serverEntryJs = await Deno.readTextFile(
path.join(res.tmp, "_fresh", "server", "server-entry.mjs"),
);
const cssRefs = serverEntryJs.match(/"\/assets\/server-entry-.*?\.css"/g) ??
[];
expect(cssRefs.length).toBeGreaterThanOrEqual(4);
},
);
integrationTest("vite build - remote island", async () => {
const fixture = path.join(FIXTURE_DIR, "remote_island");
await using res = await buildVite(fixture);
@@ -323,6 +323,77 @@ integrationTest(
},
);
integrationTest(
"vite dev - css modules in _app/_layout/_error non-island component are injected",
async () => {
const fixture = path.join(FIXTURE_DIR, "non_island_css_modules");
await launchDevServer(fixture, async (address) => {
await withBrowser(async (page) => {
{
// check _app/_layout
await page.goto(`${address}`, {
waitUntil: "networkidle2",
});
const _app = await page
.locator<HTMLHeadingElement>(".green > h1")
.evaluate((el) => window.getComputedStyle(el).color);
expect(_app).toEqual("rgb(0, 128, 0)");
const _layout = await page
.locator<HTMLHeadingElement>(".red > h1")
.evaluate((el) => window.getComputedStyle(el).color);
expect(_layout).toEqual("rgb(255, 0, 0)");
}
{
// check _app/_layout/_error
await page.goto(`${address}/boom`, {
waitUntil: "networkidle2",
});
const _app = await page
.locator<HTMLHeadingElement>(".green > h1")
.evaluate((el) => window.getComputedStyle(el).color);
expect(_app).toEqual("rgb(0, 128, 0)");
const _layout = await page
.locator<HTMLHeadingElement>(".red > h1")
.evaluate((el) => window.getComputedStyle(el).color);
expect(_layout).toEqual("rgb(255, 0, 0)");
const _error = await page
.locator<HTMLHeadingElement>(".blue > h1")
.evaluate((el) => window.getComputedStyle(el).color);
expect(_error).toEqual("rgb(0, 0, 255)");
}
{
// check _app/_layout/_404
await page.goto(`${address}/non_existent`, {
waitUntil: "networkidle2",
});
const _app = await page
.locator<HTMLHeadingElement>(".green > h1")
.evaluate((el) => window.getComputedStyle(el).color);
expect(_app).toEqual("rgb(0, 128, 0)");
const _layout = await page
.locator<HTMLHeadingElement>(".red > h1")
.evaluate((el) => window.getComputedStyle(el).color);
expect(_layout).toEqual("rgb(255, 0, 0)");
const _404 = await page
.locator<HTMLHeadingElement>(".orange > h1")
.evaluate((el) => window.getComputedStyle(el).color);
expect(_404).toEqual("rgb(255, 165, 0)");
}
});
});
},
);
integrationTest("vite dev - route css import", async () => {
await withBrowser(async (page) => {
await page.goto(`${demoServer.address()}/tests/css`, {
@@ -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,9 @@
import styles from "./CssModulesNonIsland2.module.css";
export function CssModulesNonIsland2() {
return (
<div class="blue">
<h1 class={styles.root}>blue text</h1>
</div>
);
}
@@ -0,0 +1,9 @@
import styles from "./CssModulesNonIsland3.module.css";
export function CssModulesNonIsland3() {
return (
<div class="red">
<h1 class={styles.root}>red text</h1>
</div>
);
}
@@ -0,0 +1,9 @@
import styles from "./CssModulesNonIsland4.module.css";
export function CssModulesNonIsland4() {
return (
<div class="orange">
<h1 class={styles.root}>orange text</h1>
</div>
);
}
@@ -0,0 +1,3 @@
.root {
color: green;
}
@@ -0,0 +1,3 @@
.root {
color: blue;
}
@@ -0,0 +1,3 @@
.root {
color: red;
}
@@ -0,0 +1,3 @@
.root {
color: orange;
}
@@ -0,0 +1,5 @@
import { App, staticFiles } from "@fresh/core";
export const app = new App()
.use(staticFiles())
.fsRoutes();
@@ -0,0 +1,6 @@
import { CssModulesNonIsland4 } from "../components/CssModuleNonIsland4.tsx";
import { define } from "../utils.ts";
export default define.page(() => {
return <CssModulesNonIsland4 />;
});
@@ -0,0 +1,17 @@
import type { PageProps } from "fresh";
import { CssModulesNonIsland } from "../components/CssModuleNonIsland.tsx";
export default function App({ Component }: PageProps) {
return (
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<CssModulesNonIsland />
<Component />
</body>
</html>
);
}
@@ -0,0 +1,6 @@
import { define } from "../utils.ts";
import { CssModulesNonIsland2 } from "../components/CssModuleNonIsland2.tsx";
export default define.page(() => {
return <CssModulesNonIsland2 />;
});
@@ -0,0 +1,11 @@
import { CssModulesNonIsland3 } from "../components/CssModuleNonIsland3.tsx";
import { define } from "../utils.ts";
export default define.layout(({ Component }) => {
return (
<>
<CssModulesNonIsland3 />
<Component />
</>
);
});
@@ -0,0 +1,5 @@
import { define } from "../utils.ts";
export default define.page(() => {
throw new Error("boom");
});
@@ -0,0 +1,3 @@
export default function Hello() {
return <h1>ok</h1>;
}
@@ -0,0 +1,4 @@
import { createDefine } from "@fresh/core";
// deno-lint-ignore no-explicit-any
export const define = createDefine<any>();
@@ -0,0 +1,6 @@
import { defineConfig } from "vite";
import { fresh } from "@fresh/plugin-vite";
export default defineConfig({
plugins: [fresh()],
});