Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add units to config #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const filterPropList = require("./lib/filter-prop-list");
const type = require("./lib/type");

const defaults = {
units: 'rem',
rootValue: 16,
unitPrecision: 5,
selectorBlackList: [],
Expand Down Expand Up @@ -47,7 +48,8 @@ module.exports = postcss.plugin("postcss-pxtorem", options => {
const pxReplace = createPxReplace(
rootValue,
opts.unitPrecision,
opts.minPixelValue
opts.minPixelValue,
opts.units
);

css.walkDecls((decl, i) => {
Expand Down Expand Up @@ -100,13 +102,13 @@ function convertLegacyOptions(options) {
});
}

function createPxReplace(rootValue, unitPrecision, minPixelValue) {
function createPxReplace(rootValue, unitPrecision, minPixelValue, units) {
return (m, $1) => {
if (!$1) return m;
const pixels = parseFloat($1);
if (pixels < minPixelValue) return m;
const fixedVal = toFixed(pixels / rootValue, unitPrecision);
return fixedVal === 0 ? "0" : fixedVal + "rem";
return fixedVal === 0 ? "0" : fixedVal + units;
};
}

Expand Down