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-px2rem-ts

v1.0.2

Published

Ts rewrite postcss-plugin-px2rem and custom for different files

Downloads

10

Readme

postcss-plugin-px2rem-ts

NPM version

Features

ts rewrite the postcss-plugin-px2rem and Add the functional options;

Installation

$ npm i --save postcss-plugin-px2rem-ts

Usage

input and output

// input
h1 {
  margin: 0 0 20px;
  font-size: 32px;
  line-height: 1.2;
  letter-spacing: 1px;
}

//output
h1 {
  margin: 0 0 0.4rem;
  font-size: 0.64rem;
  line-height: 1.2;
  letter-spacing: 0.02rem;
}

h1 {
  margin: 0 0 0.2rem;
  font-size: 0.32rem;
  line-height: 1.2;
  letter-spacing: 0.01rem;
}

original

import { writeFile, readFileSync } from "fs";
import postcss from "postcss";
import pxtorem from "postcss-plugin-px2rem-ts";

const css = readFileSync("/path/to/test.css", "utf8");
const options = {
  rootValue: (root) => {
    return root.source.input.file.includes("test") ? 50 : 100;
  }
};
const processedCss = postcss(pxtorem(options)).process(css, {
  from: "/path/to/test.css"
}).css;
const processedOtherCss = postcss(pxtorem(options)).process(css, {
  from: "/path/to/other.css"
}).css;

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

with webpack

import px2rem from 'postcss-plugin-px2rem-ts';
const px2remOpts = {
  ......
};

export default {
  module: {
    loaders: [
      {
        test: /\.css$/,
        loader: 'style-loader!css-loader!postcss-loader',
      },
    ],
  },
  postcss: [px2rem(px2remOpts)],
}

with vite

Vite has built-in postcss and postcss load config plugins, allowing PostCSS configuration to be loaded from a separate configuration file.

some times the ui library is design for 375px screen width, but our design is for 750px screen width, so we need to calculate the rootValue according to the file name, then we can set the options below.

postcss.config.js

/* eslint-disable no-undef */
/** @type {require('postcss-load-config').Config} */
const config = {
  plugins: [
    require("autoprefixer"),
    require("postcss-plugin-px2rem-ts")({
      rootValue: (root) => {
        return root.source.input.file.dirname.includes("node_modules/vant")
          ? 50
          : 100;
      }
    })
  ]
};

module.exports = config;

Configuration

Default:

{
  rootValue: 100,
  unitPrecision: 5,
  selectorBlackList: [],
  propWhiteList: [],
  propBlackList: [],
  ignoreIdentifier: false,
  replace: true,
  mediaQuery: false,
  minPixelValue: 0,
  exclude: ''
}

postcss-plugin-px2rem basic configuration like bellow.

  • rootValue (Number|Object) The root element font size. Default is 100.
    • If rootValue is an object, for example { px: 50, rpx: 100 }, it will replace rpx to 1/100 rem , and px to 1/50 rem.
  • unitPrecision (Number) The decimal numbers to allow the REM units to grow to.
  • propWhiteList (Array) The properties that can change from px to rem.
    • Default is an empty array that means disable the white list and enable all properties.
    • Values need to be exact matches.
  • propBlackList (Array) The properties that should not change from px to rem.
    • Values need to be exact matches.
  • exclude (Reg) a way to exclude some folder,eg. /(node_module)/.
  • selectorBlackList (Array) The selectors to ignore and leave as px.
    • If value is string, it checks to see if selector contains the string.
      • ['body'] will match .body-class
    • If value is regexp, it checks to see if the selector matches the regexp.
      • [/^body$/] will match body but not .body
  • ignoreIdentifier (Boolean/String) a way to have a single property ignored, when ignoreIdentifier enabled, then replace would be set to true automatically.
  • replace (Boolean) replaces rules containing rems instead of adding fallbacks.
  • mediaQuery (Boolean) Allow px to be converted in media queries.
  • minPixelValue (Number) Set the minimum pixel value to replace.

in postcss-plugin-px2rem-ts optionsType is FunctionalOptions

type FunctionalExtend<T> = { [Key in keyof T]: T[Key] | ((root: any) => T[Key]) };
type Options = {
  rootValue?: number | Record<string, number>;
  unitPrecision?: number;
  selectorBlackList?: (string | RegExp)[];
  propWhiteList?: (string | RegExp)[];
  propBlackList?: (string | RegExp)[];
  ignoreIdentifier?: boolean | string;
  replace?: boolean;
  mediaQuery?: boolean;
  minPixelValue?: number;
  exclude?: RegExp | string;
}
type FunctionalOptions = FunctionalExtend<Options>

License

MIT