|
@ -29,8 +29,22 @@ module.exports = postcss.plugin('postcss-px-to-viewport', function (options) { |
|
|
var pxRegex = new RegExp('"[^"]+"|\'[^\']+\'|url\\([^\\)]+\\)|(\\d*\\.?\\d+)' + opts.unitToConvert, 'ig') |
|
|
var pxRegex = new RegExp('"[^"]+"|\'[^\']+\'|url\\([^\\)]+\\)|(\\d*\\.?\\d+)' + opts.unitToConvert, 'ig') |
|
|
|
|
|
|
|
|
return function (css) { |
|
|
return function (css) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
css.walkDecls(function (decl, i) { |
|
|
css.walkDecls(function (decl, i) { |
|
|
|
|
|
|
|
|
|
|
|
// Add exlclude option to ignore some files like 'node_modules'
|
|
|
|
|
|
if (opts.exclude && decl.source.input.file) { |
|
|
|
|
|
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!'); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// This should be the fastest test and will remove most declarations
|
|
|
// This should be the fastest test and will remove most declarations
|
|
|
if (decl.value.indexOf(opts.unitToConvert) === -1) return; |
|
|
if (decl.value.indexOf(opts.unitToConvert) === -1) return; |
|
|
|
|
|
|
|
@ -51,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) { |
|
|
function getUnit(prop, opts) { |
|
|
return prop.indexOf('font') === -1 ? opts.viewportUnit : opts.fontViewportUnit; |
|
|
return prop.indexOf('font') === -1 ? opts.viewportUnit : opts.fontViewportUnit; |
|
|
} |
|
|
} |
|
|