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

cnjm-postcss-px-to-viewport

v1.0.1

Published

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

Downloads

1,227

Readme

cnjm-postcss-px-to-viewport

基于postcss-px-to-viewport插件稍加改造的 和使用postcss-px-to-viewport 是基本一样的,只是多处理了vant等375尺寸问题,你也可以选择克隆到自己仓库中使用,也可以直接使用笔者改的

顺便把插件升级了postcss到v8+,规避了postcss.plugin was deprecated. Migration guide: 的警告,

npm i cnjm-postcss-px-to-viewport
锁死v0.0.2,则为不更新postcss版本

简介(为什么有)

一直希望在vite 中 使用 postcss-px-to-viewport 时可以设置不同的设计尺寸,比如vant是375设计的

而我们可能是750或其他尺寸,一直找了好久都没有合适的解决方案(有个webpack的,不过vite的貌似不行),如果有知道其他好的解决方法的,麻烦告知一下,感激不尽!

干脆就稍加改了一下postcss-px-to-viewport的代码

其实就是在src/index.js中加入了以下代码,把当前文件路径暴露出去,接收一个新的viewportWidth并赋值给opts

具体可以看下源代码

if(opts.customFun){
  opts.viewportWidth = opts.customFun({file:rule.source.input.file});
}

配置时使用cnjm-postcss-px-to-viewport

const path = require("path");
module.exports = () => {
  return {
    plugins: {
      autoprefixer: {
        overrideBrowserslist: ["Android 4.1", "iOS 7.1", "Chrome > 31", "ff > 31", "ie >= 8"],
      },
      // 修改插件名称
      "cnjm-postcss-px-to-viewport": {
        unitToConvert: "px", // 要转化的单位
        viewportWidth: 750, // UI设计稿的宽度
        unitPrecision: 6, // 转换后的精度,即小数点位数
        propList: ["*"], // 指定转换的css属性的单位,*代表全部css属性的单位都进行转换
        viewportUnit: "vw", // 指定需要转换成的视窗单位,默认vw
        fontViewportUnit: "vw", // 指定字体需要转换成的视窗单位,默认vw
        selectorBlackList: ["ignore"], // 指定不转换为视窗单位的类名,
        minPixelValue: 1, // 默认值1,小于或等于1px则不进行转换
        mediaQuery: true, // 是否在媒体查询的css代码中也进行转换,默认false
        replace: true, // 是否转换后直接更换属性值
        exclude: [], // 设置忽略文件,用正则做目录名匹配
        landscape: false, // 是否处理横屏情况
        // 如果没有使用其他的尺寸来设计,下面这个方法可以不需要,比如vant是375的
        customFun: ({ file }) => {
          // 这个自定义的方法是针对处理vant组件下的设计稿为375问题
          const designWidth = path.join(file).includes(path.join("node_modules", "vant")) ? 375 : 750;
          return designWidth;
        },
      },
    },
  };
};

https://github.com/vitejs/vite/issues/4653 多个配置,这将导致插件处理两次,优点缺点都很明显,具体怎么用,见人见智了

const px2viewport = require("postcss-px-to-viewport");
const autoprefixer = require("autoprefixer");
module.exports = () => {
  return {
    plugins: [
      autoprefixer({
        overrideBrowserslist: ["Android 4.1", "iOS 7.1", "Chrome > 31", "ff > 31", "ie >= 8"],
      }),
      px2viewport({
        unitToConvert: "px", // 要转化的单位
        viewportWidth: 375, // UI设计稿的宽度
        unitPrecision: 6, // 转换后的精度,即小数点位数
        propList: ["*"], // 指定转换的css属性的单位,*代表全部css属性的单位都进行转换
        viewportUnit: "vw", // 指定需要转换成的视窗单位,默认vw
        fontViewportUnit: "vw", // 指定字体需要转换成的视窗单位,默认vw
        selectorBlackList: ["ignore"], // 指定不转换为视窗单位的类名,
        minPixelValue: 1, // 默认值1,小于或等于1px则不进行转换
        mediaQuery: true, // 是否在媒体查询的css代码中也进行转换,默认false
        replace: true, // 是否转换后直接更换属性值
        exclude: [/^(?!.*node_modules\/vant)/], // 设置忽略文件,用正则做目录名匹配
        landscape: false, // 是否处理横屏情况
      }),
      px2viewport({
        unitToConvert: "px", // 要转化的单位
        viewportWidth: 750, // UI设计稿的宽度
        unitPrecision: 6, // 转换后的精度,即小数点位数
        propList: ["*"], // 指定转换的css属性的单位,*代表全部css属性的单位都进行转换
        viewportUnit: "vw", // 指定需要转换成的视窗单位,默认vw
        fontViewportUnit: "vw", // 指定字体需要转换成的视窗单位,默认vw
        selectorBlackList: ["ignore"], // 指定不转换为视窗单位的类名,
        minPixelValue: 1, // 默认值1,小于或等于1px则不进行转换
        mediaQuery: true, // 是否在媒体查询的css代码中也进行转换,默认false
        replace: true, // 是否转换后直接更换属性值
        exclude: [/node_modules\/vant/i], // 设置忽略文件,用正则做目录名匹配
        landscape: false, // 是否处理横屏情况
      }),
    ],
  };
};