diff --git a/lib/index.js b/lib/index.js index abd894d..758d299 100644 --- a/lib/index.js +++ b/lib/index.js @@ -24,6 +24,7 @@ module.exports = function setupHook({ devMode, extensions = '.css', ignore, + only, preprocessCss = identity, processCss, processorOpts, @@ -122,7 +123,7 @@ module.exports = function setupHook({ }; const exts = toArray(extensions); - const isException = buildExceptionChecker(ignore); + const isException = buildExceptionChecker(ignore, only); // @todo add possibility to specify particular config for each extension exts.forEach(extension => attachHook(filename => fetch(filename, filename), extension, isException)); @@ -142,14 +143,25 @@ function toArray(option) { * @param {function|regex|string} ignore glob, regex or function * @return {function} */ -function buildExceptionChecker(ignore) { - if (ignore instanceof RegExp) { - return filepath => ignore.test(filepath); +function buildExceptionChecker(ignore, only) { + const param = ignore || only; + let fn; + + if (!ignore && !only) { + return negate(identity); + } + + if (param instanceof RegExp) { + fn = filepath => param.test(filepath); + } else if (typeof param === 'string') { + fn = filepath => globToRegex(param).test(filepath); + } else { + fn = param; } - if (typeof ignore === 'string') { - return filepath => globToRegex(ignore).test(filepath); + if (only) { + fn = negate(fn); } - return ignore || negate(identity); + return fn; } diff --git a/lib/validate.js b/lib/validate.js index 9ddc925..01e51d3 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -7,6 +7,7 @@ const rules = { devMode: 'boolean', extensions: 'array|string', ignore: 'function|regex|string', + only: 'function|regex|string', preprocessCss: 'function', processCss: 'function', processorOpts: 'object', diff --git a/test/api/only.js b/test/api/only.js new file mode 100644 index 0000000..24df8c1 --- /dev/null +++ b/test/api/only.js @@ -0,0 +1,53 @@ +const detachHook = require('../sugar').detachHook; +const dropCache = require('../sugar').dropCache; + +suite('api/only', () => { + suite('glob', () => { + setup(() => hook({ + only: '*typography*', + })); + + test('should return tokens', () => { + assert.deepEqual(require('./fixture/typography.css'), { + common: '_test_api_fixture_typography__common', + }); + }); + + test('should throw an exception', () => { + assert.throws(() => require('./fixture/oceanic.css')); + }); + }); + + suite('function', () => { + setup(() => hook({ + only: filename => !/typography/.test(filename), + })); + + test('should return tokens', () => { + assert.throws(() => require('./fixture/typography.css')); + }); + + test('should throw an exception', () => { + assert.deepEqual(require('./fixture/oceanic.css'), { + color: '_test_api_fixture_oceanic__color', + }); + }); + }); + + suite('regex', () => { + setup(() => hook({ + only: /\.(?!css$)/, + })); + + test('should throw an exception', () => { + assert.throws(() => require('./fixture/typography.css')); + assert.throws(() => require('./fixture/oceanic.css')); + }); + }); + + teardown(() => { + detachHook('.css'); + dropCache('./api/fixture/oceanic.css'); + dropCache('./api/fixture/typography.css'); + }); +});