Skip to content

Commit 3e71922

Browse files
committed
feat: customAttributes, close #36
1 parent 8733373 commit 3e71922

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

lib/index.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ const selectorParser = require('postcss-selector-parser');
55
const nameGenerators = require('./name-generators');
66

77
class MinifyClassnames {
8-
constructor({genNameClass, genNameId, filter} = {}) {
8+
constructor({genNameClass, genNameId, filter, customAttributes = []} = {}) {
99
this.filter = filter || /^.js-/;
1010
// TODO: Pass a seed for emojinamestring, make this better
1111
this.genNameClass = this.getNameGenerator(genNameClass, 7);
1212
this.genNameId = this.getNameGenerator(genNameId, 5);
1313
this.classMap = {};
1414
this.idMap = {};
15+
this.customAttributes = customAttributes;
1516
}
1617

1718
getNameGenerator(value, seed) {
@@ -60,6 +61,28 @@ class MinifyClassnames {
6061

6162
minifyElements(tree) {
6263
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+
6386
if (node.attrs && node.attrs.class) {
6487
let classes = node.attrs.class;
6588
node.attrs.class = classes

0 commit comments

Comments
 (0)