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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@minko-fe/postcss-pxtoviewport

v1.5.0

Published

✨ A CSS post-processor that converts px to viewport units (vw, vh, vmin, vmax).

Downloads

935

Readme

postcss-pxtoviewport

中文 | English

PostCSS插件,可以从像素单位生成 viewport 单位(vw, vh, vmin, vmax)

如果你不需要以下新特性,可以使用官方库:postcss-px-to-viewport

新特性

  • 在样式文件中重写任意 postcss-pxtoviewport 支持的选项
  • 在样式文件动态禁用转化viewport
  • 兼容vite,解决了打包后样式混乱的问题

🔧 安装

pnpm install postcss @minko-fe/postcss-pxtoviewport -D

✍️ 使用

postcss.config.js

example

// postcss.config.js
import pxtoviewport from '@minko-fe/postcss-pxtoviewport'

export default {
  plugins: [
    pxtoviewport({
      viewportWidth: 375,
      selectorBlackList: ['some-class'],
      propList: ['*'],
      atRules: ['media'],
      // ...
    }),
  ],
}

options

| Name | Type | Default | Description | | ----------------- | ------------------------------------------------------------------- | --------------- | ---------------------------------------------------------------------------------------------- | | unitToConvert | string | px | 需要转化的单位,默认 px | | viewportWidth | number | ((input: Input) => number) | 375 | 视图窗口宽度 | | unitPrecision | number | 5 | 小数点后精度 | | propList | string[] | ['*'] | 可以从 px 改变为 vw 的属性,参考:propList | | viewportUnit | string | vw | 转化后的单位 | | fontViewportUnit | string | vw | font 转化后的单位 | | selectorBlackList | (string \| RegExp)[] | [] | 忽略的选择器,保留为 px。参考:selectorBlackList | | replace | boolean | true | 直接在 css 规则上替换值而不是添加备用 | | atRules | boolean | string[] | false | 允许at-rules中转换。参考 At-rule | | minPixelValue | number | 0 | 最小的 px 转化值(小于这个值的不转化) | | include | string | RegExp | ((filePath: string) => boolean) | null | null | 包括的文件(与 exclude 相反)。优先级比 exclude 高。规则同 exclude | | exclude | string | RegExp | ((filePath: string) => boolean) | null | /node_modules/i | 忽略的文件路径。参考:exclude | | disable | boolean | false | 关闭插件 | | convertUnit | ConvertUnit | ConvertUnit[] | false | null | null | 插件处理的最后阶段转换单位 |

propList

  • 值需要完全匹配
  • 使用通配符 * 来启用所有属性. Example: ['*']
  • 在一个词的开头或结尾使用 *. (['*position*'] will match background-position-y)
  • 使用 ! 不匹配一个属性. Example: ['*', '!letter-spacing']
  • 组合 !*. Example: ['*', '!font*']

selectorBlackList

  • 如果值是字符串,它会检查选择器是否包含字符串.
    • ['body'] will match .body-class
  • 如果值是正则,它会检查选择器是否与正则相匹配.
    • [/^body$/] will match body but not .body

exclude

  • 如果值是字符串,它检查文件路径是否包含字符串
    • 'exclude' will match \project\postcss-pxtoviewport\exclude\path
  • 如果值是正则,它将检查文件路径是否与正则相匹配
    • /exclude/i will match \project\postcss-pxtoviewport\exclude\path
  • 如果值是函数,你可以使用排除函数返回 true,文件将被忽略
    • 回调将传递文件路径作为一个参数,它应该返回一个 boolean
    • function (file) { return file.includes('exclude') }

✨ 关于新特性

⚙️ 在 css 中,动态设置插件选项

当前文件禁用插件

/* pxtoviewport?disable=true */
.rule {
  font-size: 15px; // 15px
}

设置 viewportWidth

/* pxtoviewport?viewportWidth=750 */
.rule {
  font-size: 30px; // 4vw
}

🌰 以上只是简单的栗子,你可以在 css 文件中设置任意 postcss-pxtoviewport 支持的选项

聪明的你,或许已经看出来了,/* pxtoviewport?disable=true */ 很像浏览器 url?😼 没错。关于规范,只需参考:query-string

例子

/* pxtoviewport?disable=false&viewportWidth=750&propList[]=*&replace=false&selectorBlackList[]=/some-class/i */

在 css 中,忽略某一行

.rule {
  /* pxtoviewport-disable-next-line */
  font-size: 15px; // 15px
}

如果你写 15PX(只要不是 px),插件也会忽略,因为 unitToConvert 默认是 px 如果你希望使用 PX 忽略并且希望最后得到的单位是 px,你可以这样

// postcss.config.js
import pxtoviewport from '@minko-fe/postcss-pxtoviewport'

export default {
  plugins: [
    pxtoviewport({
      convertUnit: {
        source: /px$/i,
        target: 'px',
      },
    }),
  ],
}

❤️ 感谢

postcss-px-to-viewport

@tcstory/postcss-px-to-viewport

👀 相关

A CSS post-processor that converts px to rem: postcss-pxtorem

💕 支持

如果这个仓库帮了你的忙,请不吝给个 star,谢谢!😎