Skip to content

Commit

Permalink
Merge pull request #10 from JackyTianer/feature/add-exclude-config
Browse files Browse the repository at this point in the history
Feature/add exclude config
  • Loading branch information
soda-x committed Nov 19, 2018
2 parents 4b9d903 + ebef846 commit 6ecbc48
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ Default:
unitPrecision: 5,
propWhiteList: [],
propBlackList: [],
exclude:false,
selectorBlackList: [],
ignoreIdentifier: false,
replace: true,
Expand All @@ -128,6 +129,7 @@ Default:
- Values need to be exact matches.
- `propBlackList` (Array) The properties that should not change from px to rem.
- Values need to be exact matches.
- `exclude` (Reg) a way to exclude some folder,eg. /(node_module)/.
- `selectorBlackList` (Array) The selectors to ignore and leave as px.
- If value is string, it checks to see if selector contains the string.
- `['body']` will match `.body-class`
Expand Down
10 changes: 6 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,15 @@ export default postcss.plugin('postcss-plugin-px2rem', options => {
return css => {
css.walkDecls((decl, i) => {
const _decl = decl;
// 1st check 'px'
// 1st check exclude
if (opts.exclude && css.source.input.file && css.source.input.file.match(opts.exclude) !== null) return;
// 2st check 'px'
if (_decl.value.indexOf('px') === -1) return;
// 2nd check property black list
// 3nd check property black list
if (blacklistedProp(opts.propBlackList, _decl.prop)) return;
// 3rd check property white list
// 4rd check property white list
if (opts.propWhiteList.length && opts.propWhiteList.indexOf(_decl.prop) === -1) return;
// 4th check seletor black list
// 5th check seletor black list
if (blacklistedSelector(opts.selectorBlackList, _decl.parent.selector)) return;

const value = _decl.value.replace(pxRegex, pxReplace);
Expand Down
26 changes: 25 additions & 1 deletion test/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ describe('minPixelValue', () => {
});
});

describe('rpx support', function() {
describe('rpx support', function () {
it('should work on the readme example', () => {
const input = 'h1 { margin: 0 0 20rpx 20rpx; font-size: 32px; line-height: 1.2; letter-spacing: 1rpx; }';
const output = 'h1 { margin: 0 0 0.2rem 0.2rem; font-size: 0.64rem; line-height: 1.2; letter-spacing: 0.01rem; }';
Expand Down Expand Up @@ -262,3 +262,27 @@ describe('rpx support', function() {
expect(processed).toBe(output);
});
});

describe('exclude support', () => {
it('should work on the readme example', () => {
const input = 'h1 { margin: 0 0 20px 20px; font-size: 32px; line-height: 1.2; letter-spacing: 1px; }';
const output = 'h1 { margin: 0 0 20px 20px; font-size: 32px; line-height: 1.2; letter-spacing: 1px; }';
const processed = postcss(pxtorem({
exclude: /(node_modules)/,
})).process(input, {
from: 'node_modules/third.css',
}).css;
expect(processed).toBe(output);
});

it('should work when exclude option range doesn\'t cover', () => {
const input = 'h1 { margin: 0 0 20px 20px; font-size: 32px; line-height: 1.2; letter-spacing: 1px; }';
const output = 'h1 { margin: 0 0 0.2rem 0.2rem; font-size: 0.32rem; line-height: 1.2; letter-spacing: 0.01rem; }';
const processed = postcss(pxtorem({
exclude: /(node_modules)/,
})).process(input, {
from: 'lib/own.css',
}).css;
expect(processed).toBe(output);
});
});

0 comments on commit 6ecbc48

Please sign in to comment.