From f105f19005b980658627f35c38595e37a545a294 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Tue, 21 Nov 2023 20:59:02 +0100 Subject: [PATCH] fix: tailwind don't magically add content regex (#2060) --- plugins/tailwind.ts | 59 ++++++++----------- tests/fixture_tailwind/tailwind.config.ts | 7 +++ .../fixture_tailwind_build/tailwind.config.ts | 7 +++ 3 files changed, 40 insertions(+), 33 deletions(-) create mode 100644 tests/fixture_tailwind/tailwind.config.ts create mode 100644 tests/fixture_tailwind_build/tailwind.config.ts diff --git a/plugins/tailwind.ts b/plugins/tailwind.ts index bddc7cca..b800c221 100644 --- a/plugins/tailwind.ts +++ b/plugins/tailwind.ts @@ -9,17 +9,8 @@ async function initTailwind( config: ResolvedFreshConfig, ): Promise { const root = path.dirname(config.staticDir); - let relativeRoot = ""; - if (root !== Deno.cwd()) { - relativeRoot = path.relative(Deno.cwd(), root).split(path.sep).join("/") + - "/"; - } - let tailwindConfig: Config = { - content: [ - `${relativeRoot}{routes,components,islands}/**/*.{ts,tsx,js,jsx,mjs,cjs}`, - ], - }; + let tailwindConfig: Config | null = null; const configFilePaths = [ path.join(Deno.cwd(), "tailwind.config.ts"), @@ -38,31 +29,33 @@ async function initTailwind( // ignore } - if (importedConfig === null) continue; - importedConfig.content = importedConfig.content ?? []; - - if (!Array.isArray(importedConfig.content)) { - throw new Error(`Expected tailwind "content" option to be an array`); - } - - const configContent = importedConfig.content.map((pattern) => { - if (typeof pattern === "string") { - return path.join( - path.relative(Deno.cwd(), path.dirname(configFile)), - pattern, - ); + if (importedConfig !== null) { + if (!Array.isArray(importedConfig.content)) { + throw new Error(`Expected tailwind "content" option to be an array`); } - return pattern; - }); - tailwindConfig = { - ...importedConfig, - content: [ - ...configContent, - ...tailwindConfig.content as string[], - ], - }; - break; + 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; + } + } + + 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[] = [ diff --git a/tests/fixture_tailwind/tailwind.config.ts b/tests/fixture_tailwind/tailwind.config.ts new file mode 100644 index 00000000..63acf078 --- /dev/null +++ b/tests/fixture_tailwind/tailwind.config.ts @@ -0,0 +1,7 @@ +import { Config } from "tailwindcss"; + +export default { + content: [ + "{routes,islands,components}/**/*.{ts,tsx}", + ], +} as Config; diff --git a/tests/fixture_tailwind_build/tailwind.config.ts b/tests/fixture_tailwind_build/tailwind.config.ts new file mode 100644 index 00000000..63acf078 --- /dev/null +++ b/tests/fixture_tailwind_build/tailwind.config.ts @@ -0,0 +1,7 @@ +import { Config } from "tailwindcss"; + +export default { + content: [ + "{routes,islands,components}/**/*.{ts,tsx}", + ], +} as Config;