A tool for obfuscating Tailwindcss classes #11179
Replies: 8 comments 39 replies
-
Not WorkingI am using Astro, which is on Vite It is not working, I have followed the steps you mentioned vite config
package.json
|
Beta Was this translation helpful? Give feedback.
-
@sonofmagic This is maybe not a good solution. I think the best solution is maybe like <!-- before -->
<div class="z-10 w-full max-w-5xl items-center justify-between font-mono text-sm lg:flex"></div>
<!-- after -->
<div class="tw-eb1ac3dxd"></div> This solution may need a lot of work. And actually I can't find a solution for converting tailwindcss to the hashed style class. |
Beta Was this translation helpful? Give feedback.
-
This kind of tools should be implemented by default on Tailwind in my opinion, maybe in v4? |
Beta Was this translation helpful? Give feedback.
-
This is a weak argument. You are already looking at the developer tools. The DOM tree and the Styles panel are next to each other. You want to hide the actual class names, but the resulting styles are still there. Not really sure why you want to hide either. Tailwind class names are pretty much mapped to individual styles. |
Beta Was this translation helpful? Give feedback.
-
I'm an extension creator and particularly the business here only works on the basis of obfuscation, a lot of cheap copying being done for nothing and making our business get screwed... Indeed, Tailwind should think that we can somehow collaborate with this to improve the code for everyone who wants this and everyone wins, without getting ripped off. That said, is there any more practical way to obfuscate Tailwind styles? 👍🏽 |
Beta Was this translation helpful? Give feedback.
-
Ihuuu, with ten minutes of research I found what I wanted https://github.com/sonofmagic/tailwindcss-mangle, 100% Ts project and also with Vite support to improve and efficient DEVELOPMENT... Now I can obfuscate a critical part of Tailwind in the project. Anyone who wants here again: https://github.com/sonofmagic/tailwindcss-mangle |
Beta Was this translation helpful? Give feedback.
-
And with a good number of collaboration 👆🏽 |
Beta Was this translation helpful? Give feedback.
-
Preface
tailwindcss
is an atomic class-first css framework that is very popular nowadays. Its semanticclass names
enable front-end developers to intuitively write and maintain styles for elements.This intuitiveness, however, can sometimes be a bit of a nuisance. Sometimes we don't want users or outside developers to be able to visually observe the style of all elements.
For example, we visit https://tailwindcss.com , then open the developer tools and inspect the elements. Instantly, the layout and style of all those elements on the page can be guessed without even looking at the
style
panel on the right.So, for the sake of making it impossible for others to visualize the style of an element in a production environment, we need to obfuscate the atomic classes generated by
tailwindcss
.Solution search
When I first looked for a solution on the web, I found
mangle-css-class-webpack-plugin
, awebpack
plugin designed to compress and obfuscate class names inhtml
,js
,css
.However, I had trouble trying to use it to obfuscate class names in
tailwindcss
, at first I couldn't pass inclassNameRegExp
and other parameters accurately, which led to mywebpack build
results being either completely wrong or not at all as expected, then I found aissue#10
that seemed to solve the problem. which seemed to solve the problem, but was extremely unmaintainable.In addition, directly through the regular to modify the
js
literal amount of the way, it is easy to cause a small range of misunderstanding, resulting in the whole project does not run up to report errors.So I started to think if there is any way to precisely obfuscate the class name of
tailwindcss
, and here is my solution.How to implement obfuscation
First of all, in order to be more precise with the matching, I decided to implement it from the following point of view
tailwindcss
, I need to get the context oftailwindcss
at runtime.prase5
to parse and transformhtml
,babel
to parse and transformjs
, andpostcss
to parse and transformcss
.To achieve the first point, I designed and wrote an npm package: tailwindcss-patch to get all the generated
class
collections.To achieve the second point, I wrote a
unplugin
plugin: unplugin-tailwindcss-mangle, which is awebpack/ vite
plugin that modifieshtml
,js
, andcss
to obfuscate all class names during packaging.How to use them
So how do you use them? It's as simple as the following steps.
1. Install these 2 packages
2. Execute the script
3. Add the
prepare
script to yourpackage.json
4. Register this plugin
Here is an example of
webpack
andvite
webpack
Here I use
nextjs
as an example, but of coursevue.config.js
is similar, registerutwm()
intowebpack
and then package the build and preview it to see the effect.vite
Then execute the script:
Effect Preview
Of course, you can also customize the generated class name, check documentation for more configuration options.
Errors and feedback
If you encounter any problems or questions during use, or want to communicate, feel free to raise issue with me.
Thanks.
Beta Was this translation helpful? Give feedback.
All reactions