diff --git a/components/AutoGrid.tsx b/components/AutoGrid.tsx index bc26b2f..d9a0afd 100644 --- a/components/AutoGrid.tsx +++ b/components/AutoGrid.tsx @@ -1,5 +1,8 @@ import { ComponentChildren, JSX } from 'preact' -import { getStyleScope, useSmartStylesheet } from ':plugins/SmartStylesheet.tsx' +import { + getStyleScope, + useSmartStylesheet, +} from ':plugins/SmartStylesheetIsland.tsx' type Units = 'rem' | '%' | 'px' diff --git a/components/BlogBlocks.tsx b/components/BlogBlocks.tsx index c65b639..c926c73 100644 --- a/components/BlogBlocks.tsx +++ b/components/BlogBlocks.tsx @@ -1,6 +1,6 @@ import { Markdown } from ':components/Markdown.tsx' import { NewsFrontMatter } from ':src/blog/types.ts' -import { useSmartStylesheet } from ':plugins/SmartStylesheet.tsx' +import { useSmartStylesheet } from ':plugins/SmartStylesheetIsland.tsx' export type BlogProps = { title: string diff --git a/components/CohabitInfoTable.tsx b/components/CohabitInfoTable.tsx index e4ec886..2c233df 100644 --- a/components/CohabitInfoTable.tsx +++ b/components/CohabitInfoTable.tsx @@ -1,4 +1,4 @@ -import { useSmartStylesheet } from ':plugins/SmartStylesheet.tsx' +import { useSmartStylesheet } from ':plugins/SmartStylesheetIsland.tsx' export function CohabitInfoTable() { return ( diff --git a/components/Footer.tsx b/components/Footer.tsx index bd4e1d9..c708f0c 100644 --- a/components/Footer.tsx +++ b/components/Footer.tsx @@ -1,4 +1,7 @@ -import { getStyleScope, useSmartStylesheet } from ':plugins/SmartStylesheet.tsx' +import { + getStyleScope, + useSmartStylesheet, +} from ':plugins/SmartStylesheetIsland.tsx' const scope = getStyleScope(Footer) diff --git a/components/Header.tsx b/components/Header.tsx index 4fff423..8294c28 100644 --- a/components/Header.tsx +++ b/components/Header.tsx @@ -3,7 +3,10 @@ import AiChatBox from ':islands/AiChatBox.tsx' import MoreBox from ':islands/MoreBox.tsx' import SearchBox from ':islands/SearchBox.tsx' import ThemePicker from ':islands/ThemePicker.tsx' -import { getStyleScope, useSmartStylesheet } from ':plugins/SmartStylesheet.tsx' +import { + getStyleScope, + useSmartStylesheet, +} from ':plugins/SmartStylesheetIsland.tsx' const scope = getStyleScope(Header) diff --git a/components/Heros.tsx b/components/Heros.tsx index c2c6a90..506bba8 100644 --- a/components/Heros.tsx +++ b/components/Heros.tsx @@ -1,4 +1,7 @@ -import { getStyleScope, useSmartStylesheet } from ':plugins/SmartStylesheet.tsx' +import { + getStyleScope, + useSmartStylesheet, +} from ':plugins/SmartStylesheetIsland.tsx' const scope = getStyleScope(Heros) diff --git a/components/MachineCard.tsx b/components/MachineCard.tsx index b181e47..8a8f128 100644 --- a/components/MachineCard.tsx +++ b/components/MachineCard.tsx @@ -1,4 +1,7 @@ -import { getStyleScope, useSmartStylesheet } from ':plugins/SmartStylesheet.tsx' +import { + getStyleScope, + useSmartStylesheet, +} from ':plugins/SmartStylesheetIsland.tsx' type MachineCardProps = { img: string diff --git a/components/MemberCard.tsx b/components/MemberCard.tsx index dc41c4c..2e187d8 100644 --- a/components/MemberCard.tsx +++ b/components/MemberCard.tsx @@ -1,4 +1,7 @@ -import { getStyleScope, useSmartStylesheet } from ':plugins/SmartStylesheet.tsx' +import { + getStyleScope, + useSmartStylesheet, +} from ':plugins/SmartStylesheetIsland.tsx' export type MemberCardProps = { id: string diff --git a/components/ProjectCard.tsx b/components/ProjectCard.tsx index 833a6cf..f930909 100644 --- a/components/ProjectCard.tsx +++ b/components/ProjectCard.tsx @@ -1,5 +1,8 @@ import { JSX } from 'preact' -import { getStyleScope, useSmartStylesheet } from ':plugins/SmartStylesheet.tsx' +import { + getStyleScope, + useSmartStylesheet, +} from ':plugins/SmartStylesheetIsland.tsx' type ProjectCardProps = { id: string diff --git a/components/SponsorCards.tsx b/components/SponsorCards.tsx index 77f0022..b429148 100644 --- a/components/SponsorCards.tsx +++ b/components/SponsorCards.tsx @@ -1,6 +1,9 @@ import { asset } from 'fresh/runtime' import { Picture } from ':components/Picture.tsx' -import { getStyleScope, useSmartStylesheet } from ':plugins/SmartStylesheet.tsx' +import { + getStyleScope, + useSmartStylesheet, +} from ':plugins/SmartStylesheetIsland.tsx' const scope = getStyleScope(SponsorCards) diff --git a/deno.json b/deno.json index e9dd5a5..345542b 100644 --- a/deno.json +++ b/deno.json @@ -30,8 +30,10 @@ "@deno/gfm": "jsr:@deno/gfm@^0.10.0", "@mdxeditor/editor": "npm:@mdxeditor/editor@^3.32.3", "@std/fs": "jsr:@std/fs@^1.0.6", + "@std/media-types": "jsr:@std/media-types@^1.1.0", "@std/path": "jsr:@std/path@^1.0.8", "fresh": "jsr:@fresh/core@^2.0.0-alpha.25", + "esbuild": "npm:esbuild@^0.25.4", ":components/": "./components/", ":islands/": "./islands/", ":src/": "./src/", diff --git a/islands/AiChatBox.tsx b/islands/AiChatBox.tsx index 044af56..190ae12 100644 --- a/islands/AiChatBox.tsx +++ b/islands/AiChatBox.tsx @@ -3,7 +3,7 @@ import { Markdown } from ':components/Markdown.tsx' import { Signal, signal, useSignal } from '@preact/signals' import { JSX } from 'preact' import { useEffect, useRef } from 'preact/hooks' -import { useSmartStylesheet } from ':plugins/SmartStylesheet.tsx' +import { useSmartStylesheet } from ':plugins/SmartStylesheetIsland.tsx' const systemHistory = signal([{ role: 'system', diff --git a/islands/IsOnline.tsx b/islands/IsOnline.tsx index 476b1cd..39e7ef0 100644 --- a/islands/IsOnline.tsx +++ b/islands/IsOnline.tsx @@ -1,6 +1,9 @@ import { type Signal, useComputed, useSignal } from '@preact/signals' import { useEffect } from 'preact/hooks' -import { getStyleScope, useSmartStylesheet } from ':plugins/SmartStylesheet.tsx' +import { + getStyleScope, + useSmartStylesheet, +} from ':plugins/SmartStylesheetIsland.tsx' type NetworkConnection = { addEventListener: ( diff --git a/islands/LoginForm.tsx b/islands/LoginForm.tsx index 1993f1b..cba3226 100644 --- a/islands/LoginForm.tsx +++ b/islands/LoginForm.tsx @@ -7,7 +7,10 @@ import type { } from '../routes/api/webauthn/login/[step].ts' import { Button } from ':components/Button.tsx' import { Input } from ':components/Input.tsx' -import { getStyleScope, useSmartStylesheet } from ':plugins/SmartStylesheet.tsx' +import { + getStyleScope, + useSmartStylesheet, +} from ':plugins/SmartStylesheetIsland.tsx' const scope = getStyleScope(LoginForm) diff --git a/islands/MoreBox.tsx b/islands/MoreBox.tsx index d1fe25e..d661a87 100644 --- a/islands/MoreBox.tsx +++ b/islands/MoreBox.tsx @@ -1,6 +1,6 @@ import { VNode } from 'preact' import { useEffect, useRef } from 'preact/hooks' -import { useSmartStylesheet } from ':plugins/SmartStylesheet.tsx' +import { useSmartStylesheet } from ':plugins/SmartStylesheetIsland.tsx' export default function MoreBox({ children }: { children: VNode | VNode[] }) { useSmartStylesheet(import.meta) diff --git a/islands/SearchBox.tsx b/islands/SearchBox.tsx index 70831d9..fccdeae 100644 --- a/islands/SearchBox.tsx +++ b/islands/SearchBox.tsx @@ -1,5 +1,5 @@ import { useEffect, useRef } from 'preact/hooks' -import { useSmartStylesheet } from ':plugins/SmartStylesheet.tsx' +import { useSmartStylesheet } from ':plugins/SmartStylesheetIsland.tsx' export default function SearchBox() { useSmartStylesheet(import.meta) diff --git a/islands/ThemePicker.tsx b/islands/ThemePicker.tsx index a287138..532a93e 100644 --- a/islands/ThemePicker.tsx +++ b/islands/ThemePicker.tsx @@ -1,6 +1,9 @@ import { useComputed, useSignal, useSignalEffect } from '@preact/signals' import { IS_BROWSER } from 'fresh/runtime' -import { getStyleScope, useSmartStylesheet } from ':plugins/SmartStylesheet.tsx' +import { + getStyleScope, + useSmartStylesheet, +} from ':plugins/SmartStylesheetIsland.tsx' const scope = getStyleScope(ThemePicker) diff --git a/main.ts b/main.ts index c2fd4c7..089c2e4 100644 --- a/main.ts +++ b/main.ts @@ -1,6 +1,5 @@ import { App, fsRoutes, staticFiles } from 'fresh' import { type State } from './utils.ts' -import { contentType } from 'jsr:@std/media-types@1/content-type' import { smartStylesheetPlugin } from ':plugins/SmartStylesheet.tsx' export const app = new App() @@ -16,31 +15,6 @@ app.use(staticFiles()) smartStylesheetPlugin(app) -//TEMP fix before updating cssBundler middleware -app.use(async (ctx) => { - const response = await ctx.next() - if ( - response.status === 404 && - !ctx.url.pathname.match(/\/js\/[0-9a-f]+\/\S+\.js/) - ) { - const ext = ctx.url.pathname.split('.').at(-1) ?? '.bin' - const mime = contentType(ext) ?? 'application/octet-stream' - - try { - const file = await Deno.readFile(`./_fresh/static/${ctx.url.pathname}`) - return new Response(file, { - headers: { - 'Content-Type': mime, - }, - }) - } catch { - //TEMP don't handle specific error for now - return response - } - } - return response -}) - await fsRoutes(app, { dir: './', loadIsland: (path) => import(`./islands/${path}`), diff --git a/plugins/SmartStylesheet.tsx b/plugins/SmartStylesheet.tsx index 3b26792..51ec553 100644 --- a/plugins/SmartStylesheet.tsx +++ b/plugins/SmartStylesheet.tsx @@ -1,125 +1,16 @@ -import { asset, IS_BROWSER } from 'fresh/runtime' +import { asset } from 'fresh/runtime' import { App } from 'fresh' +import { build, type Plugin } from 'esbuild' +import { extname, fromFileUrl, toFileUrl } from '@std/path' +import { exists } from '@std/fs/exists' +import { ensureDir } from '@std/fs' +import { contentType } from '@std/media-types' +import { styles } from './SmartStylesheetCommon.tsx' -/** - * List of css files imported by the current fresh route. - */ -const styles = new Map< - string, - { url: string; layer: string | undefined; scope: string | undefined } ->() +const bundledFiles = new Set() const baseRoute = '__smart_css__' -/** - * Generate a css scope for the given component/island based on its name. - * - * @param component - Component or island to scope. - * @returns scope - css scope class. - * - * @example - * ```ts - * // ./(components|islands)/Button.tsx - * import type { JSX } from 'preact' - * import { getStyleScope, useSmartStylesheet } from './SmartStylesheet.tsx' - * - * const scope = getStyleScope(Button) - * - * export function Button(props: JSX.ButtonHTMLAttributes) { - * useSmartStylesheet(import.meta, { scope }) - * - * return