Gathering detailed insights and metrics for postcss-px-to-viewport-8-media-screen
Gathering detailed insights and metrics for postcss-px-to-viewport-8-media-screen
基于postcss-px-to-viewport-8-plugin 扩展了mediaScreen选项,支持在不同的屏幕媒体查询中定制viewportWidth
npm install postcss-px-to-viewport-8-media-screen
Typescript
Module System
Node Version
NPM Version
72
Supply Chain
98.9
Quality
75.8
Maintenance
100
Vulnerability
100
License
TypeScript (93.72%)
CSS (4.42%)
JavaScript (1.86%)
Total Downloads
135
Last Day
1
Last Week
5
Last Month
9
Last Year
135
34 Commits
1 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
1.0.0
Package Id
postcss-px-to-viewport-8-media-screen@1.0.0
Unpacked Size
27.80 kB
Size
7.82 kB
File Count
12
NPM Version
8.19.2
Node Version
16.18.0
Publised On
05 Mar 2024
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
400%
5
Compared to previous week
Last month
350%
9
Compared to previous month
Last year
0%
135
Compared to previous year
1
将 px 单位转换为视口单位的 (vw, vh, vmin, vmax) 的 PostCSS 插件.
使用 postcss-px-to-viewport 控制台报以下代码
1postcss-px-to-viewport: postcss.plugin was deprecated. Migration guide: https://evilmartians.com/chronicles/postcss-8-plugin-migration 2
postcss-px-to-viewport
替换 postcss-px-to-viewport-8-plugin
注意对应库版本
1 "postcss": "^8.3.8", // 8.0.0版本都不会转单位 2 "postcss-loader": "^6.1.1",
如果你的样式需要做根据视口大小来调整宽度,这个脚本可以将你 CSS 中的 px 单位转化为 vw,1vw 等于 1/100 视口宽度。
1.class { 2 margin: -10px 0.5vh; 3 padding: 5vmin 9.5px 1px; 4 border: 3px solid black; 5 border-bottom-width: 1px; 6 font-size: 14px; 7 line-height: 20px; 8} 9 10.class2 { 11 padding-top: 10px; /* px-to-viewport-ignore */ 12 /* px-to-viewport-ignore-next */ 13 padding-bottom: 10px; 14 /* Any other comment */ 15 border: 1px solid black; 16 margin-bottom: 1px; 17 font-size: 20px; 18 line-height: 30px; 19} 20 21@media (min-width: 750px) { 22 .class3 { 23 font-size: 16px; 24 line-height: 22px; 25 } 26}
1.class { 2 margin: -3.125vw 0.5vh; 3 padding: 5vmin 2.96875vw 1px; 4 border: 0.9375vw solid black; 5 border-bottom-width: 1px; 6 font-size: 4.375vw; 7 line-height: 6.25vw; 8} 9 10.class2 { 11 padding-top: 10px; 12 padding-bottom: 10px; 13 /* Any other comment */ 14 border: 1px solid black; 15 margin-bottom: 1px; 16 font-size: 6.25vw; 17 line-height: 9.375vw; 18} 19 20@media (min-width: 750px) { 21 .class3 { 22 font-size: 16px; 23 line-height: 22px; 24 } 25}
1 2npm install postcss-px-to-viewport-8-plugin -D 3or 4yarn add postcss-px-to-viewport-8-plugin -D
默认选项:
{
unitToConvert: 'px',
viewportWidth: 320,
unitPrecision: 5,
propList: ['*'],
viewportUnit: 'vw',
fontViewportUnit: 'vw',
selectorBlackList: [],
minPixelValue: 1,
mediaQuery: false,
mediaScreen: [],
replace: true,
exclude: [],
landscape: false,
landscapeUnit: 'vw',
landscapeWidth: 568
}
MediaScreenOption
1type MediaScreenOption = { 2 screen: string; 3 viewportWidth?: number | ((filePath: string) => number | undefined); 4 //default vw 5 viewportUnit?: string; 6 //default px 7 unitToConvert?: string; 8};
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
unitToConvert | 需要转换的单位,默认为 px | string | px |
viewportWidth | 设计稿的视口宽度,如传入函数,函数的参数为当前处理的文件路径,函数返回 undefind 跳过转换 | number | Function | 320 |
unitPrecision | 单位转换后保留的精度 | number | 5 |
propList | 能转化为 vw 的属性列表 | string[] | ['*'] |
viewportUnit | 希望使用的视口单位 | string | vw |
fontViewportUnit | 字体使用的视口单位 | string | vw |
selectorBlackList | 需要忽略的 CSS 选择器,不会转为视口单位,使用原有的 px 等单位 | string[] | [] |
minPixelValue | 设置最小的转换数值,如果为 1 的话,只有大于 1 的值会被转换 | number | 1 |
mediaQuery | 媒体查询里的单位是否需要转换单位 | boolean | false |
mediaScreen | 根据媒体查询参数进行匹配,若匹配成功,则使用匹配的 viewportWidth | MediaScreenOption[] | |
replace | 是否直接更换属性值,而不添加备用属性 | boolean | true |
landscape | 是否添加根据 landscapeWidth 生成的媒体查询条件 @media (orientation: landscape) | boolean | false |
landscapeUnit | 横屏时使用的单位 | string | vw |
landscapeWidth | 横屏时使用的视口宽度,,如传入函数,函数的参数为当前处理的文件路径,函数返回 undefind 跳过转换 | number | 568 |
exclude | 忽略某些文件夹下的文件或特定文件,例如 node_modules 下的文件,如果值是一个正则表达式,那么匹配这个正则的文件会被忽略,如果传入的值是一个数组,那么数组里的值必须为正则 | Regexp | undefined |
include | 需要转换的文件,例如只转换 'src/mobile' 下的文件 (include: /\/src\/mobile\// ),如果值是一个正则表达式,将包含匹配的文件,否则将排除该文件, 如果传入的值是一个数组,那么数组里的值必须为正则 | Regexp | undefined |
propList
(Array) 能转化为 vw 的属性列表
selectorBlackList
(Array) 需要忽略的 CSS 选择器,不会转为视口单位,使用原有的 px 等单位。
selectorBlackList
为 ['body']
的话, 那么 .body-class
就会被忽略selectorBlackList
为 [/^body$/]
, 那么 body
会被忽略,而 .body
不会你可以使用特殊的注释来忽略单行的转换:
/* px-to-viewport-ignore-next */
— 在单独的行上,防止在下一行上进行转换。/* px-to-viewport-ignore */
— 在右边的属性之后,防止在同一行上进行转换。Example:
1{ 2 mediaScreen: [{ screen: 'min-width: 750px', viewportWidth: 750 }], 3}
1/* example input: */ 2.class { 3 margin: -10px 0.5vh; 4 padding: 5vmin 9.5px 1px; 5 border: 3px solid black; 6 border-bottom-width: 1px; 7 font-size: 14px; 8 line-height: 20px; 9} 10.class2 { 11 border: 1px solid black; 12 margin-bottom: 1px; 13 font-size: 20px; 14 line-height: 30px; 15} 16@media (min-width: 750px) { 17 .class3 { 18 font-size: 16px; 19 line-height: 22px; 20 } 21} 22 23/* example output: */ 24.class { 25 margin: -3.125vw 0.5vh; 26 padding: 5vmin 2.96875vw 1px; 27 border: 0.9375vw solid black; 28 border-bottom-width: 1px; 29 font-size: 4.375vw; 30 line-height: 6.25vw; 31} 32.class2 { 33 border: 1px solid black; 34 margin-bottom: 1px; 35 font-size: 6.25vw; 36 line-height: 9.375vw; 37} 38@media (min-width: 750px) { 39 .class3 { 40 font-size: 2.13333vw; 41 line-height: 2.93333vw; 42 } 43}
There are several more reasons why your pixels may not convert, the following options may affect this: propList
, selectorBlackList
, minPixelValue
, mediaQuery
, exclude
, include
.
在postcss.config.js
文件添加如下配置
1module.exports = { 2 plugins: { 3 ... 4 'postcss-px-to-viewport-8-plugin': { 5 viewportWidth: 1920, 6 exclude: [/node_modules/], 7 unitToConvert: 'px', 8 ... 9 } 10 } 11}
在vite.config.ts
文件添加如下配置
1import { defineConfig } from 'vite'; 2import postcsspxtoviewport8plugin from 'postcss-px-to-viewport-8-plugin'; 3 4export default defineConfig({ 5 css: { 6 postcss: { 7 plugins: [ 8 postcsspxtoviewport8plugin({ 9 unitToConvert: 'px', 10 viewportWidth: file => { 11 let num = 1920; 12 if (file.indexOf('m_') !== -1) { 13 num = 375; 14 } 15 return num; 16 }, 17 unitPrecision: 5, // 单位转换后保留的精度 18 propList: ['*'], // 能转化为vw的属性列表 19 viewportUnit: 'vw', // 希望使用的视口单位 20 fontViewportUnit: 'vw', // 字体使用的视口单位 21 selectorBlackList: [], // 需要忽略的CSS选择器,不会转为视口单位,使用原有的px等单位。 22 minPixelValue: 1, // 设置最小的转换数值,如果为1的话,只有大于1的值会被转换 23 mediaQuery: true, // 媒体查询里的单位是否需要转换单位 24 mediaScreen: [], 25 replace: true, // 是否直接更换属性值,而不添加备用属性 26 exclude: [/node_modules\/ant-design-vue/], // 忽略某些文件夹下的文件或特定文件,例如 'node_modules' 下的文件 27 include: [], // 如果设置了include,那将只有匹配到的文件才会被转换 28 landscape: false, // 是否添加根据 landscapeWidth 生成的媒体查询条件 @media (orientation: landscape) 29 landscapeUnit: 'vw', // 横屏时使用的单位 30 landscapeWidth: 1024, // 横屏时使用的视口宽度 31 }), 32 ], 33 }, 34 }, 35});
No vulnerabilities found.
No security vulnerabilities found.