Replies: 9 comments 30 replies
-
Hello @IRediTOTO 👋 If I understand your question correctly, the prefix option in the Tailwind config file is exactly what you're looking for! In your config file, define a string you want to prefix all Tailwind generated classes with: // tailwind.config.js
module.exports = {
theme: {
// ...
},
prefix: 'tw-',
} This will prefix all Tailwind classes with
Hope this helps! 👍 |
Beta Was this translation helpful? Give feedback.
-
If you define a But like you said, there are probably situations where this is not helpful (classes are already written in the HTML/JSX), and in that case you need to come up with a clever approach like your |
Beta Was this translation helpful? Give feedback.
-
When I wanted to add a prefix inav- to all of my classes in the markup, I used this node script. const fs = require('fs');
const path = require('path');
const glob = require("glob");
const rootDir = "path/to/your/repo";
const classNameRegex = /className=["']([\w\s-]+)["']/g;
glob("**/*.{js,jsx,tsx}", {cwd: rootDir}, (er, files) => {
files.forEach((file) => {
const filePath = path.join(rootDir, file);
fs.readFile(filePath, 'utf-8', (err, data) => {
if (err) {
console.error(err);
return;
}
const result = data.replace(classNameRegex, (match, p1) => {
const classNames = p1.split(" ");
const prefixedClassNames = classNames.map((className) => {
if (className.startsWith("inav-")) {
return className;
} else {
return `inav-${className}`;
}
});
return `className="${prefixedClassNames.join(" ")}"`;
});
fs.writeFile(filePath, result, 'utf-8', (err) => {
if (err) console.error(err);
});
});
});
}); This will allow you to change all of your already written classes to give them the tailwind prefix you want. |
Beta Was this translation helpful? Give feedback.
-
It would be nice to add this to the prettier plugin, or add it to a library that we can use to auto-add the prefixes |
Beta Was this translation helpful? Give feedback.
-
The I have a MFEs project which each MFEs has their own prefix. The shared library can't define a prefix that works for every MFEs. That's why a bundle-time plugin with Also, another thing is about tree-shaking, which can only take effect during bundling. I don't know if there is a way to make it work in Tailwind config:
|
Beta Was this translation helpful? Give feedback.
-
You can use this tool to covert your classes into prefixed ones. Covers all the cases of latest tailwind |
Beta Was this translation helpful? Give feedback.
-
it would be nice if can have a codemod for that |
Beta Was this translation helpful? Give feedback.
-
Some of the tools in this discussion or other just don't active anymore or not covering some cases, here i just made one that work for v3.4.4. https://tw-prefixer.vercel.app/ This works for negative classes, arbitrary classes, variants/modifiers, and !important classes in html or jsx. Still have some flaws cause tailwind is just that great and flexible in providing more arbitrary values / selectors, and modifiers. |
Beta Was this translation helpful? Give feedback.
-
I wrote a script to add prefix to all my classes of my React TypeScript project but actually it was useless because I used NextUi which does not support tailwind prefix. So, to not waste my work and my time, I prefer to share my script. I hope it will help you. The script is here if you need: I used the package "classnames" imported as "cn", so my RegExp takes it in account. |
Beta Was this translation helpful? Give feedback.
-
Hi, I love Tailwind 💯
Is there any tool can help us add prefix to class name Tw?
I just add a prefix tw- to use with a webpack plugin (it need to find classname by regex)
I tried to search in my html code and there are >800 class attr :D that mean i must add thousand of tw- to class.
Beta Was this translation helpful? Give feedback.
All reactions