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-plugin-px2units

v1.0.2

Published

A CSS post-processor that converts px to viewport units (vw, vh, rem).

Downloads

14

Readme

postcss-plugin-px2units

NPM version

A plugin for PostCSS that generates viewport units (vw/wh/rem) from pixel units.

Features

Use the Flex layout, Grid layout, viewport units rem ,vw and wh to adapt to multi-screen problem

Installation

$ npm i --save postcss-plugin-px2units

Usage

input and output

//option
{
      viewportWidth: 3840, //  3840
      viewportHeight: 2304, // 2304
    }
// input
html { font-size: 100wx;}//1:100 rem
/* 100px*100/1920 => vw */
body{
    padding: 0;
    margin: 0;
    display: flex;
    height: 100vh;
    flex-direction: column;
    justify-content: space-around;
    background: yellow;
    text-align: center;
}
div{
    border: 1px solid black;  
}
.top{
    height: 217hx;
    font-size: 84rx;/*/设计稿字体大小*/
}
.bottom{
    display: grid;
    grid-template-columns: 930fr 1768fr 930fr; /* //设计稿宽度 */
    font-size: 84wx;/*/设计稿字体大小*/
}
.left div,.right div,.center div{
    margin: 40wx;
}
.bottom {
    margin:80wx;
    flex: 1;
}
.left{
    display: grid;
    grid-template-rows: repeat(3, 640fr);
}

.center{
    display: grid;
    grid-template-rows: 1fr auto;
}

.right{
    display: grid;
    grid-template-rows: repeat(2, 984fr);
}


// output
html { font-size: 2.60417vw;}
/* 100/1920 */
body{
    padding: 0;
    margin: 0;
    display: flex;
    height: 100vh;
    flex-direction: column;
    justify-content: space-around;
    background: yellow;
    text-align: center;
    
}
div{
    border: 1px solid black;  
}
.top{
    height: 9.4184vh;
    font-size: 0.84rem;
}
.bottom{
    display: grid;
    grid-template-columns: 930fr 1768fr 930fr; /* //设计稿宽度 */
    font-size: 2.1875vw;
}
.left div,.right div,.center div{
    margin: 1.04167vw;
}
.bottom {
    margin:2.08333vw;
    flex: 1;
}
.left{
    display: grid;
    grid-template-rows: repeat(3, 640fr);
}

.center{
    display: grid;
    grid-template-rows: 1fr auto;
}

.right{
    display: grid;
    grid-template-rows: repeat(2, 984fr);
}

original

import { writeFile, readFileSync } from 'fs';
import postcss from 'postcss';
import px2units from 'postcss-plugin-px2units';

const css = readFileSync('/path/to/test.css', 'utf8');
const options = {
  replace: false,
};
const processedCss = postcss(px2units(options)).process(css).css;

writeFile('/path/to/test.rem.css', processedCss, err => {
  if (err) throw err;
  console.log('Rem file written.');
});

with gulp

var gulp = require('gulp');
var postcss = require('gulp-postcss');
var px2units = require('postcss-plugin-px2units');
gulp.task('css', function () {
  var processors = [
    px2units
  ];
  return gulp.src('./src/*.css')
    .pipe(postcss(processors))
    .pipe(gulp.dest('./dest'));
});

with webpack

import px2units from 'postcss-plugin-px2units';
const px2unitsOpts = {
  ......
};
 
export default {
  module: {
    loaders: [
      {
        test: /\.css$/,
        loader: 'style-loader!css-loader!postcss-loader',
      },
    ],
  },
  postcss: [px2units(px2unitsOpts)],
}

with atool-build

webpack.connfig.js

import webpack from 'atool-build/lib/webpack';
import px2units from 'postcss-plugin-px2units';

export default webpackConfig => {
  const px2unitsOpts = {
    ......
  };
  webpackConfig.postcss.push(px2units(px2unitsOpts));

  return webpackConfig;
};

Configuration

Default:

{
    unitToConvert: {rem:"rx",vw:"wx",vh:"hx"},
    viewportWidth: 1920, 
    viewportHeight: 1080,
    rootValue:100,
    viewportwidthUnit:'vw',
    viewportheightUnit: 'vh', 
    viewportfontUnit: 'rem', 
    unitPrecision: 5,
    minPixelValue: 1,
    exclude:false,
    mediaQuery: false,
    replace:true
}
  • unitToConvert (Object) unit lits to convert, by default, it is {rem:"rx",vw:"wx",vh:"hx"}
  • viewportWidth (Number) The width of the viewport.Default is 19.20. -viewportWidth = the width of UI design
  • viewportHeight (Number) The height of the viewport.Default is 10.80.-viewportHeight = the height of UI design
  • rootValue (Number) The root element font size. Default is 100. {html:100wx=100px*100/1920vw}
  • viewportwidthUnit (Number) a way to exclude some folder,eg. /(node_module)/.
  • viewportheightUnit (Number) Expected height units.
  • unitPrecision (Boolean/String) The decimal numbers to allow the REM or VW OR VH units to grow to.
  • minPixelValue (Number) Set the minimum pixel value to replace.
  • exclude (Boolean|Reg) a way to exclude some folder,eg. /(node_module)/.
  • mediaQuery (Boolean) Allow px to be converted in media queries.
  • replace (Boolean) replaces rules containing rems instead of adding fallbacks.

License

Thanks postcss-plugin-pxtorem and postcss-px-to-viewport