Skip to content

Commit 1e16d59

Browse files
mightyalekseyAlexey Litvinov
authored andcommitted
ignore option implementation
1 parent 57caf73 commit 1e16d59

File tree

14 files changed

+94
-10
lines changed

14 files changed

+94
-10
lines changed

lib/index.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ const assign = require('lodash').assign;
22
const attachHook = require('./attachHook');
33
const dirname = require('path').dirname;
44
const genericNames = require('generic-names');
5+
const globToRegex = require('glob-to-regexp');
56
const identity = require('lodash').identity;
7+
const negate = require('lodash').negate;
68
const readFileSync = require('fs').readFileSync;
79
const relative = require('path').relative;
810
const resolve = require('path').resolve;
@@ -15,8 +17,25 @@ const ExtractImports = require('postcss-modules-extract-imports');
1517
const Scope = require('postcss-modules-scope');
1618
const Parser = require('postcss-modules-parser');
1719

20+
/**
21+
* @param {function|regex|string} ignore glob, regex or function
22+
* @return {function}
23+
*/
24+
function buildExceptionChecker(ignore) {
25+
if (ignore instanceof RegExp) {
26+
return filepath => ignore.test(filepath);
27+
}
28+
29+
if (typeof ignore === 'string') {
30+
return filepath => globToRegex(ignore).test(filepath);
31+
}
32+
33+
return ignore || negate(identity);
34+
}
35+
1836
module.exports = function setupHook({
1937
extensions = '.css',
38+
ignore,
2039
preprocessCss = identity,
2140
processCss,
2241
to,
@@ -96,5 +115,7 @@ module.exports = function setupHook({
96115
return tokens;
97116
};
98117

99-
attachHook(filename => fetch(filename, filename), '.css', () => false);
118+
const isException = buildExceptionChecker(ignore);
119+
120+
attachHook(filename => fetch(filename, filename), '.css', isException);
100121
};

lib/validate.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const keys = require('lodash').keys;
55
const rules = {
66
// hook
77
extensions: 'array|string',
8-
ignore: 'function|regex',
8+
ignore: 'function|regex|string',
99
preprocessCss: 'function',
1010
processCss: 'function',
1111
to: 'string',
@@ -22,6 +22,7 @@ const rules = {
2222
const tests = {
2323
array: require('lodash').isArray,
2424
function: require('lodash').isFunction,
25+
regex: require('lodash').isRegExp,
2526
string: require('lodash').isString,
2627
};
2728

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"dependencies": {
3434
"debug": "^2.2.0",
3535
"generic-names": "^1.0.1",
36+
"glob-to-regexp": "^0.1.0",
3637
"icss-replace-symbols": "^1.0.2",
3738
"lodash": "^4.3.0",
3839
"lookup-fs": "^1.0.0",

test/api/append.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ suite('api/append', () => {
1515
});
1616

1717
teardown(() => {
18-
dropCache('./api/fixture/oceanic.css');
1918
detachHook('.css');
19+
dropCache('./api/fixture/oceanic.css');
2020
});
2121
});
2222
});

test/api/extensions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ suite('api/extensions', () => {
1212
setup(() => hook({}));
1313

1414
teardown(() => {
15-
dropCache('./api/fixture/oceanic.css');
1615
detachHook('.css');
16+
dropCache('./api/fixture/oceanic.css');
1717
});
1818
});
1919
});

test/api/fixture/typography.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@import url(http://fonts.googleapis.com/css?family=Vollkorn:400,400italic,700,700italic&subset=latin);
2+
3+
.common
4+
{
5+
font: 1.3em 'Vollkorn', Palatino, Times;
6+
}

test/api/generateScopedName.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ suite('api/generateScopedName', () => {
5050
});
5151

5252
teardown(() => {
53-
dropCache('./api/fixture/oceanic.css');
5453
detachHook('.css');
54+
dropCache('./api/fixture/oceanic.css');
5555
});
5656
});

test/api/ignore.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const detachHook = require('../sugar').detachHook;
2+
const dropCache = require('../sugar').dropCache;
3+
4+
suite('api/ignore', () => {
5+
suite('glob', () => {
6+
setup(() => hook({
7+
ignore: '*oceanic*',
8+
}));
9+
10+
test('should return tokens', () => {
11+
assert.deepEqual(require('./fixture/typography.css'), {
12+
common: '_test_api_fixture_typography__common',
13+
});
14+
});
15+
16+
test('should throw an exception', () => {
17+
assert.throws(() => require('./fixture/oceanic.css'));
18+
});
19+
});
20+
21+
suite('function', () => {
22+
setup(() => hook({
23+
ignore: filename => /typography/.test(filename),
24+
}));
25+
26+
test('should return tokens', () => {
27+
assert.throws(() => require('./fixture/typography.css'));
28+
});
29+
30+
test('should throw an exception', () => {
31+
assert.deepEqual(require('./fixture/oceanic.css'), {
32+
color: '_test_api_fixture_oceanic__color',
33+
});
34+
});
35+
});
36+
37+
suite('regex', () => {
38+
setup(() => hook({
39+
ignore: /\.css$/,
40+
}));
41+
42+
test('should throw an exception', () => {
43+
assert.throws(() => require('./fixture/typography.css'));
44+
assert.throws(() => require('./fixture/oceanic.css'));
45+
});
46+
});
47+
48+
teardown(() => {
49+
detachHook('.css');
50+
dropCache('./api/fixture/oceanic.css');
51+
dropCache('./api/fixture/typography.css');
52+
});
53+
});

test/api/prepend.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ suite('api/prepend', () => {
1515
});
1616

1717
teardown(() => {
18-
dropCache('./api/fixture/oceanic.css');
1918
detachHook('.css');
19+
dropCache('./api/fixture/oceanic.css');
2020
});
2121
});
2222
});

test/api/preprocessCss.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ suite('api/preprocessCss', () => {
1313
});
1414

1515
teardown(() => {
16-
dropCache('./api/fixture/oceanic.css');
1716
detachHook('.css');
17+
dropCache('./api/fixture/oceanic.css');
1818
});
1919
});

0 commit comments

Comments
 (0)