Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tailwindcss-mangle not working with tailwind-merge #44

Open
mrberenben opened this issue Aug 23, 2023 · 4 comments
Open

tailwindcss-mangle not working with tailwind-merge #44

mrberenben opened this issue Aug 23, 2023 · 4 comments

Comments

@mrberenben
Copy link

mrberenben commented Aug 23, 2023

i have an utility that overrides duplicate tailwind classes like "px-5 px-6" converts to "px-6".
but when i obfuscate classes, merge doesn't work and implements to dom tw-abc tw-abd means px-5 px-6.

here is my merging utility source code:

import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs));
}
@sonofmagic
Copy link
Owner

Yes, this is the case, because tailwindcss-mangle replaces class names, mainly at compile time, while tailwind-merge is a runtime tool.
When compiling, the tailwindcss class name has been replaced, and naturally it will not take effect when doing tailwind-merge

Let me think about how to solve this situation.

@mrberenben
Copy link
Author

hey ser! did you find a solution?

sonofmagic added a commit that referenced this issue Sep 15, 2023
sonofmagic added a commit that referenced this issue Sep 15, 2023
* chore: move ctx to core

* chore: add preserve fn

* chore: add preProcessRawCode method

* chore(demo): twMerge

* chore: commit vue case

* chore: fix merge options

* chore: commit preserveFunction fn demo

* feat: add preserveFunction option for tailwindcss-mangle not working with tailwind-merge #44
@sonofmagic
Copy link
Owner

Hello, in version 2.2.1, I experimentally set up a preserveFunction option to preserve all string literals in the middle of this function.
For example, in your case, you can configure it like:

// tailwindcss-mangle.config.ts
import { defineConfig } from 'tailwindcss-patch'

export default defineConfig({
  mangle: {
    preserveFunction: ['twMerge', 'cn']
  }
})

Then:

// all be preserved
const aaa = twMerge('px-2 py-1 bg-red-100 hover:bg-red-800', 'p-3 bg-[#B91C1C]')
// all be preserved
const bbb = cn(
  {
    'p-3': true
  },
  'p-1',
  ['p-2', true && 'p-4']
)

// but in this case, only `'text-[#654321]'` will be preserved
const a = 'm-1'
const b = 'bg-[#123456]'
const ccc = cn(a,b,'text-[#654321]')

Hope this will be helpful to you :)

@mrberenben
Copy link
Author

didn't work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants