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

pxtowhatever

v1.0.2

Published

根据 [pxtorem](https://github.com/cuth/postcss-pxtorem) 魔改,将 px 改为任意单位模式

Downloads

14

Readme

pxtowhatever

根据 pxtorem 魔改,将 px 改为任意单位模式

安装

$ npm i pxtowhatever

使用方法

pxtorem的基础上去掉了rootValue添加unit参数

unit的形式为<operator><multiplier>[suffix]

举个例子:*2rpx 其中 *-><operator> 2-><multiplier> rpx->[suffix] 其中 operator,multiplier 选填 suffix 必填即目前支持以下四种模式

  • rpx
  • 2rpx
  • *2rpx
  • /2rpx 当使用 pxtorem的时候设置rootValue=16 就相当与设置 unit=/16rem

options

更多配置copypxtorem

Type: Object | Null
Default:

{
    rootValue: 16,
    unitPrecision: 5,
    propList: ['font', 'font-size', 'line-height', 'letter-spacing'],
    selectorBlackList: [],
    replace: true,
    mediaQuery: false,
    minPixelValue: 0,
    exclude: /node_modules/i
}
  • rootValue (Number | Function) Represents the root element font size or returns the root element font size based on the input parameter
  • unitPrecision (Number) The decimal numbers to allow the REM units to grow to.
  • propList (Array) The properties that can change from px to rem.
    • Values need to be exact matches.
    • Use wildcard * to enable all properties. Example: ['*']
    • Use * at the start or end of a word. (['*position*'] will match background-position-y)
    • Use ! to not match a property. Example: ['*', '!letter-spacing']
    • Combine the "not" prefix with the other prefixes. Example: ['*', '!font*']
  • 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
  • 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.
  • exclude (String, Regexp, Function) The file path to ignore and leave as px.
    • If value is string, it checks to see if file path contains the string.
      • 'exclude' will match \project\postcss-pxtorem\exclude\path
    • If value is regexp, it checks to see if file path matches the regexp.
      • /exclude/i will match \project\postcss-pxtorem\exclude\path
    • If value is function, you can use exclude function to return a true and the file will be ignored.
      • the callback will pass the file path as a parameter, it should returns a Boolean result.
      • function (file) { return file.indexOf('exclude') !== -1; }
  • include (String, Regexp, Function) The file path to include.

example

假设你有如下文件 main.css index.js 其输出结果将跟下面一致

main.css

.class {
  font-size: 14px;
  line-height: 20px;
  margin: -10px 0.5em;
  padding: 10px 10px;
  border: 2px solid black;
}

index.js

const fs = require("fs")
const postcss = require("postcss")
const pxtowhatever = require("..")
const path = require("path")
const css = fs.readFileSync(path.join(__dirname, "main.css"), "utf8")

;[
  {
    minPixelValue: 0,
    unit: "rpx",
    propList: ["*"]
  },
  {
    minPixelValue: 0,
    unit: "2rpx",
    propList: ["*"]
  },
  {
    minPixelValue: 0,
    unit: "*2rpx",
    propList: ["*"]
  },
  {
    minPixelValue: 0,
    unit: "/2rpx",
    propList: ["*"]
  }
].forEach(opts => {
  process.stdout.write(postcss(pxtowhatever(opts)).process(css).css + "\n")
})

输出

> cd example && node index.js

.class {
  font-size: 14rpx;
  line-height: 20rpx;
  margin: -10rpx 0.5em;
  padding: 10rpx 10rpx;
  border: 2rpx solid black;
}

.class {
  font-size: 28rpx;
  line-height: 40rpx;
  margin: -20rpx 0.5em;
  padding: 20rpx 20rpx;
  border: 4rpx solid black;
}

.class {
  font-size: 28rpx;
  line-height: 40rpx;
  margin: -20rpx 0.5em;
  padding: 20rpx 20rpx;
  border: 4rpx solid black;
}

.class {
  font-size: 7rpx;
  line-height: 10rpx;
  margin: -5rpx 0.5em;
  padding: 5rpx 5rpx;
  border: 1rpx solid black;
}

webpack demo

const pxtoremSettings = remUnit =>
  pxtowhatever({
    unit: "/2px",
    propList: [
      "*",
      "!letter-spacing",
      "!border",
      "!border-top",
      "!border-left",
      "!border-right",
      "!border-bottom"
    ],
    minPixelValue: 1
  });

 rules: [

   {
        test: /\.(scss|sass)$/,
        use: [
          "style-loader",
          "css-loader?minimize",
          {
            loader: "postcss-loader",
            options: {
              plugins: [pxtoremSettings()]
            }
          },

          {
            loader: "sass-loader"
            // options: {
            //   data: '$brand-primary: #000;',
            // },
          }
        ]
      },
 ]