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

vite-plugin-preset-inline-px-to-viewport

v0.0.1

Published

应用于行内样式转化为 viewport 单位的 vite 插件

Downloads

10

Readme


title: vite-plugin-preset-inline-px-to-viewport lang: zh-CN

vite-plugin-preset-inline-px-to-viewport

应用于行内样式转化为 viewport 单位的 vite 插件。

原理

1、利用 rollup transform 钩子做转化处理 2、利用 postcss API 做 css 检测、正则校验匹配替换

协议(使用方式上)

静态行内样式

<div
  fixed
  top-600px
  style="border: 5px solid #000; width: 100px; padding: 0 10px"
>
  行内
</div>

动态行内样式:暂不支持

<!-- 暂不支持 -->
<div :style="{ height: '50px' }">
  行内
</div>

API

Attributes

export interface PresetInlinePx2ViewportOptions {
  /** 应用周期,默认 build */
  apply?: 'serve' | 'build'

  /** 要转化的单位 */
  unitToConvert?: string

  /** UI设计稿的宽度 */
  viewportWidth?: number

  /** 转换后的精度,即小数点位数 */
  unitPrecision?: number

  /** 指定需要转换成的视窗单位,默认vw */
  viewportUnit?: string

  /** 指定字体需要转换成的视窗单位,默认vw */
  fontViewportUnit?: string

  /** 默认值1,小于或等于1px则不进行转换 */
  minPixelValue?: number

  /** 指定转换的css属性的单位,*代表全部css属性的单位都进行转换 */
  propList?: string[]

  include?: string | RegExp | readonly (string | RegExp)[]
  exclude?: string | RegExp | readonly (string | RegExp)[]
}

基础用法

// vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import PresetInlinePx2ViewPort from 'vite-plugin-preset-inline-px-to-viewport'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    // 行内 style 转化
    PresetInlinePx2ViewPort({
      apply: command,
      unitToConvert: 'px', // 要转化的单位
      viewportWidth: Number(env.VITE_POSTCSS_VIEWPORT_WIDTH), // UI设计稿的宽度
      unitPrecision: 6, // 转换后的精度,即小数点位数
      propList: ['*'], // 指定转换的css属性的单位,*代表全部css属性的单位都进行转换
      viewportUnit: 'vw', // 指定需要转换成的视窗单位,默认vw
      fontViewportUnit: 'vw', // 指定字体需要转换成的视窗单位,默认vw
      minPixelValue: 1 // 默认值1,小于或等于1px则不进行转换
    })
  ]
})

例子

<template>
  <div
    fixed
    top-600px
    style="border: 5px solid #000; width: 100px; padding: 0 10px"
  >
    行内
  </div>
</template>