diff --git a/README.md b/README.md index f62343f..7dacc12 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,7 @@ Default: unitPrecision: 5, propWhiteList: [], propBlackList: [], + exclude:false, selectorBlackList: [], ignoreIdentifier: false, replace: true, @@ -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` diff --git a/src/index.js b/src/index.js index d30aa0e..264041b 100644 --- a/src/index.js +++ b/src/index.js @@ -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); diff --git a/test/index-test.js b/test/index-test.js index ceb43d2..c25f4df 100644 --- a/test/index-test.js +++ b/test/index-test.js @@ -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; }'; @@ -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); + }); +});