96c24c1d551c594e426c97e2e1cf08b0bf4cf2687c39abfb04c1585af8ba0a9c18e8d5b1fae72ae907bfe08fd00af45d24ebc1680cd3301b1db18e1451c8a9 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # [postcss][postcss]-svgo [![Build Status](https://travis-ci.org/ben-eb/postcss-svgo.svg?branch=master)][ci] [![NPM version](https://badge.fury.io/js/postcss-svgo.svg)][npm] [![Dependency Status](https://gemnasium.com/ben-eb/postcss-svgo.svg)][deps]
  2. > Optimise inline SVG with PostCSS.
  3. ## Install
  4. With [npm](https://npmjs.org/package/postcss-svgo) do:
  5. ```
  6. npm install postcss-svgo --save
  7. ```
  8. ## Example
  9. ### Input
  10. ```css
  11. h1 {
  12. background: url('data:image/svg+xml;charset=utf-8,<?xml version="1.0" encoding="utf-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve"><circle cx="50" cy="50" r="40" fill="yellow" /></svg>');
  13. }
  14. ```
  15. ### Output
  16. ```css
  17. h1 {
  18. background: url('data:image/svg+xml;charset=utf-8,<svg xmlns="http://www.w3.org/2000/svg"><circle cx="50" cy="50" r="40" fill="%23ff0"/></svg>');
  19. }
  20. ```
  21. ## API
  22. ### `svgo([options])`
  23. Note that postcss-svgo is an *asynchronous* processor. It cannot be used
  24. like this:
  25. ```js
  26. var result = postcss([ svgo() ]).process(css).css;
  27. console.log(result);
  28. ```
  29. Instead make sure your PostCSS runner uses the asynchronous API:
  30. ```js
  31. postcss([ svgo() ]).process(css).then(function (result) {
  32. console.log(result.css);
  33. });
  34. ```
  35. #### options
  36. ##### encode
  37. Type: `boolean`
  38. Default: `undefined`
  39. If `true`, it will encode URL-unsafe characters such as `<`, `>` and `&`;
  40. `false` will decode these characters, and `undefined` will neither encode nor
  41. decode the original input. Note that regardless of this setting, `#` will
  42. always be URL-encoded.
  43. ##### plugins
  44. Optionally, you can customise the output by specifying the `plugins` option. You
  45. will need to provide the config in comma separated objects, like the example
  46. below. Note that you can either disable the plugin by setting it to `false`,
  47. or pass different options to change the default behaviour.
  48. ```js
  49. var postcss = require('postcss');
  50. var svgo = require('postcss-svgo');
  51. var opts = {
  52. plugins: [{
  53. removeDoctype: false
  54. }, {
  55. removeComments: false
  56. }, {
  57. cleanupNumericValues: {
  58. floatPrecision: 2
  59. }
  60. }, {
  61. convertColors: {
  62. names2hex: false,
  63. rgb2hex: false
  64. }
  65. }]
  66. };
  67. postcss([ svgo(opts) ]).process(css).then(function (result) {
  68. console.log(result.css)
  69. });
  70. ```
  71. You can view the [full list of plugins here][plugins].
  72. ## Usage
  73. See the [PostCSS documentation](https://github.com/postcss/postcss#usage) for
  74. examples for your environment.
  75. ## Contributors
  76. Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)):
  77. <!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
  78. | [<img src="https://avatars.githubusercontent.com/u/1282980?v=3" width="100px;"/><br /><sub>Ben Briggs</sub>](http://beneb.info)<br />[💻](https://github.com/ben-eb/postcss-svgo/commits?author=ben-eb) [📖](https://github.com/ben-eb/postcss-svgo/commits?author=ben-eb) 👀 [⚠️](https://github.com/ben-eb/postcss-svgo/commits?author=ben-eb) | [<img src="https://avatars.githubusercontent.com/u/7263665?v=3" width="100px;"/><br /><sub>Sebastian Misch</sub>](https://sebastian-misch.de)<br />[💻](https://github.com/ben-eb/postcss-svgo/commits?author=sbstnmsch) [⚠️](https://github.com/ben-eb/postcss-svgo/commits?author=sbstnmsch) | [<img src="https://avatars.githubusercontent.com/u/11319202?v=3" width="100px;"/><br /><sub>Вячеслав Ляшенко</sub>](https://github.com/ophyros)<br />[💻](https://github.com/ben-eb/postcss-svgo/commits?author=ophyros) [⚠️](https://github.com/ben-eb/postcss-svgo/commits?author=ophyros) | [<img src="https://avatars.githubusercontent.com/u/1131567?v=3" width="100px;"/><br /><sub>shinnn</sub>](https://shinnn.github.io)<br />[💻](https://github.com/ben-eb/postcss-svgo/commits?author=shinnn) | [<img src="https://avatars.githubusercontent.com/u/45338?v=3" width="100px;"/><br /><sub>Jung-gun Lim</sub>](https://github.com/j6lim)<br />[🐛](https://github.com/ben-eb/postcss-svgo/issues?q=author%3Aj6lim) | [<img src="https://avatars.githubusercontent.com/u/5635476?v=3" width="100px;"/><br /><sub>Bogdan Chadkin</sub>](https://github.com/TrySound)<br />[💻](https://github.com/ben-eb/postcss-svgo/commits?author=TrySound) 👀 [⚠️](https://github.com/ben-eb/postcss-svgo/commits?author=TrySound) | [<img src="https://avatars.githubusercontent.com/u/368561?v=3" width="100px;"/><br /><sub>Piotr Walczyszyn</sub>](http://outof.me)<br />[🐛](https://github.com/ben-eb/postcss-svgo/issues?q=author%3Apwalczyszyn) |
  79. | :---: | :---: | :---: | :---: | :---: | :---: | :---: |
  80. <!-- ALL-CONTRIBUTORS-LIST:END -->
  81. This project follows the [all-contributors] specification. Contributions of
  82. any kind welcome!
  83. ## License
  84. MIT © [Ben Briggs](http://beneb.info)
  85. [all-contributors]: https://github.com/kentcdodds/all-contributors
  86. [ci]: https://travis-ci.org/ben-eb/postcss-svgo
  87. [deps]: https://gemnasium.com/ben-eb/postcss-svgo
  88. [npm]: http://badge.fury.io/js/postcss-svgo
  89. [postcss]: https://github.com/postcss/postcss
  90. [plugins]: https://github.com/svg/svgo/tree/master/plugins