1
1
import * as wxml from '@vivaxy/wxml'
2
+ import micromatch from 'micromatch'
2
3
import { handleCharacters } from './utilities'
3
4
import { FileType } from './enum'
4
5
import { replaceStringLiteralPlugin } from './babel'
5
6
import * as babel from '@babel/core'
7
+ import type { Options } from './interfaces'
6
8
7
9
const matchScriptsInsideClassNames = / ( { { ) ( .+ ?) ( } } ) / g
8
10
const replaceMarker = '__MP_TW_PLUGIN_REPLACE__'
9
11
10
- export function handleTemplate ( rawSource : string ) {
11
-
12
+ export function handleTemplate ( rawSource : string , options ?: Options ) {
12
13
const parsed = wxml . parse ( rawSource )
13
14
14
15
wxml . traverse ( parsed , node => {
@@ -23,6 +24,28 @@ export function handleTemplate(rawSource: string) {
23
24
node . attributes . virtualHostClass = node . attributes . class
24
25
}
25
26
27
+ if ( options ?. utilitiesSettings ?. customAttributes ) {
28
+
29
+ for ( const [ match , attrs ] of Object . entries ( options . utilitiesSettings . customAttributes ) ) {
30
+
31
+ if ( / ^ [ \w - ] + $ / . test ( match ) ? match === node . tagName : micromatch . isMatch ( node . tagName , match ) ) {
32
+ const _attrs = Array . isArray ( attrs ) ? attrs : [ attrs ]
33
+
34
+ for ( const attrKey of _attrs ) {
35
+
36
+ // skip class because it has already been converted
37
+ if ( attrKey === 'class' ) continue
38
+
39
+ if ( node . attributes [ attrKey ] ) {
40
+ node . attributes [ attrKey ] = handleClassNameInTemplate ( node . attributes [ attrKey ] )
41
+ }
42
+
43
+ }
44
+ }
45
+
46
+ }
47
+
48
+ }
26
49
}
27
50
28
51
} )
0 commit comments