docs: port over to TailwindCSS (#2058)

This commit is contained in:
Marvin Hagemeister
2023-11-22 11:12:21 +01:00
committed by GitHub
parent f105f19005
commit bb035ae8da
26 changed files with 145 additions and 145 deletions
+49 -44
View File
@@ -5,60 +5,65 @@ import cssnano from "npm:cssnano@6.0.1";
import * as path from "https://deno.land/std@0.207.0/path/mod.ts";
import { walk } from "https://deno.land/std@0.207.0/fs/walk.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;
}
}
async function initTailwind(
config: ResolvedFreshConfig,
): Promise<postcss.Processor> {
const root = path.dirname(config.staticDir);
let tailwindConfig: Config | null = null;
const configPath = await findTailwindConfigFile(root);
console.log({ configPath, cwd: Deno.cwd() });
const url = path.toFileUrl(configPath).href;
const tailwindConfig = (await import(url)).default as Config;
const configFilePaths = [
path.join(Deno.cwd(), "tailwind.config.ts"),
path.join(Deno.cwd(), "tailwind.config.js"),
path.join(Deno.cwd(), "tailwind.config.mjs"),
path.join(root, "tailwind.config.ts"),
path.join(root, "tailwind.config.js"),
path.join(root, "tailwind.config.mjs"),
];
for (const configFile of configFilePaths) {
let importedConfig: Config | null = null;
try {
const url = path.toFileUrl(configFile).href;
importedConfig = (await import(url)).default as Config;
} catch (_err) {
// ignore
}
if (!Array.isArray(tailwindConfig.content)) {
throw new Error(`Expected tailwind "content" option to be an array`);
}
if (importedConfig !== null) {
if (!Array.isArray(importedConfig.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);
}
importedConfig.content = importedConfig.content.map((pattern) => {
if (typeof pattern === "string") {
return path.join(
path.relative(Deno.cwd(), path.dirname(configFile)),
pattern,
);
}
return pattern;
});
tailwindConfig = importedConfig;
break;
}
}
return pattern;
});
if (tailwindConfig === null) {
throw new Error(
`Could not find tailwind config file. Searched the following locations:\n\n${
configFilePaths.map((p) => `- ${p}`).join("\n")
}`,
);
}
const plugins: postcss.AcceptedPlugin[] = [
// PostCSS types cause deep recursion
// deno-lint-ignore no-explicit-any
const plugins: any[] = [
tailwindCss(tailwindConfig),
];