Skip to content

Commit 85ff95a

Browse files
Add new options includeFiles (#87)
* minor refactor add option.includeFiles add option.bethSymbol add tests to cover new features update Readme * remove unnecessary things * fix include bug rem unused test add include tests * Delete between-symbol-plus-selector.css * Delete between-symbol-plus-selector.expected.css Co-authored-by: Valentin Radulescu <radulescu.valentin@gmail.com>
1 parent 00a952b commit 85ff95a

9 files changed

+164
-1
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,13 @@ module: {
9898
postcssOptions: {
9999
plugins: {
100100
"postcss-prefix-selector": {
101-
prefix: '.my-prefix'
101+
prefix: '.my-prefix',
102+
transform(prefix, selector, prefixedSelector) {
103+
if (selector.match(/^(html|body)/)) {
104+
return selector.replace(/^([^\s]*)/, `$1 ${prefix}`);
105+
}
106+
return prefixedSelector;
107+
},
102108
},
103109
autoprefixer: {
104110
browsers: ['last 4 versions']
@@ -118,6 +124,7 @@ module: {
118124
- `exclude` - It's possible to avoid prefixing some selectors by passing an array of selectors (strings or regular expressions).
119125
- `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.
120126
- `ignoreFiles` - List of ignored files for processing.
127+
- `includeFiles` - List of included files for processing.
121128

122129
## Maintainer
123130

index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,25 @@ module.exports = function postcssPrefixSelector(options) {
22
const prefix = options.prefix;
33
const prefixWithSpace = /\s+$/.test(prefix) ? prefix : `${prefix} `;
44
const ignoreFiles = options.ignoreFiles ? [].concat(options.ignoreFiles) : [];
5+
const includeFiles = options.includeFiles
6+
? [].concat(options.includeFiles)
7+
: [];
58

69
return function (root) {
710
if (
11+
ignoreFiles.length &&
812
root.source.input.file &&
913
ignoreFiles.some((file) => root.source.input.file.includes(file))
1014
) {
1115
return;
1216
}
17+
if (
18+
includeFiles.length &&
19+
root.source.input.file &&
20+
!includeFiles.find((file) => root.source.input.file.includes(file))
21+
) {
22+
return;
23+
}
1324

1425
root.walkRules((rule) => {
1526
const keyframeRules = [
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.a {
2+
color: coral;
3+
}
4+
5+
.b {
6+
color: deepskyblue;
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.hello.a {
2+
color: coral;
3+
}
4+
5+
.hello.b {
6+
color: deepskyblue;
7+
}

test/fixtures/include-files.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.a {
2+
color: coral;
3+
}
4+
5+
.b {
6+
color: deepskyblue;
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.hello .a {
2+
color: coral;
3+
}
4+
5+
.hello .b {
6+
color: deepskyblue;
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.a {
2+
color: coral;
3+
}
4+
5+
.b {
6+
color: deepskyblue;
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.hello .world .a {
2+
color: coral;
3+
}
4+
5+
.hello .world .b {
6+
color: deepskyblue;
7+
}

test/test.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,109 @@ it('should prefix selectors in unignored files', () => {
115115
assert.equal(out, expected);
116116
});
117117

118+
it('should prefix selectors in included file', () => {
119+
const out = postcss()
120+
.use(
121+
prefixer({
122+
prefix: '.hello ',
123+
includeFiles: ['include-files.css'],
124+
})
125+
)
126+
.process(getFixtureContents('include-files.css'), {
127+
from: 'include-files.css',
128+
}).css;
129+
130+
const expected = getFixtureContents('include-files.expected.css');
131+
132+
assert.equal(out, expected);
133+
});
134+
135+
it('should work as expected when included array is empty', () => {
136+
const out = postcss()
137+
.use(
138+
prefixer({
139+
prefix: '.hello ',
140+
includeFiles: [],
141+
})
142+
)
143+
.process(getFixtureContents('include-files.css'), {
144+
from: 'include-files.css',
145+
}).css;
146+
147+
const expected = getFixtureContents('include-files.expected.css');
148+
149+
assert.equal(out, expected);
150+
});
151+
152+
it('should work as expected when included two items and mmore in array', () => {
153+
const out = postcss()
154+
.use(
155+
prefixer({
156+
prefix: '.hello ',
157+
includeFiles: [
158+
'include-files.css',
159+
'single-selector.css',
160+
'undefined.css',
161+
],
162+
})
163+
)
164+
.process(getFixtureContents('include-files.css'), {
165+
from: 'include-files.css',
166+
}).css;
167+
168+
const expected = getFixtureContents('include-files.expected.css');
169+
170+
assert.equal(out, expected);
171+
});
172+
173+
it('should not prefix selectors in unincluded files', () => {
174+
const out = postcss()
175+
.use(
176+
prefixer({
177+
prefix: '.hello ',
178+
includeFiles: ['include-files.css'],
179+
})
180+
)
181+
.process(getFixtureContents('single-selector.css'), {
182+
from: 'single-selector.css',
183+
}).css;
184+
185+
const expected = getFixtureContents('single-selector.css');
186+
187+
assert.equal(out, expected);
188+
});
189+
190+
it('should use custom symbol between prefix and selector. Use empty to glue', () => {
191+
const out = postcss()
192+
.use(
193+
prefixer({
194+
prefix: '.hello',
195+
transform(prefix, selector) {
196+
return prefix + selector;
197+
},
198+
})
199+
)
200+
.process(getFixtureContents('between-symbol-selector.css')).css;
201+
202+
const expected = getFixtureContents('between-symbol-selector.expected.css');
203+
204+
assert.equal(out, expected);
205+
});
206+
207+
it('should prefix a selector. Use ".hello .world"', () => {
208+
const out = postcss()
209+
.use(
210+
prefixer({
211+
prefix: '.hello .world',
212+
})
213+
)
214+
.process(getFixtureContents('single-long-selector.css')).css;
215+
216+
const expected = getFixtureContents('single-long-selector.expected.css');
217+
218+
assert.equal(out, expected);
219+
});
220+
118221
function getFixtureContents(name) {
119222
return fs.readFileSync(`test/fixtures/${name}`, 'utf8').trim();
120223
}

0 commit comments

Comments
 (0)