diff --git a/deno.json b/deno.json index d95a01f3..766a4d17 100644 --- a/deno.json +++ b/deno.json @@ -102,7 +102,8 @@ "base", "noscript", "template" - ] + ], + "types": ["./global.d.ts"] }, "lint": { "rules": { diff --git a/global.d.ts b/global.d.ts new file mode 100644 index 00000000..62cb7a7f --- /dev/null +++ b/global.d.ts @@ -0,0 +1,4 @@ +declare module "*.module.css" { + const CssMod: Record; + export default CssMod; +} diff --git a/packages/fresh/src/build_cache.ts b/packages/fresh/src/build_cache.ts index 91bb048b..a63cbc55 100644 --- a/packages/fresh/src/build_cache.ts +++ b/packages/fresh/src/build_cache.ts @@ -97,6 +97,7 @@ export class IslandPreparer { mod: Record, chunkName: string, modName: string, + css: string[], ) { for (const [name, value] of Object.entries(mod)) { if (typeof value !== "function") continue; @@ -110,6 +111,7 @@ export class IslandPreparer { file: chunkName, fn, name: uniqueName, + css, }); } } diff --git a/packages/fresh/src/context.ts b/packages/fresh/src/context.ts index e1a64647..3684ab5e 100644 --- a/packages/fresh/src/context.ts +++ b/packages/fresh/src/context.ts @@ -33,6 +33,7 @@ export interface Island { name: string; exportName: string; fn: ComponentType; + css: string[]; } export type ServerIslandRegistry = Map; diff --git a/packages/fresh/src/dev/builder.ts b/packages/fresh/src/dev/builder.ts index c5de0f0b..ad45b1b6 100644 --- a/packages/fresh/src/dev/builder.ts +++ b/packages/fresh/src/dev/builder.ts @@ -314,6 +314,7 @@ export class Builder { name, server: spec, browser: null, + css: [], }); } diff --git a/packages/fresh/src/dev/dev_build_cache.ts b/packages/fresh/src/dev/dev_build_cache.ts index fd5795b2..c275f23f 100644 --- a/packages/fresh/src/dev/dev_build_cache.ts +++ b/packages/fresh/src/dev/dev_build_cache.ts @@ -27,6 +27,7 @@ export interface IslandModChunk { name: string; server: string; browser: string | null; + css: string[]; } export type FsRouteFileNoMod = Omit, "mod"> & { @@ -206,7 +207,7 @@ export class MemoryBuildCache implements DevBuildCache { throw new Error(`Unexpected missing browser chunk`); } - preparer.prepare(this.islandRegistry, mod, chunk.browser, name); + preparer.prepare(this.islandRegistry, mod, chunk.browser, name, []); }, ), ); @@ -461,7 +462,8 @@ export async function generateSnapshotServer( const islandMarkers = islands.map((item) => { const browser = JSON.stringify(item.browser); const name = JSON.stringify(item.name); - return `islandPreparer.prepare(islands, ${item.name}, ${browser}, ${name});`; + const css = JSON.stringify(item.css); + return `islandPreparer.prepare(islands, ${item.name}, ${browser}, ${name}, ${css});`; }).join("\n"); const serializedFsRoutes = fsRoutesFiles diff --git a/packages/fresh/src/runtime/server/preact_hooks.tsx b/packages/fresh/src/runtime/server/preact_hooks.tsx index 46e4a181..6d8a3186 100644 --- a/packages/fresh/src/runtime/server/preact_hooks.tsx +++ b/packages/fresh/src/runtime/server/preact_hooks.tsx @@ -254,8 +254,9 @@ options[OptionsType.DIFF] = (vnode) => { RENDER_STATE!.renderedHtmlHead = true; const entryAssets = RENDER_STATE.buildCache.getEntryAssets(); + // deno-lint-ignore no-explicit-any + const items: VNode[] = []; if (entryAssets.length > 0) { - const items: VNode[] = []; for (let i = 0; i < entryAssets.length; i++) { const id = entryAssets[i]; @@ -266,32 +267,22 @@ options[OptionsType.DIFF] = (vnode) => { ); } } - - if (Array.isArray(vnode.props.children)) { - vnode.props.children.push(...items); - } else if ( - vnode.props.children !== null && - typeof vnode.props.children === "object" - ) { - // deno-lint-ignore no-explicit-any - items.push(vnode.props.children as any); - vnode.props.children = items; - } else { - vnode.props.children = items; - } } - // Append component to render remaining head nodes - const remaining = h(RemainingHead, null); + // deno-lint-ignore no-explicit-any + items.push(h(RemainingHead, null) as VNode); + if (Array.isArray(vnode.props.children)) { - vnode.props.children.push(remaining); + vnode.props.children.push(...items); } else if ( vnode.props.children !== null && typeof vnode.props.children === "object" ) { - vnode.props.children = [vnode.props.children, remaining]; + // deno-lint-ignore no-explicit-any + items.unshift(vnode.props.children as any); + vnode.props.children = items; } else { - vnode.props.children = remaining; + vnode.props.children = items; } break; @@ -412,8 +403,25 @@ options[OptionsType.DIFFED] = (vnode) => { }; function RemainingHead() { - if (RENDER_STATE !== null && RENDER_STATE.headComponents.size > 0) { - return h(Fragment, null, ...RENDER_STATE.headComponents.values()); + if (RENDER_STATE !== null) { + // deno-lint-ignore no-explicit-any + const items: VNode[] = []; + if (RENDER_STATE.headComponents.size > 0) { + items.push(...RENDER_STATE.headComponents.values()); + } + + RENDER_STATE.islands.forEach((island) => { + if (island.css.length > 0) { + for (let i = 0; i < island.css.length; i++) { + const css = island.css[i]; + items.push(h("link", { rel: "stylesheet", href: css })); + } + } + }); + + if (items.length > 0) { + return h(Fragment, null, items); + } } return null; } diff --git a/packages/plugin-vite/demo/islands/CssModules.module.css b/packages/plugin-vite/demo/islands/CssModules.module.css new file mode 100644 index 00000000..d810e456 --- /dev/null +++ b/packages/plugin-vite/demo/islands/CssModules.module.css @@ -0,0 +1,3 @@ +.root { + color: red; +} diff --git a/packages/plugin-vite/demo/islands/CssModules.tsx b/packages/plugin-vite/demo/islands/CssModules.tsx new file mode 100644 index 00000000..80e3e2b1 --- /dev/null +++ b/packages/plugin-vite/demo/islands/CssModules.tsx @@ -0,0 +1,6 @@ +// @ts-ignore upstream issue https://github.com/denoland/deno/issues/30560 +import styles from "./CssModules.module.css"; + +export function CssModules() { + return

red text

; +} diff --git a/packages/plugin-vite/demo/routes/tests/css_modules.tsx b/packages/plugin-vite/demo/routes/tests/css_modules.tsx new file mode 100644 index 00000000..93d24944 --- /dev/null +++ b/packages/plugin-vite/demo/routes/tests/css_modules.tsx @@ -0,0 +1,5 @@ +import { CssModules } from "../../islands/CssModules.tsx"; + +export default function Page() { + return ; +} diff --git a/packages/plugin-vite/deno.json b/packages/plugin-vite/deno.json index b6f53c1c..b078bcd6 100644 --- a/packages/plugin-vite/deno.json +++ b/packages/plugin-vite/deno.json @@ -10,7 +10,8 @@ }, "compilerOptions": { "jsx": "precompile", - "jsxImportSource": "preact" + "jsxImportSource": "preact", + "types": ["../../global.d.ts"] }, "tasks": { "demo": "vite demo", diff --git a/packages/plugin-vite/src/plugins/server_snapshot.ts b/packages/plugin-vite/src/plugins/server_snapshot.ts index 0097c079..a79bb606 100644 --- a/packages/plugin-vite/src/plugins/server_snapshot.ts +++ b/packages/plugin-vite/src/plugins/server_snapshot.ts @@ -153,6 +153,7 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] { name: def.name, server: id, browser: def.chunk ?? `/@id/fresh-island::${def.name}`, + css: [], }; }); } else { @@ -206,11 +207,14 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] { spec = spec.slice(1); } + const chunkCss = mod.css?.map((id) => `/${id}`) ?? []; + const name = namer.getUniqueName(specToName(id)); islandMods.push({ name, browser: spec, server: serverPath, + css: chunkCss, }); } } diff --git a/packages/plugin-vite/tests/build_test.ts b/packages/plugin-vite/tests/build_test.ts index 69892b7e..6ac78dc2 100644 --- a/packages/plugin-vite/tests/build_test.ts +++ b/packages/plugin-vite/tests/build_test.ts @@ -326,3 +326,27 @@ Deno.test({ sanitizeOps: false, sanitizeResources: false, }); + +Deno.test({ + name: "vite build - island css modules", + fn: async () => { + await launchProd( + { cwd: viteResult.tmp }, + async (address) => { + await withBrowser(async (page) => { + await page.goto(`${address}/tests/css_modules`, { + waitUntil: "networkidle2", + }); + + const color = await page + .locator("h1") + // deno-lint-ignore no-explicit-any + .evaluate((el) => window.getComputedStyle(el as any).color); + expect(color).toEqual("rgb(255, 0, 0)"); + }); + }, + ); + }, + sanitizeOps: false, + sanitizeResources: false, +}); diff --git a/packages/plugin-vite/tests/dev_server_test.ts b/packages/plugin-vite/tests/dev_server_test.ts index ed111214..e4d68a62 100644 --- a/packages/plugin-vite/tests/dev_server_test.ts +++ b/packages/plugin-vite/tests/dev_server_test.ts @@ -268,3 +268,24 @@ Deno.test({ sanitizeResources: false, sanitizeOps: false, }); + +Deno.test({ + name: "vite dev - island css modules", + fn: async () => { + await launchDevServer(tmp.dir, async (address) => { + await withBrowser(async (page) => { + await page.goto(`${address}/tests/css_modules`, { + waitUntil: "networkidle2", + }); + + const color = await page + .locator("h1") + // deno-lint-ignore no-explicit-any + .evaluate((el) => window.getComputedStyle(el as any).color); + expect(color).toEqual("rgb(255, 0, 0)"); + }); + }); + }, + sanitizeResources: false, + sanitizeOps: false, +});