Tailwind CSS Class Obfuscation - Overview ​
Introduction ​
This monorepo demonstrates different approaches to obfuscating Tailwind CSS classes across various project types and Tailwind versions. Class obfuscation (also known as "class mangling") transforms readable class names like bg-blue-500 into short, opaque names like tw-a, making it harder to reverse-engineer your CSS design system.
Why Obfuscate CSS Classes? ​
- Intellectual Property Protection: Hide your design system implementation details
- Reduced Bundle Size: Shorter class names mean smaller CSS and HTML files
- Discourage Copying: Makes it harder for competitors to copy your styling
Project Structure ​
Obfuscation Approaches by Project Type ​
| Project Type | Tailwind | Class Extraction | Obfuscation |
|---|---|---|---|
| HTML Static | v3 | tailwindcss-patch | unplugin-tailwindcss-mangle |
| HTML Static | v4 | Custom script | unplugin-tailwindcss-mangle |
| Next.js | v3 | tailwindcss-patch | unplugin-tailwindcss-mangle (webpack) |
| Next.js | v4 | Custom script | unplugin-tailwindcss-mangle (webpack) |
Key Differences: Tailwind v3 vs v4 ​
Tailwind v3 ​
- Configuration via
tailwind.config.js - Uses PostCSS plugin
tailwindcss-patchworks for class extraction- Import:
@tailwind base; @tailwind components; @tailwind utilities;
Tailwind v4 ​
- CSS-first configuration with
@themeand@import "tailwindcss" - No
tailwind.config.jsrequired tailwindcss-patchdoes NOT work (seetailwindcss_patch_v4_issues.md)- Requires custom extraction script
- New features: Container queries (
@container), improved arbitrary values
Quick Start ​
bash
# Install dependencies
pnpm install
# Run all apps in development mode
pnpm dev
# Build all apps (with obfuscation)
pnpm build
# Run tests
pnpm testDocumentation Files ​
overview.md- This filetailwind_v3_html_static.md- Guide for Tailwind v3 + HTML statictailwind_v4_html_static.md- Guide for Tailwind v4 + HTML statictailwind_v3_react_nextjs.md- Guide for Tailwind v3 + Next.jstailwind_v4_react_nextjs.md- Guide for Tailwind v4 + Next.jstailwindcss_patch_v4_issues.md- Why tailwindcss-patch doesn't work with v4compatibility_matrix.md- Full compatibility matrix
Important Rules ​
Static Classes Only ​
For obfuscation to work, classes must be written as complete static strings:
jsx
// BAD - Will NOT be obfuscated
<div className={`bg-${color}-500`}>
<div className={"bg-" + color}>
// GOOD - Will be obfuscated
<div className={color === 'red' ? 'bg-red-500' : 'bg-blue-500'}>
<div className="bg-red-500 bg-blue-500">Build Pipeline ​
- Extract Classes: Scan source files for Tailwind classes
- Generate JSON: Create a class list file
- Build: Run the normal build process
- Obfuscate: Replace class names with short versions
Contributing ​
Each app is independent and can be modified without affecting others. When adding new obfuscation techniques, please update the relevant documentation file.