Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
4,230 additions
and
5,263 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"extends": [ | ||
"mito" | ||
], | ||
"parserOptions": { | ||
"ecmaVersion": 2018 | ||
}, | ||
"rules": { | ||
"indent": [2, 2, { | ||
"SwitchCase": 1 | ||
}], | ||
"space-before-function-paren": ["error", { | ||
"anonymous": "never", | ||
"named": "never", | ||
"asyncArrow": "always" | ||
}], | ||
'newline-per-chained-call': ['off'] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"hooks": { | ||
"pre-commit": "npm test" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,17 @@ | ||
'use strict'; | ||
|
||
var fs = require('fs'); | ||
var px2rem = require('../index.js'); | ||
var css = fs.readFileSync('main.css', 'utf8'); | ||
var processedCSS = px2rem.process(css, { | ||
mediaQuery: true | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const px2rem = require('../index.js'); | ||
const css = fs.readFileSync(path.join(__dirname, 'main.css'), 'utf8'); | ||
const processedCSS = px2rem.process(css, { | ||
mediaQuery: true | ||
}); | ||
|
||
fs.writeFile('main-rem.css', processedCSS, function(err) { | ||
if (err) { | ||
throw err; | ||
} | ||
fs.writeFile(path.join(__dirname, 'main-rem.css'), processedCSS, (err) => { | ||
if (err) { | ||
throw err; | ||
} | ||
|
||
console.log('Done.'); | ||
console.log('Done.'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,124 +1,115 @@ | ||
'use strict'; | ||
|
||
var postCSS = require('postcss'); | ||
var extend = require('extend'); | ||
|
||
var pxRegExp = /(\d*\.?\d+)px/ig; | ||
|
||
var defaults = { | ||
rootValue: 16, | ||
unitPrecision: 5, | ||
propertyBlackList: [], | ||
propertyWhiteList: [], | ||
replace: false, | ||
mediaQuery: false, | ||
minPx: 1 | ||
const postCSS = require('postcss'); | ||
const extend = require('extend'); | ||
|
||
const pxRegExp = /(\d*\.?\d+)px/ig; | ||
|
||
const defaults = { | ||
rootValue: 16, | ||
unitPrecision: 5, | ||
propertyBlackList: [], | ||
propertyWhiteList: [], | ||
replace: false, | ||
mediaQuery: false, | ||
minPx: 1 | ||
}; | ||
|
||
var o; | ||
|
||
var px2rem; | ||
let o; | ||
|
||
function toPx(value) { | ||
var parts = /^(\d+\.?\d*)([a-zA-Z%]*)$/.exec(value); | ||
var number = parts[1]; | ||
var unit = parts[2]; | ||
|
||
if (unit === 'px' || unit === '') { | ||
return parseFloat(number); | ||
} else if (unit === 'em' || unit === 'rem') { | ||
return parseFloat(number) * 16; | ||
} else if (unit === '%') { | ||
return (parseFloat(number) / 100) * 16; | ||
} | ||
|
||
return 1; | ||
const parts = /^(\d+\.?\d*)([a-zA-Z%]*)$/.exec(value); | ||
const [, number, unit] = parts; | ||
|
||
if (unit === 'px' || unit === '') { | ||
return parseFloat(number); | ||
} else if (unit === 'em' || unit === 'rem') { | ||
return parseFloat(number) * 16; | ||
} else if (unit === '%') { | ||
return (parseFloat(number) / 100) * 16; | ||
} | ||
|
||
return 1; | ||
} | ||
|
||
function toFixed(number, precision) { | ||
var multiplier = Math.pow(10, precision + 1); | ||
var wholeNumber = Math.floor(number * multiplier); | ||
const multiplier = Math.pow(10, precision + 1); | ||
const wholeNumber = Math.floor(number * multiplier); | ||
|
||
return Math.round(wholeNumber / 10) * 10 / multiplier; | ||
return Math.round(wholeNumber / 10) * 10 / multiplier; | ||
} | ||
|
||
function pxReplace(strArg) { | ||
var str = parseFloat(strArg); | ||
const str = parseFloat(strArg); | ||
|
||
if (o.minPx >= str) { | ||
return str + 'px'; | ||
} | ||
if (o.minPx >= str) { | ||
return `${str}px`; | ||
} | ||
|
||
return toFixed(str / toPx(o.rootValue), o.unitPrecision) + 'rem'; | ||
return `${toFixed(str / toPx(o.rootValue), o.unitPrecision)}rem`; | ||
} | ||
|
||
function equals(decls, prop, value) { | ||
return decls.some(function(decl) { | ||
return (decl.prop === prop && decl.value === value); | ||
}); | ||
return decls.some(decl => (decl.prop === prop && decl.value === value)); | ||
} | ||
|
||
function Px2Rem(options) { | ||
o = extend(true, {}, defaults, options); | ||
o = extend(true, {}, defaults, options); | ||
} | ||
|
||
Px2Rem.prototype.process = function(css, options) { | ||
return postCSS(this.postCSS).process(css, options).css; | ||
return postCSS(this.postCSS).process(css, options).css; | ||
}; | ||
|
||
Px2Rem.prototype.postCSS = function(css) { | ||
css.walkDecls(function(decl, i) { | ||
var rule; | ||
var value; | ||
|
||
if (o.propertyBlackList.indexOf(decl.prop) !== -1) { | ||
return; | ||
} | ||
|
||
if (o.propertyWhiteList.length > 0 && | ||
o.propertyWhiteList.indexOf(decl.prop) === -1) { | ||
return; | ||
} | ||
|
||
rule = decl.parent; | ||
value = decl.value; | ||
|
||
if (value.indexOf('px') !== -1) { | ||
value = value.replace(pxRegExp, pxReplace); | ||
|
||
if (equals(rule.nodes, decl.prop, value)) { | ||
return; | ||
} | ||
|
||
if (o.replace) { | ||
decl.value = value; | ||
} else { | ||
rule.insertAfter(i, decl.clone({ | ||
value: value | ||
})); | ||
} | ||
} | ||
}); | ||
css.walkDecls((decl, i) => { | ||
if (o.propertyBlackList.indexOf(decl.prop) !== -1) { | ||
return; | ||
} | ||
|
||
if (o.mediaQuery) { | ||
css.each(function(rule) { | ||
if (rule.type !== 'atrule' && rule.name !== 'media') { | ||
return; | ||
} | ||
if (o.propertyWhiteList.length > 0 && o.propertyWhiteList.indexOf(decl.prop) === -1) { | ||
return; | ||
} | ||
|
||
const rule = decl.parent; | ||
let { value } = decl; | ||
|
||
if (value.indexOf('px') !== -1) { | ||
value = value.replace(pxRegExp, pxReplace); | ||
|
||
if (equals(rule.nodes, decl.prop, value)) { | ||
return; | ||
} | ||
|
||
if (rule.params.indexOf('px') !== -1) { | ||
rule.params = rule.params.replace(pxRegExp, pxReplace); | ||
} | ||
}); | ||
if (o.replace) { | ||
decl.value = value; | ||
} else { | ||
rule.insertAfter(i, decl.clone({ | ||
value | ||
})); | ||
} | ||
} | ||
}); | ||
|
||
if (o.mediaQuery) { | ||
css.each((rule) => { | ||
if (rule.type !== 'atrule' && rule.name !== 'media') { | ||
return; | ||
} | ||
|
||
if (rule.params.indexOf('px') !== -1) { | ||
rule.params = rule.params.replace(pxRegExp, pxReplace); | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
px2rem = function(options) { | ||
return new Px2Rem(options); | ||
const px2rem = function(options) { | ||
return new Px2Rem(options); | ||
}; | ||
|
||
px2rem.process = function(css, options, postCSSOptions) { | ||
return new Px2Rem(options).process(css, postCSSOptions); | ||
return new Px2Rem(options).process(css, postCSSOptions); | ||
}; | ||
|
||
module.exports = px2rem; |
Oops, something went wrong.