GithubHelp home page GithubHelp logo

hemengke1997 / postcss-pxtorem Goto Github PK

View Code? Open in Web Editor NEW
47.0 5.0 6.0 794 KB

A postcss plugin that converts px to rem. px 转 rem 的 postcss 插件

Home Page: https://hemengke1997.github.io/postcss-pxtorem/

License: MIT License

JavaScript 1.42% CSS 1.89% TypeScript 96.14% HTML 0.55%
css postcss postcss-pxtorem px2rem pxtorem

postcss-pxtorem's Introduction

postcss-pxtorem

npm

English | 中文

A plugin for PostCSS that generates rem units from pixel units.

If you don't need the following new features, you can use the official library: postcss-pxtorem

  • Dynamically override any postcss-pxtorem supported option in style files
  • Dynamically disable transforming rem in style files.
  • Compatible with vite, Solved the problem of px to rem failing after vite build.

Install

npm install postcss @minko-fe/postcss-pxtorem -D

Usage

Pixels are the easiest unit to use (opinion). The only issue with them is that they don't let browsers change the default font size of 16. This script converts every px value to a rem from the properties you choose to allow the browser to set the font size.

postcss.config.js

example

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

export default {
  plugins: [
    pxtorom({
      rootValue: 16,
      selectorBlackList: ['some-class'],
      propList: ['*'],
      atRules: ['media'],
      // ...
    }),
  ],
}

options

Name Type Default Description
rootValue number | ((input: Input) => number) 16 Represents the root element font size or returns the root element font size based on the input parameter
unitToConvert string px unit to convert, by default, it is px
unitPrecision number 5 The decimal numbers to allow the REM units to grow to.
propList string[] ['*'] The properties that can change from px to rem. Refer to: propList
selectorBlackList (string | RegExp)[] [] The selectors to ignore and leave as px. Refer to: selectorBlackList
replace boolean true Replaces rules containing rems instead of adding fallbacks.
atRules boolean | string[] false Allow px to be converted in at-rules. Refer to At-rule
minPixelValue number 0 Set the minimum pixel value to replace.
exclude string | RegExp | ((filePath: string) => boolean) | null /node_modules/i The file path to ignore and leave as px. Refer to: exclude
include string | RegExp | ((filePath: string) => boolean) | null null The file path to convert px to rem, in contrast to exclude, priority lower than exclude.
disable boolean false disable plugin, used to disable plugin dynamically
convertUnit ConvertUnit | ConvertUnit[] | false false convert unit when plugin process end

propList

  • Values need to be exact matches.
  • Use wildcard * to enable all properties. Example: ['*']
  • Use * at the start or end of a word. (['*position*'] will match background-position-y)
  • Use ! to not match a property. Example: ['*', '!letter-spacing']
  • Combine the "not" prefix with the other prefixes. Example: ['*', '!font*']

selectorBlackList

  • If value is string, it checks to see if selector contains the string.
    • ['body'] will match .body-class
  • If value is regexp, it checks to see if the selector matches the regexp.
    • [/^body$/] will match body but not .body

exclude

  • If value is string, it checks to see if file path contains the string.
    • 'exclude' will match \project\postcss-pxtorem\exclude\path
  • If value is regexp, it checks to see if file path matches the regexp.
    • /exclude/i will match \project\postcss-pxtorem\exclude\path
  • If value is function, you can use exclude function to return a true and the file will be ignored.
    • the callback will pass the file path as a parameter, it should returns a Boolean result.
    • function (file) { return file.includes('exclude') }

About new features

Dynamically set plugin options in css

disable plugin

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

set rootValue

/* pxtorem?rootValue=32 */
.rule {
  font-size: 30px; // 0.9375rem
}

The above is just a simple example, you can set any of the options supported by postcss-pxtorem in the css file

You may have seen that the css comment is very much like the browser url?. That's right. For the specification, just refer to: query-string

example

/* pxtorem?disable=false&rootValue=32&propList[]=*&replace=false&selectorBlackList[]=/some-class/i */

disable the next line in css file

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

If you write 15PX (as long as it's not px), the plugin also ignores it, because unitToConvert defaults to px If you want to use PX to ignore and want the final unit to be px, you can:

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

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

example

main.ts

import { flexible } from 'modern-flexible'

flexible({
  rootValue: 16,
  distinctDevice: [
    { isDevice: (w: number) => w < 750, UIWidth: 375, deviceWidthRange: [375, 750] },
    { isDevice: (w: number) => w >= 750, UIWidth: 1920, deviceWidthRange: [1080, 1920] },
  ],
})

❤️ Thanks

postcss-pxtorem

@tcstory/postcss-px-to-viewport

Related

A CSS post-processor that converts px to viewport: postcss-pxtoviewport

Support

If this has helped you, please don't hesitate to give a STAR, thanks! 😎

License

MIT

postcss-pxtorem's People

Contributors

hemengke1997 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

postcss-pxtorem's Issues

Use with Vant

vant适配文档

// postcss.config.js
module.exports = {
  plugins: [
    require("@minko-fe/postcss-pxtorem")({
      rootValue({ file }) {
        return file.includes('node_modules/vant') ? 37.5 : 75;
      },
    }),
  ],
};

打包失效

我用官方插件会出现打包失效的情况还有exclude失效,而用大佬的就没有问题,所以能请教下大佬官方包是有哪些问题,还有这个包大概做了哪些优化(我只了解css插件一些钩子)

Changelog not updated

Hi! As I’m watching the repository, I noticed the releases always refer to the changelog for the changes, but the changelog is not up-to-date.

(I don’t know if this repo uses an automatic process for this or not, sorry if this issue feels off for some reason.)

Thanks again for your work on this package, it’s a very important CSS area. 🙏

`atQuery` option is renamed `mediaQuery` in v0.5.0

In 16b66b0, the mediaQuery option has been changed to atQuery, but it isn’t documented, so I guess a choice should be made and documented.

I propose to go for atRule, because it’s the official CSS terminology (and it’s also used by PostCSS).

Also, I’d consider accepting an array (in addition to boolean) for this one, in case someone wants to convert @media and @container but leave @supports untouched for some reasons.

What do you think?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.