Gathering detailed insights and metrics for postcss-transform-unit
Gathering detailed insights and metrics for postcss-transform-unit
Gathering detailed insights and metrics for postcss-transform-unit
Gathering detailed insights and metrics for postcss-transform-unit
npm install postcss-transform-unit
Typescript
Module System
Node Version
NPM Version
71.4
Supply Chain
97.9
Quality
75.3
Maintenance
50
Vulnerability
100
License
Total Downloads
1,553
Last Day
1
Last Week
6
Last Month
27
Last Year
419
Minified
Minified + Gzipped
Latest Version
1.1.2
Package Id
postcss-transform-unit@1.1.2
Unpacked Size
34.74 kB
Size
9.37 kB
File Count
18
NPM Version
7.18.1
Node Version
14.15.0
Cumulative downloads
Total Downloads
Last Day
-75%
1
Compared to previous day
Last Week
-50%
6
Compared to previous week
Last Month
17.4%
27
Compared to previous month
Last Year
70.3%
419
Compared to previous year
1
5
本项目从项目{postcss-pxtorpx-pro(https://www.npmjs.com/package/postcss-pxtorpx-pro )}基础上修改而来, 转换默认是从rem单位为rpx,但是经过本次配置后,可以从任意单位转换到任意单位的,并提供了转换函数,支持指定黑名单prop。
This repo is fork from postcss-pxtorpx-pro, but more powerful than it. This plugin can transform pixels unit to any unit theoretically, which default transform to rpx
unit.
rpx
is the unit make for developing mini-program, see @wxa
1$ npm install postcss-transform-unit --save-dev
像素是web开发中最常用的单位,但在做响应式页面开发之时稍显无力,开发者需要编写大量适配代码。故在移动端开发中我们常搭配rem, vw等单位使用,而在开发各类小程序中,我们又常用rpx取代vw。
Pixels are the easiest unit to use (opinion). The only issue with them is that they don't let browsers change the default font size of 16. This script converts every px value to a rem from the properties you choose to allow the browser to set the font size.
With the default settings, only font related properties are targeted.
1h1 { 2 margin: 0 0 20px; 3 font-size: 32px; 4 line-height: 1.2; 5 letter-spacing: 1px; 6} 7 8h1 { 9 margin: 0 0 40rpx; 10 font-size: 64rpx; 11 line-height: 1.2; 12 letter-spacing: 2rpx; 13}
1var fs = require('fs'); 2var postcss = require('postcss'); 3var pxtorpx = require('postcss-transform-unit'); 4var css = fs.readFileSync('main.css', 'utf8'); 5var options = { 6 replace: false 7}; 8var processedCss = postcss(pxtorpx(options)).process(css).css; 9 10fs.writeFile('main-rpx.css', processedCss, function (err) { 11 if (err) { 12 throw err; 13 } 14 console.log('Rpx file written.'); 15});
Type: Object | Null
Default:
1{ 2 // 转换前的单位 3 beforeUnit: 'rem', 4 // 转化的单位 5 unit: 'rpx', 6 // 单位精度 7 unitPrecision: 5, 8 // 不需要处理的css选择器 9 selectorBlackList: [], 10 // 不需要转化的css属性 11 propBlackList: [], 12 // 直接修改px,还是新加一条css规则 13 replace: true, 14 // 是否匹配媒介查询的px 15 mediaQuery: false, 16 // 需要转化的最小的pixel值,低于该值的px单位不做转化 17 minPixelValue: 0, 18 // 不处理的文件 19 exclude: null, 20 // 转化函数 21 // 默认设计稿按照750宽,2倍图的出 22 transform: (x) => 2*x 23}
beforeUnit
(String) Unit before conversion. default rem
.unit
(String) The unit transform to. default rpx
.unitPrecision
(Number) The decimal numbers to allow the rpx units to grow to.transform
(Function) function to transform pixels to other unit. default (x) => 2 * x
.propBlackList
(Array) The properties that can change from px to rpx.
*
to enable all properties. Example: ['*']
*
at the start or end of a word. (['*position*']
will match background-position-y
)!
to not match a property. Example: ['*', '!letter-spacing']
['*', '!font*']
selectorBlackList
(Array) The selectors to ignore and leave as px.
['body']
will match .body-class
[/^body$/]
will match body
but not .body
replace
(Boolean) Replaces rules containing rpxs 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.
'exclude'
will match \project\postcss-pxtorpx\exclude\path
/exclude/i
will match \project\postcss-pxtorpx\exclude\path
function (file) { return file.indexOf('exclude') !== -1; }
1// 可以结合 vue.config.js 以及 webpack来使用,但是需要自己写个中间转换loader 来执行 2const PostcssTransformUnit = require('postcss-transform-unit'), 3 postcss = require('postcss'), 4 unitTransformOptions = { 5 transform: (x) => 100 * x 6 } 7 8/** 9 * @description 进行css默认单位的转换 rem => rpx 10 * @param source 源代码 11 * @returns {string} 返回转换后的代码 12 */ 13function loader(source) { 14 // 版本一:本loader初衷是结合uniapp进行单位转换的 如果是微信环境的话 直接转换 反之返回源代码 15 return process.env.UNI_PLATFORM === 'mp-weixin' ? postcss(PostcssTransformUnit({ 16 replace: true, 17 ...unitTransformOptions 18 })).process(source).css : source 19 20 // 版本二:如果不需要环境配置 每次都需要转换的话 可以直接返回 21 return postcss(PostcssTransformUnit({ 22 replace: true, 23 ...unitTransformOptions 24 })).process(source).css 25} 26 27module.exports = loader
Currently, the easiest way to have a single property ignored is to use a capital in the pixel unit declaration.
1// `px` is converted to `rem` 2.convert { 3 font-size: 16px; // converted to 1rem 4} 5 6// `Px` or `PX` is ignored by `postcss-pxtorpx` but still accepted by browsers 7.ignore { 8 border: 1Px solid; // ignored 9 border-width: 2PX; // ignored 10}
本插件是从任意单位 到 任意单位 所以取名是transform-unit
No vulnerabilities found.
No security vulnerabilities found.