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

postcss-px-to-viewport-plus

v1.0.1

Published

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

Downloads

7

Readme

postcss-px-to-viewport-plus

中文 | English

将 px 单位转换为视口单位的 (vw, vh, vmin, vmax) 的 PostCSS 插件.

简介

如果你的样式需要做根据视口大小来调整宽度,这个脚本可以将你 CSS 中的 px 单位转化为 vw,1vw 等于 1/100 视口宽度。

本插件基于 postcss-px-to-viewport 源代码修改,增加了一个参数 mediaConfig ({viewportWidth: Number,minWidth:Number,maxWidth:Number}[])

当 mediaQuery 为 true 时,媒体查询里的 CSS 根据配置中对应的 viewportWidth 转换,需要满足 0<=minWidth<=viewportWidth<=maxWidth<=Infinity。

参考配置

// postcss.config.js
module.exports = {
  plugins: {
    "postcss-px-to-viewport-plus": {
      viewportWidth: 375,
      // viewportHeight: 667,
      unitPrecision: 5,
      viewportUnit: "vw",
      selectorBlackList: [],
      minPixelValue: 1,
      mediaQuery: true,
      mediaConfig: [
        {
          viewportWidth: 600,
          minWidth: 500,
          maxWidth: 750,
        },
        {
          viewportWidth: 900,
          minWidth: 751,
          maxWidth: 1024,
        },
        {
          viewportWidth: 1920,
          minWidth: 1025,
          maxWidth: Infinity,
        },
      ],
      exclude: [/node_modules/],
    },
  },
};

输入

.class {
  margin: -10px 0.5vh;
  padding: 5vmin 9.5px 1px;
  border: 3px solid black;
  border-bottom-width: 1px;
  font-size: 14px;
  line-height: 20px;
}
.class2 {
  border: 1px solid black;
  margin-bottom: 1px;
  font-size: 20px;
  line-height: 30px;
}

@media (min-device-width: 500px) and (max-device-width: 750px) {
  .class3 {
    font-size: 16px;
    line-height: 22px;
  }
}

@media (min-width: 751px) and (max-width: 1024px) {
  .class3 {
    font-size: 16px;
    line-height: 22px;
  }
}

@media (min-width: 1025px) {
  .class3 {
    font-size: 16px;
    line-height: 22px;
  }
}

输出

.class {
  margin: -2.66667vw 0.5vh;
  padding: 5vmin 2.53333vw 1px;
  border: 0.8vw solid black;
  border-bottom-width: 1px;
  font-size: 3.73333vw;
  line-height: 5.33333vw;
}
.class2 {
  border: 1px solid black;
  margin-bottom: 1px;
  font-size: 5.33333vw;
  line-height: 8vw;
}

@media (min-device-width: 500px) and (max-device-width: 750px) {
  .class3 {
    font-size: 2.66667vw;
    line-height: 3.66667vw;
  }
}

@media (min-width: 751px) and (max-width: 1024px) {
  .class3 {
    font-size: 1.77778vw;
    line-height: 2.44444vw;
  }
}

@media (min-width: 1025px) {
  .class3 {
    font-size: 0.83333vw;
    line-height: 1.14583vw;
  }
}

上手

安装

使用 npm 安装

$ npm install postcss-px-to-viewport-plus --save-dev

或者使用 yarn 进行安装

$ yarn add -D postcss-px-to-viewport-plus

配置参数

默认参数:

{
  unitToConvert: 'px',
  viewportWidth: 320,
  unitPrecision: 5,
  propList: ['*'],
  viewportUnit: 'vw',
  fontViewportUnit: 'vw',
  selectorBlackList: [],
  minPixelValue: 1,
  mediaQuery: false,
  mediaConfig:[],
  replace: true,
  exclude: [],
  landscape: false,
  landscapeUnit: 'vw',
  landscapeWidth: 568
}
  • unitToConvert (String) 需要转换的单位,默认为"px"
  • viewportWidth (Number) 设计稿的视口宽度
  • unitPrecision (Number) 单位转换后保留的精度
  • propList (Array) 能转化为 vw 的属性列表
    • 传入特定的 CSS 属性;
    • 可以传入通配符""去匹配所有属性,例如:[''];
    • 在属性的前或后添加"*",可以匹配特定的属性. (例如['*position*'] 会匹配 background-position-y)
    • 在特定属性前加 "!",将不转换该属性的单位 . 例如: ['*', '!letter-spacing'],将不转换 letter-spacing
    • "!" 和 ""可以组合使用, 例如: ['', '!font*'],将不转换 font-size 以及 font-weight 等属性
  • viewportUnit (String) 希望使用的视口单位
  • fontViewportUnit (String) 字体使用的视口单位
  • selectorBlackList (Array) 需要忽略的 CSS 选择器,不会转为视口单位,使用原有的 px 等单位。
    • 如果传入的值为字符串的话,只要选择器中含有传入值就会被匹配
      • 例如 selectorBlackList['body'] 的话, 那么 .body-class 就会被忽略
    • 如果传入的值为正则表达式的话,那么就会依据 CSS 选择器是否匹配该正则
      • 例如 selectorBlackList[/^body$/] , 那么 body 会被忽略,而 .body 不会
  • minPixelValue (Number) 设置最小的转换数值,如果为 1 的话,只有大于 1 的值会被转换
  • mediaQuery (Boolean) 媒体查询里的单位是否需要转换单位
  • mediaConfig ({viewportWidth: Number,minWidth:Number,maxWidth:Number}[]) mediaQuery 为 true 时,媒体查询里的 CSS 根据配置中对应的 viewportWidth 转换,需要满足 0<=minWidth<=viewportWidth<=maxWidth<=Infinity。
  • replace (Boolean) 是否直接更换属性值,而不添加备用属性
  • exclude (Array or Regexp) 忽略某些文件夹下的文件或特定文件,例如 'node_modules' 下的文件
    • 如果值是一个正则表达式,那么匹配这个正则的文件会被忽略
    • 如果传入的值是一个数组,那么数组里的值必须为正则
  • landscape (Boolean) 是否添加根据 landscapeWidth 生成的媒体查询条件 @media (orientation: landscape)
  • landscapeUnit (String) 横屏时使用的单位
  • landscapeWidth (Number) 横屏时使用的视口宽度

直接在 gulp 中使用,添加 gulp-postcss

gulpfile.js 添加如下配置:

var gulp = require("gulp");
var postcss = require("gulp-postcss");
var pxtoviewport = require("postcss-px-to-viewport-plus");

gulp.task("css", function () {
  var processors = [
    pxtoviewport({
      viewportWidth: 320,
      viewportUnit: "vmin",
    }),
  ];

  return gulp
    .src(["build/css/**/*.css"])
    .pipe(postcss(processors))
    .pipe(gulp.dest("build/css"));
});

使用 PostCss 配置文件时

postcss.config.js添加如下配置

module.exports = {
  plugins: {
    ...
    'postcss-px-to-viewport-plus': {
      // options
    }
  }
}

测试

为了跑测试案例, 你需要全局安装 jasmine-node :

$ npm install jasmine-node -g

然后输入下面的命令:

$ npm run test

参与贡献

在提 PR 之前,请先阅读 代码指南贡献指南

版本跟踪

使用 SemVer 做版本跟踪, 可用版本可在看到

Changelog

变更日志在 .

作者

contributors 里可以看到谁参与了本项目.

许可

本项目使用 MIT License.

借鉴自

  • 受 https://github.com/cuth/postcss-pxtorem/ 启发有了这个项目