diff --git a/plugins/tailwind.ts b/plugins/tailwind.ts index faa85d06..bddc7cca 100644 --- a/plugins/tailwind.ts +++ b/plugins/tailwind.ts @@ -1,6 +1,7 @@ import { Plugin, PluginMiddleware, ResolvedFreshConfig } from "../server.ts"; import tailwindCss, { Config } from "tailwindcss"; import postcss from "npm:postcss@8.4.31"; +import cssnano from "npm:cssnano@6.0.1"; 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"; @@ -16,8 +17,7 @@ async function initTailwind( let tailwindConfig: Config = { content: [ - `${relativeRoot}routes/**/*.{ts,tsx,js,jsx,mjs,cjs}`, - `${relativeRoot}components/**/*.{ts,tsx,js,jsx,mjs,cjs}`, + `${relativeRoot}{routes,components,islands}/**/*.{ts,tsx,js,jsx,mjs,cjs}`, ], }; @@ -39,6 +39,7 @@ async function initTailwind( } if (importedConfig === null) continue; + importedConfig.content = importedConfig.content ?? []; if (!Array.isArray(importedConfig.content)) { throw new Error(`Expected tailwind "content" option to be an array`); @@ -64,7 +65,15 @@ async function initTailwind( break; } - return postcss([tailwindCss(tailwindConfig)]); + const plugins: postcss.AcceptedPlugin[] = [ + tailwindCss(tailwindConfig), + ]; + + if (!config.dev) { + plugins.push(cssnano()); + } + + return postcss(plugins); } export default function tailwind(): Plugin { diff --git a/tests/fixture_tailwind_build/deno.json b/tests/fixture_tailwind_build/deno.json new file mode 100644 index 00000000..d9ccbefa --- /dev/null +++ b/tests/fixture_tailwind_build/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_build/dev.ts b/tests/fixture_tailwind_build/dev.ts new file mode 100755 index 00000000..1fe3e340 --- /dev/null +++ b/tests/fixture_tailwind_build/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_build/fresh.config.ts b/tests/fixture_tailwind_build/fresh.config.ts new file mode 100644 index 00000000..6f7acca6 --- /dev/null +++ b/tests/fixture_tailwind_build/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_build/fresh.gen.ts b/tests/fixture_tailwind_build/fresh.gen.ts new file mode 100644 index 00000000..eff1b80e --- /dev/null +++ b/tests/fixture_tailwind_build/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_build/main.ts b/tests/fixture_tailwind_build/main.ts new file mode 100644 index 00000000..fc935921 --- /dev/null +++ b/tests/fixture_tailwind_build/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_build/routes/_app.tsx b/tests/fixture_tailwind_build/routes/_app.tsx new file mode 100644 index 00000000..46ac9767 --- /dev/null +++ b/tests/fixture_tailwind_build/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_build/routes/index.tsx b/tests/fixture_tailwind_build/routes/index.tsx new file mode 100644 index 00000000..ee3f8f3c --- /dev/null +++ b/tests/fixture_tailwind_build/routes/index.tsx @@ -0,0 +1,3 @@ +export default function Page() { + return

foo

; +} diff --git a/tests/fixture_tailwind_build/static/styles.css b/tests/fixture_tailwind_build/static/styles.css new file mode 100644 index 00000000..b5c61c95 --- /dev/null +++ b/tests/fixture_tailwind_build/static/styles.css @@ -0,0 +1,3 @@ +@tailwind base; +@tailwind components; +@tailwind utilities; diff --git a/tests/tailwind_test.ts b/tests/tailwind_test.ts index 25cfa0af..7acb2f6e 100644 --- a/tests/tailwind_test.ts +++ b/tests/tailwind_test.ts @@ -10,12 +10,16 @@ Deno.test("TailwindCSS - dev mode", async () => { }); 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 }); + await runBuild("./tests/fixture_tailwind_build/dev.ts"); + await withFakeServe( + "./tests/fixture_tailwind_build/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 () => {