Browse Source

Merge pull request #21 from evrone/add-replace-flag

add replace flag
pull/23/head
Ivan 7 years ago
committed by GitHub
parent
commit
2e360e5bd0
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      README.md
  2. 20
      index.js
  3. 19
      spec/px-to-viewport.spec.js

2
README.md

@ -101,6 +101,7 @@ Default:
selectorBlackList: [],
minPixelValue: 1,
mediaQuery: false,
replace: true,
exclude: [] // ignore some files
}
```
@ -117,6 +118,7 @@ Default:
- `[/^body$/]` will match `body` but not `.body`
- `minPixelValue` (Number) Set the minimum pixel value to replace.
- `mediaQuery` (Boolean) Allow px to be converted in media queries.
- `replace` (Boolean) replaces rules containing rems instead of adding fallbacks.
- `exclude` (Array or Regexp) Ignore some files like 'node_modules'
- If value is regexp, will ignore the matches files.
- If value is array, the elements of the array are regexp.

20
index.js

@ -12,7 +12,8 @@ var defaults = {
fontViewportUnit: 'vw', // vmin is more suitable.
selectorBlackList: [],
minPixelValue: 1,
mediaQuery: false
mediaQuery: false,
replace: true
};
module.exports = postcss.plugin('postcss-px-to-viewport', function (options) {
@ -50,9 +51,16 @@ module.exports = postcss.plugin('postcss-px-to-viewport', function (options) {
if (blacklistedSelector(opts.selectorBlackList, decl.parent.selector)) return;
var unit = getUnit(decl.prop, opts)
var unit = getUnit(decl.prop, opts);
var value = decl.value.replace(pxRegex, createPxReplace(opts.viewportWidth, opts.minPixelValue, opts.unitPrecision, unit));
decl.value = decl.value.replace(pxRegex, createPxReplace(opts.viewportWidth, opts.minPixelValue, opts.unitPrecision, unit));
if (declarationExists(decl.parent, decl.prop, value)) return;
if (opts.replace) {
decl.value = value;
} else {
decl.parent.insertAfter(i, decl.clone({ value: value }));
}
});
if (opts.mediaQuery) {
@ -100,3 +108,9 @@ function blacklistedSelector(blacklist, selector) {
return selector.match(regex);
});
}
function declarationExists(decls, prop, value) {
return decls.some(function (decl) {
return (decl.prop === prop && decl.value === value);
});
}

19
spec/px-to-viewport.spec.js

@ -43,6 +43,13 @@ describe('px-to-viewport', function() {
expect(processed).toBe(expected);
});
it('should not add properties that already exist', function () {
var expected = '.rule { font-size: 16px; font-size: 5vw; }';
var processed = postcss(pxToViewport()).process(expected).css;
expect(processed).toBe(expected);
});
});
describe('value parsing', function() {
@ -259,3 +266,15 @@ describe('exclude', function () {
expect(processed).toBe(covered);
});
});
describe('replace', function () {
it('should leave fallback pixel unit with root em value', function () {
var options = {
replace: false
};
var processed = postcss(pxToViewport(options)).process(basicCSS).css;
var expected = '.rule { font-size: 15px; font-size: 4.6875vw }';
expect(processed).toBe(expected);
});
});
Loading…
Cancel
Save