@alitajs/postcss-plugin-px2rem

中文文档
0.0.1发布于 2021/08/24MITCommonJS近 30 天下载 6979

A plugin for PostCSS that generates rem units from multi units.

原始使用文档
文档目录9 节
  1. Features
  2. Installation
  3. Usage
  4. input and output
  5. original
  6. with webpack
  7. with atool-build
  8. Configuration
  9. License

postcss-plugin-px2rem

NPM version Build Status Coverage Status NPM downloads Dependency Status

postcss plugin px2rem.

Features

A plugin for PostCSS that generates rem units from pixel units.

Installation

$ npm i --save postcss-plugin-px2rem

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.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';

const css = readFileSync('/path/to/test.css', 'utf8');
const options = {
  replace: false,
};
const processedCss = postcss(pxtorem(options)).process(css).css;

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

with webpack

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

with atool-build

webpack.connfig.js

import webpack from 'atool-build/lib/webpack';
import px2rem from 'postcss-plugin-px2rem';

export default webpackConfig => {
  const px2remOpts = {
    ......
  };
  webpackConfig.postcss.push(px2rem(px2remOpts));

  return webpackConfig;
};

Configuration

Default:

{
  rootValue: 100,
  unitPrecision: 5,
  propWhiteList: [],
  propBlackList: [],
  exclude:false,
  selectorBlackList: [],
  ignoreIdentifier: false,
  replace: true,
  mediaQuery: false,
  minPixelValue: 0
}
  • 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.

License

MIT

版本与发布记录

@alitajs/postcss-plugin-px2rem 共发布 1 个版本,首次发布于 2021/08/24;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.0.1latest2021/08/245 年前

关于 @alitajs/postcss-plugin-px2rem 的常见问题

@alitajs/postcss-plugin-px2rem 是什么?

A plugin for PostCSS that generates rem units from multi units.

如何安装 @alitajs/postcss-plugin-px2rem?

在项目目录运行 npm install @alitajs/postcss-plugin-px2rem,或使用 pnpm add / yarn add / bun add。

@alitajs/postcss-plugin-px2rem 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/alitajs__postcss-plugin-px2rem 是否存在。

@alitajs/postcss-plugin-px2rem 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@alitajs/postcss-plugin-px2rem 可以商用吗?

@alitajs/postcss-plugin-px2rem 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@alitajs/postcss-plugin-px2rem 的下载量有多少?

根据 npm 官方下载统计,@alitajs/postcss-plugin-px2rem 在 2026/10/06 – 2026/11/04 这 30 天内被下载 6,979 次,日均约 233 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@alitajs/postcss-plugin-px2rem 还在维护吗?

最新版本 0.0.1 发布于 2021/08/24(5 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/08/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。