diff --git a/.vscode/import_map.json b/.vscode/import_map.json index 1e99475a..328391a2 100644 --- a/.vscode/import_map.json +++ b/.vscode/import_map.json @@ -16,6 +16,7 @@ "$ga4": "https://raw.githubusercontent.com/denoland/ga4/main/mod.ts", "$marked-mangle": "https://esm.sh/marked-mangle@1.0.1", "$fresh-testing-library": "https://deno.land/x/fresh_testing_library@0.11.1/mod.ts", - "$fresh-testing-library/": "https://deno.land/x/fresh_testing_library@0.11.1/" + "$fresh-testing-library/": "https://deno.land/x/fresh_testing_library@0.11.1/", + "tailwindcss": "npm:tailwindcss@3.3.5" } } diff --git a/plugins/tailwind.ts b/plugins/tailwind.ts new file mode 100644 index 00000000..faa85d06 --- /dev/null +++ b/plugins/tailwind.ts @@ -0,0 +1,161 @@ +import { Plugin, PluginMiddleware, ResolvedFreshConfig } from "../server.ts"; +import tailwindCss, { Config } from "tailwindcss"; +import postcss from "npm:postcss@8.4.31"; +import * as path from "https://deno.land/std@0.207.0/path/mod.ts"; +import { walk } from "https://deno.land/std@0.207.0/fs/walk.ts"; + +async function initTailwind( + config: ResolvedFreshConfig, +): Promise { + const root = path.dirname(config.staticDir); + let relativeRoot = ""; + if (root !== Deno.cwd()) { + relativeRoot = path.relative(Deno.cwd(), root).split(path.sep).join("/") + + "/"; + } + + let tailwindConfig: Config = { + content: [ + `${relativeRoot}routes/**/*.{ts,tsx,js,jsx,mjs,cjs}`, + `${relativeRoot}components/**/*.{ts,tsx,js,jsx,mjs,cjs}`, + ], + }; + + const configFilePaths = [ + path.join(Deno.cwd(), "tailwind.config.ts"), + path.join(Deno.cwd(), "tailwind.config.js"), + path.join(Deno.cwd(), "tailwind.config.mjs"), + path.join(root, "tailwind.config.ts"), + path.join(root, "tailwind.config.js"), + path.join(root, "tailwind.config.mjs"), + ]; + for (const configFile of configFilePaths) { + let importedConfig: Config | null = null; + try { + const url = path.toFileUrl(configFile).href; + importedConfig = (await import(url)).default as Config; + } catch (_err) { + // ignore + } + + if (importedConfig === null) continue; + + if (!Array.isArray(importedConfig.content)) { + throw new Error(`Expected tailwind "content" option to be an array`); + } + + const configContent = importedConfig.content.map((pattern) => { + if (typeof pattern === "string") { + return path.join( + path.relative(Deno.cwd(), path.dirname(configFile)), + pattern, + ); + } + return pattern; + }); + + tailwindConfig = { + ...importedConfig, + content: [ + ...configContent, + ...tailwindConfig.content as string[], + ], + }; + break; + } + + return postcss([tailwindCss(tailwindConfig)]); +} + +export default function tailwind(): Plugin { + let staticDir = path.join(Deno.cwd(), "static"); + let processor: postcss.Processor | null = null; + + const cache = new Map(); + + const tailwindMiddleware: PluginMiddleware = { + path: "/", + middleware: { + handler: async (req, ctx) => { + const pathname = new URL(req.url).pathname; + + if (req.url.endsWith(".css.map")) { + const cached = cache.get(pathname); + if (cached) return Response.json(cached.map); + } + + if (!req.url.endsWith(".css") || !processor) { + return ctx.next(); + } + + let cached = cache.get(pathname); + if (!cached) { + const filePath = path.join(staticDir, pathname); + let text = ""; + try { + text = await Deno.readTextFile(filePath); + const res = await processor.process(text, { + from: undefined, + }); + + cached = { + content: res.content, + map: res.map?.toString() ?? "", + }; + cache.set(pathname, cached); + } catch (err) { + cached = { + content: text, + map: "", + }; + console.error(err); + } + } + + return new Response(cached!.content, { + status: 200, + headers: { + "Content-Type": "text/css", + "Cache-Control": "no-cache, no-store, max-age=0, must-revalidate", + }, + }); + }, + }, + }; + + const middlewares: Plugin["middlewares"] = []; + + return { + name: "tailwind", + async configResolved(config) { + if (config.dev) { + staticDir = config.staticDir; + processor = await initTailwind(config); + middlewares.push(tailwindMiddleware); + } + }, + middlewares, + async buildStart(config) { + staticDir = config.staticDir; + const outDir = path.join(config.build.outDir, "static"); + processor = await initTailwind(config); + + const files = walk(config.staticDir, { + exts: ["css"], + includeDirs: false, + includeFiles: true, + }); + + for await (const file of files) { + const content = await Deno.readTextFile(file.path); + const result = await processor.process(content, { + from: undefined, + }); + + const relFilePath = path.relative(staticDir, file.path); + const outPath = path.join(outDir, relFilePath); + await Deno.writeTextFile(outPath, result.content); + } + }, + }; +} diff --git a/tests/fixture_plugin_middleware/deno.json b/tests/fixture_plugin_middleware/deno.json new file mode 100644 index 00000000..95bc3f0c --- /dev/null +++ b/tests/fixture_plugin_middleware/deno.json @@ -0,0 +1,12 @@ +{ + "lock": false, + "imports": { + "$fresh/": "../../", + "preact": "https://esm.sh/preact@10.15.1", + "preact/": "https://esm.sh/preact@10.15.1/" + }, + "compilerOptions": { + "jsx": "react-jsx", + "jsxImportSource": "preact" + } +} diff --git a/tests/fixture_plugin_middleware/dev.ts b/tests/fixture_plugin_middleware/dev.ts new file mode 100755 index 00000000..2d85d6c1 --- /dev/null +++ b/tests/fixture_plugin_middleware/dev.ts @@ -0,0 +1,5 @@ +#!/usr/bin/env -S deno run -A --watch=static/,routes/ + +import dev from "$fresh/dev.ts"; + +await dev(import.meta.url, "./main.ts"); diff --git a/tests/fixture_plugin_middleware/fresh.config.ts b/tests/fixture_plugin_middleware/fresh.config.ts new file mode 100644 index 00000000..0af825a9 --- /dev/null +++ b/tests/fixture_plugin_middleware/fresh.config.ts @@ -0,0 +1,8 @@ +import { defineConfig } from "$fresh/server.ts"; +import { middlewarePlugin } from "./plugins/middleware.ts"; + +export default defineConfig({ + plugins: [ + middlewarePlugin(), + ], +}); diff --git a/tests/fixture_plugin_middleware/fresh.gen.ts b/tests/fixture_plugin_middleware/fresh.gen.ts new file mode 100644 index 00000000..2cbaa823 --- /dev/null +++ b/tests/fixture_plugin_middleware/fresh.gen.ts @@ -0,0 +1,19 @@ +// DO NOT EDIT. This file is generated by Fresh. +// This file SHOULD be checked into source version control. +// This file is automatically updated during development when running `dev.ts`. + +import * as $_middleware from "./routes/_middleware.ts"; +import * as $index from "./routes/index.tsx"; + +import { Manifest } from "$fresh/server.ts"; + +const manifest = { + routes: { + "./routes/_middleware.ts": $_middleware, + "./routes/index.tsx": $index, + }, + islands: {}, + baseUrl: import.meta.url, +} satisfies Manifest; + +export default manifest; diff --git a/tests/fixture_plugin_middleware/main.ts b/tests/fixture_plugin_middleware/main.ts new file mode 100644 index 00000000..fc935921 --- /dev/null +++ b/tests/fixture_plugin_middleware/main.ts @@ -0,0 +1,11 @@ +/// +/// +/// +/// +/// + +import { start } from "$fresh/server.ts"; +import manifest from "./fresh.gen.ts"; +import config from "./fresh.config.ts"; + +await start(manifest, config); diff --git a/tests/fixture_plugin_middleware/plugins/middleware.ts b/tests/fixture_plugin_middleware/plugins/middleware.ts new file mode 100644 index 00000000..a31b54d8 --- /dev/null +++ b/tests/fixture_plugin_middleware/plugins/middleware.ts @@ -0,0 +1,18 @@ +import { Plugin } from "$fresh/server.ts"; + +export function middlewarePlugin(): Plugin { + return { + name: "mw-plugin", + middlewares: [ + { + path: "/", + middleware: { + handler: (req, ctx) => { + console.log("hey", req.url); + return ctx.next(); + }, + }, + }, + ], + }; +} diff --git a/tests/fixture_plugin_middleware/routes/_middleware.ts b/tests/fixture_plugin_middleware/routes/_middleware.ts new file mode 100644 index 00000000..5bdc227c --- /dev/null +++ b/tests/fixture_plugin_middleware/routes/_middleware.ts @@ -0,0 +1,8 @@ +import { MiddlewareHandlerContext } from "$fresh/server.ts"; + +export async function handler(req: Request, ctx: MiddlewareHandlerContext) { + console.log(ctx.destination); + console.log(req.url); + const resp = await ctx.next(); + return resp; +} diff --git a/tests/fixture_plugin_middleware/routes/index.tsx b/tests/fixture_plugin_middleware/routes/index.tsx new file mode 100644 index 00000000..b365cb64 --- /dev/null +++ b/tests/fixture_plugin_middleware/routes/index.tsx @@ -0,0 +1,3 @@ +export default function Page() { + return

foo

; +} diff --git a/tests/fixture_tailwind/deno.json b/tests/fixture_tailwind/deno.json new file mode 100644 index 00000000..d9ccbefa --- /dev/null +++ b/tests/fixture_tailwind/deno.json @@ -0,0 +1,13 @@ +{ + "lock": false, + "imports": { + "$fresh/": "../../", + "preact": "https://esm.sh/preact@10.15.1", + "preact/": "https://esm.sh/preact@10.15.1/", + "tailwindcss": "npm:tailwindcss@3.3.5" + }, + "compilerOptions": { + "jsx": "react-jsx", + "jsxImportSource": "preact" + } +} diff --git a/tests/fixture_tailwind/dev.ts b/tests/fixture_tailwind/dev.ts new file mode 100755 index 00000000..1fe3e340 --- /dev/null +++ b/tests/fixture_tailwind/dev.ts @@ -0,0 +1,6 @@ +#!/usr/bin/env -S deno run -A --watch=static/,routes/ + +import dev from "$fresh/dev.ts"; +import config from "./fresh.config.ts"; + +await dev(import.meta.url, "./main.ts", config); diff --git a/tests/fixture_tailwind/fresh.config.ts b/tests/fixture_tailwind/fresh.config.ts new file mode 100644 index 00000000..6f7acca6 --- /dev/null +++ b/tests/fixture_tailwind/fresh.config.ts @@ -0,0 +1,8 @@ +import { defineConfig } from "$fresh/server.ts"; +import tailwind from "$fresh/plugins/tailwind.ts"; + +export default defineConfig({ + plugins: [ + tailwind(), + ], +}); diff --git a/tests/fixture_tailwind/fresh.gen.ts b/tests/fixture_tailwind/fresh.gen.ts new file mode 100644 index 00000000..eff1b80e --- /dev/null +++ b/tests/fixture_tailwind/fresh.gen.ts @@ -0,0 +1,19 @@ +// DO NOT EDIT. This file is generated by Fresh. +// This file SHOULD be checked into source version control. +// This file is automatically updated during development when running `dev.ts`. + +import * as $_app from "./routes/_app.tsx"; +import * as $index from "./routes/index.tsx"; + +import { Manifest } from "$fresh/server.ts"; + +const manifest = { + routes: { + "./routes/_app.tsx": $_app, + "./routes/index.tsx": $index, + }, + islands: {}, + baseUrl: import.meta.url, +} satisfies Manifest; + +export default manifest; diff --git a/tests/fixture_tailwind/main.ts b/tests/fixture_tailwind/main.ts new file mode 100644 index 00000000..fc935921 --- /dev/null +++ b/tests/fixture_tailwind/main.ts @@ -0,0 +1,11 @@ +/// +/// +/// +/// +/// + +import { start } from "$fresh/server.ts"; +import manifest from "./fresh.gen.ts"; +import config from "./fresh.config.ts"; + +await start(manifest, config); diff --git a/tests/fixture_tailwind/routes/_app.tsx b/tests/fixture_tailwind/routes/_app.tsx new file mode 100644 index 00000000..46ac9767 --- /dev/null +++ b/tests/fixture_tailwind/routes/_app.tsx @@ -0,0 +1,17 @@ +import { AppProps } from "$fresh/server.ts"; + +export default function App({ Component }: AppProps) { + return ( + + + + + My Fresh app + + + + + + + ); +} diff --git a/tests/fixture_tailwind/routes/index.tsx b/tests/fixture_tailwind/routes/index.tsx new file mode 100644 index 00000000..ee3f8f3c --- /dev/null +++ b/tests/fixture_tailwind/routes/index.tsx @@ -0,0 +1,3 @@ +export default function Page() { + return

foo

; +} diff --git a/tests/fixture_tailwind/static/styles.css b/tests/fixture_tailwind/static/styles.css new file mode 100644 index 00000000..b5c61c95 --- /dev/null +++ b/tests/fixture_tailwind/static/styles.css @@ -0,0 +1,3 @@ +@tailwind base; +@tailwind components; +@tailwind utilities; diff --git a/tests/fixture_tailwind_config/deno.json b/tests/fixture_tailwind_config/deno.json new file mode 100644 index 00000000..d9ccbefa --- /dev/null +++ b/tests/fixture_tailwind_config/deno.json @@ -0,0 +1,13 @@ +{ + "lock": false, + "imports": { + "$fresh/": "../../", + "preact": "https://esm.sh/preact@10.15.1", + "preact/": "https://esm.sh/preact@10.15.1/", + "tailwindcss": "npm:tailwindcss@3.3.5" + }, + "compilerOptions": { + "jsx": "react-jsx", + "jsxImportSource": "preact" + } +} diff --git a/tests/fixture_tailwind_config/dev.ts b/tests/fixture_tailwind_config/dev.ts new file mode 100755 index 00000000..1fe3e340 --- /dev/null +++ b/tests/fixture_tailwind_config/dev.ts @@ -0,0 +1,6 @@ +#!/usr/bin/env -S deno run -A --watch=static/,routes/ + +import dev from "$fresh/dev.ts"; +import config from "./fresh.config.ts"; + +await dev(import.meta.url, "./main.ts", config); diff --git a/tests/fixture_tailwind_config/foo/Foo.tsx b/tests/fixture_tailwind_config/foo/Foo.tsx new file mode 100644 index 00000000..965dddb6 --- /dev/null +++ b/tests/fixture_tailwind_config/foo/Foo.tsx @@ -0,0 +1,3 @@ +export function Foo() { + return

foo

; +} diff --git a/tests/fixture_tailwind_config/fresh.config.ts b/tests/fixture_tailwind_config/fresh.config.ts new file mode 100644 index 00000000..6f7acca6 --- /dev/null +++ b/tests/fixture_tailwind_config/fresh.config.ts @@ -0,0 +1,8 @@ +import { defineConfig } from "$fresh/server.ts"; +import tailwind from "$fresh/plugins/tailwind.ts"; + +export default defineConfig({ + plugins: [ + tailwind(), + ], +}); diff --git a/tests/fixture_tailwind_config/fresh.gen.ts b/tests/fixture_tailwind_config/fresh.gen.ts new file mode 100644 index 00000000..eff1b80e --- /dev/null +++ b/tests/fixture_tailwind_config/fresh.gen.ts @@ -0,0 +1,19 @@ +// DO NOT EDIT. This file is generated by Fresh. +// This file SHOULD be checked into source version control. +// This file is automatically updated during development when running `dev.ts`. + +import * as $_app from "./routes/_app.tsx"; +import * as $index from "./routes/index.tsx"; + +import { Manifest } from "$fresh/server.ts"; + +const manifest = { + routes: { + "./routes/_app.tsx": $_app, + "./routes/index.tsx": $index, + }, + islands: {}, + baseUrl: import.meta.url, +} satisfies Manifest; + +export default manifest; diff --git a/tests/fixture_tailwind_config/main.ts b/tests/fixture_tailwind_config/main.ts new file mode 100644 index 00000000..fc935921 --- /dev/null +++ b/tests/fixture_tailwind_config/main.ts @@ -0,0 +1,11 @@ +/// +/// +/// +/// +/// + +import { start } from "$fresh/server.ts"; +import manifest from "./fresh.gen.ts"; +import config from "./fresh.config.ts"; + +await start(manifest, config); diff --git a/tests/fixture_tailwind_config/routes/_app.tsx b/tests/fixture_tailwind_config/routes/_app.tsx new file mode 100644 index 00000000..46ac9767 --- /dev/null +++ b/tests/fixture_tailwind_config/routes/_app.tsx @@ -0,0 +1,17 @@ +import { AppProps } from "$fresh/server.ts"; + +export default function App({ Component }: AppProps) { + return ( + + + + + My Fresh app + + + + + + + ); +} diff --git a/tests/fixture_tailwind_config/routes/index.tsx b/tests/fixture_tailwind_config/routes/index.tsx new file mode 100644 index 00000000..d69dd510 --- /dev/null +++ b/tests/fixture_tailwind_config/routes/index.tsx @@ -0,0 +1,5 @@ +import { Foo } from "../foo/Foo.tsx"; + +export default function Page() { + return ; +} diff --git a/tests/fixture_tailwind_config/static/styles.css b/tests/fixture_tailwind_config/static/styles.css new file mode 100644 index 00000000..b5c61c95 --- /dev/null +++ b/tests/fixture_tailwind_config/static/styles.css @@ -0,0 +1,3 @@ +@tailwind base; +@tailwind components; +@tailwind utilities; diff --git a/tests/fixture_tailwind_config/tailwind.config.ts b/tests/fixture_tailwind_config/tailwind.config.ts new file mode 100644 index 00000000..eaef5a3a --- /dev/null +++ b/tests/fixture_tailwind_config/tailwind.config.ts @@ -0,0 +1,11 @@ +/** @type {import('tailwindcss').Config} */ +export default { + content: ["./foo/**/*.{ts,tsx}"], + theme: { + extend: { + colors: { + pp: "peachpuff", + }, + }, + }, +}; diff --git a/tests/tailwind_test.ts b/tests/tailwind_test.ts new file mode 100644 index 00000000..25cfa0af --- /dev/null +++ b/tests/tailwind_test.ts @@ -0,0 +1,31 @@ +import { runBuild, withFakeServe } from "$fresh/tests/test_utils.ts"; +import { assertStringIncludes } from "$std/assert/mod.ts"; + +Deno.test("TailwindCSS - dev mode", async () => { + await withFakeServe("./tests/fixture_tailwind/dev.ts", async (server) => { + const res = await server.get("/styles.css"); + const content = await res.text(); + assertStringIncludes(content, ".text-red-600"); + }, { loadConfig: true }); +}); + +Deno.test("TailwindCSS - build mode", async () => { + await runBuild("./tests/fixture_tailwind/dev.ts"); + await withFakeServe("./tests/fixture_tailwind/main.ts", async (server) => { + const res = await server.get("/styles.css"); + const content = await res.text(); + assertStringIncludes(content, ".text-red-600"); + }, { loadConfig: true }); +}); + +Deno.test("TailwindCSS - config", async () => { + await withFakeServe( + "./tests/fixture_tailwind_config/dev.ts", + async (server) => { + const res = await server.get("/styles.css"); + const content = await res.text(); + assertStringIncludes(content, ".text-pp"); + }, + { loadConfig: true }, + ); +});