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 🙏

© 2025 – Pkg Stats / Ryan Hefner

postcss-plugin-px2var

v0.1.2

Published

A plugin for PostCSS that generates var units from multi units.

Downloads

18

Readme

postcss-plugin-px2var

A PostCSS plugin that replaces px values with a CSS Variable-powered scale using calc. It turns 10px into calc(10*var(--your-unit)) so you can control a global sizing scale via a single CSS variable.

Features

  • Converts px values to calc(<px>*var(--your-unit))
  • Skips values already using the configured CSS variable
  • Supports selector and property blacklists
  • Optional inline or comment-based ignore tokens per declaration
  • Ignores px inside quoted strings and url(...)

Installation

npm i postcss-plugin-px2var

Usage (PostCSS API)

const fs = require('fs');
const postcss = require('postcss');
const px2var = require('postcss-plugin-px2var');

const css = fs.readFileSync('src/styles/app.css', 'utf8');

const options = {
  include: /my-project\/(src|styles)/i, // required: only files matching this regex are processed
  cssVariable: '--ke-unit', // required: the CSS variable to multiply by
  cssVariableFallback: (origin, num, unit) => origin, // optional: css var fallback function: (origin, num, unit) => { return origin }
  cssVariableFallbackOrigin: false, // optional: fallback to origin if cssVariableFallback is not set
  replace: true, // replace in-place; if false, add a cloned declaration after
  selectorBlackList: [/^\.no-scale/], // optional: selectors to skip
  propBlackList: ['border-width'], // optional: properties to skip
  ignoreIdentifier: 'no-px', // optional: token to skip individual declarations
};

postcss([px2var(options)])
  .process(css, { from: 'src/styles/app.css', to: 'dist/app.css' })
  .then((result) => {
    fs.writeFileSync('dist/app.css', result.css);
  });

Input / Output

/* input */
h1 {
  margin: 0 0 20px;
  font-size: 32px; /* no-px */
  line-height: 1.2;
  border-width: 1px;
}

/* output (with cssVariable: --ke-unit, propBlackList: ['border-width']) */
h1 {
  margin: 0 0 calc(20 * var(--ke-unit, 32px));
  font-size: 32px; /* no-px */
  line-height: 1.2;
  border-width: 1px;
}

Notes:

  • Declarations with ignoreIdentifier token are skipped. You can also place it as a trailing comment: font-size: 16px; /* no-px */.
  • Declarations or property names that already include cssVariable are skipped.
  • px inside quotes or url(...) is ignored.

Options

{
  include: undefined,        // RegExp (required): processed only if file path matches
  cssVariable: '',           // String (required): e.g. '--ke-unit'
  cssVariableFallback: undefined, // Function (optional): css var fallback function: (origin, num, unit) => { return origin }
  cssVariableFallbackOrigin: false, // Boolean: fallback to origin if cssVariableFallback is not set
  selectorBlackList: [],     // Array<String|RegExp>: selectors to skip
  propBlackList: [],         // Array<String|RegExp>: properties to skip
  ignoreIdentifier: false,   // String|false: token to mark declaration to skip
  replace: true              // Boolean: true to replace, false to insert cloned declaration after
}
  • include RegExp matched against the absolute file path. If not set or doesn't match, no changes are made.
  • cssVariable The CSS variable name used in calc, e.g. --ke-unit.
  • cssVariableFallback Optional fallback function for CSS variable. If not set, origin is returned.
  • cssVariableFallbackOrigin Optional fallback to origin if cssVariableFallback is not set. Defaults to false.
  • selectorBlackList Skip rules whose selector matches any string (substring) or RegExp.
  • propBlackList Skip declarations whose property matches any string (substring) or RegExp.
  • ignoreIdentifier When set to a string token (e.g. 'no-px'), skip declarations that contain this token in the value or have it as the next sibling comment.
  • replace If true, declaration value is replaced. If false, a cloned declaration with the converted value is inserted right after the original (you keep px as fallback).

PostCSS Config / Bundlers

Create postcss.config.js:

module.exports = {
  plugins: [
    require('postcss-plugin-px2var')({
      include: /my-project\/(src|styles)/i,
      cssVariable: '--ke-unit',
      cssVariableFallback: (origin, num, unit) => origin,
      cssVariableFallbackOrigin: false,
      selectorBlackList: [/^\.no-scale/],
      propBlackList: ['border-width'],
      ignoreIdentifier: 'no-px',
      replace: true,
    }),
  ],
};

Example CSS variable definition (global scale):

:root {
  --ke-unit: 0.01rem;
} /* every px becomes px * 0.01rem */

How It Works

  • The plugin scans declaration values for px and replaces them with calc(<number>*var(--your-unit)).
  • It performs several guards: file path must match include, cssVariable must be provided, selector/property blacklists, and ignore token.
  • Text inside quotes and url(...) is intentionally preserved.

License

MIT