fix: CSS modules not working in _app/_layout/_error and across routes in non-island components (#3781)
This commit is contained in:
@@ -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"));
|
||||
});
|
||||
@@ -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`, {
|
||||
|
||||
Vendored
+10
@@ -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>
|
||||
);
|
||||
}
|
||||
Vendored
+9
@@ -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>
|
||||
);
|
||||
}
|
||||
Vendored
+9
@@ -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>
|
||||
);
|
||||
}
|
||||
Vendored
+9
@@ -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>
|
||||
);
|
||||
}
|
||||
packages/plugin-vite/tests/fixtures/non_island_css_modules/components/CssModulesNonIsland.module.css
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
.root {
|
||||
color: green;
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
.root {
|
||||
color: blue;
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
.root {
|
||||
color: red;
|
||||
}
|
||||
+3
@@ -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 />;
|
||||
});
|
||||
+11
@@ -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()],
|
||||
});
|
||||
Reference in New Issue
Block a user