1- import type { SourceCodeTransformer } from '@unocss/core'
1+ import type { SourceCodeTransformer , UnoGenerator } from '@unocss/core'
2+ import type MagicString from 'magic-string'
23import { getEnvFlags } from '#integration/env'
3- import { parse } from '@babel/parser'
4- import _traverse from '@babel/traverse'
54import { toArray } from '@unocss/core'
6-
7- // @ts -expect-error ignore
8- const traverse = ( _traverse . default || _traverse ) as typeof _traverse
5+ import { attributifyJsxBabelResolver } from './resolver/babel'
6+ import { attributifyJsxRegexResolver } from './resolver/regex'
97
108export type FilterPattern = Array < string | RegExp > | string | RegExp | null
119
@@ -43,6 +41,13 @@ export interface TransformerAttributifyJsxOptions {
4341 exclude ?: FilterPattern
4442}
4543
44+ export interface AttributifyResolverParams {
45+ code : MagicString
46+ id : string
47+ uno : UnoGenerator < object >
48+ isBlocked : ( matchedRule : string ) => boolean
49+ }
50+
4651export default function transformerAttributifyJsx ( options : TransformerAttributifyJsxOptions = { } ) : SourceCodeTransformer {
4752 const {
4853 blocklist = [ ] ,
@@ -71,7 +76,7 @@ export default function transformerAttributifyJsx(options: TransformerAttributif
7176 name : '@unocss/transformer-attributify-jsx' ,
7277 enforce : 'pre' ,
7378 idFilter,
74- async transform ( code , _ , { uno } ) {
79+ async transform ( code , id , { uno } ) {
7580 // Skip if running in VSCode extension context
7681 try {
7782 if ( getEnvFlags ( ) . isVSCode )
@@ -80,34 +85,24 @@ export default function transformerAttributifyJsx(options: TransformerAttributif
8085 catch {
8186 // Ignore import error in browser environment
8287 }
83- const tasks : Promise < void > [ ] = [ ]
84- const ast = parse ( code . toString ( ) , {
85- sourceType : 'module' ,
86- plugins : [ 'jsx' , 'typescript' ] ,
87- } )
88-
89- traverse ( ast , {
90- JSXAttribute ( path ) {
91- if ( path . node . value === null ) {
92- const attr = path . node . name . type === 'JSXNamespacedName'
93- ? `${ path . node . name . namespace . name } :${ path . node . name . name . name } `
94- : path . node . name . name
95-
96- if ( isBlocked ( attr ) )
97- return
9888
99- tasks . push (
100- uno . parseToken ( attr ) . then ( ( matched ) => {
101- if ( matched ) {
102- code . appendRight ( path . node . end ! , '=""' )
103- }
104- } ) ,
105- )
106- }
107- } ,
108- } )
89+ const params : AttributifyResolverParams = {
90+ code,
91+ id,
92+ uno,
93+ isBlocked,
94+ }
10995
110- await Promise . all ( tasks )
96+ try {
97+ await attributifyJsxBabelResolver ( params )
98+ }
99+ catch ( error ) {
100+ console . warn (
101+ `[@unocss/transformer-attributify-jsx]: Babel resolver failed for "${ id } ", falling back to regex resolver:` ,
102+ error ,
103+ )
104+ await attributifyJsxRegexResolver ( params )
105+ }
111106 } ,
112107 }
113108}
0 commit comments