Gathering detailed insights and metrics for postcss-px-to-viewport-8-media
Gathering detailed insights and metrics for postcss-px-to-viewport-8-media
Gathering detailed insights and metrics for postcss-px-to-viewport-8-media
Gathering detailed insights and metrics for postcss-px-to-viewport-8-media
A postcss plugin that extends the media query conversion on the basis of postcss-px-to-viewport-8-plugin
npm install postcss-px-to-viewport-8-media
Typescript
Module System
Node Version
NPM Version
72.2
Supply Chain
98.9
Quality
76
Maintenance
100
Vulnerability
100
License
TypeScript (96%)
JavaScript (4%)
Total Downloads
203
Last Day
3
Last Week
5
Last Month
15
Last Year
130
1 Stars
4 Commits
1 Watching
1 Branches
2 Contributors
Minified
Minified + Gzipped
Latest Version
1.0.1
Package Id
postcss-px-to-viewport-8-media@1.0.1
Unpacked Size
40.46 kB
Size
7.57 kB
File Count
5
NPM Version
8.19.2
Node Version
16.18.1
Publised On
29 Mar 2024
Cumulative downloads
Total Downloads
Last day
0%
3
Compared to previous day
Last week
-28.6%
5
Compared to previous week
Last month
1,400%
15
Compared to previous month
Last year
78.1%
130
Compared to previous year
1
在postcss-px-to-viewport-8-plugin的基础上拓展了mediaOptions
配置, 使得不同视口可以使用不同的viewportWidth
进行打包。
将 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
此外,添加了mediaOptions 参数:通过媒体查询给可根据不同的视口添加不同的 viewportWidth
和 viewportUnit
默认选项:
{
unitToConvert: 'px',
viewportWidth: 320,
unitPrecision: 5,
propList: ['*'],
viewportUnit: 'vw',
fontViewportUnit: 'vw',
selectorBlackList: [],
minPixelValue: 1,
mediaQuery: false,
replace: true,
exclude: [],
landscape: false,
landscapeUnit: 'vw',
landscapeWidth: 568,
mediaOpions: []
}
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
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 |
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 |
mediaOptions | 需要额外追加的媒体查询配置, 例如在屏幕宽度大于 1200px(screen and (min-width: 1200px) )时需要配置别的viewportWidth , 可以启用这个配置。 | MediaOption[] | [] |
MediaOption
说明参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
mediaParam | @media 后面跟着的参数。例如 mediaParam: 'screen and (min-width: 1200px)' | string | - |
viewportWidth | 设计稿的视口宽度,如传入函数,函数的参数为当前处理的文件路径,函数返回 undefind 跳过转换 | number | Function | - |
viewportUnit | 希望使用的视口单位, 不传的话默认使用外面的 viewportUnit | string | vw |
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/* example input: */ 2.class { 3 /* px-to-viewport-ignore-next */ 4 width: 10px; 5 padding: 10px; 6 height: 10px; /* px-to-viewport-ignore */ 7 border: solid 2px #000; /* px-to-viewport-ignore */ 8} 9 10/* example output: */ 11.class { 12 width: 10px; 13 padding: 3.125vw; 14 height: 10px; 15 border: solid 2px #000; 16}
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 replace: true, // 是否直接更换属性值,而不添加备用属性 25 exclude: [/node_modules\/ant-design-vue/], // 忽略某些文件夹下的文件或特定文件,例如 'node_modules' 下的文件 26 include: [], // 如果设置了include,那将只有匹配到的文件才会被转换 27 landscape: false, // 是否添加根据 landscapeWidth 生成的媒体查询条件 @media (orientation: landscape) 28 landscapeUnit: 'vw', // 横屏时使用的单位 29 landscapeWidth: 1024, // 横屏时使用的视口宽度 30 }), 31 ], 32 }, 33 }, 34});
No vulnerabilities found.
No security vulnerabilities found.