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.

172 lines
4.4 KiB

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