fix(vite): support ssr css modules (#3355)
Fixes https://github.com/denoland/fresh/issues/3325
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { setAdditionalStyles } from "./context.ts";
|
||||
import { HttpError } from "./error.ts";
|
||||
import { isHandlerByMethod, type PageResponse } from "./handlers.ts";
|
||||
import type { MaybeLazyMiddleware, Middleware } from "./middlewares/mod.ts";
|
||||
@@ -282,6 +283,10 @@ function applyCommandsInner<State>(
|
||||
def = await route();
|
||||
}
|
||||
|
||||
if (def.css !== undefined) {
|
||||
setAdditionalStyles(ctx, def.css);
|
||||
}
|
||||
|
||||
return renderRoute(ctx, def);
|
||||
});
|
||||
|
||||
|
||||
@@ -52,6 +52,7 @@ 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;
|
||||
|
||||
/**
|
||||
* The context passed to every middleware. It is unique for every request.
|
||||
@@ -116,6 +117,7 @@ export class Context<State> {
|
||||
next: () => Promise<Response>;
|
||||
|
||||
#buildCache: BuildCache<State>;
|
||||
#additionalStyles: string[] | null = null;
|
||||
|
||||
Component!: FunctionComponent;
|
||||
|
||||
@@ -123,6 +125,8 @@ 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;
|
||||
}
|
||||
|
||||
constructor(
|
||||
@@ -280,6 +284,13 @@ export class Context<State> {
|
||||
partialId,
|
||||
);
|
||||
|
||||
if (this.#additionalStyles !== null) {
|
||||
for (let i = 0; i < this.#additionalStyles.length; i++) {
|
||||
const css = this.#additionalStyles[i];
|
||||
state.islandAssets.add(css);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
setRenderState(state);
|
||||
|
||||
|
||||
@@ -91,6 +91,7 @@ export async function crawlRouteDir<State>(
|
||||
pattern,
|
||||
routePattern,
|
||||
lazy,
|
||||
css: [],
|
||||
overrideConfig,
|
||||
});
|
||||
}, ignore);
|
||||
|
||||
@@ -25,6 +25,7 @@ export interface FreshFsMod<State> {
|
||||
default?:
|
||||
| AnyComponent<PageProps<unknown, State>>
|
||||
| AsyncAnyComponent<PageProps<unknown, State>>;
|
||||
css?: string[];
|
||||
}
|
||||
|
||||
export interface FsRouteFile<State> {
|
||||
@@ -35,6 +36,7 @@ export interface FsRouteFile<State> {
|
||||
type: CommandType;
|
||||
routePattern: string;
|
||||
overrideConfig: RouteConfig | undefined;
|
||||
css: string[];
|
||||
}
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
@@ -334,5 +336,6 @@ function normalizeRoute<State>(
|
||||
// deno-lint-ignore no-explicit-any
|
||||
handler: (handlers as any) ?? undefined,
|
||||
component: mod.default,
|
||||
css: rawMod.css,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -56,6 +56,8 @@ export interface Route<State> {
|
||||
component?: RouteComponent<State>;
|
||||
config?: RouteConfig;
|
||||
handler?: RouteHandler<unknown, State>;
|
||||
/** Additional .css files to load */
|
||||
css?: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user