Skip to content

Commit 3b997fd

Browse files
authored
Merge pull request #37 from posthtml/milestone-0.3.0
Milestone 0.3.0
2 parents 0b51cc8 + 34c3cbd commit 3b997fd

File tree

6 files changed

+283
-180
lines changed

6 files changed

+283
-180
lines changed

examples/basic.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Basic simple example
2+
3+
Before:
4+
```html
5+
<html>
6+
<style>
7+
#some-id {
8+
text-transform: uppercase;
9+
}
10+
.header__intro {
11+
color: blue;
12+
}
13+
.card--profile {
14+
background: white;
15+
}
16+
.js-overlay {
17+
display: none;
18+
}
19+
#js-button {
20+
color: blue;
21+
}
22+
@media (min-width: 768px) {
23+
.header__intro {
24+
color: gray;
25+
}
26+
}
27+
</style>
28+
<body>
29+
<svg style="display:none">
30+
<symbol id="icon-location"><path d=""></path></symbol>
31+
</svg>
32+
<h1 id="some-id">Title</h1>
33+
<p class="header__intro">OMG</p>
34+
<div class="js-overlay"></div>
35+
<div id="js-button"></div>
36+
<div class="card--profile">
37+
card content
38+
</div>
39+
<svg>
40+
<use href="#icon-location"></use>
41+
</svg>
42+
<label for="username">Click me</label>
43+
<input type="text" id="username">
44+
</body>
45+
</html>
46+
```
47+
48+
After:
49+
50+
```html
51+
<html>
52+
<style>
53+
#a {
54+
text-transform: uppercase;
55+
}
56+
.a {
57+
color: blue;
58+
}
59+
.b {
60+
background: white;
61+
}
62+
.js-overlay {
63+
display: none;
64+
}
65+
#js-button {
66+
color: blue;
67+
}
68+
@media (min-width: 768px) {
69+
.a {
70+
color: gray;
71+
}
72+
}
73+
</style>
74+
<body>
75+
<svg style="display:none">
76+
<symbol id="b"><path d=""></path></symbol>
77+
</svg>
78+
<h1 id="a">Title</h1>
79+
<p class="a">OMG</p>
80+
<div class="js-overlay"></div>
81+
<div id="js-button"></div>
82+
<div class="b">
83+
card content
84+
</div>
85+
<svg>
86+
<use href="#b"></use>
87+
</svg>
88+
<label for="c">Click me</label>
89+
<input type="text" id="c">
90+
</body>
91+
</html>
92+
```

examples/genName.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Gen name simple example
2+
3+
```html
4+
<html>
5+
<style>
6+
#🚧🕥🏉 {
7+
text-transform: uppercase;
8+
}
9+
.☘👙📙 {
10+
color: blue;
11+
}
12+
.⏲📂⚗ {
13+
background: white;
14+
}
15+
.js-overlay {
16+
display: none;
17+
}
18+
#js-button {
19+
color: blue;
20+
}
21+
@media (min-width: 768px) {
22+
.☘👙📙 {
23+
color: gray;
24+
}
25+
}
26+
</style>
27+
<body>
28+
<svg style="display:none">
29+
<symbol id="👂🗨🌹"><path d=""></path></symbol>
30+
</svg>
31+
<h1 id="🚧🕥🏉">Title</h1>
32+
<p class="☘👙📙">OMG</p>
33+
<div class="js-overlay"></div>
34+
<div id="js-button"></div>
35+
<div class="⏲📂⚗">
36+
card content
37+
</div>
38+
<svg>
39+
<use href="#👂🗨🌹"></use>
40+
</svg>
41+
<label for="🏻🔐🙍">Click me</label>
42+
<input type="text" id="🏻🔐🙍">
43+
</body>
44+
</html>
45+
```
46+

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "posthtml-minify-classnames",
3-
"version": "0.2.4",
3+
"version": "0.3.0",
44
"description": "PostHTML plugin for minifying classnames",
55
"main": "index.js",
66
"author": "Simon Laroche <[email protected]> (https://simon.lc)",

0 commit comments

Comments
 (0)