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.

163 lines
4.0 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
9 years ago
9 years ago
7 years ago
9 years ago
9 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. propList: ['*'],
  81. viewportUnit: 'vw',
  82. fontViewportUnit: 'vw', // vmin is more suitable.
  83. selectorBlackList: [],
  84. minPixelValue: 1,
  85. mediaQuery: false,
  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 vw units to grow to.
  93. - `propList` (Array) The properties that can change from px to vw.
  94. - Values need to be exact matches.
  95. - Use wildcard * to enable all properties. Example: ['*']
  96. - Use * at the start or end of a word. (['*position*'] will match background-position-y)
  97. - Use ! to not match a property. Example: ['*', '!letter-spacing']
  98. - Combine the "not" prefix with the other prefixes. Example: ['*', '!font*']
  99. - `viewportUnit` (String) Expected units.
  100. - `fontViewportUnit` (String) Expected units for font.
  101. - `selectorBlackList` (Array) The selectors to ignore and leave as px.
  102. - If value is string, it checks to see if selector contains the string.
  103. - `['body']` will match `.body-class`
  104. - If value is regexp, it checks to see if the selector matches the regexp.
  105. - `[/^body$/]` will match `body` but not `.body`
  106. - `minPixelValue` (Number) Set the minimum pixel value to replace.
  107. - `mediaQuery` (Boolean) Allow px to be converted in media queries.
  108. - `exclude` (Array or Regexp) Ignore some files like 'node_modules'
  109. - If value is regexp, will ignore the matches files.
  110. - If value is array, the elements of the array are regexp.
  111. ### Use with gulp-postcss
  112. add to your gulp config:
  113. ```js
  114. var gulp = require('gulp');
  115. var postcss = require('gulp-postcss');
  116. var pxtoviewport = require('postcss-px-to-viewport');
  117. gulp.task('css', function () {
  118. var processors = [
  119. pxtoviewport({
  120. viewportWidth: 320,
  121. viewportUnit: 'vmin'
  122. })
  123. ];
  124. return gulp.src(['build/css/**/*.css'])
  125. .pipe(postcss(processors))
  126. .pipe(gulp.dest('build/css'));
  127. });
  128. ```
  129. ### Use with Postcss configuration file
  130. add to postcss.config.js
  131. ```js
  132. module.exports = {
  133. plugins: {
  134. ...
  135. 'postcss-px-to-viewport': {
  136. // options
  137. }
  138. }
  139. }
  140. ```