From 8492df556cbba0c1110fb21ba89aa04b808b7281 Mon Sep 17 00:00:00 2001 From: nilianzhu Date: Thu, 30 Jan 2020 14:04:08 +0800 Subject: [PATCH] feat: change rootValue to support both number and function --- README.md | 4 ++-- index.js | 4 +++- package.json | 2 +- spec/pxtorem-spec.js | 18 ++++++++++++++++++ 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9ce3feb..b2f4b03 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ Default: } ``` -- `rootValue` (Number) The root element font size. +- `rootValue` (Number | Function) Represents the root element font size or returns the root element font size based on the [`input`](https://api.postcss.org/Input.html) parameter - `unitPrecision` (Number) The decimal numbers to allow the REM units to grow to. - `propList` (Array) The properties that can change from px to rem. - Values need to be exact matches. @@ -84,7 +84,7 @@ Default: - `['body']` will match `.body-class` - If value is regexp, it checks to see if the selector matches the regexp. - `[/^body$/]` will match `body` but not `.body` -- `replace` (Boolean) replaces rules containing rems instead of adding fallbacks. +- `replace` (Boolean) Replaces rules containing rems instead of adding fallbacks. - `mediaQuery` (Boolean) Allow px to be converted in media queries. - `minPixelValue` (Number) Set the minimum pixel value to replace. diff --git a/index.js b/index.js index fbc1919..fccb557 100644 --- a/index.js +++ b/index.js @@ -29,12 +29,14 @@ module.exports = postcss.plugin('postcss-pxtorem', function (options) { convertLegacyOptions(options); var opts = objectAssign({}, defaults, options); - var pxReplace = createPxReplace(opts.rootValue, opts.unitPrecision, opts.minPixelValue); var satisfyPropList = createPropListMatcher(opts.propList); return function (css) { + var rootValue = typeof opts.rootValue === 'function' ? opts.rootValue(css.source.input) : opts.rootValue; + var pxReplace = createPxReplace(rootValue, opts.unitPrecision, opts.minPixelValue); + css.walkDecls(function (decl, i) { // This should be the fastest test and will remove most declarations if (decl.value.indexOf('px') === -1) return; diff --git a/package.json b/package.json index cf31e7d..c6279da 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "postcss-pxtorem", "description": "A CSS post-processor that converts px to rem.", - "version": "4.0.1", + "version": "4.1.0", "author": "cuth", "license": "MIT", "repository": { diff --git a/spec/pxtorem-spec.js b/spec/pxtorem-spec.js index d3563cb..1f0ce34 100644 --- a/spec/pxtorem-spec.js +++ b/spec/pxtorem-spec.js @@ -149,6 +149,24 @@ describe('rootValue', function () { expect(processed).toBe(expected); }); + + it('should replace using different root values with different files', function () { + var css2 = '.rule { font-size: 20px }'; + var expected = '.rule { font-size: 1rem }'; + var options = { + rootValue: function (input) { + if (input.from.indexOf('basic.css') !== -1) { + return 15; + } + return 20; + } + }; + var processed1 = postcss(pxtorem(options)).process(basicCSS, { from: '/tmp/basic.css' }).css; + var processed2 = postcss(pxtorem(options)).process(css2, { from: '/tmp/whatever.css' }).css; + + expect(processed1).toBe(expected); + expect(processed2).toBe(expected); + }); }); describe('unitPrecision', function () {