diff --git a/packages/transformer-attributify-jsx/src/index.ts b/packages/transformer-attributify-jsx/src/index.ts index 6bcb415c6e..60264b3359 100644 --- a/packages/transformer-attributify-jsx/src/index.ts +++ b/packages/transformer-attributify-jsx/src/index.ts @@ -39,6 +39,7 @@ export interface TransformerAttributifyJsxOptions { const elementRE = /|<(\/?)([a-zA-Z][-.:0-9_a-zA-Z]*)((?:\s+[^>]*?(?:(?:'[^']*')|(?:"[^"]*"))?)*)\s*(\/?)>/gs const attributeRE = /([a-zA-Z()#][\[?a-zA-Z0-9-_:()#%\]?]*)(?:\s*=\s*((?:'[^']*')|(?:"[^"]*")|\S+))?/g +const classFilterRE = /(className|class)\s*=\s*\{[^\}]*\}/i export default function transformerAttributifyJsx(options: TransformerAttributifyJsxOptions = {}): SourceCodeTransformer { const { @@ -72,7 +73,13 @@ export default function transformerAttributifyJsx(options: TransformerAttributif const tasks: Promise[] = [] for (const item of Array.from(code.original.matchAll(elementRE))) { - for (const attr of item[3].matchAll(attributeRE)) { + // Get the length of the className part, and replace it with the equal length of empty string + const classNamePart = item[3].match(classFilterRE) + let attributifyPart = item[3] + if (classNamePart) + attributifyPart = item[3].replace(classFilterRE, ' '.repeat(classNamePart[0].length)) + + for (const attr of attributifyPart.matchAll(attributeRE)) { const matchedRule = attr[0].replace(/\:/i, '-') if (matchedRule.includes('=') || isBlocked(matchedRule)) continue diff --git a/test/transformer-attributify-jsx.test.ts b/test/transformer-attributify-jsx.test.ts index a831a095aa..578093a433 100644 --- a/test/transformer-attributify-jsx.test.ts +++ b/test/transformer-attributify-jsx.test.ts @@ -7,12 +7,12 @@ import transformerAttributifyJsx from '../packages/transformer-attributify-jsx/s describe('transformerAttributifyJs', () => { const originalCode = ` -
+
unocss
-
+
The instant on-demand Atomic CSS engine.
@@ -42,12 +42,12 @@ describe('transformerAttributifyJs', () => { await transformerAttributifyJsx().transform(code, 'app.tsx', { uno, tokens: new Set() } as any) expect(code.toString()).toMatchInlineSnapshot(` - "
+ "
unocss
-
+
The instant on-demand Atomic CSS engine.
@@ -75,12 +75,12 @@ describe('transformerAttributifyJs', () => { }).transform(code, 'app.jsx', { uno, tokens: new Set() } as any) expect(code.toString()).toMatchInlineSnapshot(` - "
+ "
unocss
-
+
The instant on-demand Atomic CSS engine.