From 07c63a6622f82f92dfb306eaf88f8b668b0888fc Mon Sep 17 00:00:00 2001 From: msidolphin Date: Mon, 19 Nov 2018 10:59:31 +0800 Subject: [PATCH] add exlclude option to ignore some files https://github.com/evrone/postcss-px-to-viewport/issues/11 --- index.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 533b654..521bec2 100755 --- a/index.js +++ b/index.js @@ -32,11 +32,17 @@ module.exports = postcss.plugin('postcss-px-to-viewport', function (options) { css.walkDecls(function (decl, i) { + // Add exlclude option to ignore some files like 'node_modules' if (opts.exclude) { - if (Object.prototype.toString.call(opts.exclude) !== '[object RegExp]') { - throw new Error('options.exclude should be RegExp!') + if (Object.prototype.toString.call(opts.exclude) === '[object RegExp]') { + if (!handleExclude(opts.exclude, decl.source.input.file)) return; + } else if (Object.prototype.toString.call(opts.exclude) === '[object Array]') { + for (let i = 0; i < opts.exclude.length; ++i) { + if (!handleExclude(opts.exclude[i], decl.source.input.file)) return; + } + } else { + throw new Error('options.exclude should be RegExp or Array!'); } - if (decl.source.input.file.match(opts.exclude) !== null) return; } // This should be the fastest test and will remove most declarations @@ -59,6 +65,14 @@ module.exports = postcss.plugin('postcss-px-to-viewport', function (options) { }; }); +function handleExclude (reg, file) { + if (Object.prototype.toString.call(reg) !== '[object RegExp]') { + throw new Error('options.exclude should be RegExp!'); + } + if (file.match(reg) !== null) return false; + return true; +} + function getUnit(prop, opts) { return prop.indexOf('font') === -1 ? opts.viewportUnit : opts.fontViewportUnit; }