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
@@ -13,6 +13,7 @@ import {
pathToSpec,
type PendingStaticFile,
specToName,
toPosix,
UniqueNamer,
} from "fresh/internal-dev";
import {
@@ -23,6 +24,19 @@ import {
import * as path from "@std/path";
import { getBuildId } from "./build_id.ts";
const CSS_LANG_REG = /\.(css|less|sass|scss)(\?.*)?$/;
export const FRESH_CSS_PLACEHOLDER = `["__FRESH_CSS_PLACEHOLDER__"]`;
export function replaceFreshCssPlaceholders(
content: string,
css: string[] | undefined,
): string {
return content.replaceAll(
FRESH_CSS_PLACEHOLDER,
css ? JSON.stringify(css.map((href) => `/${href}`)) : "null",
);
}
export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
const modName = "fresh:server-snapshot";
@@ -158,7 +172,7 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
const route = result.routes[i];
const name = routeNamer.getUniqueName(route.id);
routeFileToName.set(route.filePath, name);
routeFileToName.set(toPosix(route.filePath), name);
routes.set(name, route);
}
@@ -411,7 +425,7 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
},
transform: {
filter: {
id: /\.(css|less|sass|scss)(\?.*)?$/,
id: CSS_LANG_REG,
},
handler(_code, id) {
if (server) {
@@ -426,7 +440,7 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
let item: EnvironmentModuleNode | undefined;
while ((item = queue.pop()) !== undefined) {
if (item.file !== null) {
const normalized = path.normalize(item.file);
const normalized = toPosix(path.normalize(item.file));
const name = routeFileToName.get(normalized);
if (name !== undefined) {
@@ -530,19 +544,19 @@ export default ${JSON.stringify(route.css)}
const manifest = JSON.parse(asset.source as string) as Manifest;
for (const info of Object.values(manifest)) {
if (info.name?.startsWith("_fresh-route___")) {
const filePath = path.join(serverOutDir, info.file);
const content = await Deno.readTextFile(filePath);
// Utility-file(_app/_layout/_error)'s CSS can be hoisted into
// shared chunks like "server-entry", not just route chunks.
// Replace placeholders in any emitted JS chunk that contains one.
if (!/\.(?:c|m)?js$/.test(info.file)) continue;
const replaced = content.replace(
`["__FRESH_CSS_PLACEHOLDER__"]`,
info.css
? JSON.stringify(info.css.map((css) => `/${css}`))
: "null",
);
const filePath = path.join(serverOutDir, info.file);
const content = await Deno.readTextFile(filePath);
if (!content.includes(FRESH_CSS_PLACEHOLDER)) continue;
await Deno.writeTextFile(filePath, replaced);
}
// Replace all placeholders in the file with the CSS
const replaced = replaceFreshCssPlaceholders(content, info.css);
await Deno.writeTextFile(filePath, replaced);
}
}
},
@@ -0,0 +1,40 @@
import { expect } from "@std/expect/expect";
import {
FRESH_CSS_PLACEHOLDER,
replaceFreshCssPlaceholders,
} from "./server_snapshot.ts";
Deno.test("server snapshot - replaceFreshCssPlaceholders with no css", () => {
const output = replaceFreshCssPlaceholders(
`export default ${FRESH_CSS_PLACEHOLDER};`,
undefined,
);
expect(output).toEqual("export default null;");
});
Deno.test("server snapshot - replaceFreshCssPlaceholders once", () => {
const output = replaceFreshCssPlaceholders(
`const css = ${FRESH_CSS_PLACEHOLDER};`,
["assets/server-entry.css"],
);
expect(output).toEqual(`const css = ["/assets/server-entry.css"];`);
});
Deno.test("server snapshot - replaceFreshCssPlaceholders multiple times", () => {
const output = replaceFreshCssPlaceholders(
[
`const appCss = ${FRESH_CSS_PLACEHOLDER};`,
`const layoutCss = ${FRESH_CSS_PLACEHOLDER};`,
`const errorCss = ${FRESH_CSS_PLACEHOLDER};`,
].join("\n"),
["assets/server-entry.css"],
);
expect(output).toEqual([
`const appCss = ["/assets/server-entry.css"];`,
`const layoutCss = ["/assets/server-entry.css"];`,
`const errorCss = ["/assets/server-entry.css"];`,
].join("\n"));
});