From 269b74a99eeec430bf61ac008d95ba0e998204ec Mon Sep 17 00:00:00 2001 From: hztianxiang Date: Thu, 15 Nov 2018 18:14:36 +0800 Subject: [PATCH 1/3] add feature --- src/index.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index d30aa0e..b1ca925 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.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); From 0579625f6e5bda18236627c17bbb9a9b5f2c98c6 Mon Sep 17 00:00:00 2001 From: hztianxiang Date: Thu, 15 Nov 2018 19:04:44 +0800 Subject: [PATCH 2/3] @dev add readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) 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` From ebef846941ff10c8be6d9ebee4da4c29375b8c6c Mon Sep 17 00:00:00 2001 From: hztianxiang Date: Mon, 19 Nov 2018 14:36:57 +0800 Subject: [PATCH 3/3] add test case for exclude opt --- src/index.js | 2 +- test/index-test.js | 26 +++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index b1ca925..264041b 100644 --- a/src/index.js +++ b/src/index.js @@ -91,7 +91,7 @@ export default postcss.plugin('postcss-plugin-px2rem', options => { css.walkDecls((decl, i) => { const _decl = decl; // 1st check exclude - if (opts.exclude && css.source.input.file.match(opts.exclude) !== null) return; + 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; // 3nd check property black list 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); + }); +});