feat: support tailwindcss v4 (#3054)
update /www and tailwind-plugin to support tailwind v4. Thanks to the merge of #3034, the work of integrating and advancing #2903 is promoted. related #3034 related #2903 close #3056 close #2819 --patch-1 based on review, 1. del compiler and make it more clean and simple, 2. fix init tailwind part, 3. fix test tailwind part --patch-2 1. fix the docs 2. fix the docs of plugin and version --patch-3 1. Addressed various maintenance tasks (chores) 2. Updated the Tailwind CSS plugin to align with this release. Generated a fresh project using the updated init configuration and 3. deployed it via Deno Deploy EA. Deployment is functioning correctly. --patch-4 Move postcss imports to Tailwind section in init script --patch-5 Refactored Tailwind plugin type definitions by copying PluginOptions properties from @tailwindcss/postcss and adding Fresh-specific exclude option, while exporting types from package entrypoint for better accessibility. Test Project: https://fresh.fresh-press.deno.net/ Test Project Repository: https://github.com/SisyphusZheng/fresh-tailwindv4 Test Plugin: https://jsr.io/@freshpress/fresh-plugin-tailwind@1.0.0 --------- Co-authored-by: 李嘉图·M·路 <146103794+Ricardo-M-Zheng@users.noreply.github.com> Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com> Co-authored-by: Marvin Hagemeister <marvin@deno.com>
This commit is contained in:
co-authored by
李嘉图·M·路
Asher Gomez
Marvin Hagemeister
parent
2d308f6b9e
commit
bb23643779
@@ -2,26 +2,48 @@
|
||||
|
||||
A Tailwind CSS plugin to use in Fresh.
|
||||
|
||||
## Basic Usage
|
||||
|
||||
```ts
|
||||
// dev.ts
|
||||
|
||||
import { tailwind } from "@fresh/plugin-tailwind";
|
||||
import { FreshDevApp } from "fresh/dev";
|
||||
import { Builder } from "fresh/dev";
|
||||
import { app } from "./main.ts";
|
||||
import { tailwind } from "@fresh/plugin-tailwind";
|
||||
|
||||
const devApp = new FreshDevApp();
|
||||
|
||||
// Enable Tailwind CSS
|
||||
tailwind(devApp);
|
||||
|
||||
devApp.mountApp("/", app);
|
||||
const builder = new Builder();
|
||||
tailwind(builder, app);
|
||||
|
||||
if (Deno.args.includes("build")) {
|
||||
await devApp.build({
|
||||
target: "safari12",
|
||||
});
|
||||
builder.build(app);
|
||||
} else {
|
||||
await devApp.listen();
|
||||
builder.listen(app);
|
||||
}
|
||||
```
|
||||
|
||||
## Option Configuration
|
||||
|
||||
```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, app, {
|
||||
// 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")) {
|
||||
builder.build(app);
|
||||
} else {
|
||||
builder.listen(app);
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@fresh/plugin-tailwind",
|
||||
"version": "0.0.1-alpha.7",
|
||||
"version": "0.0.1-alpha.8",
|
||||
"license": "MIT",
|
||||
"exports": "./src/mod.ts"
|
||||
}
|
||||
|
||||
@@ -1,80 +0,0 @@
|
||||
import tailwindCss, { type Config } from "tailwindcss";
|
||||
import postcss from "postcss";
|
||||
import autoprefixer from "autoprefixer";
|
||||
import * as path from "@std/path";
|
||||
import type { TailwindPluginOptions } from "./types.ts";
|
||||
import type { ResolvedFreshConfig } from "fresh";
|
||||
|
||||
const CONFIG_EXTENSIONS = ["ts", "js", "mjs"];
|
||||
|
||||
async function findTailwindConfigFile(directory: string): Promise<string> {
|
||||
let dir = directory;
|
||||
while (true) {
|
||||
for (let i = 0; i < CONFIG_EXTENSIONS.length; i++) {
|
||||
const ext = CONFIG_EXTENSIONS[i];
|
||||
const filePath = path.join(dir, `tailwind.config.${ext}`);
|
||||
try {
|
||||
const stat = await Deno.stat(filePath);
|
||||
if (stat.isFile) {
|
||||
return filePath;
|
||||
}
|
||||
} catch (err) {
|
||||
if (!(err instanceof Deno.errors.NotFound)) {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const parent = path.dirname(dir);
|
||||
if (parent === dir) {
|
||||
throw new Error(
|
||||
`Could not find a tailwind config file in the current directory or any parent directory.`,
|
||||
);
|
||||
}
|
||||
|
||||
dir = parent;
|
||||
}
|
||||
}
|
||||
|
||||
export async function initTailwind(
|
||||
config: ResolvedFreshConfig,
|
||||
options: TailwindPluginOptions,
|
||||
): Promise<postcss.Processor> {
|
||||
const root = path.dirname(config.staticDir);
|
||||
|
||||
const configPath = await findTailwindConfigFile(root);
|
||||
const url = path.toFileUrl(configPath).href;
|
||||
const tailwindConfig = (await import(url)).default as Config;
|
||||
|
||||
if (!Array.isArray(tailwindConfig.content)) {
|
||||
throw new Error(`Expected tailwind "content" option to be an array`);
|
||||
}
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
tailwindConfig.content = tailwindConfig.content.map((pattern: any) => {
|
||||
if (typeof pattern === "string") {
|
||||
const relative = path.relative(Deno.cwd(), path.dirname(configPath));
|
||||
|
||||
if (!relative.startsWith("..")) {
|
||||
return path.join(relative, pattern);
|
||||
}
|
||||
}
|
||||
return pattern;
|
||||
});
|
||||
|
||||
// PostCSS types cause deep recursion
|
||||
const plugins = [
|
||||
// deno-lint-ignore no-explicit-any
|
||||
tailwindCss(tailwindConfig) as any,
|
||||
// deno-lint-ignore no-explicit-any
|
||||
autoprefixer(options.autoprefixer) as any,
|
||||
];
|
||||
|
||||
if (config.mode === "production") {
|
||||
const { default: cssnano } = await import("cssnano");
|
||||
plugins.push(cssnano());
|
||||
}
|
||||
|
||||
const res = postcss(plugins);
|
||||
return res;
|
||||
}
|
||||
@@ -1,20 +1,26 @@
|
||||
import type { TailwindPluginOptions } from "./types.ts";
|
||||
import { initTailwind } from "./compiler.ts";
|
||||
import type { Builder } from "fresh/dev";
|
||||
import type { App } from "fresh";
|
||||
import twPostcss from "@tailwindcss/postcss";
|
||||
import postcss from "postcss";
|
||||
import type { TailwindPluginOptions } from "./types.ts";
|
||||
|
||||
// Re-export types for public API
|
||||
export type { TailwindPluginOptions } from "./types.ts";
|
||||
|
||||
export function tailwind<T>(
|
||||
builder: Builder,
|
||||
app: App<T>,
|
||||
options: TailwindPluginOptions = {},
|
||||
): void {
|
||||
let processor: ReturnType<typeof initTailwind> | null;
|
||||
const { exclude, ...tailwindOptions } = options;
|
||||
const instance = postcss(twPostcss({
|
||||
optimize: app.config.mode === "production",
|
||||
...tailwindOptions,
|
||||
}));
|
||||
|
||||
builder.onTransformStaticFile(
|
||||
{ pluginName: "tailwind", filter: /\.css$/, exclude: options.exclude },
|
||||
{ pluginName: "tailwind", filter: /\.css$/, exclude },
|
||||
async (args) => {
|
||||
if (!processor) processor = initTailwind(app.config, options);
|
||||
const instance = await processor;
|
||||
const res = await instance.process(args.text, {
|
||||
from: args.path,
|
||||
});
|
||||
|
||||
@@ -1,48 +1,22 @@
|
||||
import type { OnTransformOptions } from "@fresh/core/dev";
|
||||
|
||||
export interface AutoprefixerOptions {
|
||||
/** environment for `Browserslist` */
|
||||
env?: string;
|
||||
|
||||
/** should Autoprefixer use Visual Cascade, if CSS is uncompressed */
|
||||
cascade?: boolean;
|
||||
|
||||
/** should Autoprefixer add prefixes. */
|
||||
add?: boolean;
|
||||
|
||||
/** should Autoprefixer [remove outdated] prefixes */
|
||||
remove?: boolean;
|
||||
|
||||
/** should Autoprefixer add prefixes for @supports parameters. */
|
||||
supports?: boolean;
|
||||
|
||||
/** should Autoprefixer add prefixes for flexbox properties */
|
||||
flexbox?: boolean | "no-2009";
|
||||
|
||||
/** should Autoprefixer add IE 10-11 prefixes for Grid Layout properties */
|
||||
grid?: boolean | "autoplace" | "no-autoplace";
|
||||
|
||||
/** custom usage statistics for > 10% in my stats browsers query */
|
||||
stats?: {
|
||||
[browser: string]: {
|
||||
[version: string]: number;
|
||||
};
|
||||
};
|
||||
import type { OnTransformOptions } from "fresh/dev";
|
||||
|
||||
// based on the original code, this type is used to define options for the Tailwind plugin
|
||||
type PluginOptions = {
|
||||
/**
|
||||
* list of queries for target browsers.
|
||||
* Try to not use it.
|
||||
* The best practice is to use `.browserslistrc` config or `browserslist` key in `package.json`
|
||||
* to share target browsers with Babel, ESLint and Stylelint
|
||||
* Base CSS to be included. Set to null to exclude base styles.
|
||||
*/
|
||||
overrideBrowserslist?: string | string[];
|
||||
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"
|
||||
*/
|
||||
optimize?: boolean | {
|
||||
minify?: boolean;
|
||||
};
|
||||
};
|
||||
|
||||
/** do not raise error on unknown browser version in `Browserslist` config. */
|
||||
ignoreUnknownVersions?: boolean;
|
||||
}
|
||||
|
||||
export interface TailwindPluginOptions {
|
||||
autoprefixer?: AutoprefixerOptions;
|
||||
export interface TailwindPluginOptions extends PluginOptions {
|
||||
/** Exclude paths or globs that should not be processed */
|
||||
exclude?: OnTransformOptions["exclude"];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user