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-units-transform

v2.0.0

Published

CSS 任意单位之间的互相转换的 PostCSS 插件。

Downloads

453

Readme

[email protected]

Build Status npm version change-log codecov

CSS 任意单位之间的互相转换的 PostCSS插件。

Required

  • PostCSS >= 8.0

PostCSS < 8.0 请使用 [email protected]

Install

$ npm install postcss-units-transform --save-dev

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;
}

Example

var fs = require('fs');
var postcss = require('postcss');
var pxtorem = require('postcss-pxtorem');
var css = fs.readFileSync('main.css', 'utf8');
var options = {
  replace: false,
};
var processedCss = postcss(pxtorem(options)).process(css).css;

fs.writeFile('main-rem.css', processedCss, function (err) {
  if (err) {
    throw err;
  }
  console.log('Rem file written.');
});

options

Type: Object | Null

Default:

{
  divisor: 1,
  multiple: 1,
  decimalPlaces: 2,
  comment: 'no',
  targetUnits: 'rpx',
  sourceUnits: 'px',
  declMembers: '*'
}

Detail:

  • divisor(Number): 除数,转换后的值 等于 pixel / divisor
  • multiple(Number): 倍数,转换后的值 等于 pixel * multiple
  • decimalPlaces(Number): 小数点后保留的位数,例如, width: 100px 中的 100,将会被转换成 Number(100 / divisor * multiple).toFixed(decimalPlaces)
  • comment(String): 不转换 px 单位的注释,默认为 /*no*/。如果设置 comment 的值为 'not replace', width: 100px; /* not replace */ 中的 100px 将不会被转换为 rpx。
  • sourceUnits(String): 需要转换的单位,默认值为 px
  • targetUnits(String): 转换单位,默认值为 rpx,如果设置其值为 rem,px 将会被转换为 rem。
  • declMembers(String[]): 可转换的属性成员名单。

高级用法

1. 通过全局注释开启转换

优先级高于 options.declMembers 互斥关系

/* units-transform */

/* units-transform */
.main {
  height: 1rem;
  width: 2rem;
  font-size: 0.28rem;
}

2. 通过全局注释指定属性开启转换

优先级高于 options.declMembers 互斥关系

/* units-transform:height,width */

/* units-transform:height,width */
.main {
  height: 1rem;
  width: 2rem;
  font-size: 0.28rem;
}

3. 行内注释开启转换

options.declMembers 并集的关系

.main {
  height: 1rem;
  width: 2rem; /* units-transform */
  font-size: 0.36rem;
}