|
1 | | -import tailwindCss, { type Config } from "tailwindcss"; |
| 1 | +import tailwindPoscss from "@tailwindcss/postcss"; |
2 | 2 | import postcss from "postcss"; |
3 | | -import autoprefixer from "autoprefixer"; |
4 | | -import * as path from "@std/path"; |
5 | | -import type { TailwindPluginOptions } from "./types.ts"; |
6 | 3 | import type { ResolvedFreshConfig } from "fresh"; |
| 4 | +import type { Processor } from "postcss"; |
7 | 5 |
|
8 | | -const CONFIG_EXTENSIONS = ["ts", "js", "mjs"]; |
9 | | - |
10 | | -async function findTailwindConfigFile(directory: string): Promise<string> { |
11 | | - let dir = directory; |
12 | | - while (true) { |
13 | | - for (let i = 0; i < CONFIG_EXTENSIONS.length; i++) { |
14 | | - const ext = CONFIG_EXTENSIONS[i]; |
15 | | - const filePath = path.join(dir, `tailwind.config.${ext}`); |
16 | | - try { |
17 | | - const stat = await Deno.stat(filePath); |
18 | | - if (stat.isFile) { |
19 | | - return filePath; |
20 | | - } |
21 | | - } catch (err) { |
22 | | - if (!(err instanceof Deno.errors.NotFound)) { |
23 | | - throw err; |
24 | | - } |
25 | | - } |
26 | | - } |
27 | | - |
28 | | - const parent = path.dirname(dir); |
29 | | - if (parent === dir) { |
30 | | - throw new Error( |
31 | | - `Could not find a tailwind config file in the current directory or any parent directory.`, |
32 | | - ); |
33 | | - } |
34 | | - |
35 | | - dir = parent; |
36 | | - } |
37 | | -} |
38 | | - |
39 | | -export async function initTailwind( |
| 6 | +export function initTailwind( |
40 | 7 | config: ResolvedFreshConfig, |
41 | | - options: TailwindPluginOptions, |
42 | | -): Promise<postcss.Processor> { |
43 | | - const root = path.dirname(config.staticDir); |
44 | | - |
45 | | - const configPath = await findTailwindConfigFile(root); |
46 | | - const url = path.toFileUrl(configPath).href; |
47 | | - const tailwindConfig = (await import(url)).default as Config; |
48 | | - |
49 | | - if (!Array.isArray(tailwindConfig.content)) { |
50 | | - throw new Error(`Expected tailwind "content" option to be an array`); |
51 | | - } |
52 | | - |
53 | | - // deno-lint-ignore no-explicit-any |
54 | | - tailwindConfig.content = tailwindConfig.content.map((pattern: any) => { |
55 | | - if (typeof pattern === "string") { |
56 | | - const relative = path.relative(Deno.cwd(), path.dirname(configPath)); |
57 | | - |
58 | | - if (!relative.startsWith("..")) { |
59 | | - return path.join(relative, pattern); |
60 | | - } |
61 | | - } |
62 | | - return pattern; |
63 | | - }); |
64 | | - |
65 | | - // PostCSS types cause deep recursion |
66 | | - const plugins = [ |
67 | | - // deno-lint-ignore no-explicit-any |
68 | | - tailwindCss(tailwindConfig) as any, |
69 | | - // deno-lint-ignore no-explicit-any |
70 | | - autoprefixer(options.autoprefixer) as any, |
71 | | - ]; |
72 | | - |
73 | | - if (config.mode === "production") { |
74 | | - const { default: cssnano } = await import("cssnano"); |
75 | | - plugins.push(cssnano()); |
76 | | - } |
| 8 | +): Processor { |
| 9 | + const res = postcss(tailwindPoscss({ |
| 10 | + optimize: config.mode === "production", |
| 11 | + })); |
77 | 12 |
|
78 | | - const res = postcss(plugins); |
79 | 13 | return res; |
80 | 14 | } |
0 commit comments