-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
27 lines (22 loc) · 784 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
const defaults = {
name: 'em-convert',
precision: 5
};
module.exports = (options = {}) => {
const { name, precision } = {...defaults, ...options};
const regexp = new RegExp('(?!\\W+)' + name + '\\(([^\(\)]+)\\s*,\\s*(\\d*\\.?\\d+)px\\)', 'g');
const rounded = (value, precision) => {
precision = Math.pow(10, precision);
return Math.floor(value * precision) / precision;
};
const convert = (values, context) => values.replace(/(\d*\.?\d+)px/g, (_, value) => {
return rounded(parseFloat(value) / parseFloat(context), precision) + 'em';
});
return {
postcssPlugin: 'postcss-em',
Once (root) {
root.replaceValues(regexp, { fast: name + '(' }, (_, values, context) => convert(values, context));
}
}
};
module.exports.postcss = true;