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.

250 lines
6.7 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 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
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
7 years ago
7 years ago
9 years ago
9 years ago
  1. # postcss-px-to-viewport
  2. [![NPM version](https://badge.fury.io/js/postcss-px-to-viewport.svg)](http://badge.fury.io/js/postcss-px-to-viewport)
  3. English | [中文](README_CN.md)
  4. A plugin for [PostCSS](https://github.com/postcss/postcss) that generates viewport units (vw, vh, vmin, vmax) from pixel units.
  5. ## Demo
  6. If your project involves a fixed width, this script will help to convert pixels into viewport units.
  7. ### Input
  8. ```css
  9. .class {
  10. margin: -10px .5vh;
  11. padding: 5vmin 9.5px 1px;
  12. border: 3px solid black;
  13. border-bottom-width: 1px;
  14. font-size: 14px;
  15. line-height: 20px;
  16. }
  17. .class2 {
  18. padding-top: 10px; /* px-to-viewport-ignore */
  19. /* px-to-viewport-ignore-next */
  20. padding-bottom: 10px;
  21. /* Any other comment */
  22. border: 1px solid black;
  23. margin-bottom: 1px;
  24. font-size: 20px;
  25. line-height: 30px;
  26. }
  27. @media (min-width: 750px) {
  28. .class3 {
  29. font-size: 16px;
  30. line-height: 22px;
  31. }
  32. }
  33. ```
  34. ### Output
  35. ```css
  36. .class {
  37. margin: -3.125vw .5vh;
  38. padding: 5vmin 2.96875vw 1px;
  39. border: 0.9375vw solid black;
  40. border-bottom-width: 1px;
  41. font-size: 4.375vw;
  42. line-height: 6.25vw;
  43. }
  44. .class2 {
  45. padding-top: 10px;
  46. padding-bottom: 10px;
  47. /* Any other comment */
  48. border: 1px solid black;
  49. margin-bottom: 1px;
  50. font-size: 6.25vw;
  51. line-height: 9.375vw;
  52. }
  53. @media (min-width: 750px) {
  54. .class3 {
  55. font-size: 16px;
  56. line-height: 22px;
  57. }
  58. }
  59. ```
  60. ## Getting Started
  61. ### Installation
  62. Add via npm
  63. ```
  64. $ npm install postcss-px-to-viewport --save-dev
  65. ```
  66. or yarn
  67. ```
  68. $ yarn add -D postcss-px-to-viewport
  69. ```
  70. ### Usage
  71. Default Options:
  72. ```js
  73. {
  74. unitToConvert: 'px',
  75. viewportWidth: 320,
  76. unitPrecision: 5,
  77. propList: ['*'],
  78. viewportUnit: 'vw',
  79. fontViewportUnit: 'vw',
  80. selectorBlackList: [],
  81. minPixelValue: 1,
  82. mediaQuery: false,
  83. replace: true,
  84. exclude: undefined,
  85. include: undefined,
  86. landscape: false,
  87. landscapeUnit: 'vw',
  88. landscapeWidth: 568
  89. }
  90. ```
  91. - `unitToConvert` (String) unit to convert, by default, it is px.
  92. - `viewportWidth` (Number) The width of the viewport.
  93. - `unitPrecision` (Number) The decimal numbers to allow the vw units to grow to.
  94. - `propList` (Array) The properties that can change from px to vw.
  95. - Values need to be exact matches.
  96. - Use wildcard * to enable all properties. Example: ['*']
  97. - Use * at the start or end of a word. (['*position*'] will match background-position-y)
  98. - Use ! to not match a property. Example: ['*', '!letter-spacing']
  99. - Combine the "not" prefix with the other prefixes. Example: ['*', '!font*']
  100. - `viewportUnit` (String) Expected units.
  101. - `fontViewportUnit` (String) Expected units for font.
  102. - `selectorBlackList` (Array) The selectors to ignore and leave as px.
  103. - If value is string, it checks to see if selector contains the string.
  104. - `['body']` will match `.body-class`
  105. - If value is regexp, it checks to see if the selector matches the regexp.
  106. - `[/^body$/]` will match `body` but not `.body`
  107. - `minPixelValue` (Number) Set the minimum pixel value to replace.
  108. - `mediaQuery` (Boolean) Allow px to be converted in media queries.
  109. - `replace` (Boolean) replaces rules containing vw instead of adding fallbacks.
  110. - `exclude` (Regexp or Array of Regexp) Ignore some files like 'node_modules'
  111. - If value is regexp, will ignore the matches files.
  112. - If value is array, the elements of the array are regexp.
  113. - `include` (Regexp or Array of Regexp) If `include` is set, only matching files will be converted,
  114. for example, only files under `src/mobile/` (`include: /\/src\/mobile\//`)
  115. - If the value is regexp, the matching file will be included, otherwise it will be excluded.
  116. - If value is array, the elements of the array are regexp.
  117. - `landscape` (Boolean) Adds `@media (orientation: landscape)` with values converted via `landscapeWidth`.
  118. - `landscapeUnit` (String) Expected unit for `landscape` option
  119. - `landscapeWidth` (Number) Viewport width for landscape orientation.
  120. > `exclude` and `include` can be set together, and the intersection of the two rules will be taken.
  121. #### Ignoring
  122. You can use special comments for ignore conversion of single lines:
  123. - `/* px-to-viewport-ignore-next */` — on a separate line, prevents conversion on the next line.
  124. - `/* px-to-viewport-ignore */` — after the property on the right, prevents conversion on the same line.
  125. Example:
  126. ```css
  127. /* example input: */
  128. .class {
  129. /* px-to-viewport-ignore-next */
  130. width: 10px;
  131. padding: 10px;
  132. height: 10px; /* px-to-viewport-ignore */
  133. border: solid 2px #000; /* px-to-viewport-ignore */
  134. }
  135. /* example output: */
  136. .class {
  137. width: 10px;
  138. padding: 3.125vw;
  139. height: 10px;
  140. border: solid 2px #000;
  141. }
  142. ```
  143. There are several more reasons why your pixels may not convert, the following options may affect this:
  144. `propList`, `selectorBlackList`, `minPixelValue`, `mediaQuery`, `exclude`, `include`.
  145. #### Use with PostCss configuration file
  146. add to your `postcss.config.js`
  147. ```js
  148. module.exports = {
  149. plugins: {
  150. // ...
  151. 'postcss-px-to-viewport': {
  152. // options
  153. }
  154. }
  155. }
  156. ```
  157. #### Use with gulp-postcss
  158. add to your `gulpfile.js`:
  159. ```js
  160. var gulp = require('gulp');
  161. var postcss = require('gulp-postcss');
  162. var pxtoviewport = require('postcss-px-to-viewport');
  163. gulp.task('css', function () {
  164. var processors = [
  165. pxtoviewport({
  166. viewportWidth: 320,
  167. viewportUnit: 'vmin'
  168. })
  169. ];
  170. return gulp.src(['build/css/**/*.css'])
  171. .pipe(postcss(processors))
  172. .pipe(gulp.dest('build/css'));
  173. });
  174. ```
  175. ## Contributing
  176. Please read [Code of Conduct](CODE-OF-CONDUCT.md)
  177. and [Contributing Guidelines](CONTRIBUTING.md) for submitting pull requests to us.
  178. ## Running the tests
  179. In order to run tests, you need to install dev-packages:
  180. ```
  181. $ npm install
  182. ```
  183. Then run the tests via npm script:
  184. ```
  185. $ npm run test
  186. ```
  187. ## Changelog
  188. The changelog is [here](CHANGELOG.md).
  189. ## Versioning
  190. We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/evrone/postcss-px-to-viewport/tags).
  191. ## Authors
  192. * [Dmitry Karpunin](https://github.com/KODerFunk) - *Initial work*
  193. * [Ivan Bunin](https://github.com/chernobelenkiy)
  194. See also the list of [contributors](https://github.com/evrone/postcss-px-to-viewport/contributors) who participated in this project.
  195. ## License
  196. This project is licensed under the [MIT License](LICENSE).
  197. ## Sponsors
  198. Visit [Evrone](https://evrone.com/) website to get more information about the [projects](https://evrone.com/cases) build.
  199. <a href="https://evrone.com/?utm_source=postcss-px-to-viewport">
  200. <img src="https://user-images.githubusercontent.com/417688/34437029-dbfe4ee6-ecab-11e7-9d80-2b274b4149b3.png"
  201. alt="Sponsored by Evrone" width="231" />
  202. </a>
  203. ## Acknowledgments
  204. * Hat tip to https://github.com/cuth/postcss-pxtorem/ for inspiring us for this project.