From 6eccdcf1a13a6f77b19261d4335951c69f0b9074 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Sat, 2 Dec 2023 10:34:12 +0100 Subject: [PATCH] fix: ensure tailwind is loaded lazily (#2124) --- plugins/tailwind.ts | 75 +++--------------------------------- plugins/tailwind/compiler.ts | 73 +++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 69 deletions(-) create mode 100644 plugins/tailwind/compiler.ts diff --git a/plugins/tailwind.ts b/plugins/tailwind.ts index 86fa9f68..7359bd25 100644 --- a/plugins/tailwind.ts +++ b/plugins/tailwind.ts @@ -1,76 +1,12 @@ import { Plugin, PluginMiddleware, ResolvedFreshConfig } from "../server.ts"; -import tailwindCss, { Config } from "tailwindcss"; -import postcss from "npm:postcss@8.4.31"; -import cssnano from "npm:cssnano@6.0.1"; +import type postcss from "npm:postcss@8.4.31"; 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 { - 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 { - 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 - // deno-lint-ignore no-explicit-any - const plugins: any[] = [ - tailwindCss(tailwindConfig), - ]; - - if (!config.dev) { - plugins.push(cssnano()); - } - - return postcss(plugins); +async function initTailwind(config: ResolvedFreshConfig) { + return await (await import("./tailwind/compiler.ts")).initTailwind( + config, + ); } export default function tailwind(): Plugin { @@ -150,6 +86,7 @@ export default function tailwind(): Plugin { async buildStart(config) { staticDir = config.staticDir; const outDir = path.join(config.build.outDir, "static"); + processor = await initTailwind(config); const files = walk(config.staticDir, { diff --git a/plugins/tailwind/compiler.ts b/plugins/tailwind/compiler.ts new file mode 100644 index 00000000..a7f7b6b8 --- /dev/null +++ b/plugins/tailwind/compiler.ts @@ -0,0 +1,73 @@ +import { ResolvedFreshConfig } from "../../server.ts"; +import tailwindCss, { Config } from "tailwindcss"; +import postcss from "npm:postcss@8.4.31"; +import cssnano from "npm:cssnano@6.0.1"; +import * as path from "https://deno.land/std@0.207.0/path/mod.ts"; + +const CONFIG_EXTENSIONS = ["ts", "js", "mjs"]; + +async function findTailwindConfigFile(directory: string): Promise { + 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, +): Promise { + 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 + // deno-lint-ignore no-explicit-any + const plugins: any[] = [ + tailwindCss(tailwindConfig), + ]; + + if (!config.dev) { + plugins.push(cssnano()); + } + + return postcss(plugins); +}