Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions packages/plugin-tailwindcss/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { app } from "./main.ts";
import { tailwind } from "@fresh/plugin-tailwind";

const builder = new Builder();
tailwind(builder, app);
tailwind(builder);

if (Deno.args.includes("build")) {
builder.build(app);
Expand All @@ -29,15 +29,12 @@ import { app } from "./main.ts";
import { tailwind } from "@fresh/plugin-tailwind";

const builder = new Builder();
tailwind(builder, app, {
tailwind(builder, {
// Exclude certain files from processing
exclude: ["/admin/**", "*.temp.css"],

// Force optimization (defaults to production mode)
optimize: true,

// Exclude base styles
base: null,
});

if (Deno.args.includes("build")) {
Expand Down
63 changes: 63 additions & 0 deletions packages/plugin-tailwindcss/src/mod.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
/**
* Tailwind CSS v4 plugin for Fresh.
*
* Registers a static-file transform on the supplied {@linkcode Builder} that
* runs `.css` files through `@tailwindcss/postcss`. CSS is optimized
* automatically when Fresh is in production mode.
*
* @example
* ```ts
* // dev.ts
* import { Builder } from "fresh/dev";
* import { app } from "./main.ts";
* import { tailwind } from "@fresh/plugin-tailwind";
*
* const builder = new Builder();
* tailwind(builder);
*
* if (Deno.args.includes("build")) {
* await builder.build(app);
* } else {
* await builder.listen(app);
* }
* ```
*
* @module
*/

import type { Builder } from "fresh/dev";
import twPostcss from "@tailwindcss/postcss";
import postcss from "postcss";
Expand All @@ -6,6 +33,42 @@ import type { TailwindPluginOptions } from "./types.ts";
// Re-export types for public API
export type { TailwindPluginOptions } from "./types.ts";

/**
* Register the Tailwind CSS v4 transform on a Fresh {@linkcode Builder}.
*
* The plugin attaches a `.css` static-file transform that processes stylesheets
* with `@tailwindcss/postcss`. When `builder.config.mode === "production"` the
* default `optimize` setting is `true`; in development it is `false`. Either
* default can be overridden via `options.optimize`.
*
* Files matched by `options.exclude` are passed through untouched, which is
* useful for routes or partials that ship their own CSS.
*
* @param builder The Fresh dev builder to attach the transform to.
* @param options Optional Tailwind and exclusion settings. See
* {@linkcode TailwindPluginOptions}.
*
* @example Basic usage
* ```ts
* import { Builder } from "fresh/dev";
* import { tailwind } from "@fresh/plugin-tailwind";
*
* const builder = new Builder();
* tailwind(builder);
* ```
*
* @example Customize options
* ```ts
* import { Builder } from "fresh/dev";
* import { tailwind } from "@fresh/plugin-tailwind";
*
* const builder = new Builder();
* tailwind(builder, {
* exclude: ["/admin/**", "*.temp.css"],
* optimize: { minify: true },
* });
* ```
*/
export function tailwind(
builder: Builder,
options: TailwindPluginOptions = {},
Expand Down
35 changes: 29 additions & 6 deletions packages/plugin-tailwindcss/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,45 @@
import type { OnTransformOptions } from "fresh/dev";

// based on the original code, this type is used to define options for the Tailwind plugin
/**
* Internal option shape forwarded to `@tailwindcss/postcss`. Consumers should
* use {@linkcode TailwindPluginOptions} instead.
*/
type PluginOptions = {
/**
* Base CSS to be included. Set to null to exclude base styles.
* Base CSS to be included. Set to `null` to exclude base styles.
*/
base?: string;
/**
* Enable or disable CSS optimization. Defaults to true if Fresh is in production mode and false otherwise.
* Can be a boolean or an object with minify options.
* @default app.config.mode === "production"
* Enable or disable CSS optimization. Defaults to `true` when Fresh is
* running in production mode and `false` otherwise. Pass an object to
* fine-tune minification behavior.
*
* @default {builder.config.mode === "production"}
*/
optimize?: boolean | {
minify?: boolean;
};
};

/**
* Options accepted by the {@linkcode tailwind} plugin.
*
* @example Exclude an admin route and force optimization
* ```ts
* import { Builder } from "fresh/dev";
* import { tailwind } from "@fresh/plugin-tailwind";
*
* const builder = new Builder();
* tailwind(builder, {
* exclude: ["/admin/**", "*.temp.css"],
* optimize: true,
* });
* ```
*/
export interface TailwindPluginOptions extends PluginOptions {
/** Exclude paths or globs that should not be processed */
/**
* Paths or glob patterns that should be skipped by the Tailwind transform.
* Forwarded to {@linkcode Builder.onTransformStaticFile}.
*/
exclude?: OnTransformOptions["exclude"];
}