Giter VIP home page Giter VIP logo

px2rem's Introduction

Getting Started

yarn
yarn test // then you can see the processed result in test.result.css

auto-px2rem-loader

a webpack loader to process css file which can convert px uint to rem and media query code

Before processing:

The raw stylesheet:

.selector {
  width: 150px;
  height: 64px; /*px*/
  margin-top: 10px; /*no*/
  font-size: 28px;
  border: 1px solid #ddd;
  border-right-width: 2px;
}

After processing:

Rem and Media Query version:

.selector {
  width: 2rem;
  margin-top: 10px;
  border: 1px solid #ddd;
  border-right-width: 2px;
}

@media (max-width: 1280px) {
  .selector {
    height: 32px;
    font-size: 14px;
  }
}

@media (min-width: 1281px) and (max-width: 1920px) {
  .selector {
    height: 64px;
    font-size: 28px;
  }
}

@media (min-width: 1921px) {
  .selector {
    height: 96px;
    font-size: 42px;
  }
}

Install

npm install auto-px2rem-loader

webpack config

module.exports = {
  // ...
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          {
            loader: 'style-loader'
          },
          {
            loader: 'css-loader'
          },
          {
            loader: 'auto-px2rem-loader',
            // options here
            options: {
              remUnit: 100
            }
          }
        ]
      }
    ]
  }
};

All loader options

There are all default configurations in the loader, you can also customize them in the webpack config file

{
  loader: 'auto-px2rem-loader',
  options: {
    remUnit: 75, // rem unit value (default: 75)
    remPrecision: 6, // rem value precision (default: 6)
    forcePxComment: 'px', // force px comment (default: `px`)
    forceRemComment: 'rem', // force rem comment (default: `rem`)
    keepComment: 'no', // no conversion comment (default: `no`)
    // comment has higher priority than autodeal css properties
    // these css properties will be converted into media query, fauzzy matching
    autoDealPx: ['font-size'],
    // these css properties will not be converted, fauzzy matching
    autoDealNo: [
      'border',
      'shadow',
      'min',
      'max',
    ],
    appendAutoDealPx: [], // this will merge to autoDealPx, fauzzy matching
    appendAutoDealNo: [], // this will merge to autoDealNo, fauzzy matching
    mediaQuery: [// media query rules
      {
        mediaType: '(max-width: 1280px)', // media query width
        ratio: 0.5, // conversion ratio
      },
      {
        mediaType: '(min-width: 1281px) and (max-width: 1920px)',
        ratio: 1,
      },
      {
        mediaType: '(min-width: 1921px)',
        ratio: 1.5,
      }
    ]
  }
}

px2rem's People

Contributors

songsiqi avatar bull3 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.