pxtorem-webpack-plugin
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 = ;const PxtoremWebpackPlugin = ; moduleexports = // ... plugins: filename: 'index.html' template: './index.html' // templates: 'index.html' baseWidth: 750 baseDpr: 2 remUnit: 10 // ...
This will generate a file dist/index.html containing the following:
Vue webpack demo
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:
{ var docEl = docdocumentElement var resizeEvent = 'orientationchange' in window ? 'orientationchange' : 'resize' var { var clientWidth = docElclientWidth // baseWidth will be replace by `options.baseWidth` // remUnit will be replace by `options.remUnit` docElstylefontSize = remUnit * clientWidth / baseWidth + 'px' // for dpr=2.xx -> 2, dpr=3.xx -> 3 docEl } if !docaddEventListener return win}document window
If you want calculate px to rem more customized, and want to change the convert rules, you can refer to px2rem.scss