Inspired by #2819, #2829, and https://github.com/pakornv/fresh-plugin-tailwindcss, I made minimal modifications to the plugin-tailwind, I originally intended to update based on #2829, but I was informed by the system that I couldn't push. Due to the breaking changes in Tailwind v4, some PostCSS plugins are no longer required. Additionally, I updated the syntax in the /www directory. The /www directory now uses the updated plugin supported by Tailwind v4.I think this has made initial progress. # What This PR Solves The Tailwind plugin has been updated to support Tailwind v4, and it is currently functioning well both in the repository's www directory and in the initialized project which for test. ## --patch-1 ### 1. Dependency Optimization - Removed `cssnano` dependency, using Tailwind v4's built-in optimization(Based on @csvn 's suggestion) - Removed `autoprefixer` dependency - Updated `tailwind-plugin` version number ### 2. Build Configuration Adjustments - Added `--node-modules-dir` parameter to `/www` task for Tailwind v4 support (Based on @marvinhagemeister 's suggestion) - Added `--allow-scripts` parameter to enable `@tailwindcss/oxide` execution ### 3. Code Adaptation - Modified Tailwind-related code in `/www` to support Tailwind v4 - Updated dependency configuration in `deno.json` ### 4. Feature Restoration - Added `imagescript` and `astral` dependencies to restore screenshot functionality lost in previous PR updates - Fixed the issue where the `tailwind-plugin` dependency was incorrectly pointing to the JSR package instead of the local repository source code in previous PR. ## --patch-2 - Upgraded Tailwind CSS to the latest version. - Removed unnecessary dependencies. - Enabled CSS minification by default when Fresh is in production mode. - Updated docs/canary/examples/migration-guide.md to support the new Tailwind plugin format. - Updated init/src/init.ts to support the new Tailwind version and plugin version, use new `@import "tailwindcss"`, and generate the new plugin format. - Updated init/src/init_test.ts: tests now expect `css` to match `/tailwindcss/` instead of `/@tailwind/`. - Removed plugin-tailwindcss/src/types.ts as it is no longer needed, and updated plugin-tailwindcss/src/mod.ts and www/dev.ts to support the new format. Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com> ## --patch-3 Addressing @marvinhagemeister feedback, this patch reverts the unintended loss of PR #2960's changes caused by merge conflicts. ## --patch-4 I initialized a project according to the migration documentation and used @fresh/update to test whether the plugin works properly with v2-alpha-34. The results show that it works fine. - The plugin is published at jsr:@freshpress/fresh-plugin-tailwind@0.0.1-alpha.8. - The test project is published at https://github.com/SisyphusZheng/fresh-project-test/tree/main/fresh-project. # How Testing Was Conducted ## Local Environment Testing 1. Environment Cleanup ```bash rm -rf ./.cache/deno deno.lock node_modules ``` 2. Test Execution ```bash deno task www ``` 3. Result Verification - Checked functionality in local `/www` environment - Confirmed all features working properly ## Code Inspection - Passed `deno task ok` check ## This forked /www website is hosted and running on GitHub Codespaces, with support for the latest code changes. You can check it out at: https://silver-trout-pjr4jw7xx5x9299x6-8000.app.github.dev/ --------- Co-authored-by: 李嘉图·M·路 <146103794+Ricardo-M-Zheng@users.noreply.github.com> Co-authored-by: deno-deploy[bot] <75045203+deno-deploy[bot]@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·路
deno-deploy[bot]
Asher Gomez
Marvin Hagemeister
parent
d5a2afd7ac
commit
40e7747228
@@ -1,80 +1,18 @@
|
||||
import tailwindCss, { type Config } from "tailwindcss";
|
||||
import twPostcss from "@tailwindcss/postcss";
|
||||
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";
|
||||
import type { TailwindPluginOptions } from "./types.ts";
|
||||
|
||||
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(
|
||||
export 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
|
||||
options: TailwindPluginOptions = {},
|
||||
): postcss.Processor {
|
||||
const plugins = [
|
||||
// deno-lint-ignore no-explicit-any
|
||||
tailwindCss(tailwindConfig) as any,
|
||||
// deno-lint-ignore no-explicit-any
|
||||
autoprefixer(options.autoprefixer) as any,
|
||||
twPostcss({
|
||||
optimize: config.mode === "production",
|
||||
...options,
|
||||
}),
|
||||
];
|
||||
|
||||
if (config.mode === "production") {
|
||||
const { default: cssnano } = await import("cssnano");
|
||||
plugins.push(cssnano());
|
||||
}
|
||||
|
||||
const res = postcss(plugins);
|
||||
return res;
|
||||
return postcss(plugins);
|
||||
}
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
import type { TailwindPluginOptions } from "./types.ts";
|
||||
import { initTailwind } from "./compiler.ts";
|
||||
import type { FreshBuilder } from "fresh/dev";
|
||||
import type { App } from "fresh";
|
||||
import type { TailwindPluginOptions } from "./types.ts";
|
||||
|
||||
export function tailwind<T>(
|
||||
builder: FreshBuilder,
|
||||
app: App<T>,
|
||||
options: TailwindPluginOptions = {},
|
||||
): void {
|
||||
let processor: ReturnType<typeof initTailwind> | null;
|
||||
let processor: ReturnType<typeof initTailwind> | null = null;
|
||||
|
||||
builder.onTransformStaticFile(
|
||||
{ pluginName: "tailwind", filter: /\.css$/, exclude: options.exclude },
|
||||
async (args) => {
|
||||
if (!processor) processor = initTailwind(app.config, options);
|
||||
if (!processor) processor = initTailwind(app.config);
|
||||
const instance = await processor;
|
||||
const res = await instance.process(args.text, {
|
||||
from: args.path,
|
||||
|
||||
@@ -1,48 +1,6 @@
|
||||
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;
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
overrideBrowserslist?: string | string[];
|
||||
|
||||
/** do not raise error on unknown browser version in `Browserslist` config. */
|
||||
ignoreUnknownVersions?: boolean;
|
||||
}
|
||||
|
||||
export interface TailwindPluginOptions {
|
||||
autoprefixer?: AutoprefixerOptions;
|
||||
/** Exclude paths or globs that should not be processed */
|
||||
exclude?: OnTransformOptions["exclude"];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user