Skip to content

Commit 230eaaa

Browse files
authored
feat: add exclude option (#53)
* feat: add option exclude * feat: add option exclude * fix: Resolving conflicts
1 parent 679b11a commit 230eaaa

File tree

6 files changed

+77
-7
lines changed

6 files changed

+77
-7
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ Default:
6767
selectorBlackList: [],
6868
replace: true,
6969
mediaQuery: false,
70-
minPixelValue: 0
70+
minPixelValue: 0,
71+
exclude: /node_modules/i
7172
}
7273
```
7374

@@ -87,7 +88,14 @@ Default:
8788
- `replace` (Boolean) Replaces rules containing rems instead of adding fallbacks.
8889
- `mediaQuery` (Boolean) Allow px to be converted in media queries.
8990
- `minPixelValue` (Number) Set the minimum pixel value to replace.
90-
91+
- `exclude` (String, Regexp, Function) The file path to ignore and leave as px.
92+
- If value is string, it checks to see if file path contains the string.
93+
- `'exclude'` will match `\project\postcss-pxtorem\exclude\path`
94+
- If value is regexp, it checks to see if file path matches the regexp.
95+
- `/exclude/i` will match `\project\postcss-pxtorem\exclude\path`
96+
- If value is function, you can use exclude function to return a true and the file will be ignored.
97+
- the callback will pass the file path as a parameter, it should returns a Boolean result.
98+
- `function (file) { return file.indexOf('exclude') !== -1; }`
9199

92100
### Use with gulp-postcss and autoprefixer
93101

index.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
"use strict";
2-
31
const postcss = require("postcss");
42
const pxRegex = require("./lib/pixel-unit-regex");
53
const filterPropList = require("./lib/filter-prop-list");
4+
const type = require("./lib/type");
65

76
const defaults = {
87
rootValue: 16,
@@ -11,7 +10,8 @@ const defaults = {
1110
propList: ["font", "font-size", "line-height", "letter-spacing"],
1211
replace: true,
1312
mediaQuery: false,
14-
minPixelValue: 0
13+
minPixelValue: 0,
14+
exclude: null
1515
};
1616

1717
const legacyOptions = {
@@ -29,6 +29,17 @@ module.exports = postcss.plugin("postcss-pxtorem", options => {
2929
const satisfyPropList = createPropListMatcher(opts.propList);
3030

3131
return css => {
32+
const exclude = opts.exclude;
33+
const filePath = css.source.input.file;
34+
if (
35+
exclude &&
36+
((type.isFunction(exclude) && exclude(filePath)) ||
37+
(type.isString(exclude) && filePath.indexOf(exclude) !== -1) ||
38+
filePath.match(exclude) !== null)
39+
) {
40+
return;
41+
}
42+
3243
const rootValue =
3344
typeof opts.rootValue === "function"
3445
? opts.rootValue(css.source.input)

lib/type.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var type = function (s) {
2+
return Object.prototype.toString.call(s).slice(8, -1).toLowerCase();
3+
};
4+
5+
var types = [
6+
'String',
7+
'Array',
8+
'Undefined',
9+
'Boolean',
10+
'Number',
11+
'Function',
12+
'Symbol',
13+
'Object'
14+
];
15+
16+
types.forEach(function (str) {
17+
type['is' + str] = function (val) {
18+
return type(val) === str.toLowerCase();
19+
};
20+
});
21+
22+
module.exports = type;
23+

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "postcss-pxtorem",
33
"description": "A CSS post-processor that converts px to rem.",
4-
"version": "5.0.0",
4+
"version": "5.0.1",
55
"author": "cuth",
66
"license": "MIT",
77
"repository": {

spec/pxtorem-spec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,3 +494,31 @@ describe("filter-prop-list", function() {
494494
expect(filterPropList.notEndWith(propList).join()).toBe(expected);
495495
});
496496
});
497+
498+
describe('exclude', function () {
499+
it('should ignore file path with exclude RegEx', function () {
500+
var options = {
501+
exclude: /exclude/i
502+
};
503+
var processed = postcss(pxtorem(options)).process(basicCSS, { from: 'exclude/path' }).css;
504+
expect(processed).toBe(basicCSS);
505+
});
506+
507+
it('should not ignore file path with exclude String', function () {
508+
var options = {
509+
exclude: 'exclude'
510+
};
511+
var processed = postcss(pxtorem(options)).process(basicCSS, { from: 'exclude/path' }).css;
512+
expect(processed).toBe(basicCSS);
513+
});
514+
515+
it('should not ignore file path with exclude function', function () {
516+
var options = {
517+
exclude: function (file) {
518+
return file.indexOf('exclude') !== -1;
519+
}
520+
};
521+
var processed = postcss(pxtorem(options)).process(basicCSS, { from: 'exclude/path' }).css;
522+
expect(processed).toBe(basicCSS);
523+
});
524+
});

0 commit comments

Comments
 (0)