-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
35 lines (31 loc) · 992 Bytes
/
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
const { convert } = require('startijenn-rem');
const defaults = {
name: 'rem-convert',
fallback: false,
convert: 'rem',
baseline: 16,
precision: 5
};
module.exports = (options = {}) => {
const { name, fallback, convert: to, ...convertOptions } = {...defaults, ...options};
const regexp = new RegExp('(?!\\W+)' + name + '\\(([^\(\)]+)\\)', 'g');
return {
postcssPlugin: 'postcss-rem',
Once (root) {
if (fallback && to !== 'px') {
root.walkDecls((decl) => {
if (decl.value && decl.value.includes(name + '(')) {
let values = decl.value.replace(regexp, '$1');
decl.cloneBefore({
value: convert(values, 'px', convertOptions)
});
decl.value = convert(values, 'rem', convertOptions);
}
});
} else {
root.replaceValues(regexp, { fast: name + '(' }, (_, values) => convert(values, to, convertOptions));
}
}
}
};
module.exports.postcss = true;