├── .gitignore ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *~ 3 | 4 | node_modules/ 5 | npm-debug.log -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # postcss-px-to-vw 2 | A plugin for PostCSS that converts pixel units to vw unit. 3 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var postcss = require('postcss'); 2 | 3 | // !singlequotes|!doublequotes|!url()|pixelunit 4 | var pxRegex = /"[^"]+"|'[^']+'|url\([^\)]+\)|(\d*\.?\d+)px/g; 5 | 6 | var Default = { 7 | vwUnit: 360 8 | } 9 | 10 | module.exports = postcss.plugin('postcss-px-to-vw', function (options) { 11 | return function (css) { 12 | options = options || {}; 13 | var opts = Object.assign({}, Default, options); 14 | 15 | css.walkRules(function (rule) { 16 | rule.walkDecls(function(decl) { 17 | 18 | const annotation = decl.next(); 19 | if ( annotation && annotation.type == 'comment' && annotation.text == 'px') return; 20 | 21 | if (decl.value.indexOf('px') != -1) { 22 | decl.value = decl.value.replace(pxRegex, function (pxSize) { 23 | var num = parseInt(pxSize); 24 | var vwNum = num * 100 / opts.vwUnit; 25 | return vwNum + 'vw'; 26 | }); 27 | } 28 | }) 29 | 30 | }); 31 | } 32 | }); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "postcss-px-to-vh", 3 | "version": "1.0.0", 4 | "description": "A plugin for PostCSS that converts pixel units to vh units", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "node index.js" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/Cruyun/postcss-px-to-vh.git" 12 | }, 13 | "author": "", 14 | "license": "ISC", 15 | "bugs": { 16 | "url": "https://github.com/Cruyun/postcss-px-to-vh/issues" 17 | }, 18 | "homepage": "https://github.com/Cruyun/postcss-px-to-vh#readme", 19 | "keywords": [ 20 | "css", 21 | "px", 22 | "viewport", 23 | "vh", 24 | "postcss", 25 | "postcss-plugin" 26 | ], 27 | "dependencies": { 28 | "postcss": "^6.0.16" 29 | } 30 | } 31 | --------------------------------------------------------------------------------