@@ -5,13 +5,14 @@ const selectorParser = require('postcss-selector-parser');
5
5
const nameGenerators = require ( './name-generators' ) ;
6
6
7
7
class MinifyClassnames {
8
- constructor ( { genNameClass, genNameId, filter} = { } ) {
8
+ constructor ( { genNameClass, genNameId, filter, customAttributes = [ ] } = { } ) {
9
9
this . filter = filter || / ^ .j s - / ;
10
10
// TODO: Pass a seed for emojinamestring, make this better
11
11
this . genNameClass = this . getNameGenerator ( genNameClass , 7 ) ;
12
12
this . genNameId = this . getNameGenerator ( genNameId , 5 ) ;
13
13
this . classMap = { } ;
14
14
this . idMap = { } ;
15
+ this . customAttributes = customAttributes ;
15
16
}
16
17
17
18
getNameGenerator ( value , seed ) {
@@ -60,6 +61,28 @@ class MinifyClassnames {
60
61
61
62
minifyElements ( tree ) {
62
63
return tree . walk ( node => {
64
+ if ( node . attrs && this . customAttributes . length > 0 ) {
65
+ const attributes = Object . keys ( node . attrs ) ;
66
+
67
+ this . customAttributes
68
+ . filter ( attribute => attributes . includes ( attribute ) )
69
+ . forEach ( attributes => {
70
+ node . attrs [ attributes ] = node . attrs [ attributes ]
71
+ . split ( / \s + / )
72
+ . map ( value => {
73
+ // NOTE: This removes classes that don't match any in our CSS
74
+ if ( this . isClassFiltered ( value ) ) {
75
+ return value ;
76
+ }
77
+
78
+ return this . classMap [ value ] || '' ;
79
+ } )
80
+ . filter ( Boolean )
81
+ . join ( ' ' )
82
+ . trim ( ) ;
83
+ } ) ;
84
+ }
85
+
63
86
if ( node . attrs && node . attrs . class ) {
64
87
let classes = node . attrs . class ;
65
88
node . attrs . class = classes
0 commit comments