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

v0.1.3

Published

PostCSS plugin apx2units-plus

Downloads

10

Readme

PostCSS Px2units Plus

将 px 单位转换为 rpx 单位,或者其他单位的PostCSS 插件。

基于postcss-px2units 增强

  • 增加指定选择器转换
  • 增加指定属性转换
  • 增加规则注释

Install

npm install --save-dev postcss postcss-px2units-plus

在postcs配置文件中添加以下内容postcs.config.js

module.exports = {
  plugins: [
   require('postcss-px2units-plus')({})
  ]
}

options

{
    divisor: 1,
    multiple: 1,
    decimalPlaces: 2,
    targetUnits: 'rpx',
    selector: null,
    onlyAttr: [],
    enableAllComment: 'px2units-enable',
    disableAllComment: 'px2units-disable',
    comment: 'no'
  }

Usage

Input/Output

如果使用 默认的 opts,将会得到如下的输出。

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

/* output */
p {
  margin: 0 0 20rpx;
  font-size: 32rpx;
  line-height: 1.2;
  letter-spacing: 1px;
}

配置

Type: Object | Null

Default:

{
    /**
     * 除数 int 默认值:1
     * 把像素相除  100 / divisor * multiple
     * width:20px / 1  = width:20px
     */
    divisor: 1,

    /**
     * 倍数 int
     * 把像素相乘  100 / divisor * multiple
     * width:20px * 1  = width:20px
     */
    multiple: 1,

    /**
     * 小数点后保留的位数 int 默认值:rpx
     * Number(100 / divisor * multiple).toFixed(decimalPlaces)
     * width:20.001px * 1  = width:20.00px
     */
    decimalPlaces: 2,

    /**
     * 转换单位 string 默认值:rpx
     * 设置其值为 'rem',px将会被转换为rem。
     */
    targetUnits: 'rpx',

    /**
     * 限定选择器 string|RegExp
     * 仅转换指定选择器样式
     * 设置值: .my-box  只转换.my-box
     * 设置值: /\.my-b/  转换.my-box,.my-item ,.my-title等
     * 设置值: /title$/  转换.my-title,.u-title$ ,.title等
     */
    selector: null,

    /**
     * 属性限定 array|RegExp
     * 仅转换指定属性,为空表示所有
     * 设置值: ['width']  只转换width
     * 设置值: ['width','font-size']  只转换width,font-size
     */
    onlyAttr: [],

    /**
     * 启用转换注释,属性限定单独全部转换
     * 写在每条css规则前面
     * 如属性限定设置值: ['width']
     * 转换规则
     * ps:本注释\为转义符,实际写过程中不用包含
     * /* px2units-enable *\/
     * .title{width:30px;font-size:20px}
     * 转换后
     * .title{width:30rpx;font-size:20rpx}
     */
    enableAllComment: 'px2units-enable',

    /**
     * 禁用转换注释,任何情况下,有该注释不转换
     * 写在每条css规则前面
     * 转换规则
     * ps:本注释\为转义符,实际写过程中不用包含
     * /* px2units-disable *\/
     * .title{width:30px;font-size:20px}
     * 转换后
     * .title{width:30px;font-size:20px}
     */
    disableAllComment: 'px2units-disable',

    /**
     * 单条css样式不转换注释  默认为 /*no*\/
     * ps:本注释\为转义符,实际写过程中不用包含
     * .title{
     *  width:30px; /*no*\/
     *  font-size:20px
     * }
     *  .title{
     *  width:30px; /*no*\/
     *  font-size:20rpx
     * }
     */
    comment: 'no'
  }

Example

双倍转换 将px 转换为2倍rpx

module.exports = {
  plugins: [
   require('postcss-px2units-plus')({
    multiple: 2
   })
  ]
}
/* input */
p {
  margin:20px;
}

/* output */
p {
  margin: 40rpx;
}

限定选择器 只转换指定前缀选择器

module.exports = {
  plugins: [
  require('postcss-px2units-plus')({
     multiple: 2,
    selector: /\.my-/
   })
  ]
}
/* input */
.my-title {
  margin:20px;
}

.u-title {
  margin:20px;
}


/* output */
.my-title {
  margin:40rpx;
}

.u-title {
  margin:20px;
}

限定属性 限定选择器+限定属性

module.exports = {
  plugins: [
   require('postcss-px2units-plus')({
     multiple: 2,
    selector: /\.my-/,
    onlyAttr: ['padding'], //支持正则,数组,字符串
   })
  ]
}
/* input */
.my-title {
     padding: 30px;
     margin: 40px;
}

.u-title {
    padding: 30px;
    margin: 40px;
}

/* output */
.my-title {
    padding: 60px;  /* 只改变这个值 */
    margin: 40px;
}

.u-title {
   padding: 30px;
   margin: 40px;
}

独立注释 限定选择器配合限定属性时,可通过注释单独指定规则

module.exports = {
  plugins: [
   require('postcss-px2units-plus')({
     multiple: 2,
    selector: /\.my-/,
    onlyAttr: ['padding'], //支持正则,数组,字符串
   })
  ]
}
/* input */
.my-title {
     padding: 30px;
     margin: 40px;
}

.my-title2 {
    padding: 30px;
    margin: 40px;
}

/* output */

/* 开启全部属性转换, 开启注释要放到最后 */
/*px2units-enable*/
.my-title {
    padding: 60px;
    margin: 80px;
}

.my-title2 {
   padding: 60px;
   margin: 40px;
}