postcss-px-to-viewport-tebie6
Advanced tools
Comparing version 1.1.1 to 1.1.2
184
index.js
@@ -1,7 +0,7 @@ | ||
'use strict'; | ||
'use strict' | ||
var postcss = require('postcss'); | ||
var objectAssign = require('object-assign'); | ||
var { createPropListMatcher } = require('./src/prop-list-matcher'); | ||
var { getUnitRegexp } = require('./src/pixel-unit-regexp'); | ||
var postcss = require('postcss') | ||
var objectAssign = require('object-assign') | ||
var { createPropListMatcher } = require('./src/prop-list-matcher') | ||
var { getUnitRegexp } = require('./src/pixel-unit-regexp') | ||
@@ -14,3 +14,3 @@ var defaults = { | ||
viewportUnit: 'vw', | ||
fontViewportUnit: 'vw', // vmin is more suitable. | ||
fontViewportUnit: 'vw', // vmin is more suitable. | ||
selectorBlackList: [], | ||
@@ -24,16 +24,16 @@ propList: ['*'], | ||
landscapeWidth: 568 | ||
}; | ||
} | ||
var ignoreNextComment = 'px-to-viewport-ignore-next'; | ||
var ignorePrevComment = 'px-to-viewport-ignore'; | ||
var ignoreNextComment = 'px-to-viewport-ignore-next' | ||
var ignorePrevComment = 'px-to-viewport-ignore' | ||
module.exports = postcss.plugin('postcss-px-to-viewport', function (options) { | ||
var opts = objectAssign({}, defaults, options); | ||
module.exports = postcss.plugin('postcss-px-to-viewport-tebie6', function (options) { | ||
var opts = objectAssign({}, defaults, options) | ||
checkRegExpOrArray(opts, 'exclude'); | ||
checkRegExpOrArray(opts, 'include'); | ||
checkRegExpOrArray(opts, 'exclude') | ||
checkRegExpOrArray(opts, 'include') | ||
var pxRegex = getUnitRegexp(opts.unitToConvert); | ||
var satisfyPropList = createPropListMatcher(opts.propList); | ||
var landscapeRules = []; | ||
var pxRegex = getUnitRegexp(opts.unitToConvert) | ||
var satisfyPropList = createPropListMatcher(opts.propList) | ||
var landscapeRules = [] | ||
@@ -43,16 +43,16 @@ return function (css, result) { | ||
// Add exclude option to ignore some files like 'node_modules' | ||
var file = rule.source && rule.source.input.file; | ||
var file = rule.source && rule.source.input.file | ||
if (opts.include && file) { | ||
if (Object.prototype.toString.call(opts.include) === '[object RegExp]') { | ||
if (!opts.include.test(file)) return; | ||
if (!opts.include.test(file)) return | ||
} else if (Object.prototype.toString.call(opts.include) === '[object Array]') { | ||
var flag = false; | ||
var flag = false | ||
for (var i = 0; i < opts.include.length; i++) { | ||
if (opts.include[i].test(file)) { | ||
flag = true; | ||
break; | ||
flag = true | ||
break | ||
} | ||
} | ||
if (!flag) return; | ||
if (!flag) return | ||
} | ||
@@ -63,6 +63,6 @@ } | ||
if (Object.prototype.toString.call(opts.exclude) === '[object RegExp]') { | ||
if (opts.exclude.test(file)) return; | ||
if (opts.exclude.test(file)) return | ||
} else if (Object.prototype.toString.call(opts.exclude) === '[object Array]') { | ||
for (var i = 0; i < opts.exclude.length; i++) { | ||
if (opts.exclude[i].test(file)) return; | ||
if (opts.exclude[i].test(file)) return | ||
} | ||
@@ -72,83 +72,87 @@ } | ||
if (blacklistedSelector(opts.selectorBlackList, rule.selector)) return; | ||
if (blacklistedSelector(opts.selectorBlackList, rule.selector)) return | ||
if (opts.landscape && !rule.parent.params) { | ||
var landscapeRule = rule.clone().removeAll(); | ||
var landscapeRule = rule.clone().removeAll() | ||
rule.walkDecls(function(decl) { | ||
if (decl.value.indexOf(opts.unitToConvert) === -1) return; | ||
if (!satisfyPropList(decl.prop)) return; | ||
rule.walkDecls(function (decl) { | ||
if (decl.value.indexOf(opts.unitToConvert) === -1) return | ||
if (!satisfyPropList(decl.prop)) return | ||
landscapeRule.append(decl.clone({ | ||
value: decl.value.replace(pxRegex, createPxReplace(opts, opts.landscapeUnit, opts.landscapeWidth)) | ||
})); | ||
}); | ||
landscapeRule.append( | ||
decl.clone({ | ||
value: decl.value.replace(pxRegex, createPxReplace(opts, opts.landscapeUnit, opts.landscapeWidth)) | ||
}) | ||
) | ||
}) | ||
if (landscapeRule.nodes.length > 0) { | ||
landscapeRules.push(landscapeRule); | ||
landscapeRules.push(landscapeRule) | ||
} | ||
} | ||
if (!validateParams(rule.parent.params, opts.mediaQuery)) return; | ||
if (!validateParams(rule.parent.params, opts.mediaQuery)) return | ||
rule.walkDecls(function(decl, i) { | ||
if (decl.value.indexOf(opts.unitToConvert) === -1) return; | ||
if (!satisfyPropList(decl.prop)) return; | ||
rule.walkDecls(function (decl, i) { | ||
if (decl.value.indexOf(opts.unitToConvert) === -1) return | ||
if (!satisfyPropList(decl.prop)) return | ||
var prev = decl.prev(); | ||
var prev = decl.prev() | ||
// prev declaration is ignore conversion comment at same line | ||
if (prev && prev.type === 'comment' && prev.text === ignoreNextComment) { | ||
// remove comment | ||
prev.remove(); | ||
return; | ||
prev.remove() | ||
return | ||
} | ||
var next = decl.next(); | ||
var next = decl.next() | ||
// next declaration is ignore conversion comment at same line | ||
if (next && next.type === 'comment' && next.text === ignorePrevComment) { | ||
if (/\n/.test(next.raws.before)) { | ||
result.warn('Unexpected comment /* ' + ignorePrevComment + ' */ must be after declaration at same line.', { node: next }); | ||
result.warn('Unexpected comment /* ' + ignorePrevComment + ' */ must be after declaration at same line.', { | ||
node: next | ||
}) | ||
} else { | ||
// remove comment | ||
next.remove(); | ||
return; | ||
next.remove() | ||
return | ||
} | ||
} | ||
var unit; | ||
var size; | ||
var params = rule.parent.params; | ||
var unit | ||
var size | ||
var params = rule.parent.params | ||
if (opts.landscape && params && params.indexOf('landscape') !== -1) { | ||
unit = opts.landscapeUnit; | ||
size = opts.landscapeWidth; | ||
unit = opts.landscapeUnit | ||
size = opts.landscapeWidth | ||
} else { | ||
unit = getUnit(decl.prop, opts); | ||
size = opts.viewportWidth; | ||
unit = getUnit(decl.prop, opts) | ||
size = opts.viewportWidth | ||
} | ||
var value = decl.value.replace(pxRegex, createPxReplace(opts, unit, size)); | ||
var value = decl.value.replace(pxRegex, createPxReplace(opts, unit, size)) | ||
if (declarationExists(decl.parent, decl.prop, value)) return; | ||
if (declarationExists(decl.parent, decl.prop, value)) return | ||
if (opts.replace) { | ||
decl.value = value; | ||
decl.value = value | ||
} else { | ||
decl.parent.insertAfter(i, decl.clone({ value: value })); | ||
decl.parent.insertAfter(i, decl.clone({ value: value })) | ||
} | ||
}); | ||
}); | ||
}) | ||
}) | ||
if (landscapeRules.length > 0) { | ||
var landscapeRoot = new postcss.atRule({ params: '(orientation: landscape)', name: 'media' }); | ||
var landscapeRoot = new postcss.atRule({ params: '(orientation: landscape)', name: 'media' }) | ||
landscapeRules.forEach(function(rule) { | ||
landscapeRoot.append(rule); | ||
}); | ||
css.append(landscapeRoot); | ||
landscapeRules.forEach(function (rule) { | ||
landscapeRoot.append(rule) | ||
}) | ||
css.append(landscapeRoot) | ||
} | ||
}; | ||
}); | ||
} | ||
}) | ||
function getUnit(prop, opts) { | ||
return prop.indexOf('font') === -1 ? opts.viewportUnit : opts.fontViewportUnit; | ||
return prop.indexOf('font') === -1 ? opts.viewportUnit : opts.fontViewportUnit | ||
} | ||
@@ -158,29 +162,29 @@ | ||
return function (m, $1) { | ||
if (!$1) return m; | ||
var pixels = parseFloat($1); | ||
if (pixels <= opts.minPixelValue) return m; | ||
var parsedVal = toFixed((pixels / viewportSize * 100), opts.unitPrecision); | ||
return parsedVal === 0 ? '0' : parsedVal + viewportUnit; | ||
}; | ||
if (!$1) return m | ||
var pixels = parseFloat($1) | ||
if (pixels <= opts.minPixelValue) return m | ||
var parsedVal = toFixed((pixels / viewportSize) * 100, opts.unitPrecision) | ||
return parsedVal === 0 ? '0' : parsedVal + viewportUnit | ||
} | ||
} | ||
function error(decl, message) { | ||
throw decl.error(message, { plugin: 'postcss-px-to-viewport' }); | ||
throw decl.error(message, { plugin: 'postcss-px-to-viewport-tebie6' }) | ||
} | ||
function checkRegExpOrArray(options, optionName) { | ||
var option = options[optionName]; | ||
if (!option) return; | ||
if (Object.prototype.toString.call(option) === '[object RegExp]') return; | ||
var option = options[optionName] | ||
if (!option) return | ||
if (Object.prototype.toString.call(option) === '[object RegExp]') return | ||
if (Object.prototype.toString.call(option) === '[object Array]') { | ||
var bad = false; | ||
var bad = false | ||
for (var i = 0; i < option.length; i++) { | ||
if (Object.prototype.toString.call(option[i]) !== '[object RegExp]') { | ||
bad = true; | ||
break; | ||
bad = true | ||
break | ||
} | ||
} | ||
if (!bad) return; | ||
if (!bad) return | ||
} | ||
throw new Error('options.' + optionName + ' should be RegExp or Array of RegExp.'); | ||
throw new Error('options.' + optionName + ' should be RegExp or Array of RegExp.') | ||
} | ||
@@ -190,12 +194,12 @@ | ||
var multiplier = Math.pow(10, precision + 1), | ||
wholeNumber = Math.floor(number * multiplier); | ||
return Math.round(wholeNumber / 10) * 10 / multiplier; | ||
wholeNumber = Math.floor(number * multiplier) | ||
return (Math.round(wholeNumber / 10) * 10) / multiplier | ||
} | ||
function blacklistedSelector(blacklist, selector) { | ||
if (typeof selector !== 'string') return; | ||
if (typeof selector !== 'string') return | ||
return blacklist.some(function (regex) { | ||
if (typeof regex === 'string') return selector.indexOf(regex) !== -1; | ||
return selector.match(regex); | ||
}); | ||
if (typeof regex === 'string') return selector.indexOf(regex) !== -1 | ||
return selector.match(regex) | ||
}) | ||
} | ||
@@ -205,8 +209,8 @@ | ||
return decls.some(function (decl) { | ||
return (decl.prop === prop && decl.value === value); | ||
}); | ||
return decl.prop === prop && decl.value === value | ||
}) | ||
} | ||
function validateParams(params, mediaQuery) { | ||
return !params || (params && mediaQuery); | ||
return !params || (params && mediaQuery) | ||
} |
{ | ||
"name": "postcss-px-to-viewport-tebie6", | ||
"description": "A CSS post-processor that converts px to viewport units (vw, vh, vmin, vmax).", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"author": "Ted <liumingyphp@163.com>", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -77,7 +77,7 @@ # postcss-px-to-viewport | ||
``` | ||
$ npm install postcss-px-to-viewport --save-dev | ||
$ npm install postcss-px-to-viewport-tebie6 --save-dev | ||
``` | ||
or yarn | ||
``` | ||
$ yarn add -D postcss-px-to-viewport | ||
$ yarn add -D postcss-px-to-viewport-tebi6 | ||
``` | ||
@@ -227,3 +227,3 @@ | ||
We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/evrone/postcss-px-to-viewport/tags). | ||
We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/tebie6/postcss-px-to-viewport/tags). | ||
@@ -235,3 +235,3 @@ ## Authors | ||
See also the list of [contributors](https://github.com/evrone/postcss-px-to-viewport/contributors) who participated in this project. | ||
See also the list of [contributors](https://github.com/tebie6/postcss-px-to-viewport/contributors) who participated in this project. | ||
@@ -238,0 +238,0 @@ ## License |
407
34749