fix: CSS modules not working in _app/_layout/_error and across routes in non-island components (#3781)
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import { setAdditionalStyles } from "./context.ts";
|
||||
import { HttpError } from "./error.ts";
|
||||
import { isHandlerByMethod, type PageResponse } from "./handlers.ts";
|
||||
import {
|
||||
@@ -74,11 +73,13 @@ export function newErrorCmd<State>(
|
||||
export interface AppCommand<State> {
|
||||
type: CommandType.App;
|
||||
component: RouteComponent<State>;
|
||||
css?: string[];
|
||||
}
|
||||
export function newAppCmd<State>(
|
||||
component: RouteComponent<State>,
|
||||
css?: string[],
|
||||
): AppCommand<State> {
|
||||
return { type: CommandType.App, component };
|
||||
return { type: CommandType.App, component, css };
|
||||
}
|
||||
|
||||
export interface LayoutCommand<State> {
|
||||
@@ -86,6 +87,7 @@ export interface LayoutCommand<State> {
|
||||
pattern: string;
|
||||
component: RouteComponent<State>;
|
||||
config?: LayoutConfig;
|
||||
css?: string[];
|
||||
includeLastSegment: boolean;
|
||||
}
|
||||
export function newLayoutCmd<State>(
|
||||
@@ -93,12 +95,14 @@ export function newLayoutCmd<State>(
|
||||
component: RouteComponent<State>,
|
||||
config: LayoutConfig | undefined,
|
||||
includeLastSegment: boolean,
|
||||
css?: string[],
|
||||
): LayoutCommand<State> {
|
||||
return {
|
||||
type: CommandType.Layout,
|
||||
pattern,
|
||||
component,
|
||||
config,
|
||||
css,
|
||||
includeLastSegment,
|
||||
};
|
||||
}
|
||||
@@ -253,7 +257,7 @@ function applyCommandsInner<State>(
|
||||
break;
|
||||
}
|
||||
case CommandType.App: {
|
||||
root.app = cmd.component;
|
||||
root.app = { component: cmd.component, css: cmd.css ?? null };
|
||||
break;
|
||||
}
|
||||
case CommandType.Layout: {
|
||||
@@ -265,6 +269,7 @@ function applyCommandsInner<State>(
|
||||
segment.layout = {
|
||||
component: cmd.component,
|
||||
config: cmd.config ?? null,
|
||||
css: cmd.css ?? null,
|
||||
};
|
||||
break;
|
||||
}
|
||||
@@ -290,10 +295,6 @@ function applyCommandsInner<State>(
|
||||
def = await route();
|
||||
}
|
||||
|
||||
if (def.css !== undefined) {
|
||||
setAdditionalStyles(ctx, def.css);
|
||||
}
|
||||
|
||||
return renderRoute(ctx, def);
|
||||
});
|
||||
|
||||
|
||||
@@ -120,8 +120,11 @@ export type ServerIslandRegistry = Map<ComponentType, Island>;
|
||||
export const internals: unique symbol = Symbol("fresh_internal");
|
||||
|
||||
export interface UiTree<Data, State> {
|
||||
app: AnyComponent<PageProps<Data, State>> | null;
|
||||
layouts: ComponentDef<Data, State>[];
|
||||
app: {
|
||||
component: AnyComponent<PageProps<Data, State>>;
|
||||
css: string[] | null;
|
||||
} | null;
|
||||
layouts: (ComponentDef<Data, State> & { css: string[] | null })[];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -131,7 +134,10 @@ export type FreshContext<State = unknown> = Context<State>;
|
||||
|
||||
export let getBuildCache: <T>(ctx: Context<T>) => BuildCache<T>;
|
||||
export let getInternals: <T>(ctx: Context<T>) => UiTree<unknown, T>;
|
||||
export let setAdditionalStyles: <T>(ctx: Context<T>, css: string[]) => void;
|
||||
export let setAdditionalStyles: <T>(
|
||||
ctx: Context<T>,
|
||||
css: string[] | null | undefined,
|
||||
) => void;
|
||||
|
||||
/**
|
||||
* The context passed to every middleware. It is unique for every request.
|
||||
@@ -204,8 +210,23 @@ export class Context<State> {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
getInternals = <T>(ctx: Context<T>) => ctx.#internal as any;
|
||||
getBuildCache = <T>(ctx: Context<T>) => ctx.#buildCache;
|
||||
setAdditionalStyles = <T>(ctx: Context<T>, css: string[]) =>
|
||||
ctx.#additionalStyles = css;
|
||||
setAdditionalStyles = <T>(
|
||||
ctx: Context<T>,
|
||||
css: string[] | null | undefined,
|
||||
) => {
|
||||
if (css == null) return;
|
||||
|
||||
if (ctx.#additionalStyles === null) {
|
||||
ctx.#additionalStyles = css.slice();
|
||||
return;
|
||||
}
|
||||
|
||||
for (const href of css) {
|
||||
if (!ctx.#additionalStyles.includes(href)) {
|
||||
ctx.#additionalStyles.push(href);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
constructor(
|
||||
@@ -305,6 +326,7 @@ export class Context<State> {
|
||||
props.Component = () => child;
|
||||
|
||||
const def = defs[i];
|
||||
setAdditionalStyles(this, def.css);
|
||||
|
||||
const result = await renderRouteComponent(this, def, () => child);
|
||||
if (result instanceof Response) {
|
||||
@@ -320,16 +342,20 @@ export class Context<State> {
|
||||
|
||||
let hasApp = true;
|
||||
|
||||
if (isAsyncAnyComponent(appDef)) {
|
||||
if (appDef !== null) {
|
||||
setAdditionalStyles(this, appDef.css);
|
||||
}
|
||||
|
||||
if (appDef !== null && isAsyncAnyComponent(appDef.component)) {
|
||||
props.Component = () => appChild;
|
||||
const result = await renderAsyncAnyComponent(appDef, props);
|
||||
const result = await renderAsyncAnyComponent(appDef.component, props);
|
||||
if (result instanceof Response) {
|
||||
return result;
|
||||
}
|
||||
|
||||
appVNode = result;
|
||||
} else if (appDef !== null) {
|
||||
appVNode = h(appDef, {
|
||||
appVNode = h(appDef.component, {
|
||||
Component: () => appChild,
|
||||
config: this.config,
|
||||
data: null,
|
||||
|
||||
@@ -20,7 +20,7 @@ const WINDOWS_SEPARATOR = pathWin32.SEPARATOR;
|
||||
/** Normalize a path to use forward slashes so that generated files
|
||||
* are portable across operating systems (e.g. build on Windows,
|
||||
* deploy on Linux). */
|
||||
function toPosix(p: string): string {
|
||||
export function toPosix(p: string): string {
|
||||
return p.replaceAll(WINDOWS_SEPARATOR, "/");
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { type FsAdapter, fsAdapter } from "../fs.ts";
|
||||
import type { WalkEntry } from "@std/fs/walk";
|
||||
import type { FsRouteFileNoMod } from "./dev_build_cache.ts";
|
||||
import { type FsRouteFileNoMod, toPosix } from "./dev_build_cache.ts";
|
||||
import * as path from "@std/path";
|
||||
import { pathToPattern } from "../router.ts";
|
||||
import { CommandType } from "../commands.ts";
|
||||
@@ -86,7 +86,7 @@ export async function crawlRouteDir<State>(
|
||||
|
||||
files.push({
|
||||
id,
|
||||
filePath: entry.path,
|
||||
filePath: toPosix(entry.path),
|
||||
type,
|
||||
pattern,
|
||||
routePattern,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { expect } from "@std/expect/expect";
|
||||
import { createFakeFs } from "../test_utils.ts";
|
||||
import { walkDir } from "./fs_crawl.ts";
|
||||
import { crawlRouteDir, walkDir } from "./fs_crawl.ts";
|
||||
|
||||
Deno.test("walkDir - ", async () => {
|
||||
const fs = createFakeFs({
|
||||
@@ -43,3 +43,25 @@ Deno.test("walkDir - respects skip patterns", async () => {
|
||||
"routes/api/users.ts",
|
||||
]);
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "crawlRouteDir.filePath - normalized Windows paths",
|
||||
ignore: Deno.build.os !== "windows",
|
||||
fn: async () => {
|
||||
const fs = createFakeFs({
|
||||
"foo\\bar\\baz.txt": "foo",
|
||||
"D:\\foo\\bar.tsx": "foo",
|
||||
});
|
||||
|
||||
const rawFiles = await crawlRouteDir(fs, "foo", [], () => {});
|
||||
|
||||
expect(rawFiles).toEqual(expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
filePath: "foo/bar/baz.txt",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
filePath: "D:/foo/bar.tsx",
|
||||
}),
|
||||
]));
|
||||
},
|
||||
});
|
||||
|
||||
@@ -106,7 +106,9 @@ export function fsItemsToCommands<State>(
|
||||
}
|
||||
if (!mod.default) continue;
|
||||
|
||||
commands.push(newLayoutCmd(pattern, mod.default, mod.config, true));
|
||||
commands.push(
|
||||
newLayoutCmd(pattern, mod.default, mod.config, true, mod.css),
|
||||
);
|
||||
continue;
|
||||
}
|
||||
case CommandType.Error: {
|
||||
@@ -116,6 +118,7 @@ export function fsItemsToCommands<State>(
|
||||
{
|
||||
component: mod.default ?? undefined,
|
||||
config: mod.config ?? undefined,
|
||||
css: mod.css,
|
||||
// deno-lint-ignore no-explicit-any
|
||||
handler: (handlers as any) ?? undefined,
|
||||
},
|
||||
@@ -128,6 +131,7 @@ export function fsItemsToCommands<State>(
|
||||
commands.push(newNotFoundCmd({
|
||||
config: mod.config,
|
||||
component: mod.default,
|
||||
css: mod.css,
|
||||
// deno-lint-ignore no-explicit-any
|
||||
handler: handlers as any ?? undefined,
|
||||
}));
|
||||
@@ -137,7 +141,7 @@ export function fsItemsToCommands<State>(
|
||||
const { mod } = validateFsMod<State>(filePath, rawMod, type);
|
||||
if (mod.default === undefined) continue;
|
||||
|
||||
commands.push(newAppCmd(mod.default));
|
||||
commands.push(newAppCmd(mod.default, mod.css));
|
||||
continue;
|
||||
}
|
||||
case CommandType.Route: {
|
||||
|
||||
@@ -7,6 +7,7 @@ export {
|
||||
type IslandModChunk,
|
||||
type PendingStaticFile,
|
||||
prepareStaticFile,
|
||||
toPosix,
|
||||
writeCompiledEntry,
|
||||
} from "./dev/dev_build_cache.ts";
|
||||
export { specToName } from "./dev/builder.ts";
|
||||
|
||||
@@ -2,7 +2,7 @@ import type { AnyComponent } from "preact";
|
||||
import type { MaybeLazyMiddleware, Middleware } from "./middlewares/mod.ts";
|
||||
import { type Method, patternToSegments } from "./router.ts";
|
||||
import type { LayoutConfig, Route } from "./types.ts";
|
||||
import { type Context, getInternals } from "./context.ts";
|
||||
import { type Context, getInternals, setAdditionalStyles } from "./context.ts";
|
||||
import { recordSpanError, tracer } from "./otel.ts";
|
||||
import { type HandlerFn, isHandlerByMethod } from "./handlers.ts";
|
||||
import {
|
||||
@@ -22,10 +22,14 @@ export interface Segment<State> {
|
||||
layout: {
|
||||
component: RouteComponent<State>;
|
||||
config: LayoutConfig | null;
|
||||
css: string[] | null;
|
||||
} | null;
|
||||
errorRoute: Route<State> | null;
|
||||
notFound: Middleware<State> | null;
|
||||
app: RouteComponent<State> | null;
|
||||
app: {
|
||||
component: RouteComponent<State>;
|
||||
css: string[] | null;
|
||||
} | null;
|
||||
children: Map<string, Segment<State>>;
|
||||
parent: Segment<State> | null;
|
||||
}
|
||||
@@ -105,7 +109,11 @@ export function segmentToMiddlewares<State>(
|
||||
internals.app = null;
|
||||
}
|
||||
|
||||
const def = { props: null, component: layout.component };
|
||||
const def = {
|
||||
props: null,
|
||||
component: layout.component,
|
||||
css: layout.css,
|
||||
};
|
||||
if (layout.config?.skipInheritedLayouts) {
|
||||
internals.layouts = [def];
|
||||
} else {
|
||||
@@ -145,6 +153,8 @@ export async function renderRoute<State>(
|
||||
route: Route<State>,
|
||||
status = 200,
|
||||
): Promise<Response> {
|
||||
setAdditionalStyles(ctx, route.css);
|
||||
|
||||
const internals = getInternals(ctx);
|
||||
if (route.config?.skipAppWrapper) {
|
||||
internals.app = null;
|
||||
|
||||
@@ -7,6 +7,7 @@ import { DEFAULT_CONN_INFO } from "./app.ts";
|
||||
import type { Command } from "./commands.ts";
|
||||
import { fsItemsToCommands, type FsRouteFile } from "./fs_routes.ts";
|
||||
import * as path from "@std/path";
|
||||
import { toPosix } from "./dev/dev_build_cache.ts";
|
||||
|
||||
const STUB = {} as unknown as Deno.ServeHandlerInfo;
|
||||
|
||||
@@ -123,7 +124,10 @@ export function createFakeFs(files: Record<string, unknown>): FsAdapter {
|
||||
},
|
||||
// deno-lint-ignore require-await
|
||||
async isDirectory(dir) {
|
||||
return Object.keys(files).some((file) => file.startsWith(dir + "/"));
|
||||
return Object.keys(files).some((file) =>
|
||||
// normalize path to posix before comparing
|
||||
toPosix(file).startsWith(dir + "/")
|
||||
);
|
||||
},
|
||||
async mkdirp(_dir: string) {
|
||||
},
|
||||
|
||||
@@ -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