Giter Site home page Giter Site logo

advence-liz / pxtowhatever Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 0.0 22 KB

pxtowhatever根据 [pxtorem](https://github.com/cuth/postcss-pxtorem) 魔改,将 px 改为任意单位模式如:rpx ,rem。

License: MIT License

JavaScript 98.61% CSS 1.39%

pxtowhatever's Introduction

pxtowhatever

根据 pxtorem 魔改,将 px 改为任意单位模式,如 rpx, rem ,vw

安装

$ npm i pxtowhatever

options

Type: Object | Null
Default:

{
    unit: "*2rpx",
    unitPrecision: 5,
    propList: ['font', 'font-size', 'line-height', 'letter-spacing'],
    selectorBlackList: [],
    replace: true,
    mediaQuery: false,
    minPixelValue: 0,
    exclude: /node_modules/i
}
  • unit 模式为<operator><multiplier>[suffix]举个例子:*2rpx 其中 *-><operator> 2-><multiplier> rpx->[suffix] 其中 operator,multiplier 选填 suffix 必填即目前支持以下四种模式
    • rpx
    • 2rpx
    • *2rpx
    • /2rpx 当使用 pxtorem的时候设置rootValue=16 就相当与设置 unit=/16rem
  • unitPrecision (Number) The decimal numbers to allow the REM units to grow to.
  • propList (Array) The properties that can change from px to rem.
    • 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 (Array) The selectors to ignore and leave as px.
    • 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
  • replace (Boolean) Replaces rules containing rems instead of adding fallbacks.
  • mediaQuery (Boolean) Allow px to be converted in media queries.
  • minPixelValue (Number) Set the minimum pixel value to replace.
  • exclude (String, Regexp, Function) The file path to ignore and leave as px.
    • 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.indexOf('exclude') !== -1; }
  • include (String, Regexp, Function) The file path to include. 参数形式跟 exclude 相同.

example

假设你有如下文件 main.css index.js 其输出结果将跟下面一致

main.css

.class {
  font-size: 14px;
  line-height: 20px;
  margin: -10px 0.5em;
  padding: 10px 10px;
  border: 2px solid black;
}

index.js

const fs = require("fs")
const postcss = require("postcss")
const pxtowhatever = require("..")
const path = require("path")
const css = fs.readFileSync(path.join(__dirname, "main.css"), "utf8")

;[
  {
    minPixelValue: 0,
    unit: "rpx",
    propList: ["*"]
  },
  {
    minPixelValue: 0,
    unit: "2rpx",
    propList: ["*"]
  },
  {
    minPixelValue: 0,
    unit: "*2rpx",
    propList: ["*"]
  },
  {
    minPixelValue: 0,
    unit: "/2rpx",
    propList: ["*"]
  }
].forEach(opts => {
  process.stdout.write(postcss(pxtowhatever(opts)).process(css).css + "\n")
})

输出

> cd example && node index.js

.class {
  font-size: 14rpx;
  line-height: 20rpx;
  margin: -10rpx 0.5em;
  padding: 10rpx 10rpx;
  border: 2rpx solid black;
}

.class {
  font-size: 28rpx;
  line-height: 40rpx;
  margin: -20rpx 0.5em;
  padding: 20rpx 20rpx;
  border: 4rpx solid black;
}

.class {
  font-size: 28rpx;
  line-height: 40rpx;
  margin: -20rpx 0.5em;
  padding: 20rpx 20rpx;
  border: 4rpx solid black;
}

.class {
  font-size: 7rpx;
  line-height: 10rpx;
  margin: -5rpx 0.5em;
  padding: 5rpx 5rpx;
  border: 1rpx solid black;
}

webpack demo

 rules: [

   {
        test: /\.(scss|sass)$/,
        use: [
          "style-loader",
          "css-loader?minimize",
          {
            loader: "postcss-loader",
            options: {
              plugins: [ pxtowhatever({
                        unit: "/2px",
                        propList: [
                          "*",
                          "!letter-spacing",
                          "!border",
                          "!border-top",
                          "!border-left",
                          "!border-right",
                          "!border-bottom"
                        ],
                        minPixelValue: 1
                      });]
            }
          },

          {
            loader: "sass-loader"
            // options: {
            //   data: '$brand-primary: #000;',
            // },
          }
        ]
      },
 ]

pxtowhatever's People

Contributors

advence-liz avatar dependabot[bot] avatar

Stargazers

 avatar 101ll avatar  avatar  avatar Michael Ma avatar CharlieLau avatar

Watchers

James Cloos avatar  avatar

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.