From 5a57858ee75656900763b65d453276df7dca50d1 Mon Sep 17 00:00:00 2001 From: Ivan Demidov Date: Sat, 20 Feb 2021 18:04:06 +0300 Subject: [PATCH 1/4] feat: add options ignoreFiles --- index.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index d1634ed..a1ea400 100644 --- a/index.js +++ b/index.js @@ -1,21 +1,30 @@ +const path = require('path'); module.exports = function postcssPrefixSelector(options) { const prefix = options.prefix; const prefixWithSpace = /\s+$/.test(prefix) ? prefix : `${prefix} `; + const ignoreFiles = options.ignoreFiles ? [].concat(options.ignoreFiles) : []; - return function(root) { - root.walkRules(rule => { + return function (root) { + if ( + root.source.input.file && + ignoreFiles.includes(path.basename(root.source.input.file)) + ) { + return; + } + + root.walkRules((rule) => { const keyframeRules = [ 'keyframes', '-webkit-keyframes', '-moz-keyframes', - '-o-keyframes' + '-o-keyframes', ]; if (rule.parent && keyframeRules.includes(rule.parent.name)) { return; } - rule.selectors = rule.selectors.map(selector => { + rule.selectors = rule.selectors.map((selector) => { if (options.exclude && excludeSelector(selector, options.exclude)) { return selector; } @@ -35,7 +44,7 @@ module.exports = function postcssPrefixSelector(options) { }; function excludeSelector(selector, excludeArr) { - return excludeArr.some(excludeRule => { + return excludeArr.some((excludeRule) => { if (excludeRule instanceof RegExp) { return excludeRule.test(selector); } From 870c8ee60b8b71ba457cb78debe342a8fdf34d85 Mon Sep 17 00:00:00 2001 From: Ivan Demidov Date: Sat, 20 Feb 2021 18:04:30 +0300 Subject: [PATCH 2/4] test: fro options ignoreFiles --- test/fixtures/ignore-files.css | 7 +++++++ test/fixtures/ignore-files.expected.css | 7 +++++++ test/test.js | 27 ++++++++++++++++++++----- 3 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/ignore-files.css create mode 100644 test/fixtures/ignore-files.expected.css diff --git a/test/fixtures/ignore-files.css b/test/fixtures/ignore-files.css new file mode 100644 index 0000000..12da16e --- /dev/null +++ b/test/fixtures/ignore-files.css @@ -0,0 +1,7 @@ +.a { + color: coral; +} + +.b { + color: deepskyblue; +} diff --git a/test/fixtures/ignore-files.expected.css b/test/fixtures/ignore-files.expected.css new file mode 100644 index 0000000..12da16e --- /dev/null +++ b/test/fixtures/ignore-files.expected.css @@ -0,0 +1,7 @@ +.a { + color: coral; +} + +.b { + color: deepskyblue; +} diff --git a/test/test.js b/test/test.js index fe7e524..fbb27b8 100644 --- a/test/test.js +++ b/test/test.js @@ -7,7 +7,7 @@ it('should prefix a selector', () => { const out = postcss() .use( prefixer({ - prefix: '.hello ' + prefix: '.hello ', }) ) .process(getFixtureContents('single-selector.css')).css; @@ -21,7 +21,7 @@ it('should prefix a group of selectors', () => { const out = postcss() .use( prefixer({ - prefix: '.hello ' + prefix: '.hello ', }) ) .process(getFixtureContents('group-selectors.css')).css; @@ -36,7 +36,7 @@ it('should avoid prefixing excluded selectors', () => { .use( prefixer({ prefix: '.hello ', - exclude: ['body', '.a *:not(.b)', /class-/] + exclude: ['body', '.a *:not(.b)', /class-/], }) ) .process(getFixtureContents('exclude-selectors.css')).css; @@ -50,7 +50,7 @@ it('should skip @keyframes selectors', () => { const out = postcss() .use( prefixer({ - prefix: '.hello ' + prefix: '.hello ', }) ) .process(getFixtureContents('keyframes.css')).css; @@ -71,7 +71,7 @@ it('should support an additional callback for prefix transformation', () => { } return prefixedSelector; - } + }, }) ) .process(getFixtureContents('transform.css')).css; @@ -81,6 +81,23 @@ it('should support an additional callback for prefix transformation', () => { assert.equal(out, expected); }); +it('should not set prefix a selector in ignore file', () => { + const out = postcss() + .use( + prefixer({ + prefix: '.hello ', + ignoreFiles: ['ignore-files.css'], + }) + ) + .process(getFixtureContents('ignore-files.css'), { + from: 'ignore-files.css', + }).css; + + const expected = getFixtureContents('ignore-files.expected.css'); + + assert.equal(out, expected); +}); + function getFixtureContents(name) { return fs.readFileSync(`test/fixtures/${name}`, 'utf8').trim(); } From ef828703aac824ad43c1787f942a4ac243ad018d Mon Sep 17 00:00:00 2001 From: Ivan Demidov Date: Sat, 20 Feb 2021 18:04:53 +0300 Subject: [PATCH 3/4] docs: add description for options ignoreFiles --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0002984..763d17c 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,7 @@ module: { - `prefix` - This string is added before every CSS selector. - `exclude` - It's possible to avoid prefixing some selectors by passing an array of selectors (strings or regular expressions). - `transform` - In cases where you may want to use the prefix differently for different selectors, it is possible to pass in a custom transform method. +- `ignoreFiles` - List of ignored files for processing. ## Maintainer From 181bd7cc74a03b0334ab873840b979b3599214bf Mon Sep 17 00:00:00 2001 From: Ivan Demidov Date: Wed, 24 Feb 2021 09:52:36 +0300 Subject: [PATCH 4/4] perf: match ignore file --- index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/index.js b/index.js index a1ea400..30eded1 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,3 @@ -const path = require('path'); module.exports = function postcssPrefixSelector(options) { const prefix = options.prefix; const prefixWithSpace = /\s+$/.test(prefix) ? prefix : `${prefix} `; @@ -7,7 +6,7 @@ module.exports = function postcssPrefixSelector(options) { return function (root) { if ( root.source.input.file && - ignoreFiles.includes(path.basename(root.source.input.file)) + ignoreFiles.some((file) => root.source.input.file.includes(file)) ) { return; }