feat: add separate tailwindcss v3 plugin (#3135)
Makes it easier to keep using tailwindcss v3 with newer Fresh versions.
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
"./examples",
|
||||
"./init",
|
||||
"./plugin-tailwindcss",
|
||||
"./plugin-tailwindcss-v3",
|
||||
"./update",
|
||||
"./www"
|
||||
],
|
||||
|
||||
@@ -26,11 +26,10 @@ that.
|
||||
|
||||
```diff 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);
|
||||
+ tailwind(builder);
|
||||
```
|
||||
|
||||
4. Add `@import "tailwindcss";` at the top of your main stylesheet.
|
||||
@@ -52,3 +51,16 @@ tailwind(builder, app, {
|
||||
base: null,
|
||||
});
|
||||
```
|
||||
|
||||
## Tailwindcss v3
|
||||
|
||||
If can't update to the current version of tailwindcss we have a dedicated
|
||||
`@fresh/plugin-tailwindcss-v3` plugin that uses tailwindcss v3. That way you can
|
||||
decided on your own when it's best to update to v4.
|
||||
|
||||
```ts dev.ts
|
||||
import { Builder } from "fresh/dev";
|
||||
import { tailwind } from "@fresh/plugin-tailwindcss-v3";
|
||||
|
||||
tailwind(builder, {});
|
||||
```
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
# Tailwind CSS v3 plugin for Fresh
|
||||
|
||||
A Tailwind CSS v3 plugin to use in Fresh.
|
||||
|
||||
> [info]: If you want to use latest tailwindcss use `@fresh/plugin-tailwindcss`
|
||||
> instead.
|
||||
|
||||
## Basic Usage
|
||||
|
||||
```ts
|
||||
// dev.ts
|
||||
import { Builder } from "fresh/dev";
|
||||
import { tailwind } from "@fresh/plugin-tailwind-v3";
|
||||
|
||||
const builder = new Builder();
|
||||
tailwind(builder);
|
||||
|
||||
if (Deno.args.includes("build")) {
|
||||
builder.build();
|
||||
} else {
|
||||
builder.listen(() => import("./main.ts"));
|
||||
}
|
||||
```
|
||||
|
||||
## Option Configuration
|
||||
|
||||
```ts
|
||||
tailwind(builder, {
|
||||
// Exclude certain files from processing
|
||||
exclude: ["/admin/**", "*.temp.css"],
|
||||
|
||||
// Force optimization (defaults to production mode)
|
||||
optimize: true,
|
||||
|
||||
// Exclude base styles
|
||||
base: null,
|
||||
});
|
||||
```
|
||||
|
||||
To learn more about Fresh go to
|
||||
[https://fresh.deno.dev/](https://fresh.deno.dev/).
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "@fresh/plugin-tailwind-v3",
|
||||
"version": "1.0.0",
|
||||
"license": "MIT",
|
||||
"exports": "./src/mod.ts",
|
||||
"imports": {
|
||||
"autoprefixer": "npm:autoprefixer@^10.4.21",
|
||||
"cssnano": "npm:cssnano@^6.1.2",
|
||||
"postcss": "npm:postcss@^8.5.6",
|
||||
"tailwindcss": "npm:tailwindcss@^3.4.17"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
import type { Builder, ResolvedBuildConfig } from "fresh/dev";
|
||||
import tailwindCss, { type Config } from "tailwindcss";
|
||||
import postcss from "postcss";
|
||||
import cssnano from "cssnano";
|
||||
import autoprefixer from "autoprefixer";
|
||||
import * as path from "@std/path";
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
export function tailwind(
|
||||
builder: Builder,
|
||||
options: TailwindPluginOptions = {},
|
||||
): void {
|
||||
const processor = initTailwind(builder.config, options);
|
||||
|
||||
builder.onTransformStaticFile(
|
||||
{ pluginName: "tailwind", filter: /\.css$/ },
|
||||
async (args) => {
|
||||
const instance = await processor;
|
||||
const res = await instance.process(args.text, {
|
||||
from: args.path,
|
||||
});
|
||||
return {
|
||||
content: res.content,
|
||||
map: res.map?.toString(),
|
||||
};
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
async function initTailwind(
|
||||
config: ResolvedBuildConfig,
|
||||
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") {
|
||||
plugins.push(cssnano());
|
||||
}
|
||||
|
||||
return postcss(plugins);
|
||||
}
|
||||
Reference in New Issue
Block a user