All Projects โ†’ yugasun โ†’ pxtorem-webpack-plugin

yugasun / pxtorem-webpack-plugin

Licence: MIT license
A webpack plugin for generating rem for stylesheet and inject auto calculate scripts.

Programming Languages

javascript
184084 projects - #8 most used programming language
CSS
56736 projects
HTML
75241 projects

Projects that are alternatives of or similar to pxtorem-webpack-plugin

Wxapp Webpack Plugin
๐Ÿ“ฆ ๅพฎไฟกๅฐ็จ‹ๅบ webpack ๆ’ไปถ
Stars: โœญ 185 (+670.83%)
Mutual labels:  webpack-plugin
Critters
๐Ÿฆ” A Webpack plugin to inline your critical CSS and lazy-load the rest.
Stars: โœญ 2,894 (+11958.33%)
Mutual labels:  webpack-plugin
webpack-extract-translation-keys
This plugin extracts translation keys for applications requiring runtime translations
Stars: โœญ 35 (+45.83%)
Mutual labels:  webpack-plugin
Vue Auto Routing
Generate Vue Router routing automatically
Stars: โœญ 196 (+716.67%)
Mutual labels:  webpack-plugin
Page Skeleton Webpack Plugin
Webpack plugin to generate the skeleton page automatically
Stars: โœญ 2,632 (+10866.67%)
Mutual labels:  webpack-plugin
Webpack Messages
Beautifully format Webpack messages throughout your bundle lifecycle(s)!
Stars: โœญ 238 (+891.67%)
Mutual labels:  webpack-plugin
Antd Scss Theme Plugin
A Webpack plugin for customizing Ant Design with an SCSS theme file and using Ant Design's compiled variables in SCSS files throughout your project.
Stars: โœญ 176 (+633.33%)
Mutual labels:  webpack-plugin
eruda-webpack-plugin
A webpack plugin of eruda to help you develop mobile app
Stars: โœญ 56 (+133.33%)
Mutual labels:  webpack-plugin
Hard Source Webpack Plugin
www.npmjs.com/package/hard-source-webpack-plugin
Stars: โœญ 2,608 (+10766.67%)
Mutual labels:  webpack-plugin
webpack-bugsnag-plugins
Webpack plugins for common Bugsnag actions.
Stars: โœญ 29 (+20.83%)
Mutual labels:  webpack-plugin
Bundle Buddy Webpack Plugin
๐Ÿ๐Ÿ๐Ÿ๐Ÿ bundle-buddy-webpack-plugin ๐Ÿ๐Ÿ๐Ÿ๐Ÿ
Stars: โœญ 199 (+729.17%)
Mutual labels:  webpack-plugin
Antd Dayjs Webpack Plugin
โฐ Day.js webpack plugin for antd
Stars: โœญ 215 (+795.83%)
Mutual labels:  webpack-plugin
Webpack Shell Plugin
Run shell commands either before or after webpack builds
Stars: โœญ 250 (+941.67%)
Mutual labels:  webpack-plugin
Preload Webpack Plugin
Please use https://github.com/vuejs/preload-webpack-plugin instead.
Stars: โœญ 2,174 (+8958.33%)
Mutual labels:  webpack-plugin
sitemap-webpack-plugin
Webpack plugin to generate a sitemap.
Stars: โœญ 72 (+200%)
Mutual labels:  webpack-plugin
Emojify Webpack Plugin
๐Ÿฆ„ Turn your code into emoji
Stars: โœญ 178 (+641.67%)
Mutual labels:  webpack-plugin
Copy Webpack Plugin
Copy files and directories with webpack
Stars: โœญ 2,679 (+11062.5%)
Mutual labels:  webpack-plugin
asset-map-webpack-plugin
Webpack plugin that creates a map of assets to public url slug for server agnostic usage.
Stars: โœญ 14 (-41.67%)
Mutual labels:  webpack-plugin
webpack-omit-js-for-css-plugin
This plugin will omit bundled JS files for dependencies that are exclusively CSS, which become obsolete once mini-css-extract-plugin extracts inlined CSS into its own .css file
Stars: โœญ 14 (-41.67%)
Mutual labels:  webpack-plugin
nunjucks-webpack-plugin
A webpack plugin for nunjucks.
Stars: โœญ 27 (+12.5%)
Mutual labels:  webpack-plugin

pxtorem-webpack-plugin

Downloads Version

A webpack plugin for generating rem for stylesheet and inject auto calculate scripts.

Install

npm install pxtorem-webpack-plugin --save-dev

Usage

This plugin should be used with html-webpack-plugin

const HtmlWebpackPlugin = require('html-webpack-plugin');
const PxtoremWebpackPlugin = require('pxtorem-webpack-plugin');

module.exports = {
  // ...
  plugins: [
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: './index.html',
    }),
    new PxtoremWebpackPlugin({
      // 
      templates: [
        'index.html'
      ],
      baseWidth: 750,
      baseDpr: 2,
      remUnit: 10,
    }),
  ],
  // ...
}

This will generate a file dist/index.html containing the following:

<!DOCTYPE html>
<html lang="en">
<head>
  <script>!function(e,t){var n=e.documentElement,i="orientationchange"in window?"orientationchange":"resize",d=function(){var e=n.clientWidth;n.style.fontSize=e/750*10+"px",n.setAttribute("data-dpr",Math.floor(window.devicePixelRatio))};d(),e.addEventListener&&t.addEventListener(i,d,!1)}(document,window);</script>
  <meta charset="UTF-8">
  <title>Vue webpack demo</title>
</head>
<body>
  <div id="app"></div>
<script type="text/javascript" src="build.js"></script></body>
</html>

Options

You can pass a hash of configuration options to html-webpack-plugin. Allowed values are as follows:

Name Type Default Description
templates {Array} [] The html files need to inject rem calculation scripts
baseWidth {Number} 750 The base width for UI design
baseDpr {Number} 2 base device pixel ratio
remUnit {Number} 75 rem unit value

Features

  • Auto convert px to rem unit depends on customize base configuration.
  • Inject script into template for auto calculating html root font-size

Below is the source code for injected scripts:

(function (doc, win) {
  var docEl = doc.documentElement
  var resizeEvent = 'orientationchange' in window ? 'orientationchange' : 'resize'
  var recalcFun = function () {
    var clientWidth = docEl.clientWidth
    // baseWidth will be replace by `options.baseWidth`
    // remUnit will be replace by `options.remUnit`
    docEl.style.fontSize = remUnit * (clientWidth / baseWidth) + 'px'
    // for dpr=2.xx -> 2, dpr=3.xx -> 3
    docEl.setAttribute('data-dpr', Math.floor(window.devicePixelRatio))
  }
  recalcFun()
  if (!doc.addEventListener) return
  win.addEventListener(resizeEvent, recalcFun, false)
})(document, window)

If you want calculate px to rem more customized, and want to change the convert rules, you can refer to px2rem.scss

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].