closes https://github.com/denoland/fresh/issues/2319 The issue is because `imports.ts` wasn't changed before releasing. I thought I would go through and bump some other versions as well. This handles everything except the esbuild stuff and `tests/`.
79 lines
2.2 KiB
TypeScript
79 lines
2.2 KiB
TypeScript
import { ResolvedFreshConfig } from "../../server.ts";
|
|
import tailwindCss, { Config } from "tailwindcss";
|
|
import postcss from "npm:postcss@8.4.35";
|
|
import cssnano from "npm:cssnano@6.0.3";
|
|
import autoprefixer from "npm:autoprefixer@10.4.17";
|
|
import * as path from "https://deno.land/std@0.216.0/path/mod.ts";
|
|
import { 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(
|
|
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`);
|
|
}
|
|
|
|
tailwindConfig.content = tailwindConfig.content.map((pattern) => {
|
|
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.dev) {
|
|
plugins.push(cssnano());
|
|
}
|
|
|
|
return postcss(plugins);
|
|
}
|