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-rem2px-yuze

v1.0.15

Published

Convert rem units to px or rpx units using PostCSS (JavaScript version with include feature)

Downloads

42

Readme

postcss-rem-to-responsive-pixel (JavaScript版本)

一个用于将CSS中的rem单位转换为px或rpx单位的PostCSS插件,支持include/exclude功能。

特性

  • ✅ 将rem单位转换为px或rpx单位
  • ✅ 支持include功能:指定需要处理的文件或文件夹
  • ✅ 支持exclude功能:排除不需要处理的文件
  • ✅ 支持小程序rpx单位
  • ✅ 灵活的配置选项
  • ✅ 媒体查询支持
  • ✅ 选择器黑名单
  • ✅ 属性白名单

安装

npm install postcss-rem-to-responsive-pixel-js --save-dev

使用方法

基础用法

// postcss.config.js
module.exports = {
  plugins: [
    require('postcss-rem-to-responsive-pixel-js')({
      rootValue: 16,
      propList: ['*'],
      transformUnit: 'px',
    }),
  ],
}

小程序开发

// postcss.config.js
module.exports = {
  plugins: [
    require('tailwindcss'),
    require('postcss-rem-to-responsive-pixel-js')({
      rootValue: 32,
      propList: ['*'],
      transformUnit: 'rpx',
      include: ['src/**/*.css', 'src/**/*.scss'],
      exclude: [/node_modules/],
    }),
  ],
}

配置选项

const defaultOptions = {
  rootValue: 16,                    // 根字体大小
  unitPrecision: 5,                 // 数值精度
  selectorBlackList: [],            // 选择器黑名单
  propList: ['font', 'font-size', 'line-height', 'letter-spacing'],  // 处理的属性
  replace: true,                    // 是否替换原值
  mediaQuery: false,                // 是否处理媒体查询
  minRemValue: 0,                   // 最小转换值
  exclude: [/node_modules/i],       // 排除文件
  include: [],                      // 包含文件(新功能)
  transformUnit: 'px',              // 输出单位 'px' | 'rpx'
  disabled: false,                  // 是否禁用
}

include选项详解

include选项支持多种格式:

1. 字符串数组(文件路径匹配)

{
  include: ['src/components', 'src/pages', 'index.css']
}

2. 正则表达式数组

{
  include: [/\.css$/, /\.scss$/, /components/]
}

3. 函数

{
  include: (filePath) => {
    return filePath.includes('components') || filePath.endsWith('.module.css')
  }
}

4. 混合使用

{
  include: ['src/components', /\.module\.css$/, (path) => path.includes('theme')]
}

exclude优先级

当同时设置includeexclude时,exclude具有更高的优先级:

{
  include: ['src/**/*.css'],
  exclude: ['src/vendor/**/*.css']  // 这些文件不会被处理
}

使用示例

输入

h1 {
  margin: 0 0 20px;
  font-size: 2rem;
  line-height: 1.2;
  letter-spacing: 0.0625rem;
}

输出(transformUnit: 'px')

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

输出(transformUnit: 'rpx')

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

忽略转换

使用大写的REMRem可以忽略转换:

.ignore {
  font-size: 1REM;  /* 不会被转换 */
  margin: 2rem;     /* 会被转换 */
}

文件结构

├── index.js      # 主入口文件
├── shared.js     # 共享工具函数
├── defaults.js   # 默认配置
├── regex.js      # 正则表达式
└── README.md     # 使用说明

API

主要函数

  • postcssRemToResponsivePixel(options) - 创建插件实例
  • getConfig(options) - 合并配置选项
  • createIncludeMatcher(include) - 创建文件包含匹配器
  • createExcludeMatcher(exclude) - 创建文件排除匹配器

许可证

MIT