Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 13 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
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.some((file) => root.source.input.file.includes(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;
}
Expand All @@ -35,7 +43,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);
}
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/ignore-files.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.a {
color: coral;
}

.b {
color: deepskyblue;
}
7 changes: 7 additions & 0 deletions test/fixtures/ignore-files.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.a {
color: coral;
}

.b {
color: deepskyblue;
}
27 changes: 22 additions & 5 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ it('should prefix a selector', () => {
const out = postcss()
.use(
prefixer({
prefix: '.hello '
prefix: '.hello ',
})
)
.process(getFixtureContents('single-selector.css')).css;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -50,7 +50,7 @@ it('should skip @keyframes selectors', () => {
const out = postcss()
.use(
prefixer({
prefix: '.hello '
prefix: '.hello ',
})
)
.process(getFixtureContents('keyframes.css')).css;
Expand All @@ -71,7 +71,7 @@ it('should support an additional callback for prefix transformation', () => {
}

return prefixedSelector;
}
},
})
)
.process(getFixtureContents('transform.css')).css;
Expand All @@ -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();
}