npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

postcss-convertpx

v0.1.1

Published

convert px to viewport unit

Downloads

2

Readme

postcss-convertpx

Travis (.com) branch codecov GitHub npm (tag)

forked from postcss-px-to-viewport.

requirement

Must set this plugin after postcss-nested.

Usage

If your project involves a fixed width, this script will help to convert pixels into viewport units.

Input/Output

// input

div {
	width: 20px;
	height: 30px;
	color: red;
	font-size: 18px;
	font: bold 75px/7.5px;
	line-height: 20px;
	border: 1px solid #000;
}
div::before {
	content: "75px";
}
@media (max-width: 320px) {
	div {
		width: 40px;
		font-size: 20px;
		color: blue;
	}
}

// output

div {
	width: 2.66667vw;
	height: 4vw;
	color: red;
	font-size: 18px;
	font: bold 75px/1vw;
	line-height: 2.66667vw;
	border: 1px solid #000;
}
div::before {
	content: "75px";
}
@media (max-width: 320px) {
	div {
		width: 5.33333vw;
		font-size: 20px;
		color: blue;
	}
}

Example

'use strict';
const postcss = require('postcss');
const p2v = require('postcss-convertpx');
const fs = require('fs');

const css = fs.readFileSync('./example/demo.css', 'utf8');

postcss(p2v({
    exclude(file) {
		return /node_modules/.test(file);
    }
})).process(css, {
	from: './example/demo.css'
}).then(rst => {
	console.log(rst.css);
});

Options

Default:

{
	viewportWidth: 750,
	unitPrecision: 5,
	viewportUnit: 'vw',
	convertFont: false,
	minPixelValue: 1,
	mediaQuery: false,
	convertLineHeight: true,
	declarationBlackList: undefined,
	scale: undefined,
	exclude: undefined
}
  • viewportWidth (Number) The width of the viewport.
  • unitPrecision (Number) The decimal numbers to allow the REM units to grow to.
  • viewportUnit (String) Expected units.
  • convertFont (Boolean) Allow px to be converted in font/font-size.
  • minPixelValue (Number) Set the minimum pixel value to replace.
  • mediaQuery (Boolean) Allow px to be converted in media queries.
  • convertLineHeight (Boolean) Allow px to be converted in font/line-height.
  • declarationBlackList (string[]) The declarations to ignore and leave as px. Note, if use short-hand like font will only ignore font not font-size, so if you want ignore font and font-size, you should specify both like this declarationBlackList: ['font', 'font-size'].
  • scale (Number) for vw will calculate automatic according to viewportWidth. for other unit such as rem, you can specify scale like scale: 75, then 1rem = 75px.
  • exclude(filepath) (Function) You can exclude some file by specify this option, which returns a boolean, if true will be excluded.

Skip a declaration

input

div {
	width: 20px;/* skip-p2v */
	height: 30px;
	color: red;
	font-size: 18px;
	line-height: 20px;
}
@media (max-width: 320px) {
	div {
		width: 40px;
		font-size: 20px;
		color: blue;
	}
}

output

div {
	width: 20px;/* skip-p2v */
	height: 4vw;
	color: red;
	font-size: 18px;
	line-height: 2.66667vw;
}
@media (max-width: 320px) {
	div {
		width: 5.33333vw;
		font-size: 20px;
		color: blue;
	}
}

width will be ignored.

Skip a rule

input

/* skip-p2v */
div {
	width: 20px;
	height: 30px;
	color: red;
	font-size: 18px;
	line-height: 20px;
}
@media (max-width: 320px) {
	div {
		width: 40px;
		font-size: 20px;
		color: blue;
	}
}

output

/* skip-p2v */
div {
	width: 20px;
	height: 30px;
	color: red;
	font-size: 18px;
	line-height: 20px;
}
@media (max-width: 320px) {
	div {
		width: 5.33333vw;
		font-size: 20px;
		color: blue;
	}
}

The rule div will be ignored.

Skip a media query condition

input

div {
	width: 20px;
	height: 30px;
	color: red;
	font-size: 18px;
	line-height: 20px;
}
/* skip-p2v */
@media (max-width: 320px) {
	div {
		width: 40px;
		font-size: 20px;
		color: blue;
	}
}

output

div {
	width: 2.66667vw;
	height: 4vw;
	color: red;
	font-size: 18px;
	line-height: 2.66667vw;
}
/* skip-p2v */
@media (max-width: 320px) {
	div {
		width: 5.33333vw;
		font-size: 20px;
		color: blue;
	}
}

By default, will not convert media query condition, but if you specify mediaQuery: true, this comment would be useful.