-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
39 lines (31 loc) · 1.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import postcss from 'postcss';
// !important keyword pattern
const importantMatch = /\s*!important$/;
export default postcss.plugin('postcss-short-font-size', opts => {
const prefix = 'prefix' in Object(opts) ? `-${opts.prefix}-` : '';
const skip = 'skip' in Object(opts) ? String(opts.skip) : '*';
const fontSizePropertyRegExp = new RegExp(`^${prefix}(font-size)$`);
return root => {
// for each font-size declaration
root.walkDecls(fontSizePropertyRegExp, decl => {
// conditionally unprefix the font-size property
decl.prop = decl.prop.replace(fontSizePropertyRegExp, '$1');
// slash-separated values (font-size, line-height)
const [fontSize, lineHeight] = postcss.list.split(decl.value, '/');
// whether the font-size is !important
decl.important = importantMatch.test(fontSize);
if (lineHeight) {
// conditionally prepend the line-height declaration
if (lineHeight !== skip) {
decl.cloneBefore({ prop: 'line-height', value: lineHeight });
}
// conditionally remove the font-size declaration
if (fontSize !== skip) {
decl.value = fontSize.replace(importantMatch, '');
} else {
decl.remove();
}
}
});
};
});