You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've made the following prettier config that automatically parses all tailwind entrypoints. Would LOVE to see this as a first-class feature now that it's easier to use multiple configs!
import{readdirSync,readFileSync}from"fs";importpathfrom"path";import{fileURLToPath}from"url";// Get the project root directoryconst__filename=fileURLToPath(import.meta.url);const__dirname=path.dirname(__filename);constroot=path.resolve(__dirname);// Read .prettierignoreconstignoredPaths=readFileSync(path.join(root,".prettierignore"),"utf-8").split("\n").filter((line)=>line.trim()&&!line.startsWith("#")).map((path)=>path.replace(/^\//,""));/** @type {import("prettier").Config} */constconfig={experimentalTernaries: true,plugins: ["prettier-plugin-embed","prettier-plugin-organize-imports","prettier-plugin-tailwindcss",],tailwindFunctions: ["twMerge","twJoin","cva"],overrides: getTailwindOverrides(root),};// Find CSS files recursively and configure prettier-plugin-tailwindcss to use// the closest CSS file as the Tailwind stylesheet for files in that directoryfunctiongetTailwindOverrides(startDir){returnreaddirSync(startDir,{withFileTypes: true}).flatMap((entry)=>{constfullPath=path.join(startDir,entry.name);constrelativePath=path.relative(root,fullPath);// Skip ignored pathsif(ignoredPaths.some((p)=>relativePath.startsWith(p)))return[];// Recursively look into each directoryif(entry.isDirectory())returngetTailwindOverrides(fullPath);// Only process CSS files in this directoryif(!entry.isFile()||!entry.name.endsWith(".css"))return[];// Make sure file is tailwind stylesheetconstcssContent=readFileSync(fullPath,"utf-8");constisTailwind=/^@import\s+["']tailwindcss["']/m.test(cssContent);if(!isTailwind)return[];// Escape glob-like patterns in path so prettier doesn't get confusedconstescapedPath=relativePath.replace(/([()])/g,"\\$1");return[{files: path.join(root,"**","*"),options: {tailwindStylesheet: `./${escapedPath}`},},];});}exportdefaultconfig;
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I've made the following prettier config that automatically parses all tailwind entrypoints. Would LOVE to see this as a first-class feature now that it's easier to use multiple configs!
Beta Was this translation helpful? Give feedback.
All reactions