You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
3.7 KiB

7 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
7 years ago
7 years ago
9 years ago
9 years ago
9 years ago
7 years ago
7 years ago
9 years ago
7 years ago
9 years ago
7 years ago
  1. # postcss-px-to-viewport [![NPM version](https://badge.fury.io/js/postcss-px-to-viewport.svg)](http://badge.fury.io/js/postcss-px-to-viewport)
  2. A plugin for [PostCSS](https://github.com/ai/postcss) that generates viewport units (vw, vh, vmin, vmax) from pixel units.
  3. ## Install
  4. ```
  5. $ npm install postcss-px-to-viewport --save-dev
  6. ```
  7. ## Usage
  8. If your project involves a fixed width, this script will help to convert pixels into viewport units.
  9. ### Input/Output
  10. ```css
  11. // input
  12. .class {
  13. margin: -10px .5vh;
  14. padding: 5vmin 9.5px 1px;
  15. border: 3px solid black;
  16. border-bottom-width: 1px;
  17. font-size: 14px;
  18. line-height: 20px;
  19. }
  20. .class2 {
  21. border: 1px solid black;
  22. margin-bottom: 1px;
  23. font-size: 20px;
  24. line-height: 30px;
  25. }
  26. @media (min-width: 750px) {
  27. .class3 {
  28. font-size: 16px;
  29. line-height: 22px;
  30. }
  31. }
  32. // output
  33. .class {
  34. margin: -3.125vw .5vh;
  35. padding: 5vmin 2.96875vw 1px;
  36. border: 0.9375vw solid black;
  37. border-bottom-width: 1px;
  38. font-size: 4.375vw;
  39. line-height: 6.25vw;
  40. }
  41. .class2 {
  42. border: 1px solid black;
  43. margin-bottom: 1px;
  44. font-size: 6.25vw;
  45. line-height: 9.375vw;
  46. }
  47. @media (min-width: 234.375vw) {
  48. .class3 {
  49. font-size: 5vw;
  50. line-height: 6.875vw;
  51. }
  52. }
  53. ```
  54. ### Example
  55. ```js
  56. 'use strict';
  57. var fs = require('fs');
  58. var postcss = require('postcss');
  59. var pxToViewport = require('..');
  60. var css = fs.readFileSync('main.css', 'utf8');
  61. var options = {
  62. replace: false
  63. };
  64. var processedCss = postcss(pxToViewport(options)).process(css).css;
  65. fs.writeFile('main-viewport.css', processedCss, function (err) {
  66. if (err) {
  67. throw err;
  68. }
  69. console.log('File with viewport units written.');
  70. });
  71. ```
  72. ### Options
  73. Default:
  74. ```js
  75. {
  76. unitToConvert: 'px',
  77. viewportWidth: 320,
  78. viewportHeight: 568, // not now used; TODO: need for different units and math for different properties
  79. unitPrecision: 5,
  80. viewportUnit: 'vw',
  81. fontViewportUnit: 'vw', // vmin is more suitable.
  82. selectorBlackList: [],
  83. minPixelValue: 1,
  84. mediaQuery: false,
  85. replace: true,
  86. exclude: [] // ignore some files
  87. }
  88. ```
  89. - `unitToConvert` (String) unit to convert, by default, it is px.
  90. - `viewportWidth` (Number) The width of the viewport.
  91. - `viewportHeight` (Number) The height of the viewport.
  92. - `unitPrecision` (Number) The decimal numbers to allow the REM units to grow to.
  93. - `viewportUnit` (String) Expected units.
  94. - `fontViewportUnit` (String) Expected units for font.
  95. - `selectorBlackList` (Array) The selectors to ignore and leave as px.
  96. - If value is string, it checks to see if selector contains the string.
  97. - `['body']` will match `.body-class`
  98. - If value is regexp, it checks to see if the selector matches the regexp.
  99. - `[/^body$/]` will match `body` but not `.body`
  100. - `minPixelValue` (Number) Set the minimum pixel value to replace.
  101. - `mediaQuery` (Boolean) Allow px to be converted in media queries.
  102. - `replace` (Boolean) replaces rules containing rems instead of adding fallbacks.
  103. - `exclude` (Array or Regexp) Ignore some files like 'node_modules'
  104. - If value is regexp, will ignore the matches files.
  105. - If value is array, the elements of the array are regexp.
  106. ### Use with gulp-postcss
  107. add to your gulp config:
  108. ```js
  109. var gulp = require('gulp');
  110. var postcss = require('gulp-postcss');
  111. var pxtoviewport = require('postcss-px-to-viewport');
  112. gulp.task('css', function () {
  113. var processors = [
  114. pxtoviewport({
  115. viewportWidth: 320,
  116. viewportUnit: 'vmin'
  117. })
  118. ];
  119. return gulp.src(['build/css/**/*.css'])
  120. .pipe(postcss(processors))
  121. .pipe(gulp.dest('build/css'));
  122. });
  123. ```
  124. ### Use with Postcss configuration file
  125. add to postcss.config.js
  126. ```js
  127. module.exports = {
  128. plugins: {
  129. ...
  130. 'postcss-px-to-viewport': {
  131. // options
  132. }
  133. }
  134. }
  135. ```