tailwindcss-mangle icon indicating copy to clipboard operation
tailwindcss-mangle copied to clipboard

tailwindcss-mangle not working with tailwind-merge

Open mrberenben opened this issue 2 years ago • 4 comments

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));
}

mrberenben avatar Aug 23 '23 11:08 mrberenben

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.

sonofmagic avatar Aug 25 '23 13:08 sonofmagic

hey ser! did you find a solution?

mrberenben avatar Sep 13 '23 11:09 mrberenben

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 :)

sonofmagic avatar Sep 15 '23 11:09 sonofmagic

didn't work

mrberenben avatar Dec 13 '23 13:12 mrberenben